##// END OF EJS Templates
Sets drawline animation for all points update
Michal Klocek -
r503:4fe5f7a79a8f
parent child
Show More
@@ -1,100 +1,102
1 1 #ifndef XYCHARTANIMATIONITEM_P_H_
2 2 #define XYCHARTANIMATIONITEM_P_H_
3 3 #include "qchartglobal.h"
4 4 #include "xychartanimator_p.h"
5 5 #include <QPointF>
6 6 #include <QTimer>
7 7 #include <QGraphicsItem>
8 8
9 9 Q_DECLARE_METATYPE(QVector<QPointF>)
10 10
11 11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12 12
13 13
14 14 const static int duration = 1000;
15 15
16 16 template <class T , class U>
17 17 class XYChartAnimationItem : public T {
18 18
19 19 public:
20 20 XYChartAnimationItem(U *series, QGraphicsItem *parent = 0);
21 21 virtual ~XYChartAnimationItem();
22 22
23 23 void animationStarted();
24 24
25 25 protected:
26 26 virtual void updatePoints(QVector<QPointF>& newPoints);
27 27 virtual void updatePoint(QVector<QPointF>& newPoints);
28 28
29 29 private:
30 30 XYChartAnimator<T,U> *m_animation;
31 31 QVector<QPointF> m_points;
32 32 bool m_dirty;
33 33 };
34 34
35 35 template <class T, class U>
36 36 XYChartAnimationItem<T,U>::XYChartAnimationItem(U *series,QGraphicsItem *parent):
37 37 T(series,parent),
38 38 m_animation(new XYChartAnimator<T,U>(this,this)),
39 39 m_dirty(false)
40 40 {
41 41 }
42 42
43 43 template <class T, class U>
44 44 XYChartAnimationItem<T,U>::~XYChartAnimationItem()
45 45 {
46 46 }
47 47
48 48 template <class T, class U>
49 49 void XYChartAnimationItem<T,U>::updatePoints(QVector<QPointF>& newPoints)
50 50 {
51 51 QVector<QPointF> oldPoints = T::points();
52 52
53 53 if(newPoints.count()==0) return;
54 54 oldPoints.resize(newPoints.size());
55 55
56 56 if(m_animation->state()!=QAbstractAnimation::Stopped){
57 57 m_animation->stop();
58 58 }
59 59
60 60 m_animation->setDuration(duration);
61 m_animation->setAnimationType(XYChartAnimator<T,U>::LineDrawAnimation);
61 62 m_animation->setEasingCurve(QEasingCurve::InOutBack);
62 63 m_animation->setKeyValueAt(0.0, qVariantFromValue(oldPoints));
63 64 m_animation->setKeyValueAt(1.0, qVariantFromValue(newPoints));
64 65 QTimer::singleShot(0,m_animation,SLOT(start()));
65 66
66 67 m_points = newPoints;
67 68 m_dirty=false;
68 69 }
69 70
70 71 template <class T, class U>
71 72 void XYChartAnimationItem<T,U>::updatePoint(QVector<QPointF>& newPoints)
72 73 {
73 74
74 75 if(m_animation->state()!=QAbstractAnimation::Stopped) {
75 76 m_animation->stop();
76 77 m_dirty=true;
77 78 }
78 79
79 80 if(m_dirty) {
80 81 m_points=newPoints;
81 82 m_dirty=false;
82 83 }
83 84
84 85 m_animation->setDuration(duration);
86 m_animation->setAnimationType(XYChartAnimator<T,U>::MoveDownAnimation);
85 87 m_animation->setEasingCurve(QEasingCurve::InOutBack);
86 88 m_animation->setKeyValueAt(0.0, qVariantFromValue(m_points));
87 89 m_animation->setKeyValueAt(1.0, qVariantFromValue(newPoints));
88 90
89 91 QTimer::singleShot(0,m_animation,SLOT(start()));
90 92 }
91 93
92 94 template <class T, class U>
93 95 void XYChartAnimationItem<T,U>::animationStarted()
94 96 {
95 97 m_dirty=true;
96 98 }
97 99
98 100 QTCOMMERCIALCHART_END_NAMESPACE
99 101
100 102 #endif
@@ -1,96 +1,103
1 1 #ifndef XYCHARTANIMATOR_P_H_
2 2 #define XYCHARTANIMATOR_P_H_
3 3 #include "qchartglobal.h"
4 4 #include <QVariantAnimation>
5 5 #include <QPointF>
6 6
7 7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 8
9 9 template <class T, class U>
10 10 class XYChartAnimationItem;
11 11
12 12
13 13 template <class T, class U>
14 14 class XYChartAnimator : public QVariantAnimation
15 15 {
16 16 public:
17 17 enum Animation { LineDrawAnimation, MoveDownAnimation, MoveUpAnimation };
18 18 XYChartAnimator(XYChartAnimationItem<T,U> *item, QObject *parent = 0 );
19 19 ~XYChartAnimator();
20 void setAnimationType(Animation type);
20 21
21 22 protected:
22 23 QVariant interpolated(const QVariant &start, const QVariant & end, qreal progress ) const;
23 24 void updateCurrentValue (const QVariant & value );
24 25 void updateState ( QAbstractAnimation::State newState, QAbstractAnimation::State oldState);
25 26
26 27 private:
27 28 XYChartAnimationItem<T,U> *m_item;
28 29 Animation m_type;
29 30 };
30 31
31 32 template <class T, class U>
32 33 XYChartAnimator<T,U>::XYChartAnimator(XYChartAnimationItem<T,U> *item , QObject *parent):QVariantAnimation(parent),
33 34 m_item(item),
34 35 m_type(MoveDownAnimation)
35 36 {
36 37 }
37 38
38 39 template <class T,class U>
39 40 XYChartAnimator<T,U>::~XYChartAnimator()
40 41 {
41 42 }
42 43
44 template <class T,class U>
45 void XYChartAnimator<T,U>::setAnimationType(Animation type)
46 {
47 m_type=type;
48 }
49
43 50 template <class T, class U>
44 51 QVariant XYChartAnimator<T,U>::interpolated(const QVariant &start, const QVariant & end, qreal progress ) const
45 52 {
46 53 QVector<QPointF> startVector = qVariantValue<QVector<QPointF> >(start);
47 54 QVector<QPointF> endVector = qVariantValue<QVector<QPointF> >(end);
48 55 QVector<QPointF> result;
49 56
50 57 switch(m_type) {
51 58
52 59 case MoveDownAnimation: {
53 60
54 61 Q_ASSERT(startVector.count() == endVector.count());
55 62 for(int i =0;i< startVector.count();i++) {
56 63 qreal x = startVector[i].x() + ((endVector[i].x()- startVector[i].x()) * progress);
57 64 qreal y = startVector[i].y() + ((endVector[i].y()- startVector[i].y()) * progress);
58 65 result << QPointF(x,y);
59 66 }
60 67
61 68 }
62 69 break;
63 70 case LineDrawAnimation:{
64 71 for(int i =0;i< endVector.count()* qBound(0.0, progress, 1.0);i++) {
65 72 result << endVector[i];
66 73 }
67 74 }
68 75 break;
69 76 default:
70 77 qWarning()<<"Unknow type of animation";
71 78 break;
72 79 }
73 80
74 81 return qVariantFromValue(result);
75 82 }
76 83
77 84 template <class T, class U>
78 85 void XYChartAnimator<T,U>::updateCurrentValue (const QVariant & value )
79 86 {
80 87 QVector<QPointF> vector = qVariantValue<QVector<QPointF> >(value);
81 88 if(state()!=QAbstractAnimation::Stopped){ //workaround
82 89 m_item->setGeometry(vector);
83 90 }
84 91 }
85 92
86 93 template <class T, class U>
87 94 void XYChartAnimator<T,U>::updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState)
88 95 {
89 96 Q_UNUSED(oldState)
90 97 if (newState==QAbstractAnimation::Running) m_item->animationStarted();
91 98 QVariantAnimation::updateState(newState,oldState);
92 99 }
93 100
94 101 QTCOMMERCIALCHART_END_NAMESPACE
95 102
96 103 #endif /* XYCHARTANIMATOR_P_H_ */
General Comments 0
You need to be logged in to leave comments. Login now