##// END OF EJS Templates
Refactors spline animations:...
Michal Klocek -
r1288:77f0ec765102
parent child
Show More
@@ -1,22 +1,22
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 "splineanimation_p.h"
21 #include "splineanimation_p.h"
22 #include "splinechartitem_p.h"
22 #include "splinechartitem_p.h"
@@ -28,8 +28,7 Q_DECLARE_METATYPE(SplineVector)
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29
29
30 SplineAnimation::SplineAnimation(SplineChartItem* item):XYAnimation(item),
30 SplineAnimation::SplineAnimation(SplineChartItem* item):XYAnimation(item),
31 m_item(item),
31 m_item(item)
32 m_dirty(true)
33 {
32 {
34 }
33 }
35
34
@@ -39,98 +38,167 SplineAnimation::~SplineAnimation()
39
38
40 void SplineAnimation::setup(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints, QVector<QPointF> &oldControlPoints, QVector<QPointF> &newControlPoints, int index)
39 void SplineAnimation::setup(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints, QVector<QPointF> &oldControlPoints, QVector<QPointF> &newControlPoints, int index)
41 {
40 {
42 int x = oldPoints.count();
43 int y = newPoints.count();
44
45 Q_ASSERT(newPoints.count() * 2 - 2 == newControlPoints.count());
41 Q_ASSERT(newPoints.count() * 2 - 2 == newControlPoints.count());
46
42
47 if (x != y && abs(x - y) != 1) {
43 m_type = NewAnimation;
48 m_oldSpline.first = newPoints;
44
49 m_oldSpline.second = newControlPoints;
45 if (state() != QAbstractAnimation::Stopped) {
50 oldPoints.resize(newPoints.size());
46 stop();
51 oldControlPoints.resize(newControlPoints.size());
47 m_dirty=false;
52 SplineVector oldPair;
53 oldPair.first = oldPoints;
54 oldPair.second = oldControlPoints;
55 SplineVector newPair;
56 newPair.first = newPoints;
57 newPair.second = newControlPoints;
58 setKeyValueAt(0.0, qVariantFromValue(oldPair));
59 setKeyValueAt(1.0, qVariantFromValue(newPair));
60 m_dirty = false;
61 }
48 }
62 else {
49
63 if(m_dirty) {
50 if(!m_dirty) {
64 m_oldSpline.first = oldPoints;
51 m_dirty = true;
65 m_oldSpline.second = oldControlPoints;
52 m_oldSpline.first = oldPoints;
66 m_dirty = false;
53 m_oldSpline.second = oldControlPoints;
67 }
54 }
68 oldPoints = newPoints;
55
69 oldControlPoints = newControlPoints;
56 m_newSpline.first=newPoints;
70 if (y < x) {
57 m_newSpline.second=newControlPoints;
71 m_oldSpline.first.remove(index); //remove
58
72 m_oldSpline.second.remove(index * 2);
59 int x = m_oldSpline.first.count();
73 m_oldSpline.second.remove(index * 2);
60 int y = m_newSpline.first.count();
61
62 if(x - y == 1 && index >= 0 && y>0) {
63 //remove point
64 if(index>0){
65 m_newSpline.first.insert(index, newPoints[index-1]);
66 m_newSpline.second.insert((index -1) * 2, newPoints[index-1] );
67 m_newSpline.second.insert((index -1) * 2 + 1, newPoints[index-1]);
68 }else{
69 m_newSpline.first.insert(index, newPoints[index]);
70 m_newSpline.second.insert(index * 2, newPoints[index] );
71 m_newSpline.second.insert(index * 2 + 1, newPoints[index]);
74 }
72 }
75 if (y > x) {
73 m_index=index;
76 m_oldSpline.first.insert(index, x > 0 ? m_oldSpline.first[index-1] : newPoints[index]); //add
74 m_type = RemovePointAnimation;
77 m_oldSpline.second.insert((index - 1) * 2, x > 1 ? m_oldSpline.second[(index-2)*2] : newControlPoints[(index - 1) * 2]); //add
75 }
78 m_oldSpline.second.insert((index - 1) * 2 + 1, x > 1 ? m_oldSpline.second[(index - 2) * 2 + 1] : newControlPoints[(index - 1) * 2 + 1]); //add
76
77 if(x - y == -1 && index >= 0) {
78 //add point
79 if(index>0){
80 m_oldSpline.first.insert(index, newPoints[index-1]);
81 m_oldSpline.second.insert((index - 1) * 2, newPoints[index-1]);
82 m_oldSpline.second.insert((index - 1) * 2 + 1, newPoints[index-1]);
83 }else{
84 m_oldSpline.first.insert(index, newPoints[index]);
85 m_oldSpline.second.insert((index - 1) * 2, newPoints[index]);
86 m_oldSpline.second.insert((index - 1) * 2 + 1, newPoints[index]);
79 }
87 }
80 SplineVector newPair;
88 m_index=index;
81 newPair.first=newPoints;
89 m_type = AddPointAnimation;
82 newPair.second=newControlPoints;
90 }
83 setKeyValueAt(0.0, qVariantFromValue(m_oldSpline));
91
84 setKeyValueAt(1.0, qVariantFromValue(newPair));
92 x = m_oldSpline.first.count();
93 y = m_newSpline.first.count();
94
95 if(x != y)
96 {
97 m_type = NewAnimation;
98 }
99 else if(m_type == NewAnimation)
100 {
101 m_type = ReplacePointAnimation;
85 }
102 }
103
104
105 setKeyValueAt(0.0, qVariantFromValue(m_oldSpline));
106 setKeyValueAt(1.0, qVariantFromValue(m_newSpline));
107 /*
108 int x = oldPoints.count();
109 int y = newPoints.count();
110
111 Q_ASSERT(newPoints.count() * 2 - 2 == newControlPoints.count());
112
113 if (x != y && abs(x - y) != 1) {
114 m_oldSpline.first = newPoints;
115 m_oldSpline.second = newControlPoints;
116 oldPoints.resize(newPoints.size());
117 oldControlPoints.resize(newControlPoints.size());
118 SplineVector oldPair;
119 oldPair.first = oldPoints;
120 oldPair.second = oldControlPoints;
121 SplineVector newPair;
122 newPair.first = newPoints;
123 newPair.second = newControlPoints;
124 setKeyValueAt(0.0, qVariantFromValue(oldPair));
125 setKeyValueAt(1.0, qVariantFromValue(newPair));
126 m_dirty = false;
127 }
128 else {
129 if(m_dirty) {
130 m_oldSpline.first = oldPoints;
131 m_oldSpline.second = oldControlPoints;
132 m_dirty = false;
133 }
134 oldPoints = newPoints;
135 oldControlPoints = newControlPoints;
136 if (y < x) {
137 m_oldSpline.first.remove(index); //remove
138 m_oldSpline.second.remove(index * 2);
139 m_oldSpline.second.remove(index * 2);
140 }
141 if (y > x) {
142 m_oldSpline.first.insert(index, x > 0 ? m_oldSpline.first[index-1] : newPoints[index]); //add
143 m_oldSpline.second.insert((index - 1) * 2, x > 1 ? m_oldSpline.second[(index-2)*2] : newControlPoints[(index - 1) * 2]); //add
144 m_oldSpline.second.insert((index - 1) * 2 + 1, x > 1 ? m_oldSpline.second[(index - 2) * 2 + 1] : newControlPoints[(index - 1) * 2 + 1]); //add
145 }
146 SplineVector newPair;
147 newPair.first=newPoints;
148 newPair.second=newControlPoints;
149 setKeyValueAt(0.0, qVariantFromValue(m_oldSpline));
150 setKeyValueAt(1.0, qVariantFromValue(newPair));
151 }
152 */
153
86 }
154 }
87
155
88 QVariant SplineAnimation::interpolated(const QVariant &start, const QVariant &end, qreal progress ) const
156 QVariant SplineAnimation::interpolated(const QVariant &start, const QVariant &end, qreal progress ) const
89 {
157 {
90
158
91 SplineVector startPair = qVariantValue< SplineVector >(start);
159 SplineVector startPair = qVariantValue< SplineVector >(start);
92 SplineVector endPair = qVariantValue< SplineVector >(end);
160 SplineVector endPair = qVariantValue< SplineVector >(end);
93 SplineVector result;
161 SplineVector result;
94
162
95 switch (animationType()) {
163 switch (animationType()) {
96
164
97 case RemovePointAnimation:
165 case RemovePointAnimation:
98 case AddPointAnimation:
166 case AddPointAnimation:
99 case ReplacePointAnimation:
167 case ReplacePointAnimation:
100 {
168 {
101 if (startPair.first.count() != endPair.first.count())
169 if (startPair.first.count() != endPair.first.count())
102 break;
170 break;
103 Q_ASSERT(startPair.first.count() * 2 - 2 == startPair.second.count());
171 Q_ASSERT(startPair.first.count() * 2 - 2 == startPair.second.count());
104 Q_ASSERT(endPair.first.count() * 2 - 2 == endPair.second.count());
172 Q_ASSERT(endPair.first.count() * 2 - 2 == endPair.second.count());
105 for(int i = 0; i < endPair.first.count(); i++) {
173 for(int i = 0; i < endPair.first.count(); i++) {
106 qreal x = startPair.first[i].x() + ((endPair.first[i].x() - startPair.first[i].x()) * progress);
174 qreal x = startPair.first[i].x() + ((endPair.first[i].x() - startPair.first[i].x()) * progress);
107 qreal y = startPair.first[i].y() + ((endPair.first[i].y() - startPair.first[i].y()) * progress);
175 qreal y = startPair.first[i].y() + ((endPair.first[i].y() - startPair.first[i].y()) * progress);
108 result.first << QPointF(x,y);
176 result.first << QPointF(x,y);
109 if (i + 1 >= endPair.first.count())
177 if (i + 1 >= endPair.first.count())
110 continue;
178 continue;
111 x = startPair.second[i * 2].x() + ((endPair.second[i * 2].x() - startPair.second[i * 2].x()) * progress);
179 x = startPair.second[i * 2].x() + ((endPair.second[i * 2].x() - startPair.second[i * 2].x()) * progress);
112 y = startPair.second[i * 2].y() + ((endPair.second[i * 2].y() - startPair.second[i * 2].y()) * progress);
180 y = startPair.second[i * 2].y() + ((endPair.second[i * 2].y() - startPair.second[i * 2].y()) * progress);
113 result.second << QPoint(x,y);
181 result.second << QPoint(x,y);
114 x = startPair.second[i * 2 + 1].x() + ((endPair.second[i * 2 + 1].x() - startPair.second[i * 2 + 1].x()) * progress);
182 x = startPair.second[i * 2 + 1].x() + ((endPair.second[i * 2 + 1].x() - startPair.second[i * 2 + 1].x()) * progress);
115 y = startPair.second[i * 2 + 1].y() + ((endPair.second[i * 2 + 1].y() - startPair.second[i * 2 + 1].y()) * progress);
183 y = startPair.second[i * 2 + 1].y() + ((endPair.second[i * 2 + 1].y() - startPair.second[i * 2 + 1].y()) * progress);
116 result.second << QPoint(x,y);
184 result.second << QPoint(x,y);
117 }
185 }
118
186
119 }
187 }
120 break;
188 break;
121 case NewAnimation:{
189 case NewAnimation: {
122 Q_ASSERT(endPair.first.count() * 2 - 2 == endPair.second.count());
190 Q_ASSERT(endPair.first.count() * 2 - 2 == endPair.second.count());
123 int count = endPair.first.count()* qBound(qreal(0), progress, qreal(1));
191 int count = endPair.first.count()* qBound(qreal(0), progress, qreal(1));
124 for(int i = 0; i < count; i++) {
192 for(int i = 0; i < count; i++) {
125 result.first << endPair.first[i];
193 result.first << endPair.first[i];
126 if(i + 1 == count)
194 if(i + 1 == count)
127 break;
195 break;
128 result.second << endPair.second[2 * i];
196 result.second << endPair.second[2 * i];
129 result.second << endPair.second[2 * i + 1];
197 result.second << endPair.second[2 * i + 1];
198 }
130 }
199 }
131 }
132 break;
200 break;
133 default:
201 default:
134 qWarning() << "Unknown type of animation";
202 qWarning() << "Unknown type of animation";
135 break;
203 break;
136 }
204 }
@@ -141,13 +209,29 QVariant SplineAnimation::interpolated(const QVariant &start, const QVariant &en
141 void SplineAnimation::updateCurrentValue (const QVariant &value )
209 void SplineAnimation::updateCurrentValue (const QVariant &value )
142 {
210 {
143 if (state() != QAbstractAnimation::Stopped) { //workaround
211 if (state() != QAbstractAnimation::Stopped) { //workaround
144 m_dirty = true;
145 QPair<QVector<QPointF >, QVector<QPointF > > pair = qVariantValue< QPair< QVector<QPointF>, QVector<QPointF> > >(value);
212 QPair<QVector<QPointF >, QVector<QPointF > > pair = qVariantValue< QPair< QVector<QPointF>, QVector<QPointF> > >(value);
146 m_item->setGeometryPoints(pair.first);
213 m_item->setGeometryPoints(pair.first);
147 m_item->setControlGeometryPoints(pair.second);
214 m_item->setControlGeometryPoints(pair.second);
148 m_item->updateGeometry();
215 m_item->updateGeometry();
216 m_item->setDirty(true);
217 m_dirty = false;
149 }
218 }
150 }
219 }
151
220
221 void SplineAnimation::updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState)
222 {
223 if(oldState == QAbstractAnimation::Running && newState == QAbstractAnimation::Stopped)
224 {
225 if(m_item->isDirty() && m_type==RemovePointAnimation) {
226 if(!m_newSpline.first.isEmpty()) {
227 m_newSpline.first.remove(m_index);
228 m_newSpline.second.remove((m_index-1) * 2);
229 m_newSpline.second.remove((m_index-1) * 2);
230 }
231 m_item->setGeometryPoints(m_newSpline.first);
232 m_item->setControlGeometryPoints(m_newSpline.second);
233 }
234 }
235 }
152
236
153 QTCOMMERCIALCHART_END_NAMESPACE
237 QTCOMMERCIALCHART_END_NAMESPACE
@@ -39,11 +39,12 public:
39 protected:
39 protected:
40 QVariant interpolated(const QVariant &start, const QVariant &end, qreal progress) const;
40 QVariant interpolated(const QVariant &start, const QVariant &end, qreal progress) const;
41 void updateCurrentValue(const QVariant &value);
41 void updateCurrentValue(const QVariant &value);
42 void updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState);
42
43
43 private:
44 private:
44 SplineVector m_oldSpline;
45 SplineVector m_oldSpline;
46 SplineVector m_newSpline;
45 SplineChartItem *m_item;
47 SplineChartItem *m_item;
46 bool m_dirty;
47 };
48 };
48
49
49 QTCOMMERCIALCHART_END_NAMESPACE
50 QTCOMMERCIALCHART_END_NAMESPACE
@@ -27,9 +27,10 Q_DECLARE_METATYPE(QVector<QPointF>)
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 XYAnimation::XYAnimation(XYChart *item):ChartAnimation(item),
29 XYAnimation::XYAnimation(XYChart *item):ChartAnimation(item),
30 m_item(item),
30 m_type(NewAnimation),
31 m_dirty(false),
31 m_dirty(false),
32 m_type(NewAnimation)
32 m_index(-1),
33 m_item(item)
33 {
34 {
34 setDuration(ChartAnimationDuration);
35 setDuration(ChartAnimationDuration);
35 setEasingCurve(QEasingCurve::OutQuart);
36 setEasingCurve(QEasingCurve::OutQuart);
@@ -58,16 +59,16 void XYAnimation::setup(const QVector<QPointF> &oldPoints, const QVector<QPointF
58 int x = m_oldPoints.count();
59 int x = m_oldPoints.count();
59 int y = m_newPoints.count();
60 int y = m_newPoints.count();
60
61
61 if(x - y == 1 && index >= 0 && !newPoints.isEmpty()){
62 if(x - y == 1 && index >= 0 && y > 0){
62 //remove point
63 //remove point
63 m_newPoints.insert(index, index >= 1 ? m_newPoints[index-1] : newPoints[index]);
64 m_newPoints.insert(index, index > 0 ? newPoints[index-1] : newPoints[index]);
64 m_index=index;
65 m_index=index;
65 m_type = RemovePointAnimation;
66 m_type = RemovePointAnimation;
66 }
67 }
67
68
68 if(x - y == -1 && index >= 0){
69 if(x - y == -1 && index >= 0){
69 //add point
70 //add point
70 m_oldPoints.insert(index, x > 0 && index > 1 ? m_oldPoints[index-1] : newPoints[index]);
71 m_oldPoints.insert(index, index > 0 ? newPoints[index-1] : newPoints[index]);
71 m_index=index;
72 m_index=index;
72 m_type = AddPointAnimation;
73 m_type = AddPointAnimation;
73 }
74 }
@@ -30,8 +30,9 class XYChart;
30
30
31 class XYAnimation : public ChartAnimation
31 class XYAnimation : public ChartAnimation
32 {
32 {
33 public:
33 protected:
34 enum Animation { AddPointAnimation, RemovePointAnimation, ReplacePointAnimation, NewAnimation };
34 enum Animation { AddPointAnimation, RemovePointAnimation, ReplacePointAnimation, NewAnimation };
35 public:
35 XYAnimation(XYChart *item);
36 XYAnimation(XYChart *item);
36 ~XYAnimation();
37 ~XYAnimation();
37 void setup(const QVector<QPointF> &oldPoints, const QVector<QPointF> &newPoints,int index = -1);
38 void setup(const QVector<QPointF> &oldPoints, const QVector<QPointF> &newPoints,int index = -1);
@@ -40,14 +41,15 public:
40 protected:
41 protected:
41 QVariant interpolated(const QVariant &start, const QVariant &end, qreal progress ) const;
42 QVariant interpolated(const QVariant &start, const QVariant &end, qreal progress ) const;
42 void updateCurrentValue (const QVariant &value );
43 void updateCurrentValue (const QVariant &value );
43 void updateState( QAbstractAnimation::State newState, QAbstractAnimation::State oldState );
44 void updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState);
45 protected:
46 Animation m_type;
47 bool m_dirty;
48 int m_index;
44 private:
49 private:
45 XYChart *m_item;
50 XYChart *m_item;
46 QVector<QPointF> m_oldPoints;
51 QVector<QPointF> m_oldPoints;
47 QVector<QPointF> m_newPoints;
52 QVector<QPointF> m_newPoints;
48 int m_index;
49 bool m_dirty;
50 Animation m_type;
51 };
53 };
52
54
53 QTCOMMERCIALCHART_END_NAMESPACE
55 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now