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/xyanimation_p.h>
31#include <private/xychart_p.h>
32#include <QtCore/QDebug>
33
34Q_DECLARE_METATYPE(QVector<QPointF>)
35
36QT_CHARTS_BEGIN_NAMESPACE
37
38XYAnimation::XYAnimation(XYChart *item, int duration, QEasingCurve &curve)
39 : ChartAnimation(item),
40 m_type(NewAnimation),
41 m_dirty(false),
42 m_index(-1),
43 m_item(item)
44{
45 setDuration(duration);
46 setEasingCurve(curve);
47}
48
49XYAnimation::~XYAnimation()
50{
51}
52
53void XYAnimation::setup(const QVector<QPointF> &oldPoints, const QVector<QPointF> &newPoints, int index)
54{
55 m_type = NewAnimation;
56
57 if (state() != QAbstractAnimation::Stopped) {
58 stop();
59 m_dirty = false;
60 }
61
62 if (!m_dirty) {
63 m_dirty = true;
64 m_oldPoints = oldPoints;
65 }
66
67 m_newPoints = newPoints;
68
69 int x = m_oldPoints.count();
70 int y = m_newPoints.count();
71 int diff = x - y;
72 int requestedDiff = oldPoints.count() - y;
73
74 // m_oldPoints can be whatever between 0 and actual points count if new animation setup
75 // interrupts a previous animation, so only do remove and add animations if both
76 // stored diff and requested diff indicate add or remove. Also ensure that index is not
77 // invalid.
78 if (diff == 1 && requestedDiff == 1 && index >= 0 && y > 0 && index <= y) {
79 //remove point
80 m_newPoints.insert(i: index, t: index > 0 ? newPoints[index - 1] : newPoints[index]);
81 m_index = index;
82 m_type = RemovePointAnimation;
83 }
84
85 if (diff == -1 && requestedDiff == -1 && index >= 0 && index <= x) {
86 //add point
87 m_oldPoints.insert(i: index, t: index > 0 ? newPoints[index - 1] : newPoints[index]);
88 m_index = index;
89 m_type = AddPointAnimation;
90 }
91
92 x = m_oldPoints.count();
93 y = m_newPoints.count();
94
95 if (x != y)
96 m_type = NewAnimation;
97 else if (m_type == NewAnimation)
98 m_type = ReplacePointAnimation;
99
100 setKeyValueAt(step: 0.0, value: QVariant::fromValue(value: m_oldPoints));
101 setKeyValueAt(step: 1.0, value: QVariant::fromValue(value: m_newPoints));
102}
103
104QVariant XYAnimation::interpolated(const QVariant &start, const QVariant &end, qreal progress) const
105{
106 QVector<QPointF> startVector = qvariant_cast<QVector<QPointF> >(v: start);
107 QVector<QPointF> endVector = qvariant_cast<QVector<QPointF> >(v: end);
108 QVector<QPointF> result;
109
110 switch (m_type) {
111
112 case ReplacePointAnimation:
113 case AddPointAnimation:
114 case RemovePointAnimation: {
115 if (startVector.count() != endVector.count())
116 break;
117
118 for (int i = 0; i < startVector.count(); i++) {
119 qreal x = startVector[i].x() + ((endVector[i].x() - startVector[i].x()) * progress);
120 qreal y = startVector[i].y() + ((endVector[i].y() - startVector[i].y()) * progress);
121 result << QPointF(x, y);
122 }
123
124 }
125 break;
126 case NewAnimation: {
127 for (int i = 0; i < endVector.count() * qBound(min: qreal(0), val: progress, max: qreal(1)); i++)
128 result << endVector[i];
129 }
130 break;
131 default:
132 qWarning() << "Unknown type of animation";
133 break;
134 }
135
136 return QVariant::fromValue(value: result);
137}
138
139void XYAnimation::updateCurrentValue(const QVariant &value)
140{
141 if (state() != QAbstractAnimation::Stopped) { //workaround
142
143 QVector<QPointF> vector = qvariant_cast<QVector<QPointF> >(v: value);
144 m_item->setGeometryPoints(vector);
145 m_item->updateGeometry();
146 m_item->setDirty(true);
147 m_dirty = false;
148
149 }
150}
151
152void XYAnimation::updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState)
153{
154 if (oldState == QAbstractAnimation::Running && newState == QAbstractAnimation::Stopped) {
155 if (m_item->isDirty() && m_type == RemovePointAnimation) {
156 if (!m_newPoints.isEmpty())
157 m_newPoints.remove(i: m_index);
158 m_item->setGeometryPoints(m_newPoints);
159 }
160 }
161}
162
163QT_CHARTS_END_NAMESPACE
164
165#include "moc_chartanimation_p.cpp"
166

source code of qtcharts/src/charts/animations/xyanimation.cpp