ReverseMapper.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 ** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB.  All rights reserved.
00003 **
00004 ** This file is part of the KD Chart library.
00005 **
00006 ** Licensees holding valid commercial KD Chart licenses may use this file in
00007 ** accordance with the KD Chart Commercial License Agreement provided with
00008 ** the Software.
00009 **
00010 **
00011 ** This file may be distributed and/or modified under the terms of the
00012 ** GNU General Public License version 2 and version 3 as published by the
00013 ** Free Software Foundation and appearing in the file LICENSE.GPL included.
00014 **
00015 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00016 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00017 **
00018 ** Contact info@kdab.com if any conditions of this licensing are not
00019 ** clear to you.
00020 **
00021 **********************************************************************/
00022 
00023 #include "ReverseMapper.h"
00024 
00025 #include <math.h>
00026 
00027 #include <QRect>
00028 #include <QtDebug>
00029 #include <QPolygonF>
00030 #include <QPainterPath>
00031 #include <QGraphicsScene>
00032 
00033 #include "../KDChartAbstractDiagram.h"
00034 #include "ChartGraphicsItem.h"
00035 
00036 using namespace KDChart;
00037 
00038 ReverseMapper::ReverseMapper()
00039     : m_scene( 0 )
00040     , m_diagram( 0 )
00041 {
00042 }
00043 
00044 ReverseMapper::ReverseMapper( AbstractDiagram* diagram )
00045     : m_scene( 0 )
00046     , m_diagram( diagram )
00047 {
00048 }
00049 
00050 ReverseMapper::~ReverseMapper()
00051 {
00052     delete m_scene; m_scene = 0;
00053 }
00054 
00055 void ReverseMapper::setDiagram( AbstractDiagram* diagram )
00056 {
00057 
00058     m_diagram = diagram;
00059 }
00060 
00061 void ReverseMapper::clear()
00062 {
00063     delete m_scene;
00064     m_scene = new QGraphicsScene();
00065 }
00066 
00067 QModelIndexList ReverseMapper::indexesIn( const QRect& rect ) const
00068 {
00069     Q_ASSERT( m_diagram );
00070     if ( m_scene && m_scene->sceneRect().intersects( rect ) ) {
00071         QList<QGraphicsItem *> items = m_scene->items( rect );
00072         QModelIndexList indexes;
00073         Q_FOREACH( QGraphicsItem* item, items ) {
00074             ChartGraphicsItem* i = qgraphicsitem_cast<ChartGraphicsItem*>( item );
00075             if ( i ) {
00076                 QModelIndex index ( m_diagram->model()->index( i->row(), i->column(), m_diagram->rootIndex() ) );
00077                 indexes << index;
00078             }
00079         }
00080         return indexes;
00081     } else {
00082         return QModelIndexList();
00083     }
00084 }
00085 
00086 QModelIndexList ReverseMapper::indexesAt( const QPointF& point ) const
00087 {
00088     Q_ASSERT( m_diagram );
00089     if ( m_scene && m_scene->sceneRect().contains( point ) ) {
00090         QList<QGraphicsItem *> items = m_scene->items( point );
00091         QModelIndexList indexes;
00092         Q_FOREACH( QGraphicsItem* item, items ) {
00093             ChartGraphicsItem* i = qgraphicsitem_cast<ChartGraphicsItem*>( item );
00094             if ( i ) {
00095                 QModelIndex index ( m_diagram->model()->index( i->row(), i->column(), m_diagram->rootIndex() ) );
00096                 if( !indexes.contains(index) )
00097                     indexes << index;
00098             }
00099         }
00100         return indexes;
00101     } else {
00102         return QModelIndexList();
00103     }
00104 }
00105 
00106 QPolygonF ReverseMapper::polygon( int row, int column ) const
00107 {
00108     const QModelIndex index = m_diagram->model()->index( row, column, m_diagram->rootIndex() );
00109     return m_itemMap.contains( index ) ? m_itemMap[ index ]->polygon() : QPolygon();
00110 }
00111 
00112 QRectF ReverseMapper::boundingRect( int row, int column ) const
00113 {
00114     const QModelIndex index = m_diagram->model()->index( row, column, m_diagram->rootIndex() );
00115     return m_itemMap.contains( index ) ? m_itemMap[ index ]->polygon().boundingRect() : QRectF();
00116 }
00117 
00118 void ReverseMapper::addItem( ChartGraphicsItem* item )
00119 {
00120     Q_ASSERT( m_scene );
00121     m_scene->addItem( item );
00122     m_itemMap.insert( m_diagram->model()->index( item->row(), item->column(), m_diagram->rootIndex() ), item );
00123 }
00124 
00125 void ReverseMapper::addRect( int row, int column, const QRectF& rect )
00126 {
00127     addPolygon( row, column, QPolygonF( rect ) );
00128 }
00129 
00130 void ReverseMapper::addPolygon( int row, int column, const QPolygonF& polygon )
00131 {
00132     ChartGraphicsItem* item = new ChartGraphicsItem( row, column );
00133     item->setPolygon( polygon );
00134     addItem( item );
00135 }
00136 
00137 void ReverseMapper::addCircle( int row, int column, const QPointF& location, const QSizeF& diameter )
00138 {
00139     QPainterPath path;
00140     QPointF ossfet( -0.5*diameter.width(), -0.5*diameter.height() );
00141     path.addEllipse( QRectF( location + ossfet, diameter ) );
00142     addPolygon( row, column, QPolygonF( path.toFillPolygon() ) );
00143 }
00144 
00145 void ReverseMapper::addLine( int row, int column, const QPointF& from, const QPointF& to )
00146 {
00147     // that's no line, dude... make a small circle around that point, instead
00148     if( from == to )
00149     {
00150         addCircle( row, column, from, QSizeF( 1.5, 1.5 ) );
00151         return;
00152     }
00153     // lines do not make good polygons to click on. we calculate a 2
00154     // pixel wide rectangle, where the original line is excatly
00155     // centered in.
00156     // make a 3 pixel wide polygon from the line:
00157     static const QPointF pixel( 1.0, 1.0 );
00158     QPointF left, right;
00159     if ( from.x() < to.x() ) {
00160         left = from;
00161         right = to;
00162     } else {
00163         right = from;
00164         left = to;
00165     }
00166     const QPointF lineVector( right - left );
00167     const qreal lineVectorLength = sqrt( lineVector.x() * lineVector.x() + lineVector.y() * lineVector.y() );
00168     const QPointF lineVectorUnit( lineVector / lineVectorLength );
00169     const QPointF normOfLineVectorUnit( -lineVectorUnit.y(), lineVectorUnit.x() );
00170     // now the four polygon end points:
00171     const QPointF one( left - lineVectorUnit + normOfLineVectorUnit );
00172     const QPointF two( left - lineVectorUnit - normOfLineVectorUnit );
00173     const QPointF three( right + lineVectorUnit - normOfLineVectorUnit );
00174     const QPointF four( right + lineVectorUnit + normOfLineVectorUnit );
00175     addPolygon( row, column, QPolygonF() << one << two << three << four );
00176 }