##// END OF EJS Templates
Fix axis animation crash...
Titta Heikkala -
r2784:37816a0f0581
parent child
Show More
@@ -1,133 +1,135
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd
3 ** Copyright (C) 2015 The Qt Company Ltd
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to The Qt Company, please use contact form at http://qt.io
5 ** For any questions to The Qt Company, please use contact form at http://qt.io
6 **
6 **
7 ** This file is part of the Qt Charts module.
7 ** This file is part of the Qt Charts module.
8 **
8 **
9 ** Licensees holding valid commercial license for Qt may use this file in
9 ** Licensees holding valid commercial license for Qt may use this file in
10 ** accordance with the Qt License Agreement provided with the Software
10 ** accordance with the Qt License Agreement provided with the Software
11 ** or, alternatively, in accordance with the terms contained in a written
11 ** or, alternatively, in accordance with the terms contained in a written
12 ** agreement between you and The Qt Company.
12 ** agreement between you and The Qt Company.
13 **
13 **
14 ** If you have questions regarding the use of this file, please use
14 ** If you have questions regarding the use of this file, please use
15 ** contact form at http://qt.io
15 ** contact form at http://qt.io
16 **
16 **
17 ****************************************************************************/
17 ****************************************************************************/
18
18
19 #include <private/axisanimation_p.h>
19 #include <private/axisanimation_p.h>
20 #include <private/chartaxiselement_p.h>
20 #include <private/chartaxiselement_p.h>
21 #include <private/qabstractaxis_p.h>
21 #include <private/qabstractaxis_p.h>
22
22
23 Q_DECLARE_METATYPE(QVector<qreal>)
23 Q_DECLARE_METATYPE(QVector<qreal>)
24
24
25 QT_CHARTS_BEGIN_NAMESPACE
25 QT_CHARTS_BEGIN_NAMESPACE
26
26
27
27
28 AxisAnimation::AxisAnimation(ChartAxisElement *axis)
28 AxisAnimation::AxisAnimation(ChartAxisElement *axis)
29 : ChartAnimation(axis),
29 : ChartAnimation(axis),
30 m_axis(axis),
30 m_axis(axis),
31 m_type(DefaultAnimation)
31 m_type(DefaultAnimation)
32 {
32 {
33 setDuration(ChartAnimationDuration);
33 setDuration(ChartAnimationDuration);
34 setEasingCurve(QEasingCurve::OutQuart);
34 setEasingCurve(QEasingCurve::OutQuart);
35 }
35 }
36
36
37 AxisAnimation::~AxisAnimation()
37 AxisAnimation::~AxisAnimation()
38 {
38 {
39 }
39 }
40
40
41 void AxisAnimation::setAnimationType(Animation type)
41 void AxisAnimation::setAnimationType(Animation type)
42 {
42 {
43 if (state() != QAbstractAnimation::Stopped)
43 if (state() != QAbstractAnimation::Stopped)
44 stop();
44 stop();
45 m_type = type;
45 m_type = type;
46 }
46 }
47
47
48 void AxisAnimation::setAnimationPoint(const QPointF &point)
48 void AxisAnimation::setAnimationPoint(const QPointF &point)
49 {
49 {
50 if (state() != QAbstractAnimation::Stopped)
50 if (state() != QAbstractAnimation::Stopped)
51 stop();
51 stop();
52 m_point = point;
52 m_point = point;
53 }
53 }
54
54
55 void AxisAnimation::setValues(QVector<qreal> &oldLayout, QVector<qreal> &newLayout)
55 void AxisAnimation::setValues(QVector<qreal> &oldLayout, QVector<qreal> &newLayout)
56 {
56 {
57 if (state() != QAbstractAnimation::Stopped) stop();
57 if (state() != QAbstractAnimation::Stopped) stop();
58
58
59 switch (m_type) {
59 switch (m_type) {
60 case ZoomOutAnimation: {
60 case ZoomOutAnimation: {
61 QRectF rect = m_axis->gridGeometry();
61 QRectF rect = m_axis->gridGeometry();
62 oldLayout.resize(newLayout.count());
62 oldLayout.resize(newLayout.count());
63
63
64 for (int i = 0, j = oldLayout.count() - 1; i < (oldLayout.count() + 1) / 2; ++i, --j) {
64 for (int i = 0, j = oldLayout.count() - 1; i < (oldLayout.count() + 1) / 2; ++i, --j) {
65 oldLayout[i] = m_axis->axis()->orientation() == Qt::Horizontal ? rect.left() : rect.bottom();
65 oldLayout[i] = m_axis->axis()->orientation() == Qt::Horizontal ? rect.left() : rect.bottom();
66 oldLayout[j] = m_axis->axis()->orientation() == Qt::Horizontal ? rect.right() : rect.top();
66 oldLayout[j] = m_axis->axis()->orientation() == Qt::Horizontal ? rect.right() : rect.top();
67 }
67 }
68 }
68 }
69 break;
69 break;
70 case ZoomInAnimation: {
70 case ZoomInAnimation: {
71 int index = qMin(oldLayout.count() * (m_axis->axis()->orientation() == Qt::Horizontal ? m_point.x() : (1 - m_point.y())), newLayout.count() - (qreal)1.0);
71 int index = qMin(oldLayout.count() * (m_axis->axis()->orientation() == Qt::Horizontal ? m_point.x() : (1 - m_point.y())), newLayout.count() - (qreal)1.0);
72 oldLayout.resize(newLayout.count());
72 oldLayout.resize(newLayout.count());
73
73
74 if (index < 0)
75 break;
74 for (int i = 0; i < oldLayout.count(); i++)
76 for (int i = 0; i < oldLayout.count(); i++)
75 oldLayout[i] = oldLayout[index];
77 oldLayout[i] = oldLayout[index];
76 }
78 }
77 break;
79 break;
78 case MoveForwardAnimation: {
80 case MoveForwardAnimation: {
79 oldLayout.resize(newLayout.count());
81 oldLayout.resize(newLayout.count());
80
82
81 for (int i = 0, j = i + 1; i < oldLayout.count() - 1; ++i, ++j)
83 for (int i = 0, j = i + 1; i < oldLayout.count() - 1; ++i, ++j)
82 oldLayout[i] = oldLayout[j];
84 oldLayout[i] = oldLayout[j];
83 }
85 }
84 break;
86 break;
85 case MoveBackwordAnimation: {
87 case MoveBackwordAnimation: {
86 oldLayout.resize(newLayout.count());
88 oldLayout.resize(newLayout.count());
87
89
88 for (int i = oldLayout.count() - 1, j = i - 1; i > 0; --i, --j)
90 for (int i = oldLayout.count() - 1, j = i - 1; i > 0; --i, --j)
89 oldLayout[i] = oldLayout[j];
91 oldLayout[i] = oldLayout[j];
90 }
92 }
91 break;
93 break;
92 default: {
94 default: {
93 oldLayout.resize(newLayout.count());
95 oldLayout.resize(newLayout.count());
94 QRectF rect = m_axis->gridGeometry();
96 QRectF rect = m_axis->gridGeometry();
95 for (int i = 0, j = oldLayout.count() - 1; i < oldLayout.count(); ++i, --j)
97 for (int i = 0, j = oldLayout.count() - 1; i < oldLayout.count(); ++i, --j)
96 oldLayout[i] = m_axis->axis()->orientation() == Qt::Horizontal ? rect.left() : rect.top();
98 oldLayout[i] = m_axis->axis()->orientation() == Qt::Horizontal ? rect.left() : rect.top();
97 }
99 }
98 break;
100 break;
99 }
101 }
100
102
101 QVariantAnimation::KeyValues value;
103 QVariantAnimation::KeyValues value;
102 setKeyValues(value); //workaround for wrong interpolation call
104 setKeyValues(value); //workaround for wrong interpolation call
103 setKeyValueAt(0.0, qVariantFromValue(oldLayout));
105 setKeyValueAt(0.0, qVariantFromValue(oldLayout));
104 setKeyValueAt(1.0, qVariantFromValue(newLayout));
106 setKeyValueAt(1.0, qVariantFromValue(newLayout));
105 }
107 }
106
108
107 QVariant AxisAnimation::interpolated(const QVariant &start, const QVariant &end, qreal progress) const
109 QVariant AxisAnimation::interpolated(const QVariant &start, const QVariant &end, qreal progress) const
108 {
110 {
109 QVector<qreal> startVector = qvariant_cast<QVector<qreal> >(start);
111 QVector<qreal> startVector = qvariant_cast<QVector<qreal> >(start);
110 QVector<qreal> endVecotr = qvariant_cast<QVector<qreal> >(end);
112 QVector<qreal> endVecotr = qvariant_cast<QVector<qreal> >(end);
111 QVector<qreal> result;
113 QVector<qreal> result;
112
114
113 Q_ASSERT(startVector.count() == endVecotr.count()) ;
115 Q_ASSERT(startVector.count() == endVecotr.count()) ;
114
116
115 for (int i = 0; i < startVector.count(); i++) {
117 for (int i = 0; i < startVector.count(); i++) {
116 qreal value = startVector[i] + ((endVecotr[i] - startVector[i]) * progress);
118 qreal value = startVector[i] + ((endVecotr[i] - startVector[i]) * progress);
117 result << value;
119 result << value;
118 }
120 }
119 return qVariantFromValue(result);
121 return qVariantFromValue(result);
120 }
122 }
121
123
122
124
123 void AxisAnimation::updateCurrentValue(const QVariant &value)
125 void AxisAnimation::updateCurrentValue(const QVariant &value)
124 {
126 {
125 if (state() != QAbstractAnimation::Stopped) { //workaround
127 if (state() != QAbstractAnimation::Stopped) { //workaround
126 QVector<qreal> vector = qvariant_cast<QVector<qreal> >(value);
128 QVector<qreal> vector = qvariant_cast<QVector<qreal> >(value);
127 m_axis->setLayout(vector);
129 m_axis->setLayout(vector);
128 m_axis->updateGeometry();
130 m_axis->updateGeometry();
129 }
131 }
130
132
131 }
133 }
132
134
133 QT_CHARTS_END_NAMESPACE
135 QT_CHARTS_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now