##// END OF EJS Templates
Clean up axis animation code
Michal Klocek -
r393:61d1f8f10e51
parent child
Show More
@@ -1,5 +1,5
1 #include "axisanimationitem_p.h"
1 #include "axisanimationitem_p.h"
2 #include <QPropertyAnimation>
2 #include <QTimer>
3
3
4 Q_DECLARE_METATYPE(QVector<qreal>)
4 Q_DECLARE_METATYPE(QVector<qreal>)
5
5
@@ -8,7 +8,8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 const static int duration = 500;
8 const static int duration = 500;
9
9
10 AxisAnimationItem::AxisAnimationItem(AxisType type,QGraphicsItem* parent) :
10 AxisAnimationItem::AxisAnimationItem(AxisType type,QGraphicsItem* parent) :
11 AxisItem(type,parent)
11 AxisItem(type,parent),
12 m_animation(new AxisAnimator(this,this))
12 {
13 {
13 }
14 }
14
15
@@ -16,19 +17,21 AxisAnimationItem::~AxisAnimationItem()
16 {
17 {
17 }
18 }
18
19
19 void AxisAnimationItem::updateItems(QVector<qreal>& vector1)
20 void AxisAnimationItem::updateItems(QVector<qreal>& oldLayout,QVector<qreal>& newLayout)
20 {
21 {
21 QVector<qreal> vector0 = vector1;
22 if(newLayout.count()==0) return;
22 calculateLayout(vector1);
23 oldLayout.resize(newLayout.size());
23 if(vector1.count()==0) return;
24
24 vector0.resize(vector1.size());
25 if(m_animation->state()!=QAbstractAnimation::Stopped){
25
26 m_animation->stop();
26 AxisAnimator *animation = new AxisAnimator(this,this);
27 }
27 animation->setDuration(duration);
28
28 animation->setEasingCurve(QEasingCurve::InOutBack);
29 m_animation->setDuration(duration);
29 animation->setKeyValueAt(0.0, qVariantFromValue(vector0));
30 m_animation->setEasingCurve(QEasingCurve::InOutBack);
30 animation->setKeyValueAt(1.0, qVariantFromValue(vector1));
31 m_animation->setKeyValueAt(0.0, qVariantFromValue(oldLayout));
31 animation->start(QAbstractAnimation::DeleteWhenStopped);
32 m_animation->setKeyValueAt(1.0, qVariantFromValue(newLayout));
33 QTimer::singleShot(0,m_animation,SLOT(start()));
34 oldLayout = newLayout;
32 }
35 }
33
36
34 void AxisAnimationItem::setLabelsAngle(int angle)
37 void AxisAnimationItem::setLabelsAngle(int angle)
@@ -8,7 +8,7
8
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
11 class QChartAxis;
11 class AxisAnimator;
12
12
13 class AxisAnimationItem : public AxisItem
13 class AxisAnimationItem : public AxisItem
14 {
14 {
@@ -21,8 +21,9 public:
21 void setLabelsAngle(int angle);
21 void setLabelsAngle(int angle);
22
22
23 protected:
23 protected:
24 void updateItems(QVector<qreal>& vector);
24 virtual void updateItems(QVector<qreal>& oldLayout,QVector<qreal>& newLayout);
25
25 private:
26 AxisAnimator *m_animation;
26 };
27 };
27
28
28 class AxisAnimator: public QVariantAnimation
29 class AxisAnimator: public QVariantAnimation
@@ -67,11 +67,11 void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
67 Q_UNUSED(widget);
67 Q_UNUSED(widget);
68 }
68 }
69
69
70 void AxisItem::updateItems(QVector<qreal>& vector)
70 void AxisItem::updateItems(QVector<qreal>& oldLayout,QVector<qreal>& newLayout)
71 {
71 {
72 calculateLayout(vector);
72 if(newLayout.count()==0) return;
73 if(vector.count()==0) return;
73 applyLayout(newLayout);
74 applyLayout(vector);
74 oldLayout=newLayout;
75 }
75 }
76
76
77 void AxisItem::handleAxisUpdate(QChartAxis* axis)
77 void AxisItem::handleAxisUpdate(QChartAxis* axis)
@@ -127,15 +127,19 void AxisItem::handleLabelsChanged(QChartAxis* axis,const QStringList& labels)
127 createItems(-diff);
127 createItems(-diff);
128 }
128 }
129 m_thicksList=labels;
129 m_thicksList=labels;
130 m_layoutVector.resize(m_thicksList.size());
130 QVector<qreal> vector = calculateLayout();
131 updateItems(m_layoutVector);
131 updateItems(m_layoutVector,vector);
132 if(diff!=0) handleAxisUpdate(axis);
132 if(diff!=0) handleAxisUpdate(axis);
133 }
133 }
134
134
135 void AxisItem::handleGeometryChanged(const QRectF& rect)
135 void AxisItem::handleGeometryChanged(const QRectF& rect)
136 {
136 {
137 m_rect = rect;
137 m_rect = rect;
138 updateItems(m_layoutVector);
138
139 if(m_thicksList.size()==0) return;
140
141 QVector<qreal> vector = calculateLayout();
142 updateItems(m_layoutVector,vector);
139 }
143 }
140
144
141 void AxisItem::setAxisOpacity(qreal opacity)
145 void AxisItem::setAxisOpacity(qreal opacity)
@@ -237,8 +241,11 void AxisItem::setGridPen(const QPen& pen)
237 }
241 }
238 }
242 }
239
243
240 void AxisItem::calculateLayout(QVector<qreal>& points)
244 QVector<qreal> AxisItem::calculateLayout() const
241 {
245 {
246 QVector<qreal> points;
247 points.resize(m_thicksList.size());
248
242 switch (m_type)
249 switch (m_type)
243 {
250 {
244 case X_AXIS:
251 case X_AXIS:
@@ -246,7 +253,7 void AxisItem::calculateLayout(QVector<qreal>& points)
246 const qreal deltaX = m_rect.width()/(m_thicksList.size()-1);
253 const qreal deltaX = m_rect.width()/(m_thicksList.size()-1);
247 for (int i = 0; i < m_thicksList.size(); ++i) {
254 for (int i = 0; i < m_thicksList.size(); ++i) {
248 int x = i * deltaX + m_rect.left();
255 int x = i * deltaX + m_rect.left();
249 points[i]=x;
256 points[i] = x;
250 }
257 }
251 }
258 }
252 break;
259 break;
@@ -255,11 +262,12 void AxisItem::calculateLayout(QVector<qreal>& points)
255 const qreal deltaY = m_rect.height()/(m_thicksList.size()-1);
262 const qreal deltaY = m_rect.height()/(m_thicksList.size()-1);
256 for (int i = 0; i < m_thicksList.size(); ++i) {
263 for (int i = 0; i < m_thicksList.size(); ++i) {
257 int y = i * -deltaY + m_rect.bottom();
264 int y = i * -deltaY + m_rect.bottom();
258 points[i]=y;
265 points[i] = y;
259 }
266 }
260 }
267 }
261 break;
268 break;
262 }
269 }
270 return points;
263 }
271 }
264
272
265 void AxisItem::applyLayout(const QVector<qreal>& points)
273 void AxisItem::applyLayout(const QVector<qreal>& points)
@@ -54,9 +54,10 public slots:
54 void handleLabelsChanged(QChartAxis* axis,const QStringList& labels); //labels from dataset
54 void handleLabelsChanged(QChartAxis* axis,const QStringList& labels); //labels from dataset
55 void handleGeometryChanged(const QRectF& size); // geometry from presenter
55 void handleGeometryChanged(const QRectF& size); // geometry from presenter
56 public:
56 public:
57 virtual void updateItems(QVector<qreal>& points);
57 virtual void updateItems(QVector<qreal>& oldLayout,QVector<qreal>& newLayout);
58 virtual void calculateLayout(QVector<qreal>& points);
58 QVector<qreal> calculateLayout() const;
59 virtual void applyLayout(const QVector<qreal>& points);
59 void applyLayout(const QVector<qreal>& points);
60
60 private:
61 private:
61 void clear(int count);
62 void clear(int count);
62 void createItems(int count);
63 void createItems(int count);
General Comments 0
You need to be logged in to leave comments. Login now