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