00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <math.h>
00031
00032 #include <QRect>
00033 #include <QtDebug>
00034 #include <QPolygonF>
00035 #include <QPainterPath>
00036 #include <QGraphicsScene>
00037
00038 #include "ReverseMapper.h"
00039 #include "../KDChartAbstractDiagram.h"
00040 #include "ChartGraphicsItem.h"
00041
00042 using namespace KDChart;
00043
00044 ReverseMapper::ReverseMapper()
00045 : m_scene( 0 )
00046 , m_diagram( 0 )
00047 {
00048 }
00049
00050 ReverseMapper::ReverseMapper( AbstractDiagram* diagram )
00051 : m_scene( 0 )
00052 , m_diagram( diagram )
00053 {
00054 }
00055
00056 ReverseMapper::~ReverseMapper()
00057 {
00058 delete m_scene; m_scene = 0;
00059 }
00060
00061 void ReverseMapper::setDiagram( AbstractDiagram* diagram )
00062 {
00063
00064 m_diagram = diagram;
00065 }
00066
00067 void ReverseMapper::clear()
00068 {
00069 delete m_scene;
00070 m_scene = new QGraphicsScene();
00071 }
00072
00073 QModelIndexList ReverseMapper::indexesIn( const QRect& rect ) const
00074 {
00075 Q_ASSERT( m_diagram );
00076 if ( m_scene && m_scene->sceneRect().intersects( rect ) ) {
00077 QList<QGraphicsItem *> items = m_scene->items( rect );
00078 QModelIndexList indexes;
00079 Q_FOREACH( QGraphicsItem* item, items ) {
00080 ChartGraphicsItem* i = qgraphicsitem_cast<ChartGraphicsItem*>( item );
00081 if ( i ) {
00082 QModelIndex index ( m_diagram->model()->index( i->row(), i->column() ) );
00083 indexes << index;
00084 }
00085 }
00086 return indexes;
00087 } else {
00088 return QModelIndexList();
00089 }
00090 }
00091
00092 QModelIndexList ReverseMapper::indexesAt( const QPointF& point ) const
00093 {
00094 Q_ASSERT( m_diagram );
00095 if ( m_scene && m_scene->sceneRect().contains( point ) ) {
00096 QList<QGraphicsItem *> items = m_scene->items( point );
00097 QModelIndexList indexes;
00098 Q_FOREACH( QGraphicsItem* item, items ) {
00099 ChartGraphicsItem* i = qgraphicsitem_cast<ChartGraphicsItem*>( item );
00100 if ( i ) {
00101 QModelIndex index ( m_diagram->model()->index( i->row(), i->column() ) );
00102 indexes << index;
00103 }
00104 }
00105 return indexes;
00106 } else {
00107 return QModelIndexList();
00108 }
00109 }
00110
00111 void ReverseMapper::addItem( ChartGraphicsItem* item )
00112 {
00113 Q_ASSERT( m_scene );
00114 m_scene->addItem( item );
00115 }
00116
00117 void ReverseMapper::addRect( int row, int column, const QRectF& rect )
00118 {
00119 addPolygon( row, column, QPolygonF( rect ) );
00120 }
00121
00122 void ReverseMapper::addPolygon( int row, int column, const QPolygonF& polygon )
00123 {
00124 ChartGraphicsItem* item = new ChartGraphicsItem( row, column );
00125 item->setPolygon( polygon );
00126 addItem( item );
00127 }
00128
00129 void ReverseMapper::addCircle( int row, int column, const QPointF& location, const QSizeF& diameter )
00130 {
00131 QPainterPath path;
00132 QPointF ossfet( -0.5*diameter.width(), -0.5*diameter.height() );
00133 path.addEllipse( QRectF( location + ossfet, diameter ) );
00134 addPolygon( row, column, QPolygonF( path.toFillPolygon() ) );
00135 }
00136
00137 void ReverseMapper::addLine( int row, int column, const QPointF& from, const QPointF& to )
00138 {
00139
00140
00141
00142
00143 static const QPointF pixel( 1.0, 1.0 );
00144 QPointF left, right;
00145 if ( from.x() < to.x() ) {
00146 left = from;
00147 right = to;
00148 } else {
00149 right = from;
00150 left = to;
00151 }
00152 const QPointF lineVector( right - left );
00153 const qreal lineVectorLength = sqrt( lineVector.x() * lineVector.x() + lineVector.y() * lineVector.y() );
00154 const QPointF lineVectorUnit( lineVector / lineVectorLength );
00155 const QPointF normOfLineVectorUnit( -lineVectorUnit.y(), lineVectorUnit.x() );
00156
00157 const QPointF one( left - lineVectorUnit + normOfLineVectorUnit );
00158 const QPointF two( left - lineVectorUnit - normOfLineVectorUnit );
00159 const QPointF three( right + lineVectorUnit - normOfLineVectorUnit );
00160 const QPointF four( right + lineVectorUnit + normOfLineVectorUnit );
00161 addPolygon( row, column, QPolygonF() << one << two << three << four );
00162 }