##// END OF EJS Templates
optimized calculations for stacked bar animation
sauimone -
r1430:aef513fa0d93
parent child
Show More
@@ -1,79 +1,78
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "percentbaranimation_p.h"
21 #include "percentbaranimation_p.h"
22 #include "percentbarchartitem_p.h"
22 #include "percentbarchartitem_p.h"
23 #include <QTimer>
23 #include <QTimer>
24
24
25 Q_DECLARE_METATYPE(QVector<QRectF>)
25 Q_DECLARE_METATYPE(QVector<QRectF>)
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 PercentBarAnimation::PercentBarAnimation(BarChartItem *item)
29 PercentBarAnimation::PercentBarAnimation(BarChartItem *item)
30 :ChartAnimation(item),
30 :ChartAnimation(item),
31 m_item(item)
31 m_item(item)
32 {
32 {
33 setEasingCurve(QEasingCurve::OutQuart);
33 setEasingCurve(QEasingCurve::OutQuart);
34 }
34 }
35
35
36 PercentBarAnimation::~PercentBarAnimation()
36 PercentBarAnimation::~PercentBarAnimation()
37 {
37 {
38 }
38 }
39
39
40 QVariant PercentBarAnimation::interpolated(const QVariant &from, const QVariant &to, qreal progress) const
40 QVariant PercentBarAnimation::interpolated(const QVariant &from, const QVariant &to, qreal progress) const
41 {
41 {
42 QVector<QRectF> startVector = qVariantValue<QVector<QRectF> >(from);
42 QVector<QRectF> startVector = qVariantValue<QVector<QRectF> >(from);
43 QVector<QRectF> endVector = qVariantValue<QVector<QRectF> >(to);
43 QVector<QRectF> endVector = qVariantValue<QVector<QRectF> >(to);
44 QVector<QRectF> result;
44 QVector<QRectF> result;
45
45
46 Q_ASSERT(startVector.count() == endVector.count());
46 Q_ASSERT(startVector.count() == endVector.count());
47
47
48 qreal zeroPos = ((m_item->geometry().height() + m_item->geometry().y()) * (1 - progress));
48 qreal yAxis = m_item->geometry().height() + m_item->geometry().y();
49
49
50 for(int i = 0; i < startVector.count(); i++) {
50 for(int i = 0; i < startVector.count(); i++) {
51 qreal w = endVector[i].width();
51 qreal w = endVector[i].width();
52 qreal h = startVector[i].height() + ((endVector[i].height() - startVector[i].height()) * progress);
52 qreal h = startVector[i].height() + ((endVector[i].height() - startVector[i].height()) * progress);
53 qreal x = endVector[i].topLeft().x();
53 qreal x = endVector[i].topLeft().x();
54 qreal y = startVector[i].topLeft().y() + ((endVector[i].topLeft().y() - startVector[i].topLeft().y()) * progress)
54 qreal y = yAxis + ((endVector[i].topLeft().y() - yAxis) * progress);
55 + zeroPos;
56
55
57 QRectF value(x,y,w,h);
56 QRectF value(x,y,w,h);
58 result << value;
57 result << value;
59 }
58 }
60 return qVariantFromValue(result);
59 return qVariantFromValue(result);
61 }
60 }
62
61
63 void PercentBarAnimation::updateCurrentValue(const QVariant &value)
62 void PercentBarAnimation::updateCurrentValue(const QVariant &value)
64 {
63 {
65 QVector<QRectF> layout = qVariantValue<QVector<QRectF> >(value);
64 QVector<QRectF> layout = qVariantValue<QVector<QRectF> >(value);
66 m_item->setLayout(layout);
65 m_item->setLayout(layout);
67 }
66 }
68
67
69 void PercentBarAnimation::updateLayout(const QVector<QRectF> &oldLayout, const QVector<QRectF> &newLayout)
68 void PercentBarAnimation::updateLayout(const QVector<QRectF> &oldLayout, const QVector<QRectF> &newLayout)
70 {
69 {
71 setDuration(ChartAnimationDuration);
70 setDuration(ChartAnimationDuration);
72 setKeyValueAt(0.0, qVariantFromValue(oldLayout));
71 setKeyValueAt(0.0, qVariantFromValue(oldLayout));
73 setKeyValueAt(1.0, qVariantFromValue(newLayout));
72 setKeyValueAt(1.0, qVariantFromValue(newLayout));
74 QTimer::singleShot(0, this, SLOT(start()));
73 QTimer::singleShot(0, this, SLOT(start()));
75 }
74 }
76
75
77 #include "moc_percentbaranimation_p.cpp"
76 #include "moc_percentbaranimation_p.cpp"
78
77
79 QTCOMMERCIALCHART_END_NAMESPACE
78 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,79 +1,78
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "stackedbaranimation_p.h"
21 #include "stackedbaranimation_p.h"
22 #include "stackedbarchartitem_p.h"
22 #include "stackedbarchartitem_p.h"
23 #include <QTimer>
23 #include <QTimer>
24
24
25 Q_DECLARE_METATYPE(QVector<QRectF>)
25 Q_DECLARE_METATYPE(QVector<QRectF>)
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 StackedBarAnimation::StackedBarAnimation(BarChartItem *item)
29 StackedBarAnimation::StackedBarAnimation(BarChartItem *item)
30 :ChartAnimation(item),
30 :ChartAnimation(item),
31 m_item(item)
31 m_item(item)
32 {
32 {
33 setEasingCurve(QEasingCurve::OutQuart);
33 setEasingCurve(QEasingCurve::OutQuart);
34 }
34 }
35
35
36 StackedBarAnimation::~StackedBarAnimation()
36 StackedBarAnimation::~StackedBarAnimation()
37 {
37 {
38 }
38 }
39
39
40 QVariant StackedBarAnimation::interpolated(const QVariant &from, const QVariant &to, qreal progress) const
40 QVariant StackedBarAnimation::interpolated(const QVariant &from, const QVariant &to, qreal progress) const
41 {
41 {
42 QVector<QRectF> startVector = qVariantValue<QVector<QRectF> >(from);
42 QVector<QRectF> startVector = qVariantValue<QVector<QRectF> >(from);
43 QVector<QRectF> endVector = qVariantValue<QVector<QRectF> >(to);
43 QVector<QRectF> endVector = qVariantValue<QVector<QRectF> >(to);
44 QVector<QRectF> result;
44 QVector<QRectF> result;
45
45
46 Q_ASSERT(startVector.count() == endVector.count());
46 Q_ASSERT(startVector.count() == endVector.count());
47
47
48 qreal zeroPos = ((m_item->geometry().height() + m_item->geometry().y()) * (1 - progress));
48 qreal yAxis = m_item->geometry().height() + m_item->geometry().y();
49
49
50 for(int i = 0; i < startVector.count(); i++) {
50 for(int i = 0; i < startVector.count(); i++) {
51 qreal w = endVector[i].width();
51 qreal w = endVector[i].width();
52 qreal h = startVector[i].height() + ((endVector[i].height() - startVector[i].height()) * progress);
52 qreal h = startVector[i].height() + ((endVector[i].height() - startVector[i].height()) * progress);
53 qreal x = endVector[i].topLeft().x();
53 qreal x = endVector[i].topLeft().x();
54 qreal y = startVector[i].topLeft().y() + ((endVector[i].topLeft().y() - startVector[i].topLeft().y()) * progress)
54 qreal y = yAxis + ((endVector[i].topLeft().y() - yAxis) * progress);
55 + zeroPos;
56
55
57 QRectF value(x,y,w,h);
56 QRectF value(x,y,w,h);
58 result << value;
57 result << value;
59 }
58 }
60 return qVariantFromValue(result);
59 return qVariantFromValue(result);
61 }
60 }
62
61
63 void StackedBarAnimation::updateCurrentValue(const QVariant &value)
62 void StackedBarAnimation::updateCurrentValue(const QVariant &value)
64 {
63 {
65 QVector<QRectF> layout = qVariantValue<QVector<QRectF> >(value);
64 QVector<QRectF> layout = qVariantValue<QVector<QRectF> >(value);
66 m_item->setLayout(layout);
65 m_item->setLayout(layout);
67 }
66 }
68
67
69
68
70 void StackedBarAnimation::updateLayout(const QVector<QRectF> &oldLayout, const QVector<QRectF> &newLayout)
69 void StackedBarAnimation::updateLayout(const QVector<QRectF> &oldLayout, const QVector<QRectF> &newLayout)
71 {
70 {
72 setDuration(ChartAnimationDuration);
71 setDuration(ChartAnimationDuration);
73 setKeyValueAt(0.0, qVariantFromValue(oldLayout));
72 setKeyValueAt(0.0, qVariantFromValue(oldLayout));
74 setKeyValueAt(1.0, qVariantFromValue(newLayout));
73 setKeyValueAt(1.0, qVariantFromValue(newLayout));
75 QTimer::singleShot(0, this, SLOT(start()));
74 QTimer::singleShot(0, this, SLOT(start()));
76 }
75 }
77 #include "moc_stackedbaranimation_p.cpp"
76 #include "moc_stackedbaranimation_p.cpp"
78
77
79 QTCOMMERCIALCHART_END_NAMESPACE
78 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now