~ubuntu-branches/ubuntu/precise/koffice/precise

« back to all changes in this revision

Viewing changes to plugins/chartshape/kdchart/src/Scenery/ReverseMapper.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2010-10-27 17:52:57 UTC
  • mfrom: (0.12.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20101027175257-s04zqqk5bs8ckm9o
Tags: 1:2.2.83-0ubuntu1
* Merge with Debian git remaining changes:
 - Add build-deps on librcps-dev, opengtl-dev, libqtgtl-dev, freetds-dev,
   create-resources, libspnav-dev
 - Remove needless build-dep on libwv2-dev
 - koffice-libs recommends create-resources
 - krita recommends pstoedit
 - Keep our patches
* New upstream release 2.3 beta 3
  - Remove debian/patches fixed by upstream
  - Update install files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB.  All rights reserved.
 
3
**
 
4
** This file is part of the KD Chart library.
 
5
**
 
6
** Licensees holding valid commercial KD Chart licenses may use this file in
 
7
** accordance with the KD Chart Commercial License Agreement provided with
 
8
** the Software.
 
9
**
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 and version 3 as published by the
 
13
** Free Software Foundation and appearing in the file LICENSE.GPL included.
 
14
**
 
15
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
16
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
17
**
 
18
** Contact info@kdab.com if any conditions of this licensing are not
 
19
** clear to you.
 
20
**
 
21
**********************************************************************/
 
22
 
 
23
#include "ReverseMapper.h"
 
24
 
 
25
#include <math.h>
 
26
 
 
27
#include <QRect>
 
28
#include <QtDebug>
 
29
#include <QPolygonF>
 
30
#include <QPainterPath>
 
31
#include <QGraphicsScene>
 
32
 
 
33
#include "../KDChartAbstractDiagram.h"
 
34
#include "ChartGraphicsItem.h"
 
35
 
 
36
using namespace KDChart;
 
37
 
 
38
ReverseMapper::ReverseMapper()
 
39
    : m_scene( 0 )
 
40
    , m_diagram( 0 )
 
41
{
 
42
}
 
43
 
 
44
ReverseMapper::ReverseMapper( AbstractDiagram* diagram )
 
45
    : m_scene( 0 )
 
46
    , m_diagram( diagram )
 
47
{
 
48
}
 
49
 
 
50
ReverseMapper::~ReverseMapper()
 
51
{
 
52
    delete m_scene; m_scene = 0;
 
53
}
 
54
 
 
55
void ReverseMapper::setDiagram( AbstractDiagram* diagram )
 
56
{
 
57
 
 
58
    m_diagram = diagram;
 
59
}
 
60
 
 
61
void ReverseMapper::clear()
 
62
{
 
63
    delete m_scene;
 
64
    m_scene = new QGraphicsScene();
 
65
}
 
66
 
 
67
QModelIndexList ReverseMapper::indexesIn( const QRect& rect ) const
 
68
{
 
69
    Q_ASSERT( m_diagram );
 
70
    if ( m_scene && m_scene->sceneRect().intersects( rect ) ) {
 
71
        QList<QGraphicsItem *> items = m_scene->items( rect );
 
72
        QModelIndexList indexes;
 
73
        Q_FOREACH( QGraphicsItem* item, items ) {
 
74
            ChartGraphicsItem* i = qgraphicsitem_cast<ChartGraphicsItem*>( item );
 
75
            if ( i ) {
 
76
                QModelIndex index ( m_diagram->model()->index( i->row(), i->column(), m_diagram->rootIndex() ) );
 
77
                indexes << index;
 
78
            }
 
79
        }
 
80
        return indexes;
 
81
    } else {
 
82
        return QModelIndexList();
 
83
    }
 
84
}
 
85
 
 
86
QModelIndexList ReverseMapper::indexesAt( const QPointF& point ) const
 
87
{
 
88
    Q_ASSERT( m_diagram );
 
89
    if ( m_scene && m_scene->sceneRect().contains( point ) ) {
 
90
        QList<QGraphicsItem *> items = m_scene->items( point );
 
91
        QModelIndexList indexes;
 
92
        Q_FOREACH( QGraphicsItem* item, items ) {
 
93
            ChartGraphicsItem* i = qgraphicsitem_cast<ChartGraphicsItem*>( item );
 
94
            if ( i ) {
 
95
                QModelIndex index ( m_diagram->model()->index( i->row(), i->column(), m_diagram->rootIndex() ) );
 
96
                if( !indexes.contains(index) )
 
97
                    indexes << index;
 
98
            }
 
99
        }
 
100
        return indexes;
 
101
    } else {
 
102
        return QModelIndexList();
 
103
    }
 
104
}
 
105
 
 
106
QPolygonF ReverseMapper::polygon( int row, int column ) const
 
107
{
 
108
    const QModelIndex index = m_diagram->model()->index( row, column, m_diagram->rootIndex() );
 
109
    return m_itemMap.contains( index ) ? m_itemMap[ index ]->polygon() : QPolygon();
 
110
}
 
111
 
 
112
QRectF ReverseMapper::boundingRect( int row, int column ) const
 
113
{
 
114
    const QModelIndex index = m_diagram->model()->index( row, column, m_diagram->rootIndex() );
 
115
    return m_itemMap.contains( index ) ? m_itemMap[ index ]->polygon().boundingRect() : QRectF();
 
116
}
 
117
 
 
118
void ReverseMapper::addItem( ChartGraphicsItem* item )
 
119
{
 
120
    Q_ASSERT( m_scene );
 
121
    m_scene->addItem( item );
 
122
    m_itemMap.insert( m_diagram->model()->index( item->row(), item->column(), m_diagram->rootIndex() ), item );
 
123
}
 
124
 
 
125
void ReverseMapper::addRect( int row, int column, const QRectF& rect )
 
126
{
 
127
    addPolygon( row, column, QPolygonF( rect ) );
 
128
}
 
129
 
 
130
void ReverseMapper::addPolygon( int row, int column, const QPolygonF& polygon )
 
131
{
 
132
    ChartGraphicsItem* item = new ChartGraphicsItem( row, column );
 
133
    item->setPolygon( polygon );
 
134
    addItem( item );
 
135
}
 
136
 
 
137
void ReverseMapper::addCircle( int row, int column, const QPointF& location, const QSizeF& diameter )
 
138
{
 
139
    QPainterPath path;
 
140
    QPointF ossfet( -0.5*diameter.width(), -0.5*diameter.height() );
 
141
    path.addEllipse( QRectF( location + ossfet, diameter ) );
 
142
    addPolygon( row, column, QPolygonF( path.toFillPolygon() ) );
 
143
}
 
144
 
 
145
void ReverseMapper::addLine( int row, int column, const QPointF& from, const QPointF& to )
 
146
{
 
147
    // that's no line, dude... make a small circle around that point, instead
 
148
    if( from == to )
 
149
    {
 
150
        addCircle( row, column, from, QSizeF( 1.5, 1.5 ) );
 
151
        return;
 
152
    }
 
153
    // lines do not make good polygons to click on. we calculate a 2
 
154
    // pixel wide rectangle, where the original line is excatly
 
155
    // centered in.
 
156
    // make a 3 pixel wide polygon from the line:
 
157
    static const QPointF pixel( 1.0, 1.0 );
 
158
    QPointF left, right;
 
159
    if ( from.x() < to.x() ) {
 
160
        left = from;
 
161
        right = to;
 
162
    } else {
 
163
        right = from;
 
164
        left = to;
 
165
    }
 
166
    const QPointF lineVector( right - left );
 
167
    const qreal lineVectorLength = sqrt( lineVector.x() * lineVector.x() + lineVector.y() * lineVector.y() );
 
168
    const QPointF lineVectorUnit( lineVector / lineVectorLength );
 
169
    const QPointF normOfLineVectorUnit( -lineVectorUnit.y(), lineVectorUnit.x() );
 
170
    // now the four polygon end points:
 
171
    const QPointF one( left - lineVectorUnit + normOfLineVectorUnit );
 
172
    const QPointF two( left - lineVectorUnit - normOfLineVectorUnit );
 
173
    const QPointF three( right + lineVectorUnit - normOfLineVectorUnit );
 
174
    const QPointF four( right + lineVectorUnit + normOfLineVectorUnit );
 
175
    addPolygon( row, column, QPolygonF() << one << two << three << four );
 
176
}