1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
6 | **
|
---|
7 | ** This file is part of the QtDeclarative module of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at qt-info@nokia.com.
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #ifndef QDECLARATIVEANIMATION_P_H
|
---|
43 | #define QDECLARATIVEANIMATION_P_H
|
---|
44 |
|
---|
45 | //
|
---|
46 | // W A R N I N G
|
---|
47 | // -------------
|
---|
48 | //
|
---|
49 | // This file is not part of the Qt API. It exists purely as an
|
---|
50 | // implementation detail. This header file may change from version to
|
---|
51 | // version without notice, or even be removed.
|
---|
52 | //
|
---|
53 | // We mean it.
|
---|
54 | //
|
---|
55 |
|
---|
56 | #include "private/qdeclarativeanimation_p.h"
|
---|
57 |
|
---|
58 | #include "private/qdeclarativenullablevalue_p_p.h"
|
---|
59 | #include "private/qdeclarativetimeline_p_p.h"
|
---|
60 |
|
---|
61 | #include <qdeclarative.h>
|
---|
62 | #include <qdeclarativeitem.h>
|
---|
63 | #include <qdeclarativecontext.h>
|
---|
64 |
|
---|
65 | #include <QtCore/QPauseAnimation>
|
---|
66 | #include <QtCore/QVariantAnimation>
|
---|
67 | #include <QtCore/QAnimationGroup>
|
---|
68 | #include <QtGui/QColor>
|
---|
69 | #include <QDebug>
|
---|
70 |
|
---|
71 | #include <private/qobject_p.h>
|
---|
72 | #include <private/qvariantanimation_p.h>
|
---|
73 |
|
---|
74 | QT_BEGIN_NAMESPACE
|
---|
75 |
|
---|
76 | //interface for classes that provide animation actions for QActionAnimation
|
---|
77 | class QAbstractAnimationAction
|
---|
78 | {
|
---|
79 | public:
|
---|
80 | virtual ~QAbstractAnimationAction() {}
|
---|
81 | virtual void doAction() = 0;
|
---|
82 | };
|
---|
83 |
|
---|
84 | //templated animation action
|
---|
85 | //allows us to specify an action that calls a function of a class.
|
---|
86 | //(so that class doesn't have to inherit QDeclarativeAbstractAnimationAction)
|
---|
87 | template<class T, void (T::*method)()>
|
---|
88 | class QAnimationActionProxy : public QAbstractAnimationAction
|
---|
89 | {
|
---|
90 | public:
|
---|
91 | QAnimationActionProxy(T *p) : m_p(p) {}
|
---|
92 | virtual void doAction() { (m_p->*method)(); }
|
---|
93 |
|
---|
94 | private:
|
---|
95 | T *m_p;
|
---|
96 | };
|
---|
97 |
|
---|
98 | //performs an action of type QAbstractAnimationAction
|
---|
99 | class Q_AUTOTEST_EXPORT QActionAnimation : public QAbstractAnimation
|
---|
100 | {
|
---|
101 | Q_OBJECT
|
---|
102 | public:
|
---|
103 | QActionAnimation(QObject *parent = 0) : QAbstractAnimation(parent), animAction(0), policy(KeepWhenStopped) {}
|
---|
104 | QActionAnimation(QAbstractAnimationAction *action, QObject *parent = 0)
|
---|
105 | : QAbstractAnimation(parent), animAction(action), policy(KeepWhenStopped) {}
|
---|
106 | ~QActionAnimation() { if (policy == DeleteWhenStopped) { delete animAction; animAction = 0; } }
|
---|
107 | virtual int duration() const { return 0; }
|
---|
108 | void setAnimAction(QAbstractAnimationAction *action, DeletionPolicy p)
|
---|
109 | {
|
---|
110 | if (state() == Running)
|
---|
111 | stop();
|
---|
112 | if (policy == DeleteWhenStopped)
|
---|
113 | delete animAction;
|
---|
114 | animAction = action;
|
---|
115 | policy = p;
|
---|
116 | }
|
---|
117 | protected:
|
---|
118 | virtual void updateCurrentTime(int) {}
|
---|
119 |
|
---|
120 | virtual void updateState(State newState, State /*oldState*/)
|
---|
121 | {
|
---|
122 | if (newState == Running) {
|
---|
123 | if (animAction) {
|
---|
124 | animAction->doAction();
|
---|
125 | if (state() == Stopped && policy == DeleteWhenStopped) {
|
---|
126 | delete animAction;
|
---|
127 | animAction = 0;
|
---|
128 | }
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | private:
|
---|
134 | QAbstractAnimationAction *animAction;
|
---|
135 | DeletionPolicy policy;
|
---|
136 | };
|
---|
137 |
|
---|
138 | class QDeclarativeBulkValueUpdater
|
---|
139 | {
|
---|
140 | public:
|
---|
141 | virtual ~QDeclarativeBulkValueUpdater() {}
|
---|
142 | virtual void setValue(qreal value) = 0;
|
---|
143 | };
|
---|
144 |
|
---|
145 | //animates QDeclarativeBulkValueUpdater (assumes start and end values will be reals or compatible)
|
---|
146 | class Q_AUTOTEST_EXPORT QDeclarativeBulkValueAnimator : public QVariantAnimation
|
---|
147 | {
|
---|
148 | Q_OBJECT
|
---|
149 | public:
|
---|
150 | QDeclarativeBulkValueAnimator(QObject *parent = 0) : QVariantAnimation(parent), animValue(0), fromSourced(0), policy(KeepWhenStopped) {}
|
---|
151 | ~QDeclarativeBulkValueAnimator() { if (policy == DeleteWhenStopped) { delete animValue; animValue = 0; } }
|
---|
152 | void setAnimValue(QDeclarativeBulkValueUpdater *value, DeletionPolicy p)
|
---|
153 | {
|
---|
154 | if (state() == Running)
|
---|
155 | stop();
|
---|
156 | if (policy == DeleteWhenStopped)
|
---|
157 | delete animValue;
|
---|
158 | animValue = value;
|
---|
159 | policy = p;
|
---|
160 | }
|
---|
161 | void setFromSourcedValue(bool *value)
|
---|
162 | {
|
---|
163 | fromSourced = value;
|
---|
164 | }
|
---|
165 | protected:
|
---|
166 | virtual void updateCurrentValue(const QVariant &value)
|
---|
167 | {
|
---|
168 | if (state() == QAbstractAnimation::Stopped)
|
---|
169 | return;
|
---|
170 |
|
---|
171 | if (animValue)
|
---|
172 | animValue->setValue(value.toReal());
|
---|
173 | }
|
---|
174 | virtual void updateState(State newState, State oldState)
|
---|
175 | {
|
---|
176 | QVariantAnimation::updateState(newState, oldState);
|
---|
177 | if (newState == Running) {
|
---|
178 | //check for new from every loop
|
---|
179 | if (fromSourced)
|
---|
180 | *fromSourced = false;
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | private:
|
---|
185 | QDeclarativeBulkValueUpdater *animValue;
|
---|
186 | bool *fromSourced;
|
---|
187 | DeletionPolicy policy;
|
---|
188 | };
|
---|
189 |
|
---|
190 | //an animation that just gives a tick
|
---|
191 | template<class T, void (T::*method)(int)>
|
---|
192 | class QTickAnimationProxy : public QAbstractAnimation
|
---|
193 | {
|
---|
194 | //Q_OBJECT //doesn't work with templating
|
---|
195 | public:
|
---|
196 | QTickAnimationProxy(T *p, QObject *parent = 0) : QAbstractAnimation(parent), m_p(p) {}
|
---|
197 | virtual int duration() const { return -1; }
|
---|
198 | protected:
|
---|
199 | virtual void updateCurrentTime(int msec) { (m_p->*method)(msec); }
|
---|
200 |
|
---|
201 | private:
|
---|
202 | T *m_p;
|
---|
203 | };
|
---|
204 |
|
---|
205 | class QDeclarativeAbstractAnimationPrivate : public QObjectPrivate
|
---|
206 | {
|
---|
207 | Q_DECLARE_PUBLIC(QDeclarativeAbstractAnimation)
|
---|
208 | public:
|
---|
209 | QDeclarativeAbstractAnimationPrivate()
|
---|
210 | : running(false), paused(false), alwaysRunToEnd(false),
|
---|
211 | connectedTimeLine(false), componentComplete(true),
|
---|
212 | avoidPropertyValueSourceStart(false), disableUserControl(false),
|
---|
213 | loopCount(1), group(0) {}
|
---|
214 |
|
---|
215 | bool running:1;
|
---|
216 | bool paused:1;
|
---|
217 | bool alwaysRunToEnd:1;
|
---|
218 | bool connectedTimeLine:1;
|
---|
219 | bool componentComplete:1;
|
---|
220 | bool avoidPropertyValueSourceStart:1;
|
---|
221 | bool disableUserControl:1;
|
---|
222 |
|
---|
223 | int loopCount;
|
---|
224 |
|
---|
225 | void commence();
|
---|
226 |
|
---|
227 | QDeclarativeProperty defaultProperty;
|
---|
228 |
|
---|
229 | QDeclarativeAnimationGroup *group;
|
---|
230 |
|
---|
231 | static QDeclarativeProperty createProperty(QObject *obj, const QString &str, QObject *infoObj);
|
---|
232 | };
|
---|
233 |
|
---|
234 | class QDeclarativePauseAnimationPrivate : public QDeclarativeAbstractAnimationPrivate
|
---|
235 | {
|
---|
236 | Q_DECLARE_PUBLIC(QDeclarativePauseAnimation)
|
---|
237 | public:
|
---|
238 | QDeclarativePauseAnimationPrivate()
|
---|
239 | : QDeclarativeAbstractAnimationPrivate(), pa(0) {}
|
---|
240 |
|
---|
241 | void init();
|
---|
242 |
|
---|
243 | QPauseAnimation *pa;
|
---|
244 | };
|
---|
245 |
|
---|
246 | class QDeclarativeScriptActionPrivate : public QDeclarativeAbstractAnimationPrivate
|
---|
247 | {
|
---|
248 | Q_DECLARE_PUBLIC(QDeclarativeScriptAction)
|
---|
249 | public:
|
---|
250 | QDeclarativeScriptActionPrivate()
|
---|
251 | : QDeclarativeAbstractAnimationPrivate(), hasRunScriptScript(false), reversing(false), proxy(this), rsa(0) {}
|
---|
252 |
|
---|
253 | void init();
|
---|
254 |
|
---|
255 | QDeclarativeScriptString script;
|
---|
256 | QString name;
|
---|
257 | QDeclarativeScriptString runScriptScript;
|
---|
258 | bool hasRunScriptScript;
|
---|
259 | bool reversing;
|
---|
260 |
|
---|
261 | void execute();
|
---|
262 |
|
---|
263 | QAnimationActionProxy<QDeclarativeScriptActionPrivate,
|
---|
264 | &QDeclarativeScriptActionPrivate::execute> proxy;
|
---|
265 | QActionAnimation *rsa;
|
---|
266 | };
|
---|
267 |
|
---|
268 | class QDeclarativePropertyActionPrivate : public QDeclarativeAbstractAnimationPrivate
|
---|
269 | {
|
---|
270 | Q_DECLARE_PUBLIC(QDeclarativePropertyAction)
|
---|
271 | public:
|
---|
272 | QDeclarativePropertyActionPrivate()
|
---|
273 | : QDeclarativeAbstractAnimationPrivate(), target(0), spa(0) {}
|
---|
274 |
|
---|
275 | void init();
|
---|
276 |
|
---|
277 | QObject *target;
|
---|
278 | QString propertyName;
|
---|
279 | QString properties;
|
---|
280 | QList<QObject *> targets;
|
---|
281 | QList<QObject *> exclude;
|
---|
282 |
|
---|
283 | QDeclarativeNullableValue<QVariant> value;
|
---|
284 |
|
---|
285 | QActionAnimation *spa;
|
---|
286 | };
|
---|
287 |
|
---|
288 | class QDeclarativeAnimationGroupPrivate : public QDeclarativeAbstractAnimationPrivate
|
---|
289 | {
|
---|
290 | Q_DECLARE_PUBLIC(QDeclarativeAnimationGroup)
|
---|
291 | public:
|
---|
292 | QDeclarativeAnimationGroupPrivate()
|
---|
293 | : QDeclarativeAbstractAnimationPrivate(), ag(0) {}
|
---|
294 |
|
---|
295 | static void append_animation(QDeclarativeListProperty<QDeclarativeAbstractAnimation> *list, QDeclarativeAbstractAnimation *role);
|
---|
296 | static void clear_animation(QDeclarativeListProperty<QDeclarativeAbstractAnimation> *list);
|
---|
297 | QList<QDeclarativeAbstractAnimation *> animations;
|
---|
298 | QAnimationGroup *ag;
|
---|
299 | };
|
---|
300 |
|
---|
301 | class QDeclarativePropertyAnimationPrivate : public QDeclarativeAbstractAnimationPrivate
|
---|
302 | {
|
---|
303 | Q_DECLARE_PUBLIC(QDeclarativePropertyAnimation)
|
---|
304 | public:
|
---|
305 | QDeclarativePropertyAnimationPrivate()
|
---|
306 | : QDeclarativeAbstractAnimationPrivate(), target(0), fromSourced(false), fromIsDefined(false), toIsDefined(false),
|
---|
307 | rangeIsSet(false), defaultToInterpolatorType(0), interpolatorType(0), interpolator(0), va(0), actions(0) {}
|
---|
308 |
|
---|
309 | void init();
|
---|
310 |
|
---|
311 | QVariant from;
|
---|
312 | QVariant to;
|
---|
313 |
|
---|
314 | QObject *target;
|
---|
315 | QString propertyName;
|
---|
316 | QString properties;
|
---|
317 | QList<QObject *> targets;
|
---|
318 | QList<QObject *> exclude;
|
---|
319 | QString defaultProperties;
|
---|
320 |
|
---|
321 | bool fromSourced;
|
---|
322 | bool fromIsDefined:1;
|
---|
323 | bool toIsDefined:1;
|
---|
324 | bool rangeIsSet:1;
|
---|
325 | bool defaultToInterpolatorType:1;
|
---|
326 | int interpolatorType;
|
---|
327 | QVariantAnimation::Interpolator interpolator;
|
---|
328 |
|
---|
329 | QDeclarativeBulkValueAnimator *va;
|
---|
330 |
|
---|
331 | // for animations that don't use the QDeclarativeBulkValueAnimator
|
---|
332 | QDeclarativeStateActions *actions;
|
---|
333 |
|
---|
334 | static QVariant interpolateVariant(const QVariant &from, const QVariant &to, qreal progress);
|
---|
335 | static void convertVariant(QVariant &variant, int type);
|
---|
336 | };
|
---|
337 |
|
---|
338 | class QDeclarativeRotationAnimationPrivate : public QDeclarativePropertyAnimationPrivate
|
---|
339 | {
|
---|
340 | Q_DECLARE_PUBLIC(QDeclarativeRotationAnimation)
|
---|
341 | public:
|
---|
342 | QDeclarativeRotationAnimationPrivate() : direction(QDeclarativeRotationAnimation::Numerical) {}
|
---|
343 |
|
---|
344 | QDeclarativeRotationAnimation::RotationDirection direction;
|
---|
345 | };
|
---|
346 |
|
---|
347 | class QDeclarativeParentAnimationPrivate : public QDeclarativeAnimationGroupPrivate
|
---|
348 | {
|
---|
349 | Q_DECLARE_PUBLIC(QDeclarativeParentAnimation)
|
---|
350 | public:
|
---|
351 | QDeclarativeParentAnimationPrivate()
|
---|
352 | : QDeclarativeAnimationGroupPrivate(), target(0), newParent(0),
|
---|
353 | via(0), topLevelGroup(0), startAction(0), endAction(0) {}
|
---|
354 |
|
---|
355 | QDeclarativeItem *target;
|
---|
356 | QDeclarativeItem *newParent;
|
---|
357 | QDeclarativeItem *via;
|
---|
358 |
|
---|
359 | QSequentialAnimationGroup *topLevelGroup;
|
---|
360 | QActionAnimation *startAction;
|
---|
361 | QActionAnimation *endAction;
|
---|
362 |
|
---|
363 | QPointF computeTransformOrigin(QDeclarativeItem::TransformOrigin origin, qreal width, qreal height) const;
|
---|
364 | };
|
---|
365 |
|
---|
366 | class QDeclarativeAnchorAnimationPrivate : public QDeclarativeAbstractAnimationPrivate
|
---|
367 | {
|
---|
368 | Q_DECLARE_PUBLIC(QDeclarativeAnchorAnimation)
|
---|
369 | public:
|
---|
370 | QDeclarativeAnchorAnimationPrivate() : rangeIsSet(false), va(0),
|
---|
371 | interpolator(QVariantAnimationPrivate::getInterpolator(QMetaType::QReal)) {}
|
---|
372 |
|
---|
373 | bool rangeIsSet;
|
---|
374 | QDeclarativeBulkValueAnimator *va;
|
---|
375 | QVariantAnimation::Interpolator interpolator;
|
---|
376 | QList<QDeclarativeItem*> targets;
|
---|
377 | };
|
---|
378 |
|
---|
379 | class Q_AUTOTEST_EXPORT QDeclarativeAnimationPropertyUpdater : public QDeclarativeBulkValueUpdater
|
---|
380 | {
|
---|
381 | public:
|
---|
382 | QDeclarativeStateActions actions;
|
---|
383 | int interpolatorType; //for Number/ColorAnimation
|
---|
384 | int prevInterpolatorType; //for generic
|
---|
385 | QVariantAnimation::Interpolator interpolator;
|
---|
386 | bool reverse;
|
---|
387 | bool fromSourced;
|
---|
388 | bool fromDefined;
|
---|
389 | bool *wasDeleted;
|
---|
390 | QDeclarativeAnimationPropertyUpdater() : prevInterpolatorType(0), wasDeleted(0) {}
|
---|
391 | ~QDeclarativeAnimationPropertyUpdater() { if (wasDeleted) *wasDeleted = true; }
|
---|
392 | void setValue(qreal v);
|
---|
393 | };
|
---|
394 |
|
---|
395 | QT_END_NAMESPACE
|
---|
396 |
|
---|
397 | #endif // QDECLARATIVEANIMATION_P_H
|
---|