1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QtCore/QPointF>
#include <QtGraphs/QLineSeries>
#include "private/qgraphpointanimation_p.h"
#include "private/qgraphtransition_p.h"
#include "private/qxyseries_p.h"
/*!
\qmltype GraphPointAnimation
\inqmlmodule QtGraphs
\ingroup graphs_qml_2D
\inherits XYSeriesAnimation
\brief An animation type which signifies the animation for points.
GraphPointAnimation is an animation type derived from QVariantAnimation which defines how points are animated.
It can make use of QVariantAnimation functionality and properties for its animations, such as \c duration and \c easing.
These animations are housed inside of a QParallelAnimationGroup and hence will run in parallel.
This example shows how to use a GraphPointAnimation to set points to animate with
a \c duration of 1000ms and \c easing of OutCubic:
\snippet doc_src_qmlgraphs.cpp 12
For XYSeries, this is considered to be the main list of points defined inside
the series. The point is linearly interpolated from the start to the end value.
\note GraphPointAnimation currently supports animating only the last point in
a series when a point is appended or removed. If a point is replaced, the
animation will be triggered regardless of the point’s index within the
series.
\sa GraphTransition, SplineControlAnimation
*/
QGraphPointAnimation::QGraphPointAnimation(QObject *parent)
: QXYSeriesAnimation(parent)
{
setDuration(800);
setEasingCurve(QEasingCurve::OutCubic);
}
QGraphPointAnimation::~QGraphPointAnimation() {}
QGraphAnimation::GraphAnimationType QGraphPointAnimation::animationType()
{
return QGraphAnimation::GraphAnimationType::GraphPoint;
}
void QGraphPointAnimation::setAnimatingValue(const QVariant &start, const QVariant &end)
{
setStartValue(start);
setEndValue(end);
}
QVariant QGraphPointAnimation::interpolated(const QVariant &start,
const QVariant &end,
qreal progress) const
{
auto startPoint = qvariant_cast<QPointF>(start);
auto endPoint = qvariant_cast<QPointF>(end);
auto interpolatedPoint = QPointF{
qreal(startPoint.x() * (1.0 - progress) + endPoint.x() * progress),
qreal(startPoint.y() * (1.0 - progress) + endPoint.y() * progress),
};
return QVariant::fromValue(interpolatedPoint);
}
void QGraphPointAnimation::animate()
{
// Hierarchy should look like GraphAnimation -> ParallelAnimationGroup -> GraphTransition -> QXYSeries
auto series = qobject_cast<QXYSeries *>(parent()->parent()->parent());
if (!series) {
qCCritical(lcAnimation, "QGraphPointAnimation::animate. XYSeries not found.");
return;
}
if (animating() == QGraphAnimation::AnimationState::Playing) {
end();
m_activePointIndex = m_newPointIndex;
}
setAnimating(QGraphAnimation::AnimationState::Playing);
auto &pointList = series->d_func()->m_points;
switch (m_currentTransitionType) {
default:
case QGraphTransition::TransitionType::PointAdded: {
pointList.append(series->points().size() >= 1 ? pointList.last() : m_newPoint);
auto startv = QVariant::fromValue(pointList.last());
auto endv = QVariant::fromValue(m_newPoint);
qCDebug(lcAnimation) << "transition type:" << m_currentTransitionType
<< "start value:" << startv.toPointF()
<< "end value:" << endv.toPointF();
setAnimatingValue(startv, endv);
} break;
case QGraphTransition::TransitionType::PointReplaced: {
auto startv = QVariant::fromValue(pointList[m_activePointIndex]);
auto endv = QVariant::fromValue(m_newPoint);
qCDebug(lcAnimation) << "transition type:" << m_currentTransitionType
<< "start value:" << startv.toPointF()
<< "end value:" << endv.toPointF();
setAnimatingValue(startv, endv);
} break;
case QGraphTransition::TransitionType::PointRemoved: {
if (series->points().size() < 1)
break;
auto startv = QVariant::fromValue(pointList[pointList.size() - 1]);
auto endv = QVariant::fromValue(
pointList[pointList.size() > 1 ? pointList.size() - 2 : pointList.size() - 1]);
qCDebug(lcAnimation) << "transition type:" << m_currentTransitionType
<< "start value:" << startv.toPointF()
<< "end value:" << endv.toPointF();
setAnimatingValue(startv, endv);
} break;
}
m_previousTransitionType = m_currentTransitionType;
}
void QGraphPointAnimation::end()
{
auto series = qobject_cast<QXYSeries *>(parent()->parent()->parent());
if (!series || animating() == QGraphAnimation::AnimationState::Stopped) {
m_previousTransitionType = m_currentTransitionType;
return;
}
setAnimating(QGraphAnimation::AnimationState::Stopped);
stop();
auto &points = series->d_func()->m_points;
switch (m_previousTransitionType) {
default:
case QGraphTransition::TransitionType::PointAdded: {
points.replace(m_activePointIndex, qvariant_cast<QPointF>(endValue()));
emit series->pointAdded(points.size() - 1);
emit series->countChanged();
} break;
case QGraphTransition::TransitionType::PointReplaced: {
points.replace(m_activePointIndex, qvariant_cast<QPointF>(endValue()));
emit series->pointReplaced(m_activePointIndex);
} break;
case QGraphTransition::TransitionType::PointRemoved: {
points.remove(points.size() - 1);
emit series->countChanged();
emit series->pointRemoved(points.size() - 1);
} break;
}
m_previousTransitionType = m_currentTransitionType;
emit series->update();
}
void QGraphPointAnimation::valueUpdated(const QVariant &value)
{
auto series = qobject_cast<QXYSeries *>(parent()->parent()->parent());
if (!series)
return;
auto val = qvariant_cast<QPointF>(value);
auto &points = series->d_func()->m_points;
switch (m_currentTransitionType) {
default:
case QGraphTransition::TransitionType::PointAdded: {
points.replace(m_activePointIndex, val);
} break;
case QGraphTransition::TransitionType::PointReplaced: {
points.replace(m_activePointIndex, val);
} break;
case QGraphTransition::TransitionType::PointRemoved: {
if (points.size() > 1)
points.replace(points.size() - 1, val);
} break;
}
emit series->update();
}
|