##// END OF EJS Templates
Adding zoom in out animation support
Michal Klocek -
r513:c1d8fdd92b71
parent child
Show More
@@ -24,23 +24,40 void AxisAnimationItem::updateLayout(QVector<qreal>& newLayout)
24 24
25 25 if(newLayout.count()==0) return;
26 26
27 QRectF rect = geometry();
27 if(zoomFactor()<0) {
28 28
29 oldLayout.resize(newLayout.size());
29 QRectF rect = geometry();
30 oldLayout.resize(newLayout.count());
30 31
31 for(int i=0;i<oldLayout.count();i++)
32 {
33 oldLayout[i]= axisType()==X_AXIS?rect.left():rect.top();
32 for(int i=0,j=oldLayout.count()-1;i<(oldLayout.count()+1)/2;i++,j--)
33 {
34 oldLayout[i]= axisType()==X_AXIS?rect.left():rect.bottom();
35 oldLayout[j]= axisType()==X_AXIS?rect.right():rect.top();
36 }
37
38 }
39 else {
40
41 int index = qMin(oldLayout.count()*zoomFactor(),newLayout.count()-1.0);
42 oldLayout.resize(newLayout.count());
43
44 for(int i=0;i<oldLayout.count();i++)
45 {
46 oldLayout[i]= oldLayout[index]; //axisType()==X_AXIS?rect.center.x():rect.center().y();
47 }
34 48 }
35 49
36 if(m_animation->state()!=QAbstractAnimation::Stopped){
37 m_animation->stop();
50 if(m_animation->state()!=QAbstractAnimation::Stopped) {
51 m_animation->stop();
38 52 }
39 53
40 54 m_animation->setDuration(duration);
41 55 m_animation->setEasingCurve(QEasingCurve::OutQuart);
56 QVariantAnimation::KeyValues value;
57 m_animation->setKeyValues(value); //workaround for wrong interpolation call
42 58 m_animation->setKeyValueAt(0.0, qVariantFromValue(oldLayout));
43 59 m_animation->setKeyValueAt(1.0, qVariantFromValue(newLayout));
60
44 61 QTimer::singleShot(0,m_animation,SLOT(start()));
45 62 }
46 63
@@ -63,6 +80,7 QVariant AxisAnimator::interpolated(const QVariant &start, const QVariant & end,
63 80 QVector<qreal> startVector = qVariantValue<QVector<qreal> >(start);
64 81 QVector<qreal> endVecotr = qVariantValue<QVector<qreal> >(end);
65 82 QVector<qreal> result;
83
66 84 Q_ASSERT(startVector.count() == endVecotr.count());
67 85
68 86 for(int i =0 ;i< startVector.count();i++){
@@ -75,8 +93,13 QVariant AxisAnimator::interpolated(const QVariant &start, const QVariant & end,
75 93
76 94 void AxisAnimator::updateCurrentValue (const QVariant & value )
77 95 {
78 QVector<qreal> vector = qVariantValue<QVector<qreal> >(value);
79 m_axis->setLayout(vector);
96 //Q_ASSERT(state()!=QAbstractAnimation::Stopped);
97 if(state()!=QAbstractAnimation::Stopped)//workaround
98 {
99 QVector<qreal> vector = qVariantValue<QVector<qreal> >(value);
100 m_axis->setLayout(vector);
101 }
102
80 103 }
81 104
82 105 #include "moc_axisanimationitem_p.cpp"
@@ -19,7 +19,8 m_labels(parent),
19 19 m_axis(parent),
20 20 m_min(0),
21 21 m_max(0),
22 m_ticksCount(0)
22 m_ticksCount(0),
23 m_zoomFactor(0)
23 24 {
24 25 //initial initialization
25 26 m_axis.setZValue(ChartPresenter::AxisZValue);
@@ -93,8 +94,6 QStringList AxisItem::createLabels(int ticks, qreal min, qreal max) const
93 94 labels << label;
94 95 }
95 96 }
96
97
98 97 return labels;
99 98 }
100 99
@@ -239,13 +238,17 void AxisItem::setLayout(QVector<qreal>& layout)
239 238 createItems(-diff);
240 239 }
241 240
241 if(diff!=0) handleAxisUpdated();
242
243 QStringList ticksList = createLabels(m_ticksCount,m_min,m_max);
244
242 245 QList<QGraphicsItem *> lines = m_grid.childItems();
243 246 QList<QGraphicsItem *> labels = m_labels.childItems();
244 247 QList<QGraphicsItem *> shades = m_shades.childItems();
245 248 QList<QGraphicsItem *> axis = m_axis.childItems();
246 249
247 Q_ASSERT(labels.size() == m_ticksList.size());
248 Q_ASSERT(layout.size() == m_ticksList.size());
250 Q_ASSERT(labels.size() == ticksList.size());
251 Q_ASSERT(layout.size() == ticksList.size());
249 252
250 253 switch (m_type)
251 254 {
@@ -258,7 +261,7 void AxisItem::setLayout(QVector<qreal>& layout)
258 261 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
259 262 lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom());
260 263 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
261 labelItem->setText(m_ticksList.at(i));
264 labelItem->setText(ticksList.at(i));
262 265 QPointF center = labelItem->boundingRect().center();
263 266 labelItem->setTransformOriginPoint(center.x(), center.y());
264 267 labelItem->setPos(layout[i] - center.x(), m_rect.bottom() + label_padding);
@@ -281,7 +284,7 void AxisItem::setLayout(QVector<qreal>& layout)
281 284 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
282 285 lineItem->setLine(m_rect.left() , layout[i], m_rect.right(), layout[i]);
283 286 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
284 labelItem->setText(m_ticksList.at(i));
287 labelItem->setText(ticksList.at(i));
285 288 QPointF center = labelItem->boundingRect().center();
286 289 labelItem->setTransformOriginPoint(center.x(), center.y());
287 290 labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , layout[i]-center.y());
@@ -299,7 +302,7 void AxisItem::setLayout(QVector<qreal>& layout)
299 302 break;
300 303 }
301 304
302 if(diff!=0) handleAxisUpdated();
305 //if(diff!=0) handleAxisUpdated();
303 306 m_layoutVector=layout;
304 307 }
305 308
@@ -313,7 +316,6 bool AxisItem::isEmpty()
313 316 void AxisItem::handleAxisCategoriesUpdated()
314 317 {
315 318 if(isEmpty()) return;
316 m_ticksList = createLabels(m_ticksCount,m_min,m_max);
317 319 updateLayout(m_layoutVector);
318 320 }
319 321
@@ -323,7 +325,7 void AxisItem::handleAxisUpdated()
323 325 int count = m_chartAxis->ticksCount();
324 326
325 327 if(m_ticksCount!=count){
326 handleTicksCountChanged(count);
328 //handleTicksCountChanged(count);
327 329 }
328 330
329 331 if(isEmpty()) return;
@@ -373,7 +375,6 void AxisItem::handleTicksCountChanged(int count)
373 375 m_ticksCount=count;
374 376
375 377 if(isEmpty()) return;
376 m_ticksList = createLabels(m_ticksCount,m_min,m_max);
377 378 QVector<qreal> layout = calculateLayout();
378 379 updateLayout(layout);
379 380 }
@@ -381,11 +382,25 void AxisItem::handleTicksCountChanged(int count)
381 382 void AxisItem::handleRangeChanged(qreal min, qreal max)
382 383 {
383 384
385 if(m_min<min || m_max>max){
386 m_zoomFactor = (min + (max-min)/2 - m_min)/(m_max - m_min);
387 }
388 else
389 m_zoomFactor=-1;
390
384 391 m_min = min;
385 392 m_max = max;
386 393
394 m_ticksCount = qrand()%10;
395
396 while(m_ticksCount<2){
397 m_ticksCount = qrand()%10;
398 }
399
400 qDebug()<<"Warning : This is testing . Simulating new random ticks "<< m_ticksCount;
401 //m_chartAxis->setTicksCount(m_ticksCount);
402
387 403 if(isEmpty()) return;
388 m_ticksList = createLabels(m_ticksCount,m_min,m_max);
389 404 QVector<qreal> layout = calculateLayout();
390 405 updateLayout(layout);
391 406
@@ -396,7 +411,6 void AxisItem::handleGeometryChanged(const QRectF& rect)
396 411
397 412 m_rect = rect;
398 413 if(isEmpty()) return;
399 m_ticksList = createLabels(m_ticksCount,m_min,m_max);
400 414 QVector<qreal> layout = calculateLayout();
401 415 updateLayout(layout);
402 416 }
@@ -49,7 +49,8 public:
49 49 void setLabelsBrush(const QBrush& brush);
50 50 void setLabelsFont(const QFont& font);
51 51
52 QRectF geometry() const { return m_rect; }
52 inline QRectF geometry() const { return m_rect; }
53 inline qreal zoomFactor() const { return m_zoomFactor;}
53 54
54 55 public slots:
55 56 void handleAxisUpdated();
@@ -79,11 +80,11 private:
79 80 QGraphicsItemGroup m_shades;
80 81 QGraphicsItemGroup m_labels;
81 82 QGraphicsItemGroup m_axis;
82 QStringList m_ticksList;
83 83 QVector<qreal> m_layoutVector;
84 84 qreal m_min;
85 85 qreal m_max;
86 86 int m_ticksCount;
87 qreal m_zoomFactor;
87 88
88 89 };
89 90
@@ -15,7 +15,6 m_items(this)
15 15 //m_items.setZValue(ChartPresenter::LineChartZValue);
16 16 setZValue(ChartPresenter::LineChartZValue);
17 17 QObject::connect(series,SIGNAL(updated()),this,SLOT(handleUpdated()));
18
19 18 handleUpdated();
20 19 }
21 20
@@ -51,6 +51,8 void XYChartAnimationItem<T,U>::updatePoints(QVector<QPointF>& newPoints)
51 51 QVector<QPointF> oldPoints = T::points();
52 52
53 53 if(newPoints.count()==0) return;
54
55 bool empty = oldPoints.count()==0;
54 56 oldPoints.resize(newPoints.size());
55 57
56 58 if(m_animation->state()!=QAbstractAnimation::Stopped){
@@ -58,8 +60,11 void XYChartAnimationItem<T,U>::updatePoints(QVector<QPointF>& newPoints)
58 60 }
59 61
60 62 m_animation->setDuration(duration);
61 m_animation->setAnimationType(XYChartAnimator<T,U>::LineDrawAnimation);
62 m_animation->setEasingCurve(QEasingCurve::InOutBack);
63 if(!empty)
64 m_animation->setAnimationType(XYChartAnimator<T,U>::MoveDownAnimation);
65 else
66 m_animation->setAnimationType(XYChartAnimator<T,U>::LineDrawAnimation);
67 m_animation->setEasingCurve(QEasingCurve::OutQuart);
63 68 m_animation->setKeyValueAt(0.0, qVariantFromValue(oldPoints));
64 69 m_animation->setKeyValueAt(1.0, qVariantFromValue(newPoints));
65 70 QTimer::singleShot(0,m_animation,SLOT(start()));
@@ -84,7 +89,7 void XYChartAnimationItem<T,U>::updatePoint(QVector<QPointF>& newPoints)
84 89
85 90 m_animation->setDuration(duration);
86 91 m_animation->setAnimationType(XYChartAnimator<T,U>::MoveDownAnimation);
87 m_animation->setEasingCurve(QEasingCurve::InOutBack);
92 m_animation->setEasingCurve(QEasingCurve::OutQuart);
88 93 m_animation->setKeyValueAt(0.0, qVariantFromValue(m_points));
89 94 m_animation->setKeyValueAt(1.0, qVariantFromValue(newPoints));
90 95
@@ -84,9 +84,9 QVariant XYChartAnimator<T,U>::interpolated(const QVariant &start, const QVarian
84 84 template <class T, class U>
85 85 void XYChartAnimator<T,U>::updateCurrentValue (const QVariant & value )
86 86 {
87 QVector<QPointF> vector = qVariantValue<QVector<QPointF> >(value);
88 87 if(state()!=QAbstractAnimation::Stopped){ //workaround
89 m_item->setGeometry(vector);
88 QVector<QPointF> vector = qVariantValue<QVector<QPointF> >(value);
89 m_item->setGeometry(vector);
90 90 }
91 91 }
92 92
General Comments 0
You need to be logged in to leave comments. Login now