Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / src / qtquick1 / 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 "QtQuick1/private/qdeclarativeanimation_p.h"
57
58 #include "QtDeclarative/private/qdeclarativenullablevalue_p_p.h"
59 #include "QtQuick1/private/qdeclarativetimeline_p_p.h"
60
61 #include <QtDeclarative/qdeclarative.h>
62 #include <QtQuick1/qdeclarativeitem.h>
63 #include <QtDeclarative/qdeclarativecontext.h>
64
65 #include <QtCore/QPauseAnimation>
66 #include <QtCore/QVariantAnimation>
67 #include <QtCore/QAnimationGroup>
68 #include <QDebug>
69
70 #include <private/qobject_p.h>
71 #include <private/qvariantanimation_p.h>
72
73 QT_BEGIN_NAMESPACE
74
75 //interface for classes that provide animation actions for QActionAnimation_1
76 class QAbstractAnimationAction
77 {
78 public:
79     virtual ~QAbstractAnimationAction() {}
80     virtual void doAction() = 0;
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 QDeclarative1AbstractAnimationAction)
86 template<class T, void (T::*method)()>
87 class QAnimationActionProxy_1 : public QAbstractAnimationAction
88 {
89 public:
90     QAnimationActionProxy_1(T *p) : m_p(p) {}
91     virtual void doAction() { (m_p->*method)(); }
92
93 private:
94     T *m_p;
95 };
96
97 //performs an action of type QAbstractAnimationAction
98 class Q_AUTOTEST_EXPORT QActionAnimation_1 : public QAbstractAnimation
99 {
100     Q_OBJECT
101 public:
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)
108     {
109         if (state() == Running)
110             stop();
111         if (policy == DeleteWhenStopped)
112             delete animAction;
113         animAction = action;
114         policy = p;
115     }
116 protected:
117     virtual void updateCurrentTime(int) {}
118
119     virtual void updateState(State newState, State /*oldState*/)
120     {
121         if (newState == Running) {
122             if (animAction) {
123                 animAction->doAction();
124                 if (state() == Stopped && policy == DeleteWhenStopped) {
125                     delete animAction;
126                     animAction = 0;
127                 }
128             }
129         }
130     }
131
132 private:
133     QAbstractAnimationAction *animAction;
134     DeletionPolicy policy;
135 };
136
137 class QDeclarative1BulkValueUpdater
138 {
139 public:
140     virtual ~QDeclarative1BulkValueUpdater() {}
141     virtual void setValue(qreal value) = 0;
142 };
143
144 //animates QDeclarative1BulkValueUpdater (assumes start and end values will be reals or compatible)
145 class Q_AUTOTEST_EXPORT QDeclarative1BulkValueAnimator : public QVariantAnimation
146 {
147     Q_OBJECT
148 public:
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)
152     {
153         if (state() == Running)
154             stop();
155         if (policy == DeleteWhenStopped)
156             delete animValue;
157         animValue = value;
158         policy = p;
159     }
160     void setFromSourcedValue(bool *value)
161     {
162         fromSourced = value;
163     }
164 protected:
165     virtual void updateCurrentValue(const QVariant &value)
166     {
167         if (state() == QAbstractAnimation::Stopped)
168             return;
169
170         if (animValue)
171             animValue->setValue(value.toReal());
172     }
173     virtual void updateState(State newState, State oldState)
174     {   
175         QVariantAnimation::updateState(newState, oldState);
176         if (newState == Running) {
177             //check for new from every loop
178             if (fromSourced)
179                 *fromSourced = false;
180         }
181     }
182
183 private:
184     QDeclarative1BulkValueUpdater *animValue;
185     bool *fromSourced;
186     DeletionPolicy policy;
187 };
188
189 //an animation that just gives a tick
190 template<class T, void (T::*method)(int)>
191 class QTickAnimationProxy_1 : public QAbstractAnimation
192 {
193     //Q_OBJECT //doesn't work with templating
194 public:
195     QTickAnimationProxy_1(T *p, QObject *parent = 0) : QAbstractAnimation(parent), m_p(p) {}
196     virtual int duration() const { return -1; }
197 protected:
198     virtual void updateCurrentTime(int msec) { (m_p->*method)(msec); }
199
200 private:
201     T *m_p;
202 };
203
204 class QDeclarative1AbstractAnimationPrivate : public QObjectPrivate
205 {
206     Q_DECLARE_PUBLIC(QDeclarative1AbstractAnimation)
207 public:
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) {}
213
214     bool running:1;
215     bool paused:1;
216     bool alwaysRunToEnd:1;
217     bool connectedTimeLine:1;
218     bool componentComplete:1;
219     bool avoidPropertyValueSourceStart:1;
220     bool disableUserControl:1;
221     bool registered:1;
222
223     int loopCount;
224
225     void commence();
226
227     QDeclarativeProperty defaultProperty;
228
229     QDeclarative1AnimationGroup *group;
230
231     static QDeclarativeProperty createProperty(QObject *obj, const QString &str, QObject *infoObj);
232 };
233
234 class QDeclarative1PauseAnimationPrivate : public QDeclarative1AbstractAnimationPrivate
235 {
236     Q_DECLARE_PUBLIC(QDeclarative1PauseAnimation)
237 public:
238     QDeclarative1PauseAnimationPrivate()
239     : QDeclarative1AbstractAnimationPrivate(), pa(0) {}
240
241     void init();
242
243     QPauseAnimation *pa;
244 };
245
246 class QDeclarative1ScriptActionPrivate : public QDeclarative1AbstractAnimationPrivate
247 {
248     Q_DECLARE_PUBLIC(QDeclarative1ScriptAction)
249 public:
250     QDeclarative1ScriptActionPrivate();
251
252     void init();
253
254     QDeclarativeScriptString script;
255     QString name;
256     QDeclarativeScriptString runScriptScript;
257     bool hasRunScriptScript;
258     bool reversing;
259
260     void execute();
261
262     QAnimationActionProxy_1<QDeclarative1ScriptActionPrivate,
263                   &QDeclarative1ScriptActionPrivate::execute> proxy;
264     QActionAnimation_1 *rsa;
265 };
266
267 class QDeclarative1PropertyActionPrivate : public QDeclarative1AbstractAnimationPrivate
268 {
269     Q_DECLARE_PUBLIC(QDeclarative1PropertyAction)
270 public:
271     QDeclarative1PropertyActionPrivate()
272     : QDeclarative1AbstractAnimationPrivate(), target(0), spa(0) {}
273
274     void init();
275
276     QObject *target;
277     QString propertyName;
278     QString properties;
279     QList<QObject *> targets;
280     QList<QObject *> exclude;
281
282     QDeclarativeNullableValue<QVariant> value;
283
284     QActionAnimation_1 *spa;
285 };
286
287 class QDeclarative1AnimationGroupPrivate : public QDeclarative1AbstractAnimationPrivate
288 {
289     Q_DECLARE_PUBLIC(QDeclarative1AnimationGroup)
290 public:
291     QDeclarative1AnimationGroupPrivate()
292     : QDeclarative1AbstractAnimationPrivate(), ag(0) {}
293
294     static void append_animation(QDeclarativeListProperty<QDeclarative1AbstractAnimation> *list, QDeclarative1AbstractAnimation *role);
295     static void clear_animation(QDeclarativeListProperty<QDeclarative1AbstractAnimation> *list);
296     QList<QDeclarative1AbstractAnimation *> animations;
297     QAnimationGroup *ag;
298 };
299
300 class QDeclarative1PropertyAnimationPrivate : public QDeclarative1AbstractAnimationPrivate
301 {
302     Q_DECLARE_PUBLIC(QDeclarative1PropertyAnimation)
303 public:
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) {}
307
308     void init();
309
310     QVariant from;
311     QVariant to;
312
313     QObject *target;
314     QString propertyName;
315     QString properties;
316     QList<QObject *> targets;
317     QList<QObject *> exclude;
318     QString defaultProperties;
319
320     bool fromSourced;
321     bool fromIsDefined:1;
322     bool toIsDefined:1;
323     bool rangeIsSet:1;
324     bool defaultToInterpolatorType:1;
325     int interpolatorType;
326     QVariantAnimation::Interpolator interpolator;
327
328     QDeclarative1BulkValueAnimator *va;
329
330     // for animations that don't use the QDeclarative1BulkValueAnimator
331     QDeclarative1StateActions *actions;
332
333     static QVariant interpolateVariant(const QVariant &from, const QVariant &to, qreal progress);
334     static void convertVariant(QVariant &variant, int type);
335 };
336
337 class QDeclarative1RotationAnimationPrivate : public QDeclarative1PropertyAnimationPrivate
338 {
339     Q_DECLARE_PUBLIC(QDeclarative1RotationAnimation)
340 public:
341     QDeclarative1RotationAnimationPrivate() : direction(QDeclarative1RotationAnimation::Numerical) {}
342
343     QDeclarative1RotationAnimation::RotationDirection direction;
344 };
345
346 class QDeclarative1ParentAnimationPrivate : public QDeclarative1AnimationGroupPrivate
347 {
348     Q_DECLARE_PUBLIC(QDeclarative1ParentAnimation)
349 public:
350     QDeclarative1ParentAnimationPrivate()
351     : QDeclarative1AnimationGroupPrivate(), target(0), newParent(0),
352        via(0), topLevelGroup(0), startAction(0), endAction(0) {}
353
354     QDeclarativeItem *target;
355     QDeclarativeItem *newParent;
356     QDeclarativeItem *via;
357
358     QSequentialAnimationGroup *topLevelGroup;
359     QActionAnimation_1 *startAction;
360     QActionAnimation_1 *endAction;
361
362     QPointF computeTransformOrigin(QDeclarativeItem::TransformOrigin origin, qreal width, qreal height) const;
363 };
364
365 class QDeclarative1AnchorAnimationPrivate : public QDeclarative1AbstractAnimationPrivate
366 {
367     Q_DECLARE_PUBLIC(QDeclarative1AnchorAnimation)
368 public:
369     QDeclarative1AnchorAnimationPrivate() : rangeIsSet(false), va(0),
370         interpolator(QVariantAnimationPrivate::getInterpolator(QMetaType::QReal)) {}
371
372     bool rangeIsSet;
373     QDeclarative1BulkValueAnimator *va;
374     QVariantAnimation::Interpolator interpolator;
375     QList<QDeclarativeItem*> targets;
376 };
377
378 class Q_AUTOTEST_EXPORT QDeclarative1AnimationPropertyUpdater : public QDeclarative1BulkValueUpdater
379 {
380 public:
381     QDeclarative1StateActions actions;
382     int interpolatorType;       //for Number/ColorAnimation
383     int prevInterpolatorType;   //for generic
384     QVariantAnimation::Interpolator interpolator;
385     bool reverse;
386     bool fromSourced;
387     bool fromDefined;
388     bool *wasDeleted;
389     QDeclarative1AnimationPropertyUpdater() : prevInterpolatorType(0), wasDeleted(0) {}
390     ~QDeclarative1AnimationPropertyUpdater() { if (wasDeleted) *wasDeleted = true; }
391     void setValue(qreal v);
392 };
393
394 QT_END_NAMESPACE
395
396 #endif // QDECLARATIVEANIMATION_P_H