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