1 /*
2  * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB.  All rights reserved.
3  *
4  * This file is part of the KD Chart library.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #include "ReverseMapper.h"
21 
22 #include <math.h>
23 
24 #include <QRect>
25 #include <QtDebug>
26 #include <QPolygonF>
27 #include <QPainterPath>
28 #include <QGraphicsScene>
29 
30 #include "KChartAbstractDiagram.h"
31 #include "ChartGraphicsItem.h"
32 
33 using namespace KChart;
34 
ReverseMapper()35 ReverseMapper::ReverseMapper()
36     : m_scene( nullptr )
37     , m_diagram( nullptr )
38 {
39 }
40 
ReverseMapper(AbstractDiagram * diagram)41 ReverseMapper::ReverseMapper( AbstractDiagram* diagram )
42     : m_scene( nullptr )
43     , m_diagram( diagram )
44 {
45 }
46 
~ReverseMapper()47 ReverseMapper::~ReverseMapper()
48 {
49     delete m_scene; m_scene = nullptr;
50 }
51 
setDiagram(AbstractDiagram * diagram)52 void ReverseMapper::setDiagram( AbstractDiagram* diagram )
53 {
54 
55     m_diagram = diagram;
56 }
57 
clear()58 void ReverseMapper::clear()
59 {
60     m_itemMap.clear();
61     delete m_scene;
62     m_scene = new QGraphicsScene();
63 }
64 
indexesIn(const QRect & rect) const65 QModelIndexList ReverseMapper::indexesIn( const QRect& rect ) const
66 {
67     Q_ASSERT( m_diagram );
68     if ( m_scene && m_scene->sceneRect().intersects( rect ) ) {
69         QList<QGraphicsItem *> items = m_scene->items( rect );
70         QModelIndexList indexes;
71         Q_FOREACH( QGraphicsItem* item, items ) {
72             ChartGraphicsItem* i = qgraphicsitem_cast<ChartGraphicsItem*>( item );
73             if ( i ) {
74                 QModelIndex index ( m_diagram->model()->index( i->row(), i->column(), m_diagram->rootIndex() ) ); // checked
75                 indexes << index;
76             }
77         }
78         return indexes;
79     } else {
80         return QModelIndexList();
81     }
82 }
83 
indexesAt(const QPointF & point) const84 QModelIndexList ReverseMapper::indexesAt( const QPointF& point ) const
85 {
86     Q_ASSERT( m_diagram );
87     if ( m_scene && m_scene->sceneRect().contains( point ) ) {
88         QList<QGraphicsItem *> items = m_scene->items( point );
89         QModelIndexList indexes;
90         Q_FOREACH( QGraphicsItem* item, items ) {
91             ChartGraphicsItem* i = qgraphicsitem_cast<ChartGraphicsItem*>( item );
92             if ( i ) {
93                 QModelIndex index ( m_diagram->model()->index( i->row(), i->column(), m_diagram->rootIndex() ) ); // checked
94                 if ( !indexes.contains(index) )
95                     indexes << index;
96             }
97         }
98         return indexes;
99     } else {
100         return QModelIndexList();
101     }
102 }
103 
polygon(int row,int column) const104 QPolygonF ReverseMapper::polygon( int row, int column ) const
105 {
106     if ( !m_diagram->model()->hasIndex( row, column, m_diagram->rootIndex() ) )
107         return QPolygon();
108     const QModelIndex index = m_diagram->model()->index( row, column, m_diagram->rootIndex() ); // checked
109     return m_itemMap.contains( index ) ? m_itemMap[ index ]->polygon() : QPolygon();
110 }
111 
boundingRect(int row,int column) const112 QRectF ReverseMapper::boundingRect( int row, int column ) const
113 {
114     if ( !m_diagram->model()->hasIndex( row, column, m_diagram->rootIndex() ) )
115         return QRectF();
116     const QModelIndex index = m_diagram->model()->index( row, column, m_diagram->rootIndex() ); // checked
117     return m_itemMap.contains( index ) ? m_itemMap[ index ]->polygon().boundingRect() : QRectF();
118 }
119 
addItem(ChartGraphicsItem * item)120 void ReverseMapper::addItem( ChartGraphicsItem* item )
121 {
122     Q_ASSERT( m_scene );
123     m_scene->addItem( item );
124     m_itemMap.insert( m_diagram->model()->index( item->row(), item->column(), m_diagram->rootIndex() ), item ); // checked
125 }
126 
addRect(int row,int column,const QRectF & rect)127 void ReverseMapper::addRect( int row, int column, const QRectF& rect )
128 {
129     addPolygon( row, column, QPolygonF( rect ) );
130 }
131 
addPolygon(int row,int column,const QPolygonF & polygon)132 void ReverseMapper::addPolygon( int row, int column, const QPolygonF& polygon )
133 {
134     ChartGraphicsItem* item = new ChartGraphicsItem( row, column );
135     item->setPolygon( polygon );
136     addItem( item );
137 }
138 
addCircle(int row,int column,const QPointF & location,const QSizeF & diameter)139 void ReverseMapper::addCircle( int row, int column, const QPointF& location, const QSizeF& diameter )
140 {
141     QPainterPath path;
142     QPointF ossfet( -0.5*diameter.width(), -0.5*diameter.height() );
143     path.addEllipse( QRectF( location + ossfet, diameter ) );
144     addPolygon( row, column, QPolygonF( path.toFillPolygon() ) );
145 }
146 
addLine(int row,int column,const QPointF & from,const QPointF & to)147 void ReverseMapper::addLine( int row, int column, const QPointF& from, const QPointF& to )
148 {
149     // that's no line, dude... make a small circle around that point, instead
150     if ( from == to )
151     {
152         addCircle( row, column, from, QSizeF( 1.5, 1.5 ) );
153         return;
154     }
155     // lines do not make good polygons to click on. we calculate a 2
156     // pixel wide rectangle, where the original line is excatly
157     // centered in.
158     // make a 3 pixel wide polygon from the line:
159     QPointF left, right;
160     if ( from.x() < to.x() ) {
161         left = from;
162         right = to;
163     } else {
164         right = from;
165         left = to;
166     }
167     const QPointF lineVector( right - left );
168     const qreal lineVectorLength = sqrt( lineVector.x() * lineVector.x() + lineVector.y() * lineVector.y() );
169     const QPointF lineVectorUnit( lineVector / lineVectorLength );
170     const QPointF normOfLineVectorUnit( -lineVectorUnit.y(), lineVectorUnit.x() );
171     // now the four polygon end points:
172     const QPointF one( left - lineVectorUnit + normOfLineVectorUnit );
173     const QPointF two( left - lineVectorUnit - normOfLineVectorUnit );
174     const QPointF three( right + lineVectorUnit - normOfLineVectorUnit );
175     const QPointF four( right + lineVectorUnit + normOfLineVectorUnit );
176     addPolygon( row, column, QPolygonF() << one << two << three << four );
177 }
178