1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QQUICKANIMATION2_P_H
5#define QQUICKANIMATION2_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include "qquickanimation_p.h"
19
20#include <private/qqmlnullablevalue_p.h>
21
22#include <qqml.h>
23#include <qqmlcontext.h>
24
25#include <private/qvariantanimation_p.h>
26#include "private/qpauseanimationjob_p.h"
27#include <QDebug>
28
29#include <private/qobject_p.h>
30#include "private/qanimationgroupjob_p.h"
31#include <QDebug>
32
33#include <private/qobject_p.h>
34
35
36QT_BEGIN_NAMESPACE
37
38//interface for classes that provide animation actions for QActionAnimation
39class QAbstractAnimationAction
40{
41public:
42 virtual ~QAbstractAnimationAction() {}
43 virtual void doAction() = 0;
44 virtual void debugAction(QDebug, int) const {}
45};
46
47//templated animation action
48//allows us to specify an action that calls a function of a class.
49//(so that class doesn't have to inherit QQuickAbstractAnimationAction)
50template<class T, void (T::*method)(), void (T::*debugMethod)(QDebug, int) const>
51class QAnimationActionProxy : public QAbstractAnimationAction
52{
53public:
54 QAnimationActionProxy(T *instance) : m_instance(instance) {}
55 void doAction() override { (m_instance->*method)(); }
56 void debugAction(QDebug d, int indentLevel) const override { (m_instance->*debugMethod)(d, indentLevel); }
57private:
58 T *m_instance;
59};
60
61//performs an action of type QAbstractAnimationAction
62class Q_AUTOTEST_EXPORT QActionAnimation : public QAbstractAnimationJob
63{
64 Q_DISABLE_COPY(QActionAnimation)
65public:
66 QActionAnimation();
67
68 QActionAnimation(QAbstractAnimationAction *action);
69 ~QActionAnimation() override;
70
71 int duration() const override;
72 void setAnimAction(QAbstractAnimationAction *action);
73
74protected:
75 void updateCurrentTime(int) override;
76 void updateState(State newState, State oldState) override;
77 void debugAnimation(QDebug d) const override;
78
79private:
80 QAbstractAnimationAction *animAction;
81};
82
83class QQuickBulkValueUpdater
84{
85public:
86 virtual ~QQuickBulkValueUpdater() {}
87 virtual void setValue(qreal value) = 0;
88 virtual void debugUpdater(QDebug, int) const {}
89};
90
91//animates QQuickBulkValueUpdater (assumes start and end values will be reals or compatible)
92class Q_QUICK_AUTOTEST_EXPORT QQuickBulkValueAnimator : public QAbstractAnimationJob
93{
94 Q_DISABLE_COPY(QQuickBulkValueAnimator)
95public:
96 QQuickBulkValueAnimator();
97 ~QQuickBulkValueAnimator() override;
98
99 void setAnimValue(QQuickBulkValueUpdater *value);
100 QQuickBulkValueUpdater *getAnimValue() const { return animValue; }
101
102 void setFromIsSourcedValue(bool *value) { fromIsSourced = value; }
103
104 int duration() const override { return m_duration; }
105 void setDuration(int msecs) { m_duration = msecs; }
106
107 QEasingCurve easingCurve() const { return easing; }
108 void setEasingCurve(const QEasingCurve &curve) { easing = curve; }
109
110protected:
111 void updateCurrentTime(int currentTime) override;
112 void topLevelAnimationLoopChanged() override;
113 void debugAnimation(QDebug d) const override;
114
115private:
116 QQuickBulkValueUpdater *animValue;
117 bool *fromIsSourced;
118 int m_duration;
119 QEasingCurve easing;
120};
121
122//an animation that just gives a tick
123template<class T, void (T::*method)(int)>
124class QTickAnimationProxy : public QAbstractAnimationJob
125{
126 Q_DISABLE_COPY(QTickAnimationProxy)
127public:
128 QTickAnimationProxy(T *instance) : QAbstractAnimationJob(), m_instance(instance) {}
129 int duration() const override { return -1; }
130protected:
131 void updateCurrentTime(int msec) override { (m_instance->*method)(msec); }
132
133private:
134 T *m_instance;
135};
136
137class Q_QUICK_PRIVATE_EXPORT QQuickAbstractAnimationPrivate : public QObjectPrivate, public QAnimationJobChangeListener
138{
139 Q_DECLARE_PUBLIC(QQuickAbstractAnimation)
140public:
141 QQuickAbstractAnimationPrivate()
142 : running(false), paused(false), alwaysRunToEnd(false),
143 /*connectedTimeLine(false), */componentComplete(true),
144 avoidPropertyValueSourceStart(false), disableUserControl(false),
145 needsDeferredSetRunning(false), loopCount(1), group(nullptr), animationInstance(nullptr) {}
146
147 bool running:1;
148 bool paused:1;
149 bool alwaysRunToEnd:1;
150 //bool connectedTimeLine:1;
151 bool componentComplete:1;
152 bool avoidPropertyValueSourceStart:1;
153 bool disableUserControl:1;
154 bool needsDeferredSetRunning:1;
155
156 int loopCount;
157
158 void commence();
159 void animationFinished(QAbstractAnimationJob *) override;
160
161 QQmlProperty defaultProperty;
162
163 QQuickAnimationGroup *group;
164 QAbstractAnimationJob* animationInstance;
165
166 static QQmlProperty createProperty(QObject *obj, const QString &str, QObject *infoObj, QString *errorMessage = nullptr);
167 void animationGroupDirty();
168};
169
170class QQuickPauseAnimationPrivate : public QQuickAbstractAnimationPrivate
171{
172 Q_DECLARE_PUBLIC(QQuickPauseAnimation)
173public:
174 QQuickPauseAnimationPrivate()
175 : QQuickAbstractAnimationPrivate(), duration(250) {}
176
177 int duration;
178};
179
180class QQuickScriptActionPrivate : public QQuickAbstractAnimationPrivate
181{
182 Q_DECLARE_PUBLIC(QQuickScriptAction)
183public:
184 QQuickScriptActionPrivate();
185
186 QQmlScriptString script;
187 QString name;
188 QQmlScriptString runScriptScript;
189 bool hasRunScriptScript;
190 bool reversing;
191
192 void execute();
193 QAbstractAnimationAction* createAction();
194 void debugAction(QDebug d, int indentLevel) const;
195 typedef QAnimationActionProxy<QQuickScriptActionPrivate,
196 &QQuickScriptActionPrivate::execute,
197 &QQuickScriptActionPrivate::debugAction> Proxy;
198};
199
200class QQuickPropertyActionPrivate : public QQuickAbstractAnimationPrivate
201{
202 Q_DECLARE_PUBLIC(QQuickPropertyAction)
203public:
204 QQuickPropertyActionPrivate()
205 : QQuickAbstractAnimationPrivate(), target(nullptr) {}
206
207 QObject *target;
208 QString propertyName;
209 QString properties;
210 QList<QObject *> targets;
211 QList<QObject *> exclude;
212
213 QQmlNullableValue<QVariant> value;
214};
215
216class QQuickAnimationGroupPrivate : public QQuickAbstractAnimationPrivate
217{
218 Q_DECLARE_PUBLIC(QQuickAnimationGroup)
219public:
220 QQuickAnimationGroupPrivate()
221 : QQuickAbstractAnimationPrivate(), animationDirty(false) {}
222
223 static void append_animation(QQmlListProperty<QQuickAbstractAnimation> *list, QQuickAbstractAnimation *role);
224 static QQuickAbstractAnimation *at_animation(QQmlListProperty<QQuickAbstractAnimation> *list, qsizetype index);
225 static qsizetype count_animation(QQmlListProperty<QQuickAbstractAnimation> *list);
226 static void clear_animation(QQmlListProperty<QQuickAbstractAnimation> *list);
227 static void replace_animation(QQmlListProperty<QQuickAbstractAnimation> *list, qsizetype index,
228 QQuickAbstractAnimation *role);
229 static void removeLast_animation(QQmlListProperty<QQuickAbstractAnimation> *list);
230 QList<QQuickAbstractAnimation *> animations;
231
232 void restartFromCurrentLoop();
233 void animationCurrentLoopChanged(QAbstractAnimationJob *job) override;
234 bool animationDirty: 1;
235};
236
237class Q_QUICK_PRIVATE_EXPORT QQuickPropertyAnimationPrivate : public QQuickAbstractAnimationPrivate
238{
239 Q_DECLARE_PUBLIC(QQuickPropertyAnimation)
240public:
241 QQuickPropertyAnimationPrivate()
242 : QQuickAbstractAnimationPrivate(), target(nullptr), fromIsDefined(false), toIsDefined(false), ourPropertiesDirty(false),
243 defaultToInterpolatorType(0), interpolatorType(0), interpolator(nullptr), duration(250), actions(nullptr) {}
244
245 void animationCurrentLoopChanged(QAbstractAnimationJob *job) override;
246
247 QVariant from;
248 QVariant to;
249
250 QObject *target;
251 QString propertyName;
252 QString properties;
253 QList<QPointer<QObject>> targets;
254 QList<QObject *> exclude;
255 QString defaultProperties;
256
257 bool fromIsDefined:1;
258 bool toIsDefined:1;
259 bool ourPropertiesDirty : 1;
260 bool defaultToInterpolatorType:1;
261 int interpolatorType;
262 QVariantAnimation::Interpolator interpolator;
263 int duration;
264 QEasingCurve easing;
265
266 // for animations that don't use the QQuickBulkValueAnimator
267 QQuickStateActions *actions;
268
269 static QVariant interpolateVariant(const QVariant &from, const QVariant &to, qreal progress);
270 static void convertVariant(QVariant &variant, QMetaType type);
271};
272
273class QQuickRotationAnimationPrivate : public QQuickPropertyAnimationPrivate
274{
275 Q_DECLARE_PUBLIC(QQuickRotationAnimation)
276public:
277 QQuickRotationAnimationPrivate() : direction(QQuickRotationAnimation::Numerical) {}
278
279 QQuickRotationAnimation::RotationDirection direction;
280};
281
282class Q_AUTOTEST_EXPORT QQuickAnimationPropertyUpdater : public QQuickBulkValueUpdater
283{
284public:
285 QQuickAnimationPropertyUpdater() : interpolatorType(0), interpolator(nullptr), prevInterpolatorType(0), reverse(false), fromIsSourced(false), fromIsDefined(false), wasDeleted(nullptr) {}
286 ~QQuickAnimationPropertyUpdater() override;
287
288 void setValue(qreal v) override;
289
290 void debugUpdater(QDebug d, int indentLevel) const override;
291
292 QQuickStateActions actions;
293 int interpolatorType; //for Number/ColorAnimation
294 QVariantAnimation::Interpolator interpolator;
295 int prevInterpolatorType; //for generic
296 bool reverse;
297 bool fromIsSourced;
298 bool fromIsDefined;
299 bool *wasDeleted;
300};
301
302QT_END_NAMESPACE
303
304#endif // QQUICKANIMATION2_P_H
305

source code of qtdeclarative/src/quick/util/qquickanimation_p_p.h