##// END OF EJS Templates
Refactor piechart to avoid using invalid QPieSlice pointers....
Jani Honkonen -
r1053:615ed2b99418
parent child
Show More
@@ -1,301 +1,301
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "chartanimator_p.h"
21 #include "chartanimator_p.h"
22 #include "axisanimation_p.h"
22 #include "axisanimation_p.h"
23 #include "xyanimation_p.h"
23 #include "xyanimation_p.h"
24 #include "splineanimation_p.h"
24 #include "splineanimation_p.h"
25 #include "xychartitem_p.h"
25 #include "xychartitem_p.h"
26 #include "pieanimation_p.h"
26 #include "pieanimation_p.h"
27 #include "baranimation_p.h"
27 #include "baranimation_p.h"
28 #include "barchartitem_p.h"
28 #include "barchartitem_p.h"
29 #include "areachartitem_p.h"
29 #include "areachartitem_p.h"
30 #include "splinechartitem_p.h"
30 #include "splinechartitem_p.h"
31 #include "scatterchartitem_p.h"
31 #include "scatterchartitem_p.h"
32 #include <QTimer>
32 #include <QTimer>
33
33
34 Q_DECLARE_METATYPE(QVector<QPointF>)
34 Q_DECLARE_METATYPE(QVector<QPointF>)
35 Q_DECLARE_METATYPE(QVector<qreal>)
35 Q_DECLARE_METATYPE(QVector<qreal>)
36 Q_DECLARE_METATYPE(QVector<QRectF>)
36 Q_DECLARE_METATYPE(QVector<QRectF>)
37
37
38 QTCOMMERCIALCHART_BEGIN_NAMESPACE
38 QTCOMMERCIALCHART_BEGIN_NAMESPACE
39
39
40 const static int duration = 1000;
40 const static int duration = 1000;
41
41
42 ChartAnimator::ChartAnimator(QObject *parent):QObject(parent),
42 ChartAnimator::ChartAnimator(QObject *parent):QObject(parent),
43 m_state(ShowState)
43 m_state(ShowState)
44 {
44 {
45 }
45 }
46
46
47 ChartAnimator::~ChartAnimator()
47 ChartAnimator::~ChartAnimator()
48 {
48 {
49 }
49 }
50
50
51 void ChartAnimator::addAnimation(ChartAxis *item)
51 void ChartAnimator::addAnimation(ChartAxis *item)
52 {
52 {
53 ChartAnimation *animation = m_animations.value(item);
53 ChartAnimation *animation = m_animations.value(item);
54
54
55 if (!animation) {
55 if (!animation) {
56 animation = new AxisAnimation(item);
56 animation = new AxisAnimation(item);
57 m_animations.insert(item, animation);
57 m_animations.insert(item, animation);
58 }
58 }
59
59
60 item->setAnimator(this);
60 item->setAnimator(this);
61 }
61 }
62
62
63 void ChartAnimator::addAnimation(SplineChartItem *item)
63 void ChartAnimator::addAnimation(SplineChartItem *item)
64 {
64 {
65 ChartAnimation *animation = m_animations.value(item);
65 ChartAnimation *animation = m_animations.value(item);
66
66
67 if (!animation) {
67 if (!animation) {
68 animation = new SplineAnimation(item);
68 animation = new SplineAnimation(item);
69 m_animations.insert(item, animation);
69 m_animations.insert(item, animation);
70 }
70 }
71
71
72 item->setAnimator(this);
72 item->setAnimator(this);
73 }
73 }
74
74
75 void ChartAnimator::addAnimation(ScatterChartItem *item)
75 void ChartAnimator::addAnimation(ScatterChartItem *item)
76 {
76 {
77 ChartAnimation *animation = m_animations.value(item);
77 ChartAnimation *animation = m_animations.value(item);
78
78
79 if (!animation) {
79 if (!animation) {
80 animation = new XYAnimation(item);
80 animation = new XYAnimation(item);
81 m_animations.insert(item, animation);
81 m_animations.insert(item, animation);
82 }
82 }
83
83
84 item->setAnimator(this);
84 item->setAnimator(this);
85 }
85 }
86
86
87 void ChartAnimator::addAnimation(LineChartItem *item)
87 void ChartAnimator::addAnimation(LineChartItem *item)
88 {
88 {
89 ChartAnimation *animation = m_animations.value(item);
89 ChartAnimation *animation = m_animations.value(item);
90
90
91 if (!animation) {
91 if (!animation) {
92 animation = new XYAnimation(item);
92 animation = new XYAnimation(item);
93 m_animations.insert(item, animation);
93 m_animations.insert(item, animation);
94 }
94 }
95
95
96 item->setAnimator(this);
96 item->setAnimator(this);
97 }
97 }
98
98
99 void ChartAnimator::addAnimation(PieChartItem *item)
99 void ChartAnimator::addAnimation(PieChartItem *item)
100 {
100 {
101 ChartAnimation *animation = m_animations.value(item);
101 ChartAnimation *animation = m_animations.value(item);
102
102
103 if (!animation) {
103 if (!animation) {
104 animation = new PieAnimation(item);
104 animation = new PieAnimation(item);
105 m_animations.insert(item, animation);
105 m_animations.insert(item, animation);
106 }
106 }
107
107
108 item->setAnimator(this);
108 item->setAnimator(this);
109 }
109 }
110
110
111 void ChartAnimator::addAnimation(BarChartItem *item)
111 void ChartAnimator::addAnimation(BarChartItem *item)
112 {
112 {
113 ChartAnimation *animation = m_animations.value(item);
113 ChartAnimation *animation = m_animations.value(item);
114
114
115 if (!animation) {
115 if (!animation) {
116 animation = new BarAnimation(item);
116 animation = new BarAnimation(item);
117 m_animations.insert(item, animation);
117 m_animations.insert(item, animation);
118 }
118 }
119
119
120 item->setAnimator(this);
120 item->setAnimator(this);
121 }
121 }
122
122
123
123
124 void ChartAnimator::removeAnimation(Chart *item)
124 void ChartAnimator::removeAnimation(Chart *item)
125 {
125 {
126 item->setAnimator(0);
126 item->setAnimator(0);
127 m_animations.remove(item);
127 m_animations.remove(item);
128 }
128 }
129
129
130 void ChartAnimator::updateLayout(ChartAxis *item , QVector<qreal> &newLayout)
130 void ChartAnimator::updateLayout(ChartAxis *item , QVector<qreal> &newLayout)
131 {
131 {
132 AxisAnimation *animation = static_cast<AxisAnimation*>(m_animations.value(item));
132 AxisAnimation *animation = static_cast<AxisAnimation*>(m_animations.value(item));
133
133
134 Q_ASSERT(animation);
134 Q_ASSERT(animation);
135
135
136 QVector<qreal> oldLayout = item->layout();
136 QVector<qreal> oldLayout = item->layout();
137
137
138 if (newLayout.count() == 0)
138 if (newLayout.count() == 0)
139 return;
139 return;
140
140
141 switch (m_state) {
141 switch (m_state) {
142 case ZoomOutState: {
142 case ZoomOutState: {
143 QRectF rect = item->geometry();
143 QRectF rect = item->geometry();
144 oldLayout.resize(newLayout.count());
144 oldLayout.resize(newLayout.count());
145
145
146 for(int i = 0, j = oldLayout.count() - 1; i < (oldLayout.count() + 1) / 2; ++i, --j) {
146 for(int i = 0, j = oldLayout.count() - 1; i < (oldLayout.count() + 1) / 2; ++i, --j) {
147 oldLayout[i] = item->axisType() == ChartAxis::X_AXIS ? rect.left() : rect.bottom();
147 oldLayout[i] = item->axisType() == ChartAxis::X_AXIS ? rect.left() : rect.bottom();
148 oldLayout[j] = item->axisType() == ChartAxis::X_AXIS ? rect.right() : rect.top();
148 oldLayout[j] = item->axisType() == ChartAxis::X_AXIS ? rect.right() : rect.top();
149 }
149 }
150 }
150 }
151 break;
151 break;
152 case ZoomInState: {
152 case ZoomInState: {
153 int index = qMin(oldLayout.count() * (item->axisType() == ChartAxis::X_AXIS ? m_point.x() : (1 - m_point.y())), newLayout.count() - 1.0);
153 int index = qMin(oldLayout.count() * (item->axisType() == ChartAxis::X_AXIS ? m_point.x() : (1 - m_point.y())), newLayout.count() - 1.0);
154 oldLayout.resize(newLayout.count());
154 oldLayout.resize(newLayout.count());
155
155
156 for(int i = 0; i < oldLayout.count(); i++)
156 for(int i = 0; i < oldLayout.count(); i++)
157 oldLayout[i]= oldLayout[index];
157 oldLayout[i]= oldLayout[index];
158 }
158 }
159 break;
159 break;
160 case ScrollDownState:
160 case ScrollDownState:
161 case ScrollRightState: {
161 case ScrollRightState: {
162 oldLayout.resize(newLayout.count());
162 oldLayout.resize(newLayout.count());
163
163
164 for(int i = 0, j = i + 1; i < oldLayout.count() - 1; ++i, ++j)
164 for(int i = 0, j = i + 1; i < oldLayout.count() - 1; ++i, ++j)
165 oldLayout[i]= oldLayout[j];
165 oldLayout[i]= oldLayout[j];
166 }
166 }
167 break;
167 break;
168 case ScrollUpState:
168 case ScrollUpState:
169 case ScrollLeftState: {
169 case ScrollLeftState: {
170 oldLayout.resize(newLayout.count());
170 oldLayout.resize(newLayout.count());
171
171
172 for(int i = oldLayout.count() - 1, j = i - 1; i > 0; --i, --j)
172 for(int i = oldLayout.count() - 1, j = i - 1; i > 0; --i, --j)
173 oldLayout[i]= oldLayout[j];
173 oldLayout[i]= oldLayout[j];
174 }
174 }
175 break;
175 break;
176 default: {
176 default: {
177 oldLayout.resize(newLayout.count());
177 oldLayout.resize(newLayout.count());
178 QRectF rect = item->geometry();
178 QRectF rect = item->geometry();
179 for(int i = 0, j = oldLayout.count() - 1; i < oldLayout.count(); ++i, --j)
179 for(int i = 0, j = oldLayout.count() - 1; i < oldLayout.count(); ++i, --j)
180 oldLayout[i] = item->axisType() == ChartAxis::X_AXIS ? rect.left() : rect.top();
180 oldLayout[i] = item->axisType() == ChartAxis::X_AXIS ? rect.left() : rect.top();
181 }
181 }
182 break;
182 break;
183 }
183 }
184
184
185
185
186 if (animation->state() != QAbstractAnimation::Stopped)
186 if (animation->state() != QAbstractAnimation::Stopped)
187 animation->stop();
187 animation->stop();
188
188
189 animation->setDuration(duration);
189 animation->setDuration(duration);
190 animation->setEasingCurve(QEasingCurve::OutQuart);
190 animation->setEasingCurve(QEasingCurve::OutQuart);
191 QVariantAnimation::KeyValues value;
191 QVariantAnimation::KeyValues value;
192 animation->setKeyValues(value); //workaround for wrong interpolation call
192 animation->setKeyValues(value); //workaround for wrong interpolation call
193 animation->setKeyValueAt(0.0, qVariantFromValue(oldLayout));
193 animation->setKeyValueAt(0.0, qVariantFromValue(oldLayout));
194 animation->setKeyValueAt(1.0, qVariantFromValue(newLayout));
194 animation->setKeyValueAt(1.0, qVariantFromValue(newLayout));
195
195
196 QTimer::singleShot(0, animation, SLOT(start()));
196 QTimer::singleShot(0, animation, SLOT(start()));
197 }
197 }
198
198
199 void ChartAnimator::updateLayout(SplineChartItem *item, QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints, QVector<QPointF> &oldControlPoints, QVector<QPointF> &newControlPoints, int index)
199 void ChartAnimator::updateLayout(SplineChartItem *item, QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints, QVector<QPointF> &oldControlPoints, QVector<QPointF> &newControlPoints, int index)
200 {
200 {
201 SplineAnimation *animation = static_cast<SplineAnimation *>(m_animations.value(item));
201 SplineAnimation *animation = static_cast<SplineAnimation *>(m_animations.value(item));
202
202
203 Q_ASSERT(animation);
203 Q_ASSERT(animation);
204
204
205 if (newPoints.count() < 2 || newControlPoints.count() < 2)
205 if (newPoints.count() < 2 || newControlPoints.count() < 2)
206 return;
206 return;
207
207
208 bool empty = oldPoints.count() == 0;
208 bool empty = oldPoints.count() == 0;
209
209
210
210
211 if (animation->state() != QAbstractAnimation::Stopped)
211 if (animation->state() != QAbstractAnimation::Stopped)
212 animation->stop();
212 animation->stop();
213
213
214 animation->setDuration(duration);
214 animation->setDuration(duration);
215 if (!empty)
215 if (!empty)
216 animation->setAnimationType(ChartAnimation::MoveDownAnimation);
216 animation->setAnimationType(ChartAnimation::MoveDownAnimation);
217 else
217 else
218 animation->setAnimationType(ChartAnimation::LineDrawAnimation);
218 animation->setAnimationType(ChartAnimation::LineDrawAnimation);
219
219
220 animation->setEasingCurve(QEasingCurve::OutQuart);
220 animation->setEasingCurve(QEasingCurve::OutQuart);
221 animation->setValues(oldPoints, newPoints, oldControlPoints, newControlPoints, index);
221 animation->setValues(oldPoints, newPoints, oldControlPoints, newControlPoints, index);
222
222
223 QTimer::singleShot(0, animation, SLOT(start()));
223 QTimer::singleShot(0, animation, SLOT(start()));
224 }
224 }
225
225
226
226
227 void ChartAnimator::updateLayout(XYChartItem *item, QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints, int index)
227 void ChartAnimator::updateLayout(XYChartItem *item, QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints, int index)
228 {
228 {
229 XYAnimation *animation = static_cast<XYAnimation *>(m_animations.value(item));
229 XYAnimation *animation = static_cast<XYAnimation *>(m_animations.value(item));
230
230
231 Q_ASSERT(animation);
231 Q_ASSERT(animation);
232
232
233 if (newPoints.count() == 0)
233 if (newPoints.count() == 0)
234 return;
234 return;
235
235
236 bool empty = oldPoints.count() == 0;
236 bool empty = oldPoints.count() == 0;
237
237
238
238
239 if (animation->state() != QAbstractAnimation::Stopped)
239 if (animation->state() != QAbstractAnimation::Stopped)
240 animation->stop();
240 animation->stop();
241
241
242 animation->setDuration(duration);
242 animation->setDuration(duration);
243 if (!empty)
243 if (!empty)
244 animation->setAnimationType(ChartAnimation::MoveDownAnimation);
244 animation->setAnimationType(ChartAnimation::MoveDownAnimation);
245 else
245 else
246 animation->setAnimationType(ChartAnimation::LineDrawAnimation);
246 animation->setAnimationType(ChartAnimation::LineDrawAnimation);
247
247
248 animation->setEasingCurve(QEasingCurve::OutQuart);
248 animation->setEasingCurve(QEasingCurve::OutQuart);
249 animation->setValues(oldPoints, newPoints, index);
249 animation->setValues(oldPoints, newPoints, index);
250
250
251 QTimer::singleShot(0, animation, SLOT(start()));
251 QTimer::singleShot(0, animation, SLOT(start()));
252 }
252 }
253
253
254 void ChartAnimator::addAnimation(PieChartItem *item, QPieSlice *slice, const PieSliceData &sliceData, bool isEmpty)
254 void ChartAnimator::addAnimation(PieChartItem *item, PieSliceItem *sliceItem, const PieSliceData &sliceData, bool isEmpty)
255 {
255 {
256 PieAnimation *animation = static_cast<PieAnimation *>(m_animations.value(item));
256 PieAnimation *animation = static_cast<PieAnimation *>(m_animations.value(item));
257 Q_ASSERT(animation);
257 Q_ASSERT(animation);
258 animation->addSlice(slice, sliceData, isEmpty);
258 animation->addSlice(sliceItem, sliceData, isEmpty);
259 }
259 }
260
260
261 void ChartAnimator::removeAnimation(PieChartItem *item, QPieSlice *slice)
261 void ChartAnimator::removeAnimation(PieChartItem *item, PieSliceItem *sliceItem)
262 {
262 {
263 PieAnimation *animation = static_cast<PieAnimation *>(m_animations.value(item));
263 PieAnimation *animation = static_cast<PieAnimation *>(m_animations.value(item));
264 Q_ASSERT(animation);
264 Q_ASSERT(animation);
265 animation->removeSlice(slice);
265 animation->removeSlice(sliceItem);
266 }
266 }
267
267
268 void ChartAnimator::updateLayout(PieChartItem *item, const PieLayout &layout)
268 void ChartAnimator::updateLayout(PieChartItem *item, const PieLayout &layout)
269 {
269 {
270 PieAnimation *animation = static_cast<PieAnimation *>(m_animations.value(item));
270 PieAnimation *animation = static_cast<PieAnimation *>(m_animations.value(item));
271 Q_ASSERT(animation);
271 Q_ASSERT(animation);
272 animation->updateValues(layout);
272 animation->updateValues(layout);
273 }
273 }
274
274
275 void ChartAnimator::updateLayout(PieChartItem *item, QPieSlice *slice, const PieSliceData &sliceData)
275 void ChartAnimator::updateLayout(PieChartItem *item, PieSliceItem *sliceItem, const PieSliceData &sliceData)
276 {
276 {
277 PieAnimation *animation = static_cast<PieAnimation *>(m_animations.value(item));
277 PieAnimation *animation = static_cast<PieAnimation *>(m_animations.value(item));
278 Q_ASSERT(animation);
278 Q_ASSERT(animation);
279 animation->updateValue(slice, sliceData);
279 animation->updateValue(sliceItem, sliceData);
280 }
280 }
281
281
282 void ChartAnimator::updateLayout(BarChartItem *item, const QVector<QRectF> &oldLayout, const QVector<QRectF> &newLayout)
282 void ChartAnimator::updateLayout(BarChartItem *item, const QVector<QRectF> &oldLayout, const QVector<QRectF> &newLayout)
283 {
283 {
284 BarAnimation *animation = static_cast<BarAnimation *>(m_animations.value(item));
284 BarAnimation *animation = static_cast<BarAnimation *>(m_animations.value(item));
285 Q_ASSERT(animation);
285 Q_ASSERT(animation);
286 animation->setDuration(duration);
286 animation->setDuration(duration);
287 animation->setKeyValueAt(0.0, qVariantFromValue(oldLayout));
287 animation->setKeyValueAt(0.0, qVariantFromValue(oldLayout));
288 animation->setKeyValueAt(1.0, qVariantFromValue(newLayout));
288 animation->setKeyValueAt(1.0, qVariantFromValue(newLayout));
289 QTimer::singleShot(0, animation, SLOT(start()));
289 QTimer::singleShot(0, animation, SLOT(start()));
290 }
290 }
291
291
292
292
293 void ChartAnimator::setState(State state, const QPointF &point)
293 void ChartAnimator::setState(State state, const QPointF &point)
294 {
294 {
295 m_state = state;
295 m_state = state;
296 m_point = point;
296 m_point = point;
297 }
297 }
298
298
299 #include "moc_chartanimator_p.cpp"
299 #include "moc_chartanimator_p.cpp"
300
300
301 QTCOMMERCIALCHART_END_NAMESPACE
301 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,79 +1,79
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef CHARTANIMATOR_P_H
21 #ifndef CHARTANIMATOR_P_H
22 #define CHARTANIMATOR_P_H
22 #define CHARTANIMATOR_P_H
23
23
24 #include "qchartglobal.h"
24 #include "qchartglobal.h"
25 #include "chartanimation_p.h"
25 #include "chartanimation_p.h"
26 #include "piechartitem_p.h"
26 #include "piechartitem_p.h"
27 #include "barchartitem_p.h"
27 #include "barchartitem_p.h"
28 #include <QPointF>
28 #include <QPointF>
29
29
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31
31
32 class ChartItem;
32 class ChartItem;
33 class ChartAxis;
33 class ChartAxis;
34 class AreaChartItem;
34 class AreaChartItem;
35 class SplineChartItem;
35 class SplineChartItem;
36 class ScatterChartItem;
36 class ScatterChartItem;
37 class LineChartItem;
37 class LineChartItem;
38 class XYChartItem;
38 class XYChartItem;
39
39
40 class ChartAnimator : public QObject
40 class ChartAnimator : public QObject
41 {
41 {
42 Q_OBJECT
42 Q_OBJECT
43 public:
43 public:
44 enum State{ShowState, ScrollUpState, ScrollDownState, ScrollLeftState, ScrollRightState, ZoomInState, ZoomOutState};
44 enum State{ShowState, ScrollUpState, ScrollDownState, ScrollLeftState, ScrollRightState, ZoomInState, ZoomOutState};
45
45
46 ChartAnimator(QObject *parent = 0);
46 ChartAnimator(QObject *parent = 0);
47 virtual ~ChartAnimator();
47 virtual ~ChartAnimator();
48
48
49 void addAnimation(ChartAxis *item);
49 void addAnimation(ChartAxis *item);
50 void addAnimation(PieChartItem *item);
50 void addAnimation(PieChartItem *item);
51 void addAnimation(ScatterChartItem *item);
51 void addAnimation(ScatterChartItem *item);
52 void addAnimation(LineChartItem *item);
52 void addAnimation(LineChartItem *item);
53 void addAnimation(SplineChartItem *item);
53 void addAnimation(SplineChartItem *item);
54 void addAnimation(BarChartItem *item);
54 void addAnimation(BarChartItem *item);
55 void removeAnimation(Chart *item);
55 void removeAnimation(Chart *item);
56
56
57 void animationStarted();
57 void animationStarted();
58 void updateLayout(XYChartItem *item, QVector<QPointF> &oldLayout, QVector<QPointF> &newLayout, int index);
58 void updateLayout(XYChartItem *item, QVector<QPointF> &oldLayout, QVector<QPointF> &newLayout, int index);
59 void updateLayout(SplineChartItem *item, QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints, QVector<QPointF> &oldControlPoints, QVector<QPointF> &newContorlPoints, int index);
59 void updateLayout(SplineChartItem *item, QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints, QVector<QPointF> &oldControlPoints, QVector<QPointF> &newContorlPoints, int index);
60 void updateLayout(ChartAxis *item, QVector<qreal> &layout);
60 void updateLayout(ChartAxis *item, QVector<qreal> &layout);
61
61
62 void addAnimation(PieChartItem *item, QPieSlice *slice, const PieSliceData &sliceData, bool isEmpty);
62 void addAnimation(PieChartItem *item, PieSliceItem *sliceItem, const PieSliceData &sliceData, bool isEmpty);
63 void removeAnimation(PieChartItem *item, QPieSlice *slice);
63 void removeAnimation(PieChartItem *item, PieSliceItem *sliceItem);
64 void updateLayout(PieChartItem *item, const PieLayout &layout);
64 void updateLayout(PieChartItem *item, const PieLayout &layout);
65 void updateLayout(PieChartItem *item, QPieSlice *slice, const PieSliceData &sliceData);
65 void updateLayout(PieChartItem *item, PieSliceItem *sliceItem, const PieSliceData &sliceData);
66
66
67 void updateLayout(BarChartItem *item, const QVector<QRectF> &oldLayout, const QVector<QRectF> &newLayout);
67 void updateLayout(BarChartItem *item, const QVector<QRectF> &oldLayout, const QVector<QRectF> &newLayout);
68
68
69 void setState(State state,const QPointF &point = QPointF());
69 void setState(State state,const QPointF &point = QPointF());
70
70
71 private:
71 private:
72 QMap<Chart *, ChartAnimation *> m_animations;
72 QMap<Chart *, ChartAnimation *> m_animations;
73 State m_state;
73 State m_state;
74 QPointF m_point;
74 QPointF m_point;
75 };
75 };
76
76
77 QTCOMMERCIALCHART_END_NAMESPACE
77 QTCOMMERCIALCHART_END_NAMESPACE
78
78
79 #endif
79 #endif
@@ -1,112 +1,106
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "pieanimation_p.h"
21 #include "pieanimation_p.h"
22 #include "piesliceanimation_p.h"
22 #include "piesliceanimation_p.h"
23 #include "piechartitem_p.h"
23 #include "piechartitem_p.h"
24 #include <QParallelAnimationGroup>
24 #include <QParallelAnimationGroup>
25 #include <QTimer>
25 #include <QTimer>
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 PieAnimation::PieAnimation(PieChartItem *item)
29 PieAnimation::PieAnimation(PieChartItem *item)
30 :ChartAnimation(item),
30 :ChartAnimation(item),
31 m_item(item)
31 m_item(item)
32 {
32 {
33 }
33 }
34
34
35 PieAnimation::~PieAnimation()
35 PieAnimation::~PieAnimation()
36 {
36 {
37 }
37 }
38
38
39 void PieAnimation::updateValues(const PieLayout &newValues)
39 void PieAnimation::updateValues(const PieLayout &newValues)
40 {
40 {
41 foreach (QPieSlice *s, newValues.keys())
41 foreach (PieSliceItem *s, newValues.keys())
42 updateValue(s, newValues.value(s));
42 updateValue(s, newValues.value(s));
43 }
43 }
44
44
45 void PieAnimation::updateValue(QPieSlice *slice, const PieSliceData &sliceData)
45 void PieAnimation::updateValue(PieSliceItem *sliceItem, const PieSliceData &sliceData)
46 {
46 {
47 PieSliceAnimation *animation = m_animations.value(slice);
47 PieSliceAnimation *animation = m_animations.value(sliceItem);
48 Q_ASSERT(animation);
48 Q_ASSERT(animation);
49 animation->stop();
49 animation->stop();
50
50
51 animation->updateValue(sliceData);
51 animation->updateValue(sliceData);
52 animation->setDuration(1000);
52 animation->setDuration(1000);
53 animation->setEasingCurve(QEasingCurve::OutQuart);
53 animation->setEasingCurve(QEasingCurve::OutQuart);
54
54
55 QTimer::singleShot(0, animation, SLOT(start()));
55 QTimer::singleShot(0, animation, SLOT(start()));
56 }
56 }
57
57
58 void PieAnimation::addSlice(QPieSlice *slice, const PieSliceData &sliceData, bool isEmpty)
58 void PieAnimation::addSlice(PieSliceItem *sliceItem, const PieSliceData &sliceData, bool isEmpty)
59 {
59 {
60 PieSliceAnimation *animation = new PieSliceAnimation(m_item, slice);
60 PieSliceAnimation *animation = new PieSliceAnimation(sliceItem);
61 m_animations.insert(slice, animation);
61 m_animations.insert(sliceItem, animation);
62
62
63 PieSliceData startValue = sliceData;
63 PieSliceData startValue = sliceData;
64 startValue.m_radius = 0;
64 startValue.m_radius = 0;
65 if (isEmpty)
65 if (isEmpty)
66 startValue.m_startAngle = 0;
66 startValue.m_startAngle = 0;
67 else
67 else
68 startValue.m_startAngle = sliceData.m_startAngle + (sliceData.m_angleSpan / 2);
68 startValue.m_startAngle = sliceData.m_startAngle + (sliceData.m_angleSpan / 2);
69 startValue.m_angleSpan = 0;
69 startValue.m_angleSpan = 0;
70 animation->setValue(startValue, sliceData);
70 animation->setValue(startValue, sliceData);
71
71
72 animation->setDuration(1000);
72 animation->setDuration(1000);
73 animation->setEasingCurve(QEasingCurve::OutQuart);
73 animation->setEasingCurve(QEasingCurve::OutQuart);
74 QTimer::singleShot(0, animation, SLOT(start()));
74 QTimer::singleShot(0, animation, SLOT(start()));
75 }
75 }
76
76
77 void PieAnimation::removeSlice(QPieSlice *slice)
77 void PieAnimation::removeSlice(PieSliceItem *sliceItem)
78 {
78 {
79 PieSliceAnimation *animation = m_animations.value(slice);
79 PieSliceAnimation *animation = m_animations.value(sliceItem);
80 Q_ASSERT(animation);
80 Q_ASSERT(animation);
81 animation->stop();
81 animation->stop();
82
82
83 PieSliceData endValue = animation->currentSliceValue();
83 PieSliceData endValue = animation->currentSliceValue();
84 endValue.m_radius = 0;
84 endValue.m_radius = 0;
85 // TODO: find the actual angle where this slice disappears
86 endValue.m_startAngle = endValue.m_startAngle + endValue.m_angleSpan;
85 endValue.m_startAngle = endValue.m_startAngle + endValue.m_angleSpan;
87 endValue.m_angleSpan = 0;
86 endValue.m_angleSpan = 0;
88
87
89 animation->updateValue(endValue);
88 animation->updateValue(endValue);
90 animation->setDuration(1000);
89 animation->setDuration(1000);
91 animation->setEasingCurve(QEasingCurve::OutQuart);
90 animation->setEasingCurve(QEasingCurve::OutQuart);
92
91
93 connect(animation, SIGNAL(finished()), this, SLOT(destroySliceAnimationComplete()));
92 // PieSliceItem is the parent of PieSliceAnimation so the animation will be deleted as well..
93 connect(animation, SIGNAL(finished()), sliceItem, SLOT(deleteLater()));
94 m_animations.remove(sliceItem);
95
94 QTimer::singleShot(0, animation, SLOT(start()));
96 QTimer::singleShot(0, animation, SLOT(start()));
95 }
97 }
96
98
97 void PieAnimation::updateCurrentValue(const QVariant &)
99 void PieAnimation::updateCurrentValue(const QVariant &)
98 {
100 {
99 // nothing to do...
101 // nothing to do...
100 }
102 }
101
103
102 void PieAnimation::destroySliceAnimationComplete()
103 {
104 PieSliceAnimation *animation = static_cast<PieSliceAnimation *>(sender());
105 QPieSlice *slice = m_animations.key(animation);
106 m_item->destroySlice(slice);
107 delete m_animations.take(slice);
108 }
109
110 #include "moc_pieanimation_p.cpp"
104 #include "moc_pieanimation_p.cpp"
111
105
112 QTCOMMERCIALCHART_END_NAMESPACE
106 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,57 +1,54
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef PIEANIMATION_P_H
21 #ifndef PIEANIMATION_P_H
22 #define PIEANIMATION_P_H
22 #define PIEANIMATION_P_H
23
23
24 #include "chartanimation_p.h"
24 #include "chartanimation_p.h"
25 #include "piechartitem_p.h"
25 #include "piechartitem_p.h"
26 #include "piesliceanimation_p.h"
26 #include "piesliceanimation_p.h"
27
27
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29
29
30 class PieChartItem;
30 class PieChartItem;
31
31
32 class PieAnimation : public ChartAnimation
32 class PieAnimation : public ChartAnimation
33 {
33 {
34 Q_OBJECT
34 Q_OBJECT
35
35
36 public:
36 public:
37 PieAnimation(PieChartItem *item);
37 PieAnimation(PieChartItem *item);
38 ~PieAnimation();
38 ~PieAnimation();
39 void updateValues(const PieLayout &newValues);
39 void updateValues(const PieLayout &newValues);
40 void updateValue(QPieSlice *slice, const PieSliceData &newValue);
40 void updateValue(PieSliceItem *sliceItem, const PieSliceData &newValue);
41 void addSlice(QPieSlice *slice, const PieSliceData &endValue, bool isEmpty);
41 void addSlice(PieSliceItem *sliceItem, const PieSliceData &endValue, bool isEmpty);
42 void removeSlice(QPieSlice *slice);
42 void removeSlice(PieSliceItem *sliceItem);
43
43
44 public: // from QVariantAnimation
44 public: // from QVariantAnimation
45 void updateCurrentValue(const QVariant &value);
45 void updateCurrentValue(const QVariant &value);
46
46
47 public Q_SLOTS:
48 void destroySliceAnimationComplete();
49
50 private:
47 private:
51 PieChartItem *m_item;
48 PieChartItem *m_item;
52 QHash<QPieSlice *, PieSliceAnimation *> m_animations;
49 QHash<PieSliceItem *, PieSliceAnimation *> m_animations;
53 };
50 };
54
51
55 QTCOMMERCIALCHART_END_NAMESPACE
52 QTCOMMERCIALCHART_END_NAMESPACE
56
53
57 #endif
54 #endif
@@ -1,127 +1,127
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "piesliceanimation_p.h"
21 #include "piesliceanimation_p.h"
22 #include "piechartitem_p.h"
22 #include "piechartitem_p.h"
23 #include "qpieslice.h"
24
23
25 Q_DECLARE_METATYPE(QTCOMMERCIALCHART_NAMESPACE::PieSliceData)
24 Q_DECLARE_METATYPE(QTCOMMERCIALCHART_NAMESPACE::PieSliceData)
26
25
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
27
29 qreal linearPos(qreal start, qreal end, qreal pos)
28 qreal linearPos(qreal start, qreal end, qreal pos)
30 {
29 {
31 return start + ((end - start) * pos);
30 return start + ((end - start) * pos);
32 }
31 }
33
32
34 QPointF linearPos(QPointF start, QPointF end, qreal pos)
33 QPointF linearPos(QPointF start, QPointF end, qreal pos)
35 {
34 {
36 qreal x = linearPos(start.x(), end.x(), pos);
35 qreal x = linearPos(start.x(), end.x(), pos);
37 qreal y = linearPos(start.y(), end.y(), pos);
36 qreal y = linearPos(start.y(), end.y(), pos);
38 return QPointF(x, y);
37 return QPointF(x, y);
39 }
38 }
40
39
41 QPen linearPos(QPen start, QPen end, qreal pos)
40 QPen linearPos(QPen start, QPen end, qreal pos)
42 {
41 {
43 QColor c;
42 QColor c;
44 c.setRedF(linearPos(start.color().redF(), end.color().redF(), pos));
43 c.setRedF(linearPos(start.color().redF(), end.color().redF(), pos));
45 c.setGreenF(linearPos(start.color().greenF(), end.color().greenF(), pos));
44 c.setGreenF(linearPos(start.color().greenF(), end.color().greenF(), pos));
46 c.setBlueF(linearPos(start.color().blueF(), end.color().blueF(), pos));
45 c.setBlueF(linearPos(start.color().blueF(), end.color().blueF(), pos));
47 end.setColor(c);
46 end.setColor(c);
48 return end;
47 return end;
49 }
48 }
50
49
51 QBrush linearPos(QBrush start, QBrush end, qreal pos)
50 QBrush linearPos(QBrush start, QBrush end, qreal pos)
52 {
51 {
53 QColor c;
52 QColor c;
54 c.setRedF(linearPos(start.color().redF(), end.color().redF(), pos));
53 c.setRedF(linearPos(start.color().redF(), end.color().redF(), pos));
55 c.setGreenF(linearPos(start.color().greenF(), end.color().greenF(), pos));
54 c.setGreenF(linearPos(start.color().greenF(), end.color().greenF(), pos));
56 c.setBlueF(linearPos(start.color().blueF(), end.color().blueF(), pos));
55 c.setBlueF(linearPos(start.color().blueF(), end.color().blueF(), pos));
57 end.setColor(c);
56 end.setColor(c);
58 return end;
57 return end;
59 }
58 }
60
59
61 PieSliceAnimation::PieSliceAnimation(PieChartItem *item, QPieSlice *slice)
60 PieSliceAnimation::PieSliceAnimation(PieSliceItem *sliceItem)
62 :QVariantAnimation(item),
61 :QVariantAnimation(sliceItem),
63 m_item(item),
62 m_sliceItem(sliceItem)
64 m_slice(slice)
65 {
63 {
66 }
64 }
67
65
68 PieSliceAnimation::~PieSliceAnimation()
66 PieSliceAnimation::~PieSliceAnimation()
69 {
67 {
70 }
68 }
71
69
72 void PieSliceAnimation::setValue(const PieSliceData &startValue, const PieSliceData &endValue)
70 void PieSliceAnimation::setValue(const PieSliceData &startValue, const PieSliceData &endValue)
73 {
71 {
74 if (state() != QAbstractAnimation::Stopped)
72 if (state() != QAbstractAnimation::Stopped)
75 stop();
73 stop();
76
74
77 m_currentValue = startValue;
75 m_currentValue = startValue;
78
76
79 setKeyValueAt(0.0, qVariantFromValue(startValue));
77 setKeyValueAt(0.0, qVariantFromValue(startValue));
80 setKeyValueAt(1.0, qVariantFromValue(endValue));
78 setKeyValueAt(1.0, qVariantFromValue(endValue));
81 }
79 }
82
80
83 void PieSliceAnimation::updateValue(const PieSliceData &endValue)
81 void PieSliceAnimation::updateValue(const PieSliceData &endValue)
84 {
82 {
85 if (state() != QAbstractAnimation::Stopped)
83 if (state() != QAbstractAnimation::Stopped)
86 stop();
84 stop();
87
85
88 setKeyValueAt(0.0, qVariantFromValue(m_currentValue));
86 setKeyValueAt(0.0, qVariantFromValue(m_currentValue));
89 setKeyValueAt(1.0, qVariantFromValue(endValue));
87 setKeyValueAt(1.0, qVariantFromValue(endValue));
90 }
88 }
91
89
92 PieSliceData PieSliceAnimation::currentSliceValue()
90 PieSliceData PieSliceAnimation::currentSliceValue()
93 {
91 {
94 // NOTE:
92 // NOTE:
95 // We must use an internal current value because QVariantAnimation::currentValue() is updated
93 // We must use an internal current value because QVariantAnimation::currentValue() is updated
96 // before the animation is actually started. So if we get 2 updateValue() calls in a row the currentValue()
94 // before the animation is actually started. So if we get 2 updateValue() calls in a row the currentValue()
97 // will have the end value set from the first call and the second call will interpolate that instead of
95 // will have the end value set from the first call and the second call will interpolate that instead of
98 // the original current value as it was before the first call.
96 // the original current value as it was before the first call.
99 return m_currentValue;
97 return m_currentValue;
100 }
98 }
101
99
102 QVariant PieSliceAnimation::interpolated(const QVariant &start, const QVariant &end, qreal progress) const
100 QVariant PieSliceAnimation::interpolated(const QVariant &start, const QVariant &end, qreal progress) const
103 {
101 {
104 PieSliceData startValue = qVariantValue<PieSliceData>(start);
102 PieSliceData startValue = qVariantValue<PieSliceData>(start);
105 PieSliceData endValue = qVariantValue<PieSliceData>(end);
103 PieSliceData endValue = qVariantValue<PieSliceData>(end);
106
104
107 PieSliceData result;
105 PieSliceData result;
108 result = endValue;
106 result = endValue;
109 result.m_center = linearPos(startValue.m_center, endValue.m_center, progress);
107 result.m_center = linearPos(startValue.m_center, endValue.m_center, progress);
110 result.m_radius = linearPos(startValue.m_radius, endValue.m_radius, progress);
108 result.m_radius = linearPos(startValue.m_radius, endValue.m_radius, progress);
111 result.m_startAngle = linearPos(startValue.m_startAngle, endValue.m_startAngle, progress);
109 result.m_startAngle = linearPos(startValue.m_startAngle, endValue.m_startAngle, progress);
112 result.m_angleSpan = linearPos(startValue.m_angleSpan, endValue.m_angleSpan, progress);
110 result.m_angleSpan = linearPos(startValue.m_angleSpan, endValue.m_angleSpan, progress);
113 result.m_slicePen = linearPos(startValue.m_slicePen, endValue.m_slicePen, progress);
111 result.m_slicePen = linearPos(startValue.m_slicePen, endValue.m_slicePen, progress);
114 result.m_sliceBrush = linearPos(startValue.m_sliceBrush, endValue.m_sliceBrush, progress);
112 result.m_sliceBrush = linearPos(startValue.m_sliceBrush, endValue.m_sliceBrush, progress);
115
113
116 return qVariantFromValue(result);
114 return qVariantFromValue(result);
117 }
115 }
118
116
119 void PieSliceAnimation::updateCurrentValue(const QVariant &value)
117 void PieSliceAnimation::updateCurrentValue(const QVariant &value)
120 {
118 {
121 if (state() != QAbstractAnimation::Stopped) { //workaround
119 if (state() != QAbstractAnimation::Stopped) { //workaround
122 m_currentValue = qVariantValue<PieSliceData>(value);
120 m_currentValue = qVariantValue<PieSliceData>(value);
123 m_item->setLayout(m_slice, m_currentValue);
121 m_sliceItem->setSliceData(m_currentValue);
122 m_sliceItem->updateGeometry();
123 m_sliceItem->update();
124 }
124 }
125 }
125 }
126
126
127 QTCOMMERCIALCHART_END_NAMESPACE
127 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,53 +1,51
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef PIESLICEANIMATION_P_H
21 #ifndef PIESLICEANIMATION_P_H
22 #define PIESLICEANIMATION_P_H
22 #define PIESLICEANIMATION_P_H
23
23
24 #include "piechartitem_p.h"
25 #include <QVariantAnimation>
24 #include <QVariantAnimation>
25 #include "piesliceitem_p.h"
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 class PieChartItem;
29 class PieChartItem;
30 class QPieSlice;
31
30
32 class PieSliceAnimation : public QVariantAnimation
31 class PieSliceAnimation : public QVariantAnimation
33 {
32 {
34 public:
33 public:
35 PieSliceAnimation(PieChartItem *item, QPieSlice *slice);
34 PieSliceAnimation(PieSliceItem *sliceItem);
36 ~PieSliceAnimation();
35 ~PieSliceAnimation();
37 void setValue(const PieSliceData &startValue, const PieSliceData &endValue);
36 void setValue(const PieSliceData &startValue, const PieSliceData &endValue);
38 void updateValue(const PieSliceData &endValue);
37 void updateValue(const PieSliceData &endValue);
39 PieSliceData currentSliceValue();
38 PieSliceData currentSliceValue();
40
39
41 protected:
40 protected:
42 QVariant interpolated(const QVariant &start, const QVariant &end, qreal progress) const;
41 QVariant interpolated(const QVariant &start, const QVariant &end, qreal progress) const;
43 void updateCurrentValue(const QVariant &value);
42 void updateCurrentValue(const QVariant &value);
44
43
45 private:
44 private:
46 PieChartItem *m_item;
45 PieSliceItem *m_sliceItem;
47 QPieSlice *m_slice;
48 PieSliceData m_currentValue;
46 PieSliceData m_currentValue;
49 };
47 };
50
48
51 QTCOMMERCIALCHART_END_NAMESPACE
49 QTCOMMERCIALCHART_END_NAMESPACE
52
50
53 #endif
51 #endif
@@ -1,212 +1,208
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "piechartitem_p.h"
21 #include "piechartitem_p.h"
22 #include "piesliceitem_p.h"
22 #include "piesliceitem_p.h"
23 #include "qpieslice.h"
23 #include "qpieslice.h"
24 #include "qpieseries.h"
24 #include "qpieseries.h"
25 #include "qpieseries_p.h"
25 #include "qpieseries_p.h"
26 #include "chartpresenter_p.h"
26 #include "chartpresenter_p.h"
27 #include "chartdataset_p.h"
27 #include "chartdataset_p.h"
28 #include "chartanimator_p.h"
28 #include "chartanimator_p.h"
29 #include <QPainter>
29 #include <QPainter>
30 #include <QTimer>
30 #include <QTimer>
31
31
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33
33
34 PieChartItem::PieChartItem(QPieSeries *series, ChartPresenter* presenter)
34 PieChartItem::PieChartItem(QPieSeries *series, ChartPresenter* presenter)
35 :ChartItem(presenter),
35 :ChartItem(presenter),
36 m_series(series)
36 m_series(series)
37 {
37 {
38 Q_ASSERT(series);
38 Q_ASSERT(series);
39
39
40 QPieSeriesPrivate *d = QPieSeriesPrivate::seriesData(*series);
40 QPieSeriesPrivate *d = QPieSeriesPrivate::seriesData(*series);
41 connect(d, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleSlicesAdded(QList<QPieSlice*>)));
41 connect(d, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleSlicesAdded(QList<QPieSlice*>)));
42 connect(d, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleSlicesRemoved(QList<QPieSlice*>)));
42 connect(d, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleSlicesRemoved(QList<QPieSlice*>)));
43 connect(d, SIGNAL(piePositionChanged()), this, SLOT(handlePieLayoutChanged()));
43 connect(d, SIGNAL(piePositionChanged()), this, SLOT(handlePieLayoutChanged()));
44 connect(d, SIGNAL(pieSizeChanged()), this, SLOT(handlePieLayoutChanged()));
44 connect(d, SIGNAL(pieSizeChanged()), this, SLOT(handlePieLayoutChanged()));
45
45
46 QTimer::singleShot(0, this, SLOT(initialize())); // TODO: get rid of this
46 QTimer::singleShot(0, this, SLOT(initialize())); // TODO: get rid of this
47
47
48 // Note: the following does not affect as long as the item does not have anything to paint
48 // Note: the following does not affect as long as the item does not have anything to paint
49 setZValue(ChartPresenter::PieSeriesZValue);
49 setZValue(ChartPresenter::PieSeriesZValue);
50 }
50 }
51
51
52 PieChartItem::~PieChartItem()
52 PieChartItem::~PieChartItem()
53 {
53 {
54 // slices deleted automatically through QGraphicsItem
54 // slices deleted automatically through QGraphicsItem
55 }
55 }
56
56
57 void PieChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
57 void PieChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
58 {
58 {
59 Q_UNUSED(painter)
59 Q_UNUSED(painter)
60 // TODO: paint shadows for all components
60 // TODO: paint shadows for all components
61 // - get paths from items & merge & offset and draw with shadow color?
61 // - get paths from items & merge & offset and draw with shadow color?
62 //painter->setBrush(QBrush(Qt::red));
62 //painter->setBrush(QBrush(Qt::red));
63 //painter->drawRect(m_debugRect);
63 //painter->drawRect(m_debugRect);
64 }
64 }
65
65
66 void PieChartItem::initialize()
66 void PieChartItem::initialize()
67 {
67 {
68 handleSlicesAdded(m_series->slices());
68 handleSlicesAdded(m_series->slices());
69 }
69 }
70
70
71 void PieChartItem::handleSlicesAdded(QList<QPieSlice*> slices)
71 void PieChartItem::handleSlicesAdded(QList<QPieSlice*> slices)
72 {
72 {
73 bool isEmpty = m_slices.isEmpty();
73 bool isEmpty = m_slices.isEmpty();
74
74
75 presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series));
75 presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series));
76
76
77 foreach (QPieSlice *s, slices) {
77 foreach (QPieSlice *s, slices) {
78 PieSliceItem* item = new PieSliceItem(this);
78 PieSliceItem* item = new PieSliceItem(this);
79 m_slices.insert(s, item);
79 m_slices.insert(s, item);
80 connect(s, SIGNAL(changed()), this, SLOT(handleSliceChanged()));
80 connect(s, SIGNAL(changed()), this, SLOT(handleSliceChanged()));
81 connect(item, SIGNAL(clicked(Qt::MouseButtons)), s, SIGNAL(clicked()));
81 connect(item, SIGNAL(clicked(Qt::MouseButtons)), s, SIGNAL(clicked()));
82 connect(item, SIGNAL(hovered(bool)), s, SIGNAL(hovered(bool)));
82 connect(item, SIGNAL(hovered(bool)), s, SIGNAL(hovered(bool)));
83
83
84 PieSliceData data = sliceData(s);
84 PieSliceData data = sliceData(s);
85
85
86 if (animator())
86 if (animator())
87 animator()->addAnimation(this, s, data, isEmpty);
87 animator()->addAnimation(this, item, data, isEmpty);
88 else
88 else
89 setLayout(s, data);
89 setLayout(item, data);
90 }
90 }
91 }
91 }
92
92
93 void PieChartItem::handleSlicesRemoved(QList<QPieSlice*> slices)
93 void PieChartItem::handleSlicesRemoved(QList<QPieSlice*> slices)
94 {
94 {
95 presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series));
95 presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series));
96
96
97 foreach (QPieSlice *s, slices) {
97 foreach (QPieSlice *slice, slices) {
98
99 PieSliceItem *item = m_slices.value(slice);
100 Q_ASSERT(item);
101 m_slices.remove(slice);
102
98 if (animator())
103 if (animator())
99 animator()->removeAnimation(this, s);
104 animator()->removeAnimation(this, item); // animator deletes the PieSliceItem
100 else
105 else
101 destroySlice(s);
106 delete item;
102 }
107 }
103 }
108 }
104
109
105 void PieChartItem::handlePieLayoutChanged()
110 void PieChartItem::handlePieLayoutChanged()
106 {
111 {
107 PieLayout layout = calculateLayout();
112 PieLayout layout = calculateLayout();
108 applyLayout(layout);
113 applyLayout(layout);
109 update();
114 update();
110 }
115 }
111
116
112 void PieChartItem::handleSliceChanged()
117 void PieChartItem::handleSliceChanged()
113 {
118 {
114 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
119 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
115 Q_ASSERT(m_slices.contains(slice));
120 Q_ASSERT(m_slices.contains(slice));
116 PieSliceData data = sliceData(slice);
121 PieSliceData data = sliceData(slice);
117 updateLayout(slice, data);
122 updateLayout(m_slices.value(slice), data);
118 update();
123 update();
119 }
124 }
120
125
121 void PieChartItem::handleDomainChanged(qreal, qreal, qreal, qreal)
126 void PieChartItem::handleDomainChanged(qreal, qreal, qreal, qreal)
122 {
127 {
123 // TODO
128 // TODO
124 }
129 }
125
130
126 void PieChartItem::handleGeometryChanged(const QRectF& rect)
131 void PieChartItem::handleGeometryChanged(const QRectF& rect)
127 {
132 {
128 prepareGeometryChange();
133 prepareGeometryChange();
129 m_rect = rect;
134 m_rect = rect;
130 handlePieLayoutChanged();
135 handlePieLayoutChanged();
131 }
136 }
132
137
133 void PieChartItem::calculatePieLayout()
138 void PieChartItem::calculatePieLayout()
134 {
139 {
135 // find pie center coordinates
140 // find pie center coordinates
136 m_pieCenter.setX(m_rect.left() + (m_rect.width() * m_series->horizontalPosition()));
141 m_pieCenter.setX(m_rect.left() + (m_rect.width() * m_series->horizontalPosition()));
137 m_pieCenter.setY(m_rect.top() + (m_rect.height() * m_series->verticalPosition()));
142 m_pieCenter.setY(m_rect.top() + (m_rect.height() * m_series->verticalPosition()));
138
143
139 // find maximum radius for pie
144 // find maximum radius for pie
140 m_pieRadius = m_rect.height() / 2;
145 m_pieRadius = m_rect.height() / 2;
141 if (m_rect.width() < m_rect.height())
146 if (m_rect.width() < m_rect.height())
142 m_pieRadius = m_rect.width() / 2;
147 m_pieRadius = m_rect.width() / 2;
143
148
144 // apply size factor
149 // apply size factor
145 m_pieRadius *= m_series->pieSize();
150 m_pieRadius *= m_series->pieSize();
146 }
151 }
147
152
148 PieSliceData PieChartItem::sliceData(QPieSlice *slice)
153 PieSliceData PieChartItem::sliceData(QPieSlice *slice)
149 {
154 {
150 // TODO: This function is kid of useless now. Refactor.
155 // TODO: This function is kid of useless now. Refactor.
151 PieSliceData sliceData = PieSliceData::data(slice);
156 PieSliceData sliceData = PieSliceData::data(slice);
152 sliceData.m_center = PieSliceItem::sliceCenter(m_pieCenter, m_pieRadius, slice);
157 sliceData.m_center = PieSliceItem::sliceCenter(m_pieCenter, m_pieRadius, slice);
153 sliceData.m_radius = m_pieRadius;
158 sliceData.m_radius = m_pieRadius;
154 return sliceData;
159 return sliceData;
155 }
160 }
156
161
157 PieLayout PieChartItem::calculateLayout()
162 PieLayout PieChartItem::calculateLayout()
158 {
163 {
159 calculatePieLayout();
164 calculatePieLayout();
160 PieLayout layout;
165 PieLayout layout;
161 foreach (QPieSlice* s, m_series->slices()) {
166 foreach (QPieSlice* s, m_series->slices()) {
162 if (m_slices.contains(s)) // calculate layout only for those slices that are already visible
167 if (m_slices.contains(s)) // calculate layout only for those slices that are already visible
163 layout.insert(s, sliceData(s));
168 layout.insert(m_slices.value(s), sliceData(s));
164 }
169 }
165 return layout;
170 return layout;
166 }
171 }
167
172
168 void PieChartItem::applyLayout(const PieLayout &layout)
173 void PieChartItem::applyLayout(const PieLayout &layout)
169 {
174 {
170 if (animator())
175 if (animator())
171 animator()->updateLayout(this, layout);
176 animator()->updateLayout(this, layout);
172 else
177 else
173 setLayout(layout);
178 setLayout(layout);
174 }
179 }
175
180
176 void PieChartItem::updateLayout(QPieSlice *slice, const PieSliceData &sliceData)
181 void PieChartItem::updateLayout(PieSliceItem *sliceItem, const PieSliceData &sliceData)
177 {
182 {
178 if (animator())
183 if (animator())
179 animator()->updateLayout(this, slice, sliceData);
184 animator()->updateLayout(this, sliceItem, sliceData);
180 else
185 else
181 setLayout(slice, sliceData);
186 setLayout(sliceItem, sliceData);
182 }
187 }
183
188
184 void PieChartItem::setLayout(const PieLayout &layout)
189 void PieChartItem::setLayout(const PieLayout &layout)
185 {
190 {
186 foreach (QPieSlice *slice, layout.keys()) {
191 foreach (PieSliceItem *item, layout.keys()) {
187 PieSliceItem *item = m_slices.value(slice);
192 item->setSliceData(layout.value(item));
188 Q_ASSERT(item);
189 item->setSliceData(layout.value(slice));
190 item->updateGeometry();
193 item->updateGeometry();
191 item->update();
194 item->update();
192 }
195 }
193 }
196 }
194
197
195 void PieChartItem::setLayout(QPieSlice *slice, const PieSliceData &sliceData)
198 void PieChartItem::setLayout(PieSliceItem *sliceItem, const PieSliceData &sliceData)
196 {
197 // find slice
198 PieSliceItem *item = m_slices.value(slice);
199 Q_ASSERT(item);
200 item->setSliceData(sliceData);
201 item->updateGeometry();
202 item->update();
203 }
204
205 void PieChartItem::destroySlice(QPieSlice *slice)
206 {
199 {
207 delete m_slices.take(slice);
200 Q_ASSERT(sliceItem);
201 sliceItem->setSliceData(sliceData);
202 sliceItem->updateGeometry();
203 sliceItem->update();
208 }
204 }
209
205
210 #include "moc_piechartitem_p.cpp"
206 #include "moc_piechartitem_p.cpp"
211
207
212 QTCOMMERCIALCHART_END_NAMESPACE
208 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,78 +1,77
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef PIECHARTITEM_H
21 #ifndef PIECHARTITEM_H
22 #define PIECHARTITEM_H
22 #define PIECHARTITEM_H
23
23
24 #include "qpieseries.h"
24 #include "qpieseries.h"
25 #include "chartitem_p.h"
25 #include "chartitem_p.h"
26 #include "piesliceitem_p.h"
26 #include "piesliceitem_p.h"
27
27
28 class QGraphicsItem;
28 class QGraphicsItem;
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 class QPieSlice;
30 class QPieSlice;
31 class ChartPresenter;
31 class ChartPresenter;
32
32
33 typedef QHash<QPieSlice*, PieSliceData> PieLayout;
33 typedef QHash<PieSliceItem*, PieSliceData> PieLayout;
34
34
35 class PieChartItem : public ChartItem
35 class PieChartItem : public ChartItem
36 {
36 {
37 Q_OBJECT
37 Q_OBJECT
38
38
39 public:
39 public:
40 // TODO: use a generic data class instead of x and y
40 // TODO: use a generic data class instead of x and y
41 PieChartItem(QPieSeries *series, ChartPresenter *presenter);
41 PieChartItem(QPieSeries *series, ChartPresenter *presenter);
42 ~PieChartItem();
42 ~PieChartItem();
43
43
44 public: // from QGraphicsItem
44 public: // from QGraphicsItem
45 QRectF boundingRect() const { return m_rect; }
45 QRectF boundingRect() const { return m_rect; }
46 void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *);
46 void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *);
47
47
48 public Q_SLOTS:
48 public Q_SLOTS:
49 void initialize();
49 void initialize();
50 void handleSlicesAdded(QList<QPieSlice*> slices);
50 void handleSlicesAdded(QList<QPieSlice*> slices);
51 void handleSlicesRemoved(QList<QPieSlice*> slices);
51 void handleSlicesRemoved(QList<QPieSlice*> slices);
52 void handlePieLayoutChanged();
52 void handlePieLayoutChanged();
53 void handleSliceChanged();
53 void handleSliceChanged();
54 void handleDomainChanged(qreal, qreal, qreal, qreal);
54 void handleDomainChanged(qreal, qreal, qreal, qreal);
55 void handleGeometryChanged(const QRectF& rect);
55 void handleGeometryChanged(const QRectF& rect);
56
56
57 public:
57 public:
58 void calculatePieLayout();
58 void calculatePieLayout();
59 PieSliceData sliceData(QPieSlice *slice);
59 PieSliceData sliceData(QPieSlice *slice);
60 PieLayout calculateLayout();
60 PieLayout calculateLayout();
61 void applyLayout(const PieLayout &layout);
61 void applyLayout(const PieLayout &layout);
62 void updateLayout(QPieSlice *slice, const PieSliceData &sliceData);
62 void updateLayout(PieSliceItem *sliceItem, const PieSliceData &sliceData);
63 void setLayout(const PieLayout &layout);
63 void setLayout(const PieLayout &layout);
64 void setLayout(QPieSlice *slice, const PieSliceData &sliceData);
64 void setLayout(PieSliceItem *sliceItem, const PieSliceData &sliceData);
65 void destroySlice(QPieSlice *slice);
66
65
67 private:
66 private:
68 friend class PieSliceItem;
67 friend class PieSliceItem;
69 QHash<QPieSlice*, PieSliceItem*> m_slices;
68 QHash<QPieSlice*, PieSliceItem*> m_slices;
70 QPieSeries *m_series;
69 QPieSeries *m_series;
71 QRectF m_rect;
70 QRectF m_rect;
72 QPointF m_pieCenter;
71 QPointF m_pieCenter;
73 qreal m_pieRadius;
72 qreal m_pieRadius;
74 };
73 };
75
74
76 QTCOMMERCIALCHART_END_NAMESPACE
75 QTCOMMERCIALCHART_END_NAMESPACE
77
76
78 #endif // PIECHARTITEM_H
77 #endif // PIECHARTITEM_H
General Comments 0
You need to be logged in to leave comments. Login now