Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / src / quick / util / qdeclarativeanimation_p_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
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 "qdeclarativeanimation_p.h"
57
58 #include <private/qdeclarativenullablevalue_p_p.h>
59
60 #include <qdeclarative.h>
61 #include <qdeclarativecontext.h>
62
63 #include <QtCore/QPauseAnimation>
64 #include <QtCore/QVariantAnimation>
65 #include <QtCore/QAnimationGroup>
66 #include <QDebug>
67
68 #include <private/qobject_p.h>
69 #include <private/qvariantanimation_p.h>
70
71 QT_BEGIN_NAMESPACE
72
73 //interface for classes that provide animation actions for QActionAnimation
74 class QAbstractAnimationAction
75 {
76 public:
77     virtual ~QAbstractAnimationAction() {}
78     virtual void doAction() = 0;
79 };
80
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
86 {
87 public:
88     QAnimationActionProxy(T *p) : m_p(p) {}
89     virtual void doAction() { (m_p->*method)(); }
90
91 private:
92     T *m_p;
93 };
94
95 //performs an action of type QAbstractAnimationAction
96 class Q_AUTOTEST_EXPORT QActionAnimation : public QAbstractAnimation
97 {
98     Q_OBJECT
99 public:
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)
106     {
107         if (state() == Running)
108             stop();
109         if (policy == DeleteWhenStopped)
110             delete animAction;
111         animAction = action;
112         policy = p;
113     }
114 protected:
115     virtual void updateCurrentTime(int) {}
116
117     virtual void updateState(State newState, State /*oldState*/)
118     {
119         if (newState == Running) {
120             if (animAction) {
121                 animAction->doAction();
122                 if (state() == Stopped && policy == DeleteWhenStopped) {
123                     delete animAction;
124                     animAction = 0;
125                 }
126             }
127         }
128     }
129
130 private:
131     QAbstractAnimationAction *animAction;
132     DeletionPolicy policy;
133 };
134
135 class QDeclarativeBulkValueUpdater
136 {
137 public:
138     virtual ~QDeclarativeBulkValueUpdater() {}
139     virtual void setValue(qreal value) = 0;
140 };
141
142 //animates QDeclarativeBulkValueUpdater (assumes start and end values will be reals or compatible)
143 class Q_AUTOTEST_EXPORT QDeclarativeBulkValueAnimator : public QVariantAnimation
144 {
145     Q_OBJECT
146 public:
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)
150     {
151         if (state() == Running)
152             stop();
153         if (policy == DeleteWhenStopped)
154             delete animValue;
155         animValue = value;
156         policy = p;
157     }
158     QDeclarativeBulkValueUpdater *getAnimValue() const
159     {
160         return animValue;
161     }
162     void setFromSourcedValue(bool *value)
163     {
164         fromSourced = value;
165     }
166 protected:
167     virtual void updateCurrentValue(const QVariant &value)
168     {
169         if (state() == QAbstractAnimation::Stopped)
170             return;
171
172         if (animValue)
173             animValue->setValue(value.toReal());
174     }
175     virtual void updateState(State newState, State oldState)
176     {   
177         QVariantAnimation::updateState(newState, oldState);
178         if (newState == Running) {
179             //check for new from every loop
180             if (fromSourced)
181                 *fromSourced = false;
182         }
183     }
184
185 private:
186     QDeclarativeBulkValueUpdater *animValue;
187     bool *fromSourced;
188     DeletionPolicy policy;
189 };
190
191 //an animation that just gives a tick
192 template<class T, void (T::*method)(int)>
193 class QTickAnimationProxy : public QAbstractAnimation
194 {
195     //Q_OBJECT //doesn't work with templating
196 public:
197     QTickAnimationProxy(T *p, QObject *parent = 0) : QAbstractAnimation(parent), m_p(p) {}
198     virtual int duration() const { return -1; }
199 protected:
200     virtual void updateCurrentTime(int msec) { (m_p->*method)(msec); }
201
202 private:
203     T *m_p;
204 };
205
206 class QDeclarativeAbstractAnimationPrivate : public QObjectPrivate
207 {
208     Q_DECLARE_PUBLIC(QDeclarativeAbstractAnimation)
209 public:
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) {}
215
216     bool running:1;
217     bool paused:1;
218     bool alwaysRunToEnd:1;
219     bool connectedTimeLine:1;
220     bool componentComplete:1;
221     bool avoidPropertyValueSourceStart:1;
222     bool disableUserControl:1;
223     bool registered:1;
224
225     int loopCount;
226
227     void commence();
228
229     QDeclarativeProperty defaultProperty;
230
231     QDeclarativeAnimationGroup *group;
232
233     static QDeclarativeProperty createProperty(QObject *obj, const QString &str, QObject *infoObj);
234 };
235
236 class QDeclarativePauseAnimationPrivate : public QDeclarativeAbstractAnimationPrivate
237 {
238     Q_DECLARE_PUBLIC(QDeclarativePauseAnimation)
239 public:
240     QDeclarativePauseAnimationPrivate()
241     : QDeclarativeAbstractAnimationPrivate(), pa(0) {}
242
243     void init();
244
245     QPauseAnimation *pa;
246 };
247
248 class QDeclarativeScriptActionPrivate : public QDeclarativeAbstractAnimationPrivate
249 {
250     Q_DECLARE_PUBLIC(QDeclarativeScriptAction)
251 public:
252     QDeclarativeScriptActionPrivate();
253
254     void init();
255
256     QDeclarativeScriptString script;
257     QString name;
258     QDeclarativeScriptString runScriptScript;
259     bool hasRunScriptScript;
260     bool reversing;
261
262     void execute();
263
264     QAnimationActionProxy<QDeclarativeScriptActionPrivate,
265                   &QDeclarativeScriptActionPrivate::execute> proxy;
266     QActionAnimation *rsa;
267 };
268
269 class QDeclarativePropertyActionPrivate : public QDeclarativeAbstractAnimationPrivate
270 {
271     Q_DECLARE_PUBLIC(QDeclarativePropertyAction)
272 public:
273     QDeclarativePropertyActionPrivate()
274     : QDeclarativeAbstractAnimationPrivate(), target(0), spa(0) {}
275
276     void init();
277
278     QObject *target;
279     QString propertyName;
280     QString properties;
281     QList<QObject *> targets;
282     QList<QObject *> exclude;
283
284     QDeclarativeNullableValue<QVariant> value;
285
286     QActionAnimation *spa;
287 };
288
289 class QDeclarativeAnimationGroupPrivate : public QDeclarativeAbstractAnimationPrivate
290 {
291     Q_DECLARE_PUBLIC(QDeclarativeAnimationGroup)
292 public:
293     QDeclarativeAnimationGroupPrivate()
294     : QDeclarativeAbstractAnimationPrivate(), ag(0) {}
295
296     static void append_animation(QDeclarativeListProperty<QDeclarativeAbstractAnimation> *list, QDeclarativeAbstractAnimation *role);
297     static void clear_animation(QDeclarativeListProperty<QDeclarativeAbstractAnimation> *list);
298     QList<QDeclarativeAbstractAnimation *> animations;
299     QAnimationGroup *ag;
300 };
301
302 class QDeclarativePropertyAnimationPrivate : public QDeclarativeAbstractAnimationPrivate
303 {
304     Q_DECLARE_PUBLIC(QDeclarativePropertyAnimation)
305 public:
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) {}
309
310     void init();
311
312     QVariant from;
313     QVariant to;
314
315     QObject *target;
316     QString propertyName;
317     QString properties;
318     QList<QObject *> targets;
319     QList<QObject *> exclude;
320     QString defaultProperties;
321
322     bool fromSourced;
323     bool fromIsDefined:1;
324     bool toIsDefined:1;
325     bool rangeIsSet:1;
326     bool defaultToInterpolatorType:1;
327     int interpolatorType;
328     QVariantAnimation::Interpolator interpolator;
329
330     QDeclarativeBulkValueAnimator *va;
331
332     // for animations that don't use the QDeclarativeBulkValueAnimator
333     QDeclarativeStateActions *actions;
334
335     static QVariant interpolateVariant(const QVariant &from, const QVariant &to, qreal progress);
336     static void convertVariant(QVariant &variant, int type);
337 };
338
339 class QDeclarativeRotationAnimationPrivate : public QDeclarativePropertyAnimationPrivate
340 {
341     Q_DECLARE_PUBLIC(QDeclarativeRotationAnimation)
342 public:
343     QDeclarativeRotationAnimationPrivate() : direction(QDeclarativeRotationAnimation::Numerical) {}
344
345     QDeclarativeRotationAnimation::RotationDirection direction;
346 };
347
348 class Q_AUTOTEST_EXPORT QDeclarativeAnimationPropertyUpdater : public QDeclarativeBulkValueUpdater
349 {
350 public:
351     QDeclarativeStateActions actions;
352     int interpolatorType;       //for Number/ColorAnimation
353     int prevInterpolatorType;   //for generic
354     QVariantAnimation::Interpolator interpolator;
355     bool reverse;
356     bool fromSourced;
357     bool fromDefined;
358     bool *wasDeleted;
359     QDeclarativeAnimationPropertyUpdater() : prevInterpolatorType(0), wasDeleted(0) {}
360     ~QDeclarativeAnimationPropertyUpdater() { if (wasDeleted) *wasDeleted = true; }
361     void setValue(qreal v);
362 };
363
364 QT_END_NAMESPACE
365
366 #endif // QDECLARATIVEANIMATION_P_H