##// END OF EJS Templates
barchart animation fix
sauimone -
r693:1d2255713343
parent child
Show More
@@ -1,48 +1,52
1 1 #include "baranimation_p.h"
2 2 #include "barchartitem_p.h"
3 3 #include <QParallelAnimationGroup>
4 4 #include <QTimer>
5 5
6 6 Q_DECLARE_METATYPE(QVector<QRectF>)
7 7 //Q_DECLARE_METATYPE(BarLayout) // TODO?
8 8
9 9
10 10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 11
12 12 BarAnimation::BarAnimation(BarChartItem *item)
13 13 :ChartAnimation(item),
14 14 m_item(item)
15 15 {
16 16 }
17 17
18 18 BarAnimation::~BarAnimation()
19 19 {
20 20 }
21 21
22 22 QVariant BarAnimation::interpolated(const QVariant &from, const QVariant &to, qreal progress) const
23 23 {
24 24 QVector<QRectF> startVector = qVariantValue<QVector<QRectF> > (from);
25 25 QVector<QRectF> endVector = qVariantValue<QVector<QRectF> > (to);
26 26 QVector<QRectF> result;
27 27
28 28 Q_ASSERT(startVector.count() == endVector.count()) ;
29 29
30 30 for(int i =0 ;i< startVector.count();i++){
31 //QRectF value = startVector[i] + ((endVector[i] - startVector[i]) * progress);
32 QPointF topLeft = startVector[i].topLeft() + ((endVector[i].topLeft() - startVector[i].topLeft()) * progress);
33 QSizeF size = startVector[i].size() + ((endVector[i].size() - startVector[i].size()) * progress);
31 qreal w = endVector[i].width();
32 qreal h = startVector[i].height() + ((endVector[i].height() - startVector[i].height()) * progress);
33 qreal x = endVector[i].topLeft().x();
34 qreal y = endVector[i].topLeft().y() + endVector[i].height() - h;
35
36 QPointF topLeft(x,y);
37 QSizeF size(w,h);
34 38 QRectF value(topLeft,size);
35 39 result << value;
36 40 }
37 41 return qVariantFromValue(result);
38 42 }
39 43
40 44 void BarAnimation::updateCurrentValue(const QVariant &value)
41 45 {
42 46 QVector<QRectF> layout = qVariantValue<QVector<QRectF> >(value);
43 47 m_item->setLayout(layout);
44 48 }
45 49
46 50 #include "moc_baranimation_p.cpp"
47 51
48 52 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,290 +1,289
1 1 #include "chartanimator_p.h"
2 2 #include "axisanimation_p.h"
3 3 #include "xyanimation_p.h"
4 4 #include "splineanimation_p.h"
5 5 #include "xychartitem_p.h"
6 6 #include "pieanimation_p.h"
7 7 #include "baranimation_p.h"
8 8 #include "barchartitem_p.h"
9 9 #include "areachartitem_p.h"
10 10 #include "splinechartitem_p.h"
11 11 #include "scatterchartitem_p.h"
12 12 #include <QTimer>
13 13
14 14 Q_DECLARE_METATYPE(QVector<QPointF>)
15 15 Q_DECLARE_METATYPE(QVector<qreal>)
16 16 Q_DECLARE_METATYPE(QVector<QRectF>)
17 17
18 18 QTCOMMERCIALCHART_BEGIN_NAMESPACE
19 19
20 20 const static int duration = 1000;
21 21
22 22 ChartAnimator::ChartAnimator(QObject *parent):QObject(parent)
23 23 {
24 24 }
25 25
26 26 ChartAnimator::~ChartAnimator()
27 27 {
28 28 }
29 29
30 30 void ChartAnimator::addAnimation(Axis* item)
31 31 {
32 32 ChartAnimation* animation = m_animations.value(item);
33 33
34 34 if(!animation) {
35 35 animation = new AxisAnimation(item);
36 36 m_animations.insert(item,animation);
37 37 }
38 38
39 39 item->setAnimator(this);
40 40 }
41 41
42 42 void ChartAnimator::addAnimation(SplineChartItem* item)
43 43 {
44 44 ChartAnimation* animation = m_animations.value(item);
45 45
46 46 if(!animation) {
47 47 animation = new SplineAnimation(item);
48 48 m_animations.insert(item,animation);
49 49 }
50 50
51 51 item->setAnimator(this);
52 52 }
53 53
54 54 void ChartAnimator::addAnimation(ScatterChartItem* item)
55 55 {
56 56 ChartAnimation* animation = m_animations.value(item);
57 57
58 58 if(!animation) {
59 59 animation = new XYAnimation(item);
60 60 m_animations.insert(item,animation);
61 61 }
62 62
63 63 item->setAnimator(this);
64 64 }
65 65
66 66 void ChartAnimator::addAnimation(LineChartItem* item)
67 67 {
68 68 ChartAnimation* animation = m_animations.value(item);
69 69
70 70 if(!animation) {
71 71 animation = new XYAnimation(item);
72 72 m_animations.insert(item,animation);
73 73 }
74 74
75 75 item->setAnimator(this);
76 76 }
77 77
78 78 void ChartAnimator::addAnimation(PieChartItem* item)
79 79 {
80 80 ChartAnimation* animation = m_animations.value(item);
81 81
82 82 if(!animation) {
83 83 animation = new PieAnimation(item);
84 84 m_animations.insert(item,animation);
85 85 }
86 86
87 87 item->setAnimator(this);
88 88 }
89 89
90 90 void ChartAnimator::addAnimation(BarChartItem* item)
91 91 {
92 92 ChartAnimation* animation = m_animations.value(item);
93 93
94 94 if(!animation) {
95 95 animation = new BarAnimation(item);
96 96 m_animations.insert(item,animation);
97 97 }
98 98
99 99 item->setAnimator(this);
100 100 }
101 101
102 102
103 103 void ChartAnimator::removeAnimation(Chart* item)
104 104 {
105 105 item->setAnimator(0);
106 106 m_animations.remove(item);
107 107 }
108 108
109 109 void ChartAnimator::updateLayout(Axis* item , QVector<qreal>& newLayout)
110 110 {
111 111 AxisAnimation* animation = static_cast<AxisAnimation*>(m_animations.value(item));
112 112
113 113 Q_ASSERT(animation);
114 114
115 115 QVector<qreal> oldLayout = item->layout();
116 116
117 117 if(newLayout.count()==0) return;
118 118
119 119 switch(m_state)
120 120 {
121 121 case ZoomOutState: {
122 122 QRectF rect = item->geometry();
123 123 oldLayout.resize(newLayout.count());
124 124
125 125 for(int i=0,j=oldLayout.count()-1;i<(oldLayout.count()+1)/2;i++,j--)
126 126 {
127 127 oldLayout[i]= item->axisType()==Axis::X_AXIS?rect.left():rect.bottom();
128 128 oldLayout[j]= item->axisType()==Axis::X_AXIS?rect.right():rect.top();
129 129 }
130 130 }
131 131 break;
132 132 case ZoomInState: {
133 133 int index = qMin(oldLayout.count()*(item->axisType()==Axis::X_AXIS?m_point.x():(1 -m_point.y())),newLayout.count()-1.0);
134 134 oldLayout.resize(newLayout.count());
135 135
136 136 for(int i=0;i<oldLayout.count();i++)
137 137 {
138 138 oldLayout[i]= oldLayout[index];
139 139 }
140 140 }
141 141 break;
142 142 case ScrollDownState:
143 143 case ScrollRightState: {
144 144 oldLayout.resize(newLayout.count());
145 145
146 146 for(int i=0, j=i+1;i<oldLayout.count()-1;i++,j++)
147 147 {
148 148 oldLayout[i]= oldLayout[j];
149 149 }
150 150 }
151 151 break;
152 152 case ScrollUpState:
153 153 case ScrollLeftState: {
154 154 oldLayout.resize(newLayout.count());
155 155
156 156 for(int i=oldLayout.count()-1, j=i-1;i>0;i--,j--)
157 157 {
158 158 oldLayout[i]= oldLayout[j];
159 159 }
160 160 }
161 161 break;
162 162 default: {
163 163 oldLayout.resize(newLayout.count());
164 164 QRectF rect = item->geometry();
165 165 for(int i=0, j=oldLayout.count()-1;i<oldLayout.count();i++,j--)
166 166 {
167 167 oldLayout[i]= item->axisType()==Axis::X_AXIS?rect.left():rect.top();
168 168 }
169 169 }
170 170 break;
171 171 }
172 172
173 173
174 174 if(animation->state()!=QAbstractAnimation::Stopped) {
175 175 animation->stop();
176 176 }
177 177
178 178 animation->setDuration(duration);
179 179 animation->setEasingCurve(QEasingCurve::OutQuart);
180 180 QVariantAnimation::KeyValues value;
181 181 animation->setKeyValues(value); //workaround for wrong interpolation call
182 182 animation->setKeyValueAt(0.0, qVariantFromValue(oldLayout));
183 183 animation->setKeyValueAt(1.0, qVariantFromValue(newLayout));
184 184
185 185 QTimer::singleShot(0,animation,SLOT(start()));
186 186 }
187 187
188 188 void ChartAnimator::updateLayout(SplineChartItem* item, QVector<QPointF>& oldPoints ,QVector<QPointF>& newPoints, QVector<QPointF>& oldControlPoints, QVector<QPointF>& newControlPoints,int index)
189 189 {
190 190 SplineAnimation* animation = static_cast<SplineAnimation*>(m_animations.value(item));
191 191
192 192 Q_ASSERT(animation);
193 193
194 194 if(newPoints.count()<2 || newControlPoints.count()<2) return;
195 195
196 196 bool empty = oldPoints.count()==0;
197 197
198 198
199 199 if(animation->state()!=QAbstractAnimation::Stopped) {
200 200 animation->stop();
201 201 }
202 202
203 203 animation->setDuration(duration);
204 204 if(!empty)
205 205 animation->setAnimationType(ChartAnimation::MoveDownAnimation);
206 206 else
207 207 animation->setAnimationType(ChartAnimation::LineDrawAnimation);
208 208
209 209 animation->setEasingCurve(QEasingCurve::OutQuart);
210 210 animation->setValues(oldPoints,newPoints,oldControlPoints,newControlPoints,index);
211 211
212 212 QTimer::singleShot(0,animation,SLOT(start()));
213 213 }
214 214
215 215
216 216 void ChartAnimator::updateLayout(XYChartItem* item, QVector<QPointF>& oldPoints , QVector<QPointF>& newPoints, int index)
217 217 {
218 218 XYAnimation* animation = static_cast<XYAnimation*>(m_animations.value(item));
219 219
220 220 Q_ASSERT(animation);
221 221
222 222 if(newPoints.count()==0) return;
223 223
224 224 bool empty = oldPoints.count()==0;
225 225
226 226
227 227 if(animation->state()!=QAbstractAnimation::Stopped) {
228 228 animation->stop();
229 229 }
230 230
231 231 animation->setDuration(duration);
232 232 if(!empty)
233 233 animation->setAnimationType(ChartAnimation::MoveDownAnimation);
234 234 else
235 235 animation->setAnimationType(ChartAnimation::LineDrawAnimation);
236 236
237 237 animation->setEasingCurve(QEasingCurve::OutQuart);
238 238 animation->setValues(oldPoints,newPoints,index);
239 239
240 240 QTimer::singleShot(0,animation,SLOT(start()));
241 241 }
242 242
243 243 void ChartAnimator::addAnimation(PieChartItem* item, QPieSlice *slice, const PieSliceData &sliceData, bool isEmpty)
244 244 {
245 245 PieAnimation* animation = static_cast<PieAnimation*>(m_animations.value(item));
246 246 Q_ASSERT(animation);
247 247 animation->addSlice(slice, sliceData, isEmpty);
248 248 }
249 249
250 250 void ChartAnimator::removeAnimation(PieChartItem* item, QPieSlice *slice)
251 251 {
252 252 PieAnimation* animation = static_cast<PieAnimation*>(m_animations.value(item));
253 253 Q_ASSERT(animation);
254 254 animation->removeSlice(slice);
255 255 }
256 256
257 257 void ChartAnimator::updateLayout(PieChartItem* item, const PieLayout &layout)
258 258 {
259 259 PieAnimation* animation = static_cast<PieAnimation*>(m_animations.value(item));
260 260 Q_ASSERT(animation);
261 261 animation->updateValues(layout);
262 262 }
263 263
264 264 void ChartAnimator::updateLayout(PieChartItem* item, QPieSlice *slice, const PieSliceData &sliceData)
265 265 {
266 266 PieAnimation* animation = static_cast<PieAnimation*>(m_animations.value(item));
267 267 Q_ASSERT(animation);
268 268 animation->updateValue(slice, sliceData);
269 269 }
270 270
271 271 void ChartAnimator::updateLayout(BarChartItem* item, const QVector<QRectF> &oldLayout, const QVector<QRectF> &newLayout)
272 272 {
273 273 qDebug() << "ChartAnimator::updateLayout";
274 274 BarAnimation* animation = static_cast<BarAnimation*>(m_animations.value(item));
275 275 Q_ASSERT(animation);
276 // animation->updateValues(layout);
277 276 animation->setDuration(duration);
278 277 animation->setKeyValueAt(0.0, qVariantFromValue(oldLayout));
279 278 animation->setKeyValueAt(1.0, qVariantFromValue(newLayout));
280 279 QTimer::singleShot(0,animation,SLOT(start()));
281 280 }
282 281
283 282
284 283 void ChartAnimator::setState(State state,const QPointF& point)
285 284 {
286 285 m_state=state;
287 286 m_point=point;
288 287 }
289 288
290 289 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now