1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt Charts module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 or (at your option) any later version |
20 | ** approved by the KDE Free Qt Foundation. The licenses are as published by |
21 | ** the Free Software Foundation and appearing in the file LICENSE.GPL3 |
22 | ** included in the packaging of this file. Please review the following |
23 | ** information to ensure the GNU General Public License requirements will |
24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
25 | ** |
26 | ** $QT_END_LICENSE$ |
27 | ** |
28 | ****************************************************************************/ |
29 | |
30 | #include <private/axisanimation_p.h> |
31 | #include <private/chartaxiselement_p.h> |
32 | #include <private/qabstractaxis_p.h> |
33 | |
34 | Q_DECLARE_METATYPE(QVector<qreal>) |
35 | |
36 | QT_CHARTS_BEGIN_NAMESPACE |
37 | |
38 | |
39 | AxisAnimation::AxisAnimation(ChartAxisElement *axis, int duration, QEasingCurve &curve) |
40 | : ChartAnimation(axis), |
41 | m_axis(axis), |
42 | m_type(DefaultAnimation) |
43 | { |
44 | setDuration(duration); |
45 | setEasingCurve(curve); |
46 | } |
47 | |
48 | AxisAnimation::~AxisAnimation() |
49 | { |
50 | } |
51 | |
52 | void AxisAnimation::setAnimationType(Animation type) |
53 | { |
54 | if (state() != QAbstractAnimation::Stopped) |
55 | stop(); |
56 | m_type = type; |
57 | } |
58 | |
59 | void AxisAnimation::setAnimationPoint(const QPointF &point) |
60 | { |
61 | if (state() != QAbstractAnimation::Stopped) |
62 | stop(); |
63 | m_point = point; |
64 | } |
65 | |
66 | void AxisAnimation::setValues(QVector<qreal> &oldLayout, QVector<qreal> &newLayout) |
67 | { |
68 | if (state() != QAbstractAnimation::Stopped) stop(); |
69 | |
70 | switch (m_type) { |
71 | case ZoomOutAnimation: { |
72 | QRectF rect = m_axis->gridGeometry(); |
73 | oldLayout.resize(asize: newLayout.count()); |
74 | |
75 | for (int i = 0, j = oldLayout.count() - 1; i < (oldLayout.count() + 1) / 2; ++i, --j) { |
76 | oldLayout[i] = m_axis->axis()->orientation() == Qt::Horizontal ? rect.left() : rect.bottom(); |
77 | oldLayout[j] = m_axis->axis()->orientation() == Qt::Horizontal ? rect.right() : rect.top(); |
78 | } |
79 | } |
80 | break; |
81 | case ZoomInAnimation: { |
82 | int index = qMin(a: oldLayout.count() * (m_axis->axis()->orientation() == Qt::Horizontal ? m_point.x() : (1 - m_point.y())), b: newLayout.count() - (qreal)1.0); |
83 | oldLayout.resize(asize: newLayout.count()); |
84 | |
85 | if (index < 0) |
86 | break; |
87 | for (int i = 0; i < oldLayout.count(); i++) |
88 | oldLayout[i] = oldLayout[index]; |
89 | } |
90 | break; |
91 | case MoveForwardAnimation: { |
92 | oldLayout.resize(asize: newLayout.count()); |
93 | |
94 | for (int i = 0, j = i + 1; i < oldLayout.count() - 1; ++i, ++j) |
95 | oldLayout[i] = oldLayout[j]; |
96 | } |
97 | break; |
98 | case MoveBackwordAnimation: { |
99 | oldLayout.resize(asize: newLayout.count()); |
100 | |
101 | for (int i = oldLayout.count() - 1, j = i - 1; i > 0; --i, --j) |
102 | oldLayout[i] = oldLayout[j]; |
103 | } |
104 | break; |
105 | default: { |
106 | oldLayout.resize(asize: newLayout.count()); |
107 | QRectF rect = m_axis->gridGeometry(); |
108 | for (int i = 0, j = oldLayout.count() - 1; i < oldLayout.count(); ++i, --j) |
109 | oldLayout[i] = m_axis->axis()->orientation() == Qt::Horizontal ? rect.left() : rect.top(); |
110 | } |
111 | break; |
112 | } |
113 | |
114 | QVariantAnimation::KeyValues value; |
115 | setKeyValues(value); //workaround for wrong interpolation call |
116 | setKeyValueAt(step: 0.0, value: QVariant::fromValue(value: oldLayout)); |
117 | setKeyValueAt(step: 1.0, value: QVariant::fromValue(value: newLayout)); |
118 | } |
119 | |
120 | QVariant AxisAnimation::interpolated(const QVariant &start, const QVariant &end, qreal progress) const |
121 | { |
122 | QVector<qreal> startVector = qvariant_cast<QVector<qreal> >(v: start); |
123 | QVector<qreal> endVecotr = qvariant_cast<QVector<qreal> >(v: end); |
124 | QVector<qreal> result; |
125 | |
126 | Q_ASSERT(startVector.count() == endVecotr.count()) ; |
127 | |
128 | for (int i = 0; i < startVector.count(); i++) { |
129 | qreal value = startVector[i] + ((endVecotr[i] - startVector[i]) * progress); |
130 | result << value; |
131 | } |
132 | return QVariant::fromValue(value: result); |
133 | } |
134 | |
135 | |
136 | void AxisAnimation::updateCurrentValue(const QVariant &value) |
137 | { |
138 | if (state() != QAbstractAnimation::Stopped) { //workaround |
139 | QVector<qreal> vector = qvariant_cast<QVector<qreal> >(v: value); |
140 | m_axis->setLayout(vector); |
141 | m_axis->updateGeometry(); |
142 | } |
143 | |
144 | } |
145 | |
146 | QT_CHARTS_END_NAMESPACE |
147 | |