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