1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #ifndef QDECLARATIVEANIMATION_P_H
43 #define QDECLARATIVEANIMATION_P_H
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.
56 #include "QtQuick1/private/qdeclarativeanimation_p.h"
58 #include "QtDeclarative/private/qdeclarativenullablevalue_p_p.h"
59 #include "QtQuick1/private/qdeclarativetimeline_p_p.h"
61 #include <QtDeclarative/qdeclarative.h>
62 #include <QtQuick1/qdeclarativeitem.h>
63 #include <QtDeclarative/qdeclarativecontext.h>
65 #include <QtCore/QPauseAnimation>
66 #include <QtCore/QVariantAnimation>
67 #include <QtCore/QAnimationGroup>
70 #include <private/qobject_p.h>
71 #include <private/qvariantanimation_p.h>
75 //interface for classes that provide animation actions for QActionAnimation_1
76 class QAbstractAnimationAction
79 virtual ~QAbstractAnimationAction() {}
80 virtual void doAction() = 0;
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 QDeclarative1AbstractAnimationAction)
86 template<class T, void (T::*method)()>
87 class QAnimationActionProxy_1 : public QAbstractAnimationAction
90 QAnimationActionProxy_1(T *p) : m_p(p) {}
91 virtual void doAction() { (m_p->*method)(); }
97 //performs an action of type QAbstractAnimationAction
98 class Q_AUTOTEST_EXPORT QActionAnimation_1 : public QAbstractAnimation
102 QActionAnimation_1(QObject *parent = 0) : QAbstractAnimation(parent), animAction(0), policy(KeepWhenStopped) {}
103 QActionAnimation_1(QAbstractAnimationAction *action, QObject *parent = 0)
104 : QAbstractAnimation(parent), animAction(action), policy(KeepWhenStopped) {}
105 ~QActionAnimation_1() { if (policy == DeleteWhenStopped) { delete animAction; animAction = 0; } }
106 virtual int duration() const { return 0; }
107 void setAnimAction(QAbstractAnimationAction *action, DeletionPolicy p)
109 if (state() == Running)
111 if (policy == DeleteWhenStopped)
117 virtual void updateCurrentTime(int) {}
119 virtual void updateState(State newState, State /*oldState*/)
121 if (newState == Running) {
123 animAction->doAction();
124 if (state() == Stopped && policy == DeleteWhenStopped) {
133 QAbstractAnimationAction *animAction;
134 DeletionPolicy policy;
137 class QDeclarative1BulkValueUpdater
140 virtual ~QDeclarative1BulkValueUpdater() {}
141 virtual void setValue(qreal value) = 0;
144 //animates QDeclarative1BulkValueUpdater (assumes start and end values will be reals or compatible)
145 class Q_AUTOTEST_EXPORT QDeclarative1BulkValueAnimator : public QVariantAnimation
149 QDeclarative1BulkValueAnimator(QObject *parent = 0) : QVariantAnimation(parent), animValue(0), fromSourced(0), policy(KeepWhenStopped) {}
150 ~QDeclarative1BulkValueAnimator() { if (policy == DeleteWhenStopped) { delete animValue; animValue = 0; } }
151 void setAnimValue(QDeclarative1BulkValueUpdater *value, DeletionPolicy p)
153 if (state() == Running)
155 if (policy == DeleteWhenStopped)
160 void setFromSourcedValue(bool *value)
165 virtual void updateCurrentValue(const QVariant &value)
167 if (state() == QAbstractAnimation::Stopped)
171 animValue->setValue(value.toReal());
173 virtual void updateState(State newState, State oldState)
175 QVariantAnimation::updateState(newState, oldState);
176 if (newState == Running) {
177 //check for new from every loop
179 *fromSourced = false;
184 QDeclarative1BulkValueUpdater *animValue;
186 DeletionPolicy policy;
189 //an animation that just gives a tick
190 template<class T, void (T::*method)(int)>
191 class QTickAnimationProxy_1 : public QAbstractAnimation
193 //Q_OBJECT //doesn't work with templating
195 QTickAnimationProxy_1(T *p, QObject *parent = 0) : QAbstractAnimation(parent), m_p(p) {}
196 virtual int duration() const { return -1; }
198 virtual void updateCurrentTime(int msec) { (m_p->*method)(msec); }
204 class QDeclarative1AbstractAnimationPrivate : public QObjectPrivate
206 Q_DECLARE_PUBLIC(QDeclarative1AbstractAnimation)
208 QDeclarative1AbstractAnimationPrivate()
209 : running(false), paused(false), alwaysRunToEnd(false),
210 connectedTimeLine(false), componentComplete(true),
211 avoidPropertyValueSourceStart(false), disableUserControl(false),
212 registered(false), loopCount(1), group(0) {}
216 bool alwaysRunToEnd:1;
217 bool connectedTimeLine:1;
218 bool componentComplete:1;
219 bool avoidPropertyValueSourceStart:1;
220 bool disableUserControl:1;
227 QDeclarativeProperty defaultProperty;
229 QDeclarative1AnimationGroup *group;
231 static QDeclarativeProperty createProperty(QObject *obj, const QString &str, QObject *infoObj);
234 class QDeclarative1PauseAnimationPrivate : public QDeclarative1AbstractAnimationPrivate
236 Q_DECLARE_PUBLIC(QDeclarative1PauseAnimation)
238 QDeclarative1PauseAnimationPrivate()
239 : QDeclarative1AbstractAnimationPrivate(), pa(0) {}
246 class QDeclarative1ScriptActionPrivate : public QDeclarative1AbstractAnimationPrivate
248 Q_DECLARE_PUBLIC(QDeclarative1ScriptAction)
250 QDeclarative1ScriptActionPrivate();
254 QDeclarativeScriptString script;
256 QDeclarativeScriptString runScriptScript;
257 bool hasRunScriptScript;
262 QAnimationActionProxy_1<QDeclarative1ScriptActionPrivate,
263 &QDeclarative1ScriptActionPrivate::execute> proxy;
264 QActionAnimation_1 *rsa;
267 class QDeclarative1PropertyActionPrivate : public QDeclarative1AbstractAnimationPrivate
269 Q_DECLARE_PUBLIC(QDeclarative1PropertyAction)
271 QDeclarative1PropertyActionPrivate()
272 : QDeclarative1AbstractAnimationPrivate(), target(0), spa(0) {}
277 QString propertyName;
279 QList<QObject *> targets;
280 QList<QObject *> exclude;
282 QDeclarativeNullableValue<QVariant> value;
284 QActionAnimation_1 *spa;
287 class QDeclarative1AnimationGroupPrivate : public QDeclarative1AbstractAnimationPrivate
289 Q_DECLARE_PUBLIC(QDeclarative1AnimationGroup)
291 QDeclarative1AnimationGroupPrivate()
292 : QDeclarative1AbstractAnimationPrivate(), ag(0) {}
294 static void append_animation(QDeclarativeListProperty<QDeclarative1AbstractAnimation> *list, QDeclarative1AbstractAnimation *role);
295 static void clear_animation(QDeclarativeListProperty<QDeclarative1AbstractAnimation> *list);
296 QList<QDeclarative1AbstractAnimation *> animations;
300 class QDeclarative1PropertyAnimationPrivate : public QDeclarative1AbstractAnimationPrivate
302 Q_DECLARE_PUBLIC(QDeclarative1PropertyAnimation)
304 QDeclarative1PropertyAnimationPrivate()
305 : QDeclarative1AbstractAnimationPrivate(), target(0), fromSourced(false), fromIsDefined(false), toIsDefined(false),
306 rangeIsSet(false), defaultToInterpolatorType(0), interpolatorType(0), interpolator(0), va(0), actions(0) {}
314 QString propertyName;
316 QList<QObject *> targets;
317 QList<QObject *> exclude;
318 QString defaultProperties;
321 bool fromIsDefined:1;
324 bool defaultToInterpolatorType:1;
325 int interpolatorType;
326 QVariantAnimation::Interpolator interpolator;
328 QDeclarative1BulkValueAnimator *va;
330 // for animations that don't use the QDeclarative1BulkValueAnimator
331 QDeclarative1StateActions *actions;
333 static QVariant interpolateVariant(const QVariant &from, const QVariant &to, qreal progress);
334 static void convertVariant(QVariant &variant, int type);
337 class QDeclarative1RotationAnimationPrivate : public QDeclarative1PropertyAnimationPrivate
339 Q_DECLARE_PUBLIC(QDeclarative1RotationAnimation)
341 QDeclarative1RotationAnimationPrivate() : direction(QDeclarative1RotationAnimation::Numerical) {}
343 QDeclarative1RotationAnimation::RotationDirection direction;
346 class QDeclarative1ParentAnimationPrivate : public QDeclarative1AnimationGroupPrivate
348 Q_DECLARE_PUBLIC(QDeclarative1ParentAnimation)
350 QDeclarative1ParentAnimationPrivate()
351 : QDeclarative1AnimationGroupPrivate(), target(0), newParent(0),
352 via(0), topLevelGroup(0), startAction(0), endAction(0) {}
354 QDeclarativeItem *target;
355 QDeclarativeItem *newParent;
356 QDeclarativeItem *via;
358 QSequentialAnimationGroup *topLevelGroup;
359 QActionAnimation_1 *startAction;
360 QActionAnimation_1 *endAction;
362 QPointF computeTransformOrigin(QDeclarativeItem::TransformOrigin origin, qreal width, qreal height) const;
365 class QDeclarative1AnchorAnimationPrivate : public QDeclarative1AbstractAnimationPrivate
367 Q_DECLARE_PUBLIC(QDeclarative1AnchorAnimation)
369 QDeclarative1AnchorAnimationPrivate() : rangeIsSet(false), va(0),
370 interpolator(QVariantAnimationPrivate::getInterpolator(QMetaType::QReal)) {}
373 QDeclarative1BulkValueAnimator *va;
374 QVariantAnimation::Interpolator interpolator;
375 QList<QDeclarativeItem*> targets;
378 class Q_AUTOTEST_EXPORT QDeclarative1AnimationPropertyUpdater : public QDeclarative1BulkValueUpdater
381 QDeclarative1StateActions actions;
382 int interpolatorType; //for Number/ColorAnimation
383 int prevInterpolatorType; //for generic
384 QVariantAnimation::Interpolator interpolator;
389 QDeclarative1AnimationPropertyUpdater() : prevInterpolatorType(0), wasDeleted(0) {}
390 ~QDeclarative1AnimationPropertyUpdater() { if (wasDeleted) *wasDeleted = true; }
391 void setValue(qreal v);
396 #endif // QDECLARATIVEANIMATION_P_H