##// END OF EJS Templates
Refactor xychartitem -> xychart
Michal Klocek -
r1218:b8de085c4198
parent child
Show More
@@ -1,211 +1,211
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "chartanimator_p.h"
22 22 #include "axisanimation_p.h"
23 23 #include "xyanimation_p.h"
24 24 #include "splineanimation_p.h"
25 #include "xychartitem_p.h"
25 #include "xychart_p.h"
26 26 #include "pieanimation_p.h"
27 27 #include "baranimation_p.h"
28 28 #include "barchartitem_p.h"
29 29 #include "areachartitem_p.h"
30 30 #include "splinechartitem_p.h"
31 31 #include "scatterchartitem_p.h"
32 32 #include <QTimer>
33 33
34 34 Q_DECLARE_METATYPE(QVector<QPointF>)
35 35 Q_DECLARE_METATYPE(QVector<qreal>)
36 36 Q_DECLARE_METATYPE(QVector<QRectF>)
37 37
38 38 QTCOMMERCIALCHART_BEGIN_NAMESPACE
39 39
40 40 ChartAnimator::ChartAnimator(QObject *parent):QObject(parent),
41 41 m_state(ShowState)
42 42 {
43 43 }
44 44
45 45 ChartAnimator::~ChartAnimator()
46 46 {
47 47 }
48 48
49 49 void ChartAnimator::addAnimation(ChartAxis *item)
50 50 {
51 51 ChartAnimation *animation = m_animations.value(item);
52 52
53 53 if (!animation) {
54 54 animation = new AxisAnimation(item);
55 55 m_animations.insert(item, animation);
56 56 }
57 57
58 58 item->setAnimator(this);
59 59 }
60 60
61 61 void ChartAnimator::addAnimation(PieChartItem *item)
62 62 {
63 63 ChartAnimation *animation = m_animations.value(item);
64 64
65 65 if (!animation) {
66 66 animation = new PieAnimation(item);
67 67 m_animations.insert(item, animation);
68 68 }
69 69
70 70 item->setAnimator(this);
71 71 }
72 72
73 73 void ChartAnimator::addAnimation(BarChartItem *item)
74 74 {
75 75 ChartAnimation *animation = m_animations.value(item);
76 76
77 77 if (!animation) {
78 78 animation = new BarAnimation(item);
79 79 m_animations.insert(item, animation);
80 80 }
81 81
82 82 item->setAnimator(this);
83 83 }
84 84
85 85
86 86 void ChartAnimator::removeAnimation(Chart *item)
87 87 {
88 88 item->setAnimator(0);
89 89 m_animations.remove(item);
90 90 }
91 91
92 92 void ChartAnimator::updateLayout(ChartAxis *item , QVector<qreal> &newLayout)
93 93 {
94 94 AxisAnimation *animation = static_cast<AxisAnimation*>(m_animations.value(item));
95 95
96 96 Q_ASSERT(animation);
97 97
98 98 QVector<qreal> oldLayout = item->layout();
99 99
100 100 if (newLayout.count() == 0)
101 101 return;
102 102
103 103 switch (m_state) {
104 104 case ZoomOutState: {
105 105 QRectF rect = item->geometry();
106 106 oldLayout.resize(newLayout.count());
107 107
108 108 for(int i = 0, j = oldLayout.count() - 1; i < (oldLayout.count() + 1) / 2; ++i, --j) {
109 109 oldLayout[i] = item->axisType() == ChartAxis::X_AXIS ? rect.left() : rect.bottom();
110 110 oldLayout[j] = item->axisType() == ChartAxis::X_AXIS ? rect.right() : rect.top();
111 111 }
112 112 }
113 113 break;
114 114 case ZoomInState: {
115 115 int index = qMin(oldLayout.count() * (item->axisType() == ChartAxis::X_AXIS ? m_point.x() : (1 - m_point.y())), newLayout.count() - 1.0);
116 116 oldLayout.resize(newLayout.count());
117 117
118 118 for(int i = 0; i < oldLayout.count(); i++)
119 119 oldLayout[i]= oldLayout[index];
120 120 }
121 121 break;
122 122 case ScrollDownState:
123 123 case ScrollRightState: {
124 124 oldLayout.resize(newLayout.count());
125 125
126 126 for(int i = 0, j = i + 1; i < oldLayout.count() - 1; ++i, ++j)
127 127 oldLayout[i]= oldLayout[j];
128 128 }
129 129 break;
130 130 case ScrollUpState:
131 131 case ScrollLeftState: {
132 132 oldLayout.resize(newLayout.count());
133 133
134 134 for(int i = oldLayout.count() - 1, j = i - 1; i > 0; --i, --j)
135 135 oldLayout[i]= oldLayout[j];
136 136 }
137 137 break;
138 138 default: {
139 139 oldLayout.resize(newLayout.count());
140 140 QRectF rect = item->geometry();
141 141 for(int i = 0, j = oldLayout.count() - 1; i < oldLayout.count(); ++i, --j)
142 142 oldLayout[i] = item->axisType() == ChartAxis::X_AXIS ? rect.left() : rect.top();
143 143 }
144 144 break;
145 145 }
146 146
147 147
148 148 if (animation->state() != QAbstractAnimation::Stopped)
149 149 animation->stop();
150 150
151 151 animation->setDuration(ChartAnimationDuration);
152 152 animation->setEasingCurve(QEasingCurve::OutQuart);
153 153 QVariantAnimation::KeyValues value;
154 154 animation->setKeyValues(value); //workaround for wrong interpolation call
155 155 animation->setKeyValueAt(0.0, qVariantFromValue(oldLayout));
156 156 animation->setKeyValueAt(1.0, qVariantFromValue(newLayout));
157 157
158 158 QTimer::singleShot(0, animation, SLOT(start()));
159 159 }
160 160
161 161 void ChartAnimator::addAnimation(PieChartItem *item, PieSliceItem *sliceItem, const PieSliceData &sliceData, bool startupAnimation)
162 162 {
163 163 PieAnimation *animation = static_cast<PieAnimation *>(m_animations.value(item));
164 164 Q_ASSERT(animation);
165 165 animation->addSlice(sliceItem, sliceData, startupAnimation);
166 166 }
167 167
168 168 void ChartAnimator::removeAnimation(PieChartItem *item, PieSliceItem *sliceItem)
169 169 {
170 170 PieAnimation *animation = static_cast<PieAnimation *>(m_animations.value(item));
171 171 Q_ASSERT(animation);
172 172 animation->removeSlice(sliceItem);
173 173 }
174 174
175 175 void ChartAnimator::updateAnimation(PieChartItem *item, PieSliceItem *sliceItem, const PieSliceData &sliceData)
176 176 {
177 177 PieAnimation *animation = static_cast<PieAnimation *>(m_animations.value(item));
178 178 Q_ASSERT(animation);
179 179 animation->updateValue(sliceItem, sliceData);
180 180 }
181 181
182 182 void ChartAnimator::updateLayout(BarChartItem *item, const QVector<QRectF> &oldLayout, const QVector<QRectF> &newLayout)
183 183 {
184 184 BarAnimation *animation = static_cast<BarAnimation *>(m_animations.value(item));
185 185 Q_ASSERT(animation);
186 186 animation->setDuration(ChartAnimationDuration);
187 187 animation->setKeyValueAt(0.0, qVariantFromValue(oldLayout));
188 188 animation->setKeyValueAt(1.0, qVariantFromValue(newLayout));
189 189 QTimer::singleShot(0, animation, SLOT(start()));
190 190 }
191 191
192 192
193 193 void ChartAnimator::setState(State state, const QPointF &point)
194 194 {
195 195 m_state = state;
196 196 m_point = point;
197 197 }
198 198
199 199 void ChartAnimator::startAnimation(XYAnimation* animation)
200 200 {
201 201 Q_ASSERT(animation);
202 202 if (animation->state() != QAbstractAnimation::Stopped)
203 203 animation->stop();
204 204 animation->setDuration(ChartAnimationDuration);
205 205 animation->setEasingCurve(QEasingCurve::OutQuart);
206 206 QTimer::singleShot(0, animation, SLOT(start()));
207 207 }
208 208
209 209 #include "moc_chartanimator_p.cpp"
210 210
211 211 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,117 +1,117
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "xyanimation_p.h"
22 #include "xychartitem_p.h"
22 #include "xychart_p.h"
23 23 #include <QDebug>
24 24
25 25 Q_DECLARE_METATYPE(QVector<QPointF>)
26 26
27 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 28
29 XYAnimation::XYAnimation(XYChartItem *item):ChartAnimation(item),
29 XYAnimation::XYAnimation(XYChart *item):ChartAnimation(item),
30 30 m_item(item),
31 31 m_dirty(false),
32 32 m_type(MoveDownAnimation)
33 33 {
34 34 }
35 35
36 36 XYAnimation::~XYAnimation()
37 37 {
38 38 }
39 39
40 40 void XYAnimation::setAnimationType(Animation type)
41 41 {
42 42 m_type=type;
43 43 }
44 44
45 45 void XYAnimation::setValues(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints, int index)
46 46 {
47 47 int x = oldPoints.count();
48 48 int y = newPoints.count();
49 49
50 50 if (x != y && abs(x - y) != 1) {
51 51 m_oldPoints = newPoints;
52 52 oldPoints.resize(newPoints.size());
53 53 setKeyValueAt(0.0, qVariantFromValue(oldPoints));
54 54 setKeyValueAt(1.0, qVariantFromValue(newPoints));
55 55 m_dirty = false;
56 56 }
57 57 else {
58 58 if (m_dirty) {
59 59 m_oldPoints = oldPoints;
60 60 m_dirty = false;
61 61 }
62 62 oldPoints = newPoints;
63 63 if (y < x)
64 64 m_oldPoints.remove(index); //remove
65 65 if (y > x)
66 66 m_oldPoints.insert(index, x > 0 ? m_oldPoints[index-1] : newPoints[index]); //add
67 67 setKeyValueAt(0.0, qVariantFromValue(m_oldPoints));
68 68 setKeyValueAt(1.0, qVariantFromValue(newPoints));
69 69 Q_ASSERT(m_oldPoints.count() == newPoints.count());
70 70 }
71 71 }
72 72
73 73 QVariant XYAnimation::interpolated(const QVariant &start, const QVariant &end, qreal progress ) const
74 74 {
75 75 QVector<QPointF> startVector = qVariantValue<QVector<QPointF> >(start);
76 76 QVector<QPointF> endVector = qVariantValue<QVector<QPointF> >(end);
77 77 QVector<QPointF> result;
78 78
79 79 switch (m_type) {
80 80
81 81 case MoveDownAnimation: {
82 82
83 83 if (startVector.count() != endVector.count())
84 84 break;
85 85
86 86 for(int i = 0; i < startVector.count(); i++) {
87 87 qreal x = startVector[i].x() + ((endVector[i].x() - startVector[i].x()) * progress);
88 88 qreal y = startVector[i].y() + ((endVector[i].y() - startVector[i].y()) * progress);
89 89 result << QPointF(x, y);
90 90 }
91 91
92 92 }
93 93 break;
94 94 case LineDrawAnimation: {
95 95 for(int i = 0; i < endVector.count() * qBound(qreal(0), progress, qreal(1)); i++)
96 96 result << endVector[i];
97 97 }
98 98 break;
99 99 default:
100 100 qWarning() << "Unknown type of animation";
101 101 break;
102 102 }
103 103
104 104 return qVariantFromValue(result);
105 105 }
106 106
107 107 void XYAnimation::updateCurrentValue (const QVariant &value)
108 108 {
109 109 if(state()!=QAbstractAnimation::Stopped){ //workaround
110 110 m_dirty = true;
111 111 QVector<QPointF> vector = qVariantValue<QVector<QPointF> >(value);
112 112 m_item->setGeometryPoints(vector);
113 113 m_item->updateGeometry();
114 114 }
115 115 }
116 116
117 117 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,54 +1,54
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef XYANIMATION_P_H
22 22 #define XYANIMATION_P_H
23 23
24 24 #include "chartanimation_p.h"
25 25 #include <QPointF>
26 26
27 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 28
29 class XYChartItem;
29 class XYChart;
30 30
31 31 class XYAnimation : public ChartAnimation
32 32 {
33 33 public:
34 34 enum Animation { LineDrawAnimation, MoveDownAnimation, MoveUpAnimation };
35 XYAnimation(XYChartItem *item);
35 XYAnimation(XYChart *item);
36 36 ~XYAnimation();
37 37 void setValues(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints,int index);
38 38 void setAnimationType(Animation type);
39 39 Animation animationType() const { return m_type; };
40 40
41 41 protected:
42 42 QVariant interpolated(const QVariant &start, const QVariant &end, qreal progress ) const;
43 43 void updateCurrentValue (const QVariant &value );
44 44
45 45 private:
46 XYChartItem *m_item;
46 XYChart *m_item;
47 47 QVector<QPointF> m_oldPoints;
48 48 bool m_dirty;
49 49 Animation m_type;
50 50 };
51 51
52 52 QTCOMMERCIALCHART_END_NAMESPACE
53 53
54 54 #endif
@@ -1,101 +1,109
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "linechartitem_p.h"
22 22 #include "qlineseries.h"
23 23 #include "qlineseries_p.h"
24 24 #include "chartpresenter_p.h"
25 25 #include <QPainter>
26 #include <QGraphicsSceneMouseEvent>
26 27
27 28
28 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 30
30 31 //TODO: optimize : remove points which are not visible
31 32
32 LineChartItem::LineChartItem(QLineSeries* series,ChartPresenter *presenter):XYChartItem(series,presenter),
33 LineChartItem::LineChartItem(QLineSeries* series,ChartPresenter *presenter):XYChart(series,presenter),
34 QGraphicsItem(presenter ? presenter->rootItem() : 0),
33 35 m_series(series),
34 36 m_pointsVisible(false)
35 37 {
36 38 setZValue(ChartPresenter::LineChartZValue);
37 39 QObject::connect(series->d_func(),SIGNAL(updated()),this,SLOT(handleUpdated()));
38 40 handleUpdated();
39 41 }
40 42
41 43 QRectF LineChartItem::boundingRect() const
42 44 {
43 45 return m_rect;
44 46 }
45 47
46 48 QPainterPath LineChartItem::shape() const
47 49 {
48 50 return m_path;
49 51 }
50 52
51 53 void LineChartItem::updateGeometry()
52 54 {
53 55 const QVector<QPointF>& points = geometryPoints();
54 56
55 57 if(points.size()==0)
56 58 {
57 59 m_path = QPainterPath();
58 60 return;
59 61 }
60 62
61 63 QPainterPath linePath(points.at(0));
62 64
63 65 for(int i=1; i< points.size();i++) {
64 66 linePath.lineTo(points.at(i));
65 67 }
66 68
67 69 prepareGeometryChange();
68 70 m_path = linePath;
69 71 m_rect = linePath.boundingRect();
72 setPos(origin());
70 73 }
71 74
72 75 void LineChartItem::handleUpdated()
73 76 {
74 77 m_pointsVisible = m_series->pointsVisible();
75 78 m_linePen = m_series->pen();
76 79 m_pointPen = m_series->pen();
77 80 m_pointPen.setWidthF(2*m_pointPen.width());
78 81 update();
79 82 }
80 83
81 84 //painter
82 85
83 86 void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
84 87 {
85 88 Q_UNUSED(widget)
86 89 Q_UNUSED(option)
87 90
88 91 painter->save();
89 92 painter->setPen(m_linePen);
90 93 painter->setClipRect(clipRect());
91 94 painter->drawPath(m_path);
92 95 if(m_pointsVisible){
93 96 painter->setPen(m_pointPen);
94 97 painter->drawPoints(geometryPoints());
95 98 }
96 99 painter->restore();
97 100 }
98 101
102 void LineChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
103 {
104 emit XYChart::clicked(calculateDomainPoint(event->pos()));
105 }
106
99 107 #include "moc_linechartitem_p.cpp"
100 108
101 109 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,62 +1,64
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef LINECHARTITEM_H
22 22 #define LINECHARTITEM_H
23 23
24 24 #include "qchartglobal.h"
25 #include "xychartitem_p.h"
25 #include "xychart_p.h"
26 26 #include <QPen>
27 27
28 28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 29
30 30 class QLineSeries;
31 31 class ChartPresenter;
32 32
33 class LineChartItem : public XYChartItem
33 class LineChartItem : public XYChart , public QGraphicsItem
34 34 {
35 35 Q_OBJECT
36 Q_INTERFACES(QGraphicsItem)
36 37 public:
37 38 explicit LineChartItem(QLineSeries *series,ChartPresenter *presenter);
38 39 ~LineChartItem() {};
39 40
40 41 //from QGraphicsItem
41 42 QRectF boundingRect() const;
42 43 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
43 44 QPainterPath shape() const;
44 45
45 46 public Q_SLOTS:
46 47 void handleUpdated();
47 48 protected:
48 49 void updateGeometry();
50 void mousePressEvent(QGraphicsSceneMouseEvent *event);
49 51
50 52 private:
51 53 QLineSeries* m_series;
52 54 QPainterPath m_path;
53 55 QRectF m_rect;
54 56 QPen m_linePen;
55 57 QPen m_pointPen;
56 58 bool m_pointsVisible;
57 59
58 60 };
59 61
60 62 QTCOMMERCIALCHART_END_NAMESPACE
61 63
62 64 #endif
@@ -1,193 +1,198
1 1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20 20
21 21 #include "scatterchartitem_p.h"
22 22 #include "qscatterseries.h"
23 23 #include "qscatterseries_p.h"
24 24 #include "chartpresenter_p.h"
25 25 #include <QPainter>
26 26 #include <QGraphicsScene>
27 27 #include <QDebug>
28 #include <QGraphicsSceneMouseEvent>
28 29
29 30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 31
31 32 ScatterChartItem::ScatterChartItem(QScatterSeries *series, ChartPresenter *presenter) :
32 XYChartItem(series,presenter),
33 m_series(series),
34 m_items(this),
35 m_shape(QScatterSeries::MarkerShapeRectangle),
36 m_size(15)
37
33 XYChart(series,presenter),
34 QGraphicsItem(presenter ? presenter->rootItem() : 0),
35 m_series(series),
36 m_items(this),
37 m_shape(QScatterSeries::MarkerShapeRectangle),
38 m_size(15)
38 39 {
39 40 QObject::connect(m_series->d_func(),SIGNAL(updated()), this, SLOT(handleUpdated()));
40 41
41 42 setZValue(ChartPresenter::ScatterSeriesZValue);
42 43 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
43 44
44 45 handleUpdated();
45 46
46 47 m_items.setHandlesChildEvents(false);
47 48
48 49 // TODO: how to draw a drop shadow?
49 50 // QGraphicsDropShadowEffect *dropShadow = new QGraphicsDropShadowEffect();
50 51 // dropShadow->setOffset(2.0);
51 52 // dropShadow->setBlurRadius(2.0);
52 53 // setGraphicsEffect(dropShadow);
53 54 }
54 55
55
56 56 QRectF ScatterChartItem::boundingRect() const
57 57 {
58 58 return m_rect;
59 59 }
60 60
61 61 void ScatterChartItem::createPoints(int count)
62 62 {
63 63 for (int i = 0; i < count; ++i) {
64 64
65 65 QGraphicsItem *item = 0;
66 66
67 67 switch (m_shape) {
68 case QScatterSeries::MarkerShapeCircle:{
69 QGraphicsEllipseItem* i = new QGraphicsEllipseItem(0,0,m_size,m_size);
70 const QRectF& rect = i->boundingRect();
71 i->setPos(-rect.width()/2,-rect.height()/2);
72 item = new Marker(i,this);
73 break;
74 }
75 case QScatterSeries::MarkerShapeRectangle:{
76 QGraphicsRectItem* i = new QGraphicsRectItem(0,0,m_size,m_size);
77 i->setPos(-m_size/2,-m_size/2);
78 item = new Marker(i,this);
79 break;
80 }
81 default:
82 qWarning()<<"Unsupported marker type";
83 break;
68 case QScatterSeries::MarkerShapeCircle: {
69 QGraphicsEllipseItem* i = new QGraphicsEllipseItem(0,0,m_size,m_size);
70 const QRectF& rect = i->boundingRect();
71 i->setPos(-rect.width()/2,-rect.height()/2);
72 item = new Marker(i,this);
73 break;
74 }
75 case QScatterSeries::MarkerShapeRectangle: {
76 QGraphicsRectItem* i = new QGraphicsRectItem(0,0,m_size,m_size);
77 i->setPos(-m_size/2,-m_size/2);
78 item = new Marker(i,this);
79 break;
80 }
81 default:
82 qWarning()<<"Unsupported marker type";
83 break;
84 84
85 85 }
86 86 m_items.addToGroup(item);
87 87 }
88 88 }
89 89
90 90 void ScatterChartItem::deletePoints(int count)
91 91 {
92 92 QList<QGraphicsItem *> items = m_items.childItems();
93 93
94 94 for (int i = 0; i < count; ++i) {
95 95 delete(items.takeLast());
96 96 }
97 97 }
98 98
99 99 void ScatterChartItem::markerSelected(Marker *marker)
100 100 {
101 emit XYChartItem::clicked(marker->point());
101 emit XYChart::clicked(marker->point());
102 102 }
103 103
104 104 void ScatterChartItem::updateGeometry()
105 105 {
106 106
107 107 const QVector<QPointF>& points = geometryPoints();
108 108
109 109 if(points.size()==0)
110 110 {
111 111 deletePoints(m_items.childItems().count());
112 112 return;
113 113 }
114 114
115 115 int diff = m_items.childItems().size() - points.size();
116 116
117 117 if(diff>0) {
118 118 deletePoints(diff);
119 119 }
120 120 else if(diff<0) {
121 121 createPoints(-diff);
122 122 }
123 123
124 124 if(diff!=0) handleUpdated();
125 125
126 126 QList<QGraphicsItem*> items = m_items.childItems();
127 127
128 128 for (int i = 0; i < points.size(); i++) {
129 129 Marker* item = static_cast<Marker*>(items.at(i));
130 130 const QPointF& point = points.at(i);
131 131 const QRectF& rect = item->boundingRect();
132 132 item->setPoint(point);
133 133 item->setPos(point.x()-rect.width()/2,point.y()-rect.height()/2);
134 134 if(!clipRect().contains(point)) {
135 135 item->setVisible(false);
136 136 }
137 137 else {
138 138 item->setVisible(true);
139 139 }
140 140 }
141 141
142 142 prepareGeometryChange();
143 143 m_rect = clipRect();
144 setPos(origin());
144 145 }
145 146
146
147 147 void ScatterChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
148 148 {
149 149 Q_UNUSED(painter)
150 150 Q_UNUSED(option)
151 151 Q_UNUSED(widget)
152 152 }
153 153
154 154 void ScatterChartItem::setPen(const QPen& pen)
155 155 {
156 156 foreach(QGraphicsItem* item , m_items.childItems()) {
157 static_cast<Marker*>(item)->setPen(pen);
157 static_cast<Marker*>(item)->setPen(pen);
158 158 }
159 159 }
160 160
161 161 void ScatterChartItem::setBrush(const QBrush& brush)
162 162 {
163 163 foreach(QGraphicsItem* item , m_items.childItems()) {
164 static_cast<Marker*>(item)->setBrush(brush);
165 }
164 static_cast<Marker*>(item)->setBrush(brush);
165 }
166 166 }
167 167
168 168 void ScatterChartItem::handleUpdated()
169 169 {
170 170
171 171 int count = m_items.childItems().count();
172 172
173 173 if(count==0) return;
174 174
175 175 bool recreate = m_size != m_series->size() || m_shape != m_series->shape();
176 176
177 177 //TODO: only rewrite on size change
178 178
179 179 m_size = m_series->size();
180 180 m_shape = m_series->shape();
181 181
182 if(recreate){
182 if(recreate) {
183 183 deletePoints(count);
184 184 createPoints(count);
185 185 }
186 186
187 187 setPen(m_series->pen());
188 188 setBrush(m_series->brush());
189 189 }
190 190
191 void ScatterChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
192 {
193 emit XYChart::clicked(calculateDomainPoint(event->pos()));
194 }
195
191 196 #include "moc_scatterchartitem_p.cpp"
192 197
193 198 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,140 +1,141
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef SCATTERCHARTITEM_H
22 22 #define SCATTERCHARTITEM_H
23 23
24 24 #include "qchartglobal.h"
25 #include "xychartitem_p.h"
25 #include "xychart_p.h"
26 26 #include <QGraphicsEllipseItem>
27 27 #include <QPen>
28 28
29 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 30
31 31 class QScatterSeries;
32 32 class Marker;
33 33
34 class ScatterChartItem : public XYChartItem
34 class ScatterChartItem : public XYChart, public QGraphicsItem
35 35 {
36 36 Q_OBJECT
37 37 public:
38 38 explicit ScatterChartItem(QScatterSeries *series, ChartPresenter *presenter);
39 39
40 40 public:
41 41 //from QGraphicsItem
42 42 QRectF boundingRect() const;
43 43 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
44 44
45 45 void setPen(const QPen &pen);
46 46 void setBrush(const QBrush &brush);
47 47
48 48 void markerSelected(Marker *item);
49 49
50 50 public Q_SLOTS:
51 51 void handleUpdated();
52 52
53 53 private:
54 54 void createPoints(int count);
55 55 void deletePoints(int count);
56 56
57 57 protected:
58 58 void updateGeometry();
59 void mousePressEvent(QGraphicsSceneMouseEvent *event);
59 60
60 61 private:
61 62 QScatterSeries *m_series;
62 63 QGraphicsItemGroup m_items;
63 64 int m_shape;
64 65 int m_size;
65 66 QRectF m_rect;
66 67
67 68 };
68 69
69 70
70 71 class Marker: public QAbstractGraphicsShapeItem
71 72 {
72 73
73 74 public:
74 75
75 76 Marker(QAbstractGraphicsShapeItem *item , ScatterChartItem *parent) : QAbstractGraphicsShapeItem(0) ,m_item(item), m_parent(parent)
76 77 {
77 78 }
78 79
79 80 ~Marker()
80 81 {
81 82 delete m_item;
82 83 }
83 84
84 85 void setPoint(const QPointF& point)
85 86 {
86 87 m_point=point;
87 88 }
88 89
89 90 QPointF point() const
90 91 {
91 92 return m_point;
92 93 }
93 94
94 95 QPainterPath shape() const
95 96 {
96 97 return m_item->shape();
97 98 }
98 99
99 100 QRectF boundingRect() const
100 101 {
101 102 return m_item->boundingRect();
102 103 }
103 104
104 105 bool contains(const QPointF &point) const
105 106 {
106 107 return m_item->contains(point);
107 108 }
108 109
109 110 void setPen(const QPen &pen)
110 111 {
111 112 m_item->setPen(pen);
112 113 }
113 114
114 115 void setBrush(const QBrush &brush)
115 116 {
116 117 m_item->setBrush(brush);
117 118 }
118 119
119 120 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
120 121 {
121 122 m_item->paint(painter,option,widget);
122 123 }
123 124
124 125 protected:
125 126
126 127 void mousePressEvent(QGraphicsSceneMouseEvent *event)
127 128 {
128 129 Q_UNUSED(event)
129 130 m_parent->markerSelected(this);
130 131 }
131 132
132 133 private:
133 134 QAbstractGraphicsShapeItem* m_item;
134 135 ScatterChartItem* m_parent;
135 136 QPointF m_point;
136 137 };
137 138
138 139 QTCOMMERCIALCHART_END_NAMESPACE
139 140
140 141 #endif // SCATTERPRESENTER_H
@@ -1,159 +1,164
1 1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20 20
21 21 #include "splinechartitem_p.h"
22 22 #include "qsplineseries_p.h"
23 23 #include "chartpresenter_p.h"
24 24 #include "chartanimator_p.h"
25 25 #include <QPainter>
26 #include <QDebug>
26 #include <QGraphicsSceneMouseEvent>
27 27
28 28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 29
30 30 SplineChartItem::SplineChartItem(QSplineSeries *series, ChartPresenter *presenter) :
31 XYChartItem(series, presenter),
32 m_series(series),
33 m_pointsVisible(false),
34 m_animation(0)
31 XYChart(series, presenter),
32 QGraphicsItem(presenter ? presenter->rootItem() : 0),
33 m_series(series),
34 m_pointsVisible(false),
35 m_animation(0)
35 36 {
36 37 setZValue(ChartPresenter::LineChartZValue);
37 38 QObject::connect(m_series->d_func(),SIGNAL(updated()),this,SLOT(handleUpdated()));
38 39 handleUpdated();
39 40 }
40 41
41 42 QRectF SplineChartItem::boundingRect() const
42 43 {
43 44 return m_rect;
44 45 }
45 46
46 47 QPainterPath SplineChartItem::shape() const
47 48 {
48 49 return m_path;
49 50 }
50 51
51 52 void SplineChartItem::setAnimation(SplineAnimation* animation)
52 53 {
53 54 m_animation=animation;
54 XYChartItem::setAnimation(animation);
55 XYChart::setAnimation(animation);
55 56 }
56 57
57 58 void SplineChartItem::setControlGeometryPoints(QVector<QPointF>& points)
58 59 {
59 60 m_controlPoints=points;
60 61 }
61 62
62 63 QVector<QPointF> SplineChartItem::controlGeometryPoints() const
63 64 {
64 65 return m_controlPoints;
65 66 }
66 67
67 68 void SplineChartItem::updateChart(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints,int index)
68 69 {
69 70 QVector<QPointF> controlPoints;
70 71
71 72 if(newPoints.count()>=2) {
72 73 controlPoints.resize(newPoints.count()*2-2);
73 74 }
74 75
75 76 for (int i = 0; i < newPoints.size() - 1; i++) {
76 77 controlPoints[2*i] = calculateGeometryControlPoint(2 * i);
77 78 controlPoints[2 * i + 1] = calculateGeometryControlPoint(2 * i + 1);
78 79 }
79 80
80 81 if (controlPoints.count()<2) {
81 82 setGeometryPoints(newPoints);
82 83 setControlGeometryPoints(controlPoints);
83 84 updateGeometry();
84 85 return;
85 86 }
86 87
87 88 if (m_animation) {
88 89 m_animation->setValues(oldPoints,newPoints,m_controlPoints,controlPoints,index);
89 90 animator()->startAnimation(m_animation);
90 91 }
91 92 else {
92 93 setGeometryPoints(newPoints);
93 94 setControlGeometryPoints(controlPoints);
94 95 updateGeometry();
95 96 }
96 97 }
97 98
98 99 QPointF SplineChartItem::calculateGeometryControlPoint(int index) const
99 100 {
100 return XYChartItem::calculateGeometryPoint(m_series->d_func()->controlPoint(index));
101 return XYChart::calculateGeometryPoint(m_series->d_func()->controlPoint(index));
101 102 }
102 103
103 104 void SplineChartItem::updateGeometry()
104 105 {
105 106 const QVector<QPointF> &points = geometryPoints();
106 107 const QVector<QPointF> &controlPoints = controlGeometryPoints();
107 108
108 109 if ((points.size()<2) || (controlPoints.size()<2)) {
109 110 m_path = QPainterPath();
110 111 return;
111 112 }
112 113
113 114 Q_ASSERT(points.count()*2-2 == controlPoints.count());
114 115
115 116 QPainterPath splinePath(points.at(0));
116 117
117 118 for (int i = 0; i < points.size() - 1; i++) {
118 119 const QPointF& point = points.at(i + 1);
119 120 splinePath.cubicTo(controlPoints[2*i],controlPoints[2 * i + 1],point);
120 121 }
121 122
122 123 prepareGeometryChange();
123 124 m_path = splinePath;
124 125 m_rect = splinePath.boundingRect();
126 setPos(origin());
125 127 }
126 128
127 129 //handlers
128 130
129 131 void SplineChartItem::handleUpdated()
130 132 {
131 133 m_pointsVisible = m_series->pointsVisible();
132 134 m_linePen = m_series->pen();
133 135 m_pointPen = m_series->pen();
134 136 m_pointPen.setWidthF(2*m_pointPen.width());
135 137 update();
136 138 }
137 139
138 140 //painter
139 141
140 142 void SplineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
141 143 {
142 144 Q_UNUSED(widget)
143 145 Q_UNUSED(option)
144 146 painter->save();
145 147 painter->setClipRect(clipRect());
146 148 painter->setPen(m_linePen);
147 149 painter->drawPath(m_path);
148 150 if (m_pointsVisible) {
149 151 painter->setPen(m_pointPen);
150 152 painter->drawPoints(geometryPoints());
151 153 }
152 154 painter->restore();
153 155 }
154 156
155
157 void SplineChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
158 {
159 emit XYChart::clicked(calculateDomainPoint(event->pos()));
160 }
156 161
157 162 #include "moc_splinechartitem_p.cpp"
158 163
159 164 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,72 +1,74
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef SPLINECHARTITEM_P_H
22 22 #define SPLINECHARTITEM_P_H
23 23
24 24 #include "qsplineseries.h"
25 #include "xychartitem_p.h"
25 #include "xychart_p.h"
26 26 #include "splineanimation_p.h"
27 27
28 28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 29
30 class SplineChartItem : public XYChartItem
30 class SplineChartItem : public XYChart, public QGraphicsItem
31 31 {
32 32 Q_OBJECT
33 Q_INTERFACES(QGraphicsItem)
33 34 public:
34 35 SplineChartItem(QSplineSeries *series, ChartPresenter *presenter);
35 36
36 37 //from QGraphicsItem
37 38 QRectF boundingRect() const;
38 39 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
39 40 QPainterPath shape() const;
40 41
41 42 void setControlGeometryPoints(QVector<QPointF>& points);
42 43 QVector<QPointF> controlGeometryPoints() const;
43 44
44 45 void setAnimation(SplineAnimation* animation);
45 46 ChartAnimation* animation() const { return m_animation; }
46 47
47 48 public Q_SLOTS:
48 49 void handleUpdated();
49 50
50 51 protected:
51 52 void updateGeometry();
52 53 void updateChart(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints,int index);
54 void mousePressEvent(QGraphicsSceneMouseEvent *event);
53 55
54 56 private:
55 57 QPointF calculateGeometryControlPoint(int index) const;
56 58
57 59 private:
58 60 QSplineSeries *m_series;
59 61 QPainterPath m_path;
60 62 QRectF m_rect;
61 63 QPen m_linePen;
62 64 QPen m_pointPen;
63 65 bool m_pointsVisible;
64 66 QVector<QPointF> m_controlPoints;
65 67 SplineAnimation* m_animation;
66 68
67 69 friend class SplineAnimation;
68 70 };
69 71
70 72 QTCOMMERCIALCHART_END_NAMESPACE
71 73
72 74 #endif // SPLINECHARTITEM_P_H
@@ -1,86 +1,86
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef QXYSERIES_H
22 22 #define QXYSERIES_H
23 23
24 24 #include <qchartglobal.h>
25 25 #include <qabstractseries.h>
26 26 #include <QPen>
27 27 #include <QBrush>
28 28
29 29 class QModelIndex;
30 30
31 31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 32
33 33 class QXYSeriesPrivate;
34 34 class QXYModelMapper;
35 35
36 36 class QTCOMMERCIALCHART_EXPORT QXYSeries : public QAbstractSeries
37 37 {
38 38 Q_OBJECT
39 39 Q_PROPERTY(QXYModelMapper *modelMapper READ modelMapper)
40 40
41 41 protected:
42 42 explicit QXYSeries(QXYSeriesPrivate &d,QObject *parent = 0);
43 43 ~QXYSeries();
44 44
45 45 public:
46 46 void append(qreal x, qreal y);
47 47 void append(const QPointF &point);
48 48 void append(const QList<QPointF> &points);
49 49 void replace(qreal oldX,qreal oldY,qreal newX,qreal newY);
50 50 void replace(const QPointF &oldPoint,const QPointF &newPoint);
51 51 void remove(qreal x, qreal y);
52 52 void remove(const QPointF &point);
53 53 void removeAll();
54 54
55 55 int count() const;
56 56 QList<QPointF> points() const;
57 57
58 58 QXYSeries& operator << (const QPointF &point);
59 59 QXYSeries& operator << (const QList<QPointF> &points);
60 60
61 61 void setPen(const QPen &pen);
62 62 QPen pen() const;
63 63
64 64 void setBrush(const QBrush &brush);
65 65 QBrush brush() const;
66 66
67 67 void setPointsVisible(bool visible = true);
68 68 bool pointsVisible() const;
69 69
70 70 void setModel(QAbstractItemModel *model);
71 71 virtual void setModelMapper(QXYModelMapper *mapper);
72 72 QXYModelMapper* modelMapper() const;
73 73
74 74 Q_SIGNALS:
75 75 void clicked(const QPointF &point);
76 76
77 77 private:
78 78 Q_DECLARE_PRIVATE(QXYSeries);
79 79 Q_DISABLE_COPY(QXYSeries);
80 80 friend class XYLegendMarker;
81 friend class XYChartItem;
81 friend class XYChart;
82 82 };
83 83
84 84 QTCOMMERCIALCHART_END_NAMESPACE
85 85
86 86 #endif
@@ -1,315 +1,304
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 #include "xychartitem_p.h"
21 #include "xychart_p.h"
22 22 #include "qxyseries.h"
23 23 #include "qxyseries_p.h"
24 24 #include "chartpresenter_p.h"
25 25 #include "chartanimator_p.h"
26 26 #include <QPainter>
27 #include <QGraphicsSceneMouseEvent>
28 27 #include <QAbstractItemModel>
29 28 #include "qxymodelmapper.h"
30 #include <QDebug>
31 29
32 30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33 31
34 32 //TODO: optimize : remove points which are not visible
35 33
36 XYChartItem::XYChartItem(QXYSeries *series, ChartPresenter *presenter):ChartItem(presenter),
34 XYChart::XYChart(QXYSeries *series, ChartPresenter *presenter):Chart(presenter),
37 35 m_minX(0),
38 36 m_maxX(0),
39 37 m_minY(0),
40 38 m_maxY(0),
41 39 m_series(series),
42 40 m_animation(0)
43 41 {
44 42 QObject::connect(series->d_func(),SIGNAL(pointReplaced(int)),this,SLOT(handlePointReplaced(int)));
45 43 QObject::connect(series->d_func(),SIGNAL(pointAdded(int)),this,SLOT(handlePointAdded(int)));
46 44 QObject::connect(series->d_func(),SIGNAL(pointsAdded(int, int)),this,SLOT(handlePointsAdded(int, int)));
47 45 QObject::connect(series->d_func(),SIGNAL(pointRemoved(int)),this,SLOT(handlePointRemoved(int)));
48 46 QObject::connect(series->d_func(),SIGNAL(pointsRemoved(int, int)),this,SLOT(handlePointsRemoved(int, int)));
49 47 QObject::connect(series->d_func(),SIGNAL(reinitialized()),this,SLOT(handleReinitialized()));
50 48 QObject::connect(this,SIGNAL(clicked(QPointF)),series,SIGNAL(clicked(QPointF)));
51 49 }
52 50
53 void XYChartItem::setGeometryPoints(QVector<QPointF>& points)
51 void XYChart::setGeometryPoints(QVector<QPointF>& points)
54 52 {
55 53 m_points = points;
56 54 }
57 55
58 void XYChartItem::setClipRect(const QRectF &rect)
56 void XYChart::setClipRect(const QRectF &rect)
59 57 {
60 58 m_clipRect = rect;
61 59 }
62 60
63 void XYChartItem::setAnimation(XYAnimation* animation)
61 void XYChart::setAnimation(XYAnimation* animation)
64 62 {
65 63 m_animation=animation;
66 64 }
67 65
68 QPointF XYChartItem::calculateGeometryPoint(const QPointF &point) const
66 QPointF XYChart::calculateGeometryPoint(const QPointF &point) const
69 67 {
70 68 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
71 69 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
72 70 qreal x = (point.x() - m_minX)* deltaX;
73 71 qreal y = (point.y() - m_minY)*-deltaY + m_size.height();
74 72 return QPointF(x,y);
75 73 }
76 74
77 QPointF XYChartItem::calculateGeometryPoint(int index) const
75 QPointF XYChart::calculateGeometryPoint(int index) const
78 76 {
79 77 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
80 78 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
81 79 const QList<QPointF>& vector = m_series->points();
82 80 qreal x = (vector[index].x() - m_minX)* deltaX;
83 81 qreal y = (vector[index].y() - m_minY)*-deltaY + m_size.height();
84 82 return QPointF(x,y);
85 83 }
86 84
87 QVector<QPointF> XYChartItem::calculateGeometryPoints() const
85 QVector<QPointF> XYChart::calculateGeometryPoints() const
88 86 {
89 87 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
90 88 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
91 89
92 90 QVector<QPointF> result;
93 91 result.resize(m_series->count());
94 92 const QList<QPointF>& vector = m_series->points();
95 93 for (int i = 0; i < m_series->count(); ++i) {
96 94 qreal x = (vector[i].x() - m_minX)* deltaX;
97 95 qreal y = (vector[i].y() - m_minY)*-deltaY + m_size.height();
98 96 result[i].setX(x);
99 97 result[i].setY(y);
100 98 }
101 99 return result;
102 100 }
103 101
104 QPointF XYChartItem::calculateDomainPoint(const QPointF &point) const
102 QPointF XYChart::calculateDomainPoint(const QPointF &point) const
105 103 {
106 104 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
107 105 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
108 106 qreal x = point.x()/deltaX +m_minX;
109 107 qreal y = (point.y()-m_size.height())/(-deltaY)+ m_minY;
110 108 return QPointF(x,y);
111 109 }
112 110
113 void XYChartItem::updateChart(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints,int index)
111 void XYChart::updateChart(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints,int index)
114 112 {
115 113 if (m_animation) {
116 114 m_animation->setValues(oldPoints, newPoints, index);
117 115 animator()->startAnimation(m_animation);
118 116 }
119 117 else {
120 118 setGeometryPoints(newPoints);
121 119 updateGeometry();
122 120 }
123 121 }
124 122
125 void XYChartItem::updateGeometry()
126 {
127 update();
128 }
129 123 //handlers
130 124
131 void XYChartItem::handlePointAdded(int index)
125 void XYChart::handlePointAdded(int index)
132 126 {
133 127 if (m_series->model() == 0) {
134 128 Q_ASSERT(index<m_series->count());
135 129 Q_ASSERT(index>=0);
136 130 }
137 131
138 132 QVector<QPointF> points = m_points;
139 133 QPointF point;
140 134 point = calculateGeometryPoint(index);
141 135 points.insert(index, point);
142 136
143 137 if(m_animation) {
144 138 m_animation->setAnimationType(XYAnimation::LineDrawAnimation);
145 139 }
146 140
147 141 updateChart(m_points,points,index);
148 142 }
149 143
150 void XYChartItem::handlePointsAdded(int start, int end)
144 void XYChart::handlePointsAdded(int start, int end)
151 145 {
152 146 if (m_series->model() == 0) {
153 147 for (int i = start; i <= end; i++)
154 148 handlePointAdded(i);
155 149 }
156 150 else {
157 151 int mapFirst = m_series->modelMapper()->first();
158 152 int mapCount = m_series->modelMapper()->count();
159 153 if (mapCount != -1 && start >= mapFirst + mapCount) {
160 154 return;
161 155 }
162 156 else {
163 157 int addedCount = end - start + 1;
164 158 if (mapCount != -1 && addedCount > mapCount)
165 159 addedCount = mapCount;
166 160 int first = qMax(start, mapFirst); // get the index of the first item that will be added
167 161 int last = qMin(first + addedCount - 1, mapCount + mapFirst - 1);// get the index of the last item that will be added
168 162 for (int i = first; i <= last; i++) {
169 163 handlePointAdded(i - mapFirst);
170 164 }
171 165 // the map is limited therefore the items that are now outside the map
172 166 // need to be removed from the drawn points
173 167 if (mapCount != -1 && m_points.size() > mapCount)
174 168 for (int i = m_points.size() - 1; i >= mapCount; i--)
175 169 handlePointRemoved(i);
176 170 }
177 171 }
178 172 }
179 173
180 void XYChartItem::handlePointRemoved(int index)
174 void XYChart::handlePointRemoved(int index)
181 175 {
182 176 if (m_series->model() == 0) {
183 177 Q_ASSERT(index<m_series->count() + 1);
184 178 Q_ASSERT(index>=0);
185 179 }
186 180 QVector<QPointF> points = m_points;
187 181 points.remove(index);
188 182
189 183 if(m_animation) {
190 184 m_animation->setAnimationType(XYAnimation::LineDrawAnimation);
191 185 }
192 186
193 187 updateChart(m_points,points,index);
194 188 }
195 189
196 void XYChartItem::handlePointsRemoved(int start, int end)
190 void XYChart::handlePointsRemoved(int start, int end)
197 191 {
198 192 Q_UNUSED(start)
199 193 Q_UNUSED(end)
200 194 if (m_series->model() == 0) {
201 195 for (int i = end; i >= start; i--)
202 196 handlePointRemoved(i);
203 197 }
204 198 else {
205 199 // series uses model as a data source
206 200 int mapFirst = m_series->modelMapper()->first();
207 201 int mapCount = m_series->modelMapper()->count();
208 202 int removedCount = end - start + 1;
209 203 if (mapCount != -1 && start >= mapFirst + mapCount) {
210 204 return;
211 205 }
212 206 else {
213 207 int toRemove = qMin(m_points.size(), removedCount); // first find how many items can actually be removed
214 208 int first = qMax(start, mapFirst);// get the index of the first item that will be removed.
215 209 int last = qMin(first + toRemove - 1, m_points.size() + mapFirst - 1);// get the index of the last item that will be removed.
216 210 if (last - first == 0) {
217 211 for (int i = last; i >= first; i--) {
218 212 handlePointRemoved(i - mapFirst);
219 213
220 214 }
221 215 }
222 216 else {
223 217 QVector<QPointF> points = m_points;
224 218 for (int i = last; i >= first; i--)
225 219 points.remove(i - mapFirst);
226 220 setGeometryPoints(points);
227 221 updateGeometry();
228 222 }
229 223 if (mapCount != -1) {
230 224 int itemsAvailable; // check how many are available to be added
231 225 if (m_series->modelMapper()->orientation() == Qt::Vertical)
232 226 itemsAvailable = m_series->model()->rowCount() - mapFirst - m_points.size();
233 227 else
234 228 itemsAvailable = m_series->model()->columnCount() - mapFirst - m_points.size();
235 229 int toBeAdded = qMin(itemsAvailable, mapCount - m_points.size());// add not more items than there is space left to be filled.
236 230 int currentSize = m_points.size();
237 231 if (toBeAdded > 0)
238 232 for (int i = m_points.size(); i < currentSize + toBeAdded; i++) {
239 233 handlePointAdded(i);
240 234 }
241 235 }
242 236 }
243 237 }
244 238
245 239 }
246 240
247 void XYChartItem::handlePointReplaced(int index)
241 void XYChart::handlePointReplaced(int index)
248 242 {
249 243 Q_ASSERT(index<m_series->count());
250 244 Q_ASSERT(index>=0);
251 245 QPointF point = calculateGeometryPoint(index);
252 246 QVector<QPointF> points = m_points;
253 247 points.replace(index,point);
254 248
255 249 if(m_animation) {
256 250 m_animation->setAnimationType(XYAnimation::MoveDownAnimation);
257 251 }
258 252
259 253 updateChart(m_points,points,index);
260 254 }
261 255
262 void XYChartItem::handleReinitialized()
256 void XYChart::handleReinitialized()
263 257 {
264 258 QVector<QPointF> points = calculateGeometryPoints();
265 259
266 260 if(m_animation) {
267 261 m_animation->setAnimationType(XYAnimation::LineDrawAnimation);
268 262 }
269 263
270 264 updateChart(m_points,points);
271 265 }
272 266
273 void XYChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
267 void XYChart::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
274 268 {
275 269 m_minX=minX;
276 270 m_maxX=maxX;
277 271 m_minY=minY;
278 272 m_maxY=maxY;
279 273 if (isEmpty()) return;
280 274 QVector<QPointF> points = calculateGeometryPoints();
281 275
282 276 if(m_animation) {
283 277 m_animation->setAnimationType(XYAnimation::LineDrawAnimation);
284 278 }
285 279 updateChart(m_points,points);
286 280 }
287 281
288 void XYChartItem::handleGeometryChanged(const QRectF &rect)
282 void XYChart::handleGeometryChanged(const QRectF &rect)
289 283 {
290 284 Q_ASSERT(rect.isValid());
291 285 m_size=rect.size();
292 286 m_clipRect=rect.translated(-rect.topLeft());
293 setPos(rect.topLeft());
287 m_origin=rect.topLeft();
294 288
295 289 if (isEmpty()) return;
296 290 QVector<QPointF> points = calculateGeometryPoints();
297 291 if(m_animation) {
298 292 m_animation->setAnimationType(XYAnimation::LineDrawAnimation);
299 293 }
300 294 updateChart(m_points,points);
301 295 }
302 296
303 bool XYChartItem::isEmpty()
297 bool XYChart::isEmpty()
304 298 {
305 299 return !m_clipRect.isValid() || qFuzzyIsNull(m_maxX - m_minX) || qFuzzyIsNull(m_maxY - m_minY) || m_series->points().isEmpty();
306 300 }
307 301
308 void XYChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
309 {
310 emit clicked(calculateDomainPoint(event->pos()));
311 }
312
313 #include "moc_xychartitem_p.cpp"
302 #include "moc_xychart_p.cpp"
314 303
315 304 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,16 +1,16
1 1 INCLUDEPATH += $$PWD
2 2 DEPENDPATH += $$PWD
3 3
4 4 SOURCES += \
5 $$PWD/xychartitem.cpp \
5 $$PWD/xychart.cpp \
6 6 $$PWD/qxyseries.cpp \
7 7 $$PWD/qxymodelmapper.cpp
8 8
9 9 PRIVATE_HEADERS += \
10 $$PWD/xychartitem_p.h \
10 $$PWD/xychart_p.h \
11 11 $$PWD/qxyseries_p.h
12 12
13 13
14 14 PUBLIC_HEADERS += \
15 15 $$PWD/qxyseries.h \
16 16 $$PWD/qxymodelmapper.h
@@ -1,92 +1,95
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef XYCHARTITEM_H
22 22 #define XYCHARTITEM_H
23 23
24 24 #include "qchartglobal.h"
25 25 #include "chartitem_p.h"
26 26 #include "xyanimation_p.h"
27 27 #include <QPen>
28 28
29 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 30
31 31 class ChartPresenter;
32 32 class QXYSeries;
33 33
34 class XYChartItem : public ChartItem
34 class XYChart : public Chart
35 35 {
36 36 Q_OBJECT
37 37 public:
38 explicit XYChartItem(QXYSeries *series, ChartPresenter *presenter);
39 ~XYChartItem(){};
38 explicit XYChart(QXYSeries *series, ChartPresenter *presenter);
39 ~XYChart(){};
40 40
41 41 void setGeometryPoints(QVector<QPointF>& points);
42 42 QVector<QPointF> geometryPoints() const { return m_points; }
43 43
44 44 void setClipRect(const QRectF &rect);
45 45 QRectF clipRect() const { return m_clipRect; }
46 46
47 QSizeF size() const { return m_size; }
48 QPointF origin() const { return m_origin; }
49
47 50 void setAnimation(XYAnimation* animation);
48 51 ChartAnimation* animation() const { return m_animation; }
49 virtual void updateGeometry();
52 virtual void updateGeometry() = 0;
50 53
51 54 public Q_SLOTS:
52 55 void handlePointAdded(int index);
53 56 void handlePointsAdded(int start, int end);
54 57 void handlePointRemoved(int index);
55 58 void handlePointsRemoved(int start, int end);
56 59 void handlePointReplaced(int index);
57 60 void handleReinitialized();
58 61 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
59 62 void handleGeometryChanged(const QRectF &size);
60 63
61 64 Q_SIGNALS:
62 65 void clicked(const QPointF& point);
63 66
64 67 protected:
65 68 virtual void updateChart(QVector<QPointF> &oldPoints,QVector<QPointF> &newPoints,int index = 0);
66 69 QPointF calculateGeometryPoint(const QPointF &point) const;
67 70 QPointF calculateGeometryPoint(int index) const;
68 71 QPointF calculateDomainPoint(const QPointF &point) const;
69 72 QVector<QPointF> calculateGeometryPoints() const;
70 void mousePressEvent(QGraphicsSceneMouseEvent *event);
71 73
72 74 private:
73 75 inline bool isEmpty();
74 76
75 77 private:
76 78 qreal m_minX;
77 79 qreal m_maxX;
78 80 qreal m_minY;
79 81 qreal m_maxY;
80 82 QXYSeries* m_series;
81 83 QSizeF m_size;
84 QPointF m_origin;
82 85 QRectF m_clipRect;
83 86 QVector<QPointF> m_points;
84 87 XYAnimation* m_animation;
85 88
86 89 friend class AreaChartItem;
87 90
88 91 };
89 92
90 93 QTCOMMERCIALCHART_END_NAMESPACE
91 94
92 95 #endif
General Comments 0
You need to be logged in to leave comments. Login now