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 "qdeclarativeanimation_p.h"
58 #include <private/qdeclarativenullablevalue_p_p.h>
60 #include <qdeclarative.h>
61 #include <qdeclarativecontext.h>
63 #include <QtCore/QPauseAnimation>
64 #include <QtCore/QVariantAnimation>
65 #include <QtCore/QAnimationGroup>
68 #include <private/qobject_p.h>
69 #include <private/qvariantanimation_p.h>
73 //interface for classes that provide animation actions for QActionAnimation
74 class QAbstractAnimationAction
77 virtual ~QAbstractAnimationAction() {}
78 virtual void doAction() = 0;
81 //templated animation action
82 //allows us to specify an action that calls a function of a class.
83 //(so that class doesn't have to inherit QDeclarativeAbstractAnimationAction)
84 template<class T, void (T::*method)()>
85 class QAnimationActionProxy : public QAbstractAnimationAction
88 QAnimationActionProxy(T *p) : m_p(p) {}
89 virtual void doAction() { (m_p->*method)(); }
95 //performs an action of type QAbstractAnimationAction
96 class Q_AUTOTEST_EXPORT QActionAnimation : public QAbstractAnimation
100 QActionAnimation(QObject *parent = 0) : QAbstractAnimation(parent), animAction(0), policy(KeepWhenStopped) {}
101 QActionAnimation(QAbstractAnimationAction *action, QObject *parent = 0)
102 : QAbstractAnimation(parent), animAction(action), policy(KeepWhenStopped) {}
103 ~QActionAnimation() { if (policy == DeleteWhenStopped) { delete animAction; animAction = 0; } }
104 virtual int duration() const { return 0; }
105 void setAnimAction(QAbstractAnimationAction *action, DeletionPolicy p)
107 if (state() == Running)
109 if (policy == DeleteWhenStopped)
115 virtual void updateCurrentTime(int) {}
117 virtual void updateState(State newState, State /*oldState*/)
119 if (newState == Running) {
121 animAction->doAction();
122 if (state() == Stopped && policy == DeleteWhenStopped) {
131 QAbstractAnimationAction *animAction;
132 DeletionPolicy policy;
135 class QDeclarativeBulkValueUpdater
138 virtual ~QDeclarativeBulkValueUpdater() {}
139 virtual void setValue(qreal value) = 0;
142 //animates QDeclarativeBulkValueUpdater (assumes start and end values will be reals or compatible)
143 class Q_AUTOTEST_EXPORT QDeclarativeBulkValueAnimator : public QVariantAnimation
147 QDeclarativeBulkValueAnimator(QObject *parent = 0) : QVariantAnimation(parent), animValue(0), fromSourced(0), policy(KeepWhenStopped) {}
148 ~QDeclarativeBulkValueAnimator() { if (policy == DeleteWhenStopped) { delete animValue; animValue = 0; } }
149 void setAnimValue(QDeclarativeBulkValueUpdater *value, DeletionPolicy p)
151 if (state() == Running)
153 if (policy == DeleteWhenStopped)
158 QDeclarativeBulkValueUpdater *getAnimValue() const
162 void setFromSourcedValue(bool *value)
167 virtual void updateCurrentValue(const QVariant &value)
169 if (state() == QAbstractAnimation::Stopped)
173 animValue->setValue(value.toReal());
175 virtual void updateState(State newState, State oldState)
177 QVariantAnimation::updateState(newState, oldState);
178 if (newState == Running) {
179 //check for new from every loop
181 *fromSourced = false;
186 QDeclarativeBulkValueUpdater *animValue;
188 DeletionPolicy policy;
191 //an animation that just gives a tick
192 template<class T, void (T::*method)(int)>
193 class QTickAnimationProxy : public QAbstractAnimation
195 //Q_OBJECT //doesn't work with templating
197 QTickAnimationProxy(T *p, QObject *parent = 0) : QAbstractAnimation(parent), m_p(p) {}
198 virtual int duration() const { return -1; }
200 virtual void updateCurrentTime(int msec) { (m_p->*method)(msec); }
206 class QDeclarativeAbstractAnimationPrivate : public QObjectPrivate
208 Q_DECLARE_PUBLIC(QDeclarativeAbstractAnimation)
210 QDeclarativeAbstractAnimationPrivate()
211 : running(false), paused(false), alwaysRunToEnd(false),
212 connectedTimeLine(false), componentComplete(true),
213 avoidPropertyValueSourceStart(false), disableUserControl(false),
214 registered(false), loopCount(1), group(0) {}
218 bool alwaysRunToEnd:1;
219 bool connectedTimeLine:1;
220 bool componentComplete:1;
221 bool avoidPropertyValueSourceStart:1;
222 bool disableUserControl:1;
229 QDeclarativeProperty defaultProperty;
231 QDeclarativeAnimationGroup *group;
233 static QDeclarativeProperty createProperty(QObject *obj, const QString &str, QObject *infoObj);
236 class QDeclarativePauseAnimationPrivate : public QDeclarativeAbstractAnimationPrivate
238 Q_DECLARE_PUBLIC(QDeclarativePauseAnimation)
240 QDeclarativePauseAnimationPrivate()
241 : QDeclarativeAbstractAnimationPrivate(), pa(0) {}
248 class QDeclarativeScriptActionPrivate : public QDeclarativeAbstractAnimationPrivate
250 Q_DECLARE_PUBLIC(QDeclarativeScriptAction)
252 QDeclarativeScriptActionPrivate();
256 QDeclarativeScriptString script;
258 QDeclarativeScriptString runScriptScript;
259 bool hasRunScriptScript;
264 QAnimationActionProxy<QDeclarativeScriptActionPrivate,
265 &QDeclarativeScriptActionPrivate::execute> proxy;
266 QActionAnimation *rsa;
269 class QDeclarativePropertyActionPrivate : public QDeclarativeAbstractAnimationPrivate
271 Q_DECLARE_PUBLIC(QDeclarativePropertyAction)
273 QDeclarativePropertyActionPrivate()
274 : QDeclarativeAbstractAnimationPrivate(), target(0), spa(0) {}
279 QString propertyName;
281 QList<QObject *> targets;
282 QList<QObject *> exclude;
284 QDeclarativeNullableValue<QVariant> value;
286 QActionAnimation *spa;
289 class QDeclarativeAnimationGroupPrivate : public QDeclarativeAbstractAnimationPrivate
291 Q_DECLARE_PUBLIC(QDeclarativeAnimationGroup)
293 QDeclarativeAnimationGroupPrivate()
294 : QDeclarativeAbstractAnimationPrivate(), ag(0) {}
296 static void append_animation(QDeclarativeListProperty<QDeclarativeAbstractAnimation> *list, QDeclarativeAbstractAnimation *role);
297 static void clear_animation(QDeclarativeListProperty<QDeclarativeAbstractAnimation> *list);
298 QList<QDeclarativeAbstractAnimation *> animations;
302 class QDeclarativePropertyAnimationPrivate : public QDeclarativeAbstractAnimationPrivate
304 Q_DECLARE_PUBLIC(QDeclarativePropertyAnimation)
306 QDeclarativePropertyAnimationPrivate()
307 : QDeclarativeAbstractAnimationPrivate(), target(0), fromSourced(false), fromIsDefined(false), toIsDefined(false),
308 rangeIsSet(false), defaultToInterpolatorType(0), interpolatorType(0), interpolator(0), va(0), actions(0) {}
316 QString propertyName;
318 QList<QObject *> targets;
319 QList<QObject *> exclude;
320 QString defaultProperties;
323 bool fromIsDefined:1;
326 bool defaultToInterpolatorType:1;
327 int interpolatorType;
328 QVariantAnimation::Interpolator interpolator;
330 QDeclarativeBulkValueAnimator *va;
332 // for animations that don't use the QDeclarativeBulkValueAnimator
333 QDeclarativeStateActions *actions;
335 static QVariant interpolateVariant(const QVariant &from, const QVariant &to, qreal progress);
336 static void convertVariant(QVariant &variant, int type);
339 class QDeclarativeRotationAnimationPrivate : public QDeclarativePropertyAnimationPrivate
341 Q_DECLARE_PUBLIC(QDeclarativeRotationAnimation)
343 QDeclarativeRotationAnimationPrivate() : direction(QDeclarativeRotationAnimation::Numerical) {}
345 QDeclarativeRotationAnimation::RotationDirection direction;
348 class Q_AUTOTEST_EXPORT QDeclarativeAnimationPropertyUpdater : public QDeclarativeBulkValueUpdater
351 QDeclarativeStateActions actions;
352 int interpolatorType; //for Number/ColorAnimation
353 int prevInterpolatorType; //for generic
354 QVariantAnimation::Interpolator interpolator;
359 QDeclarativeAnimationPropertyUpdater() : prevInterpolatorType(0), wasDeleted(0) {}
360 ~QDeclarativeAnimationPropertyUpdater() { if (wasDeleted) *wasDeleted = true; }
361 void setValue(qreal v);
366 #endif // QDECLARATIVEANIMATION_P_H