|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 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 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
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 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
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 "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 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 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 QEasingCurve easing; |
|
315 |
|
316 QObject *target; |
|
317 QString propertyName; |
|
318 QString properties; |
|
319 QList<QObject *> targets; |
|
320 QList<QObject *> exclude; |
|
321 QString defaultProperties; |
|
322 |
|
323 bool fromSourced; |
|
324 bool fromIsDefined:1; |
|
325 bool toIsDefined:1; |
|
326 bool rangeIsSet:1; |
|
327 bool defaultToInterpolatorType:1; |
|
328 int interpolatorType; |
|
329 QVariantAnimation::Interpolator interpolator; |
|
330 |
|
331 QDeclarativeBulkValueAnimator *va; |
|
332 |
|
333 // for animations that dont use the QDeclarativeBulkValueAnimator |
|
334 QDeclarativeStateActions *actions; |
|
335 |
|
336 static QVariant interpolateVariant(const QVariant &from, const QVariant &to, qreal progress); |
|
337 static void convertVariant(QVariant &variant, int type); |
|
338 }; |
|
339 |
|
340 class QDeclarativeRotationAnimationPrivate : public QDeclarativePropertyAnimationPrivate |
|
341 { |
|
342 Q_DECLARE_PUBLIC(QDeclarativeRotationAnimation) |
|
343 public: |
|
344 QDeclarativeRotationAnimationPrivate() : direction(QDeclarativeRotationAnimation::Numerical) {} |
|
345 |
|
346 QDeclarativeRotationAnimation::RotationDirection direction; |
|
347 }; |
|
348 |
|
349 class QDeclarativeParentAnimationPrivate : public QDeclarativeAnimationGroupPrivate |
|
350 { |
|
351 Q_DECLARE_PUBLIC(QDeclarativeParentAnimation) |
|
352 public: |
|
353 QDeclarativeParentAnimationPrivate() |
|
354 : QDeclarativeAnimationGroupPrivate(), target(0), newParent(0), |
|
355 via(0), topLevelGroup(0), startAction(0), endAction(0) {} |
|
356 |
|
357 QDeclarativeItem *target; |
|
358 QDeclarativeItem *newParent; |
|
359 QDeclarativeItem *via; |
|
360 |
|
361 QSequentialAnimationGroup *topLevelGroup; |
|
362 QActionAnimation *startAction; |
|
363 QActionAnimation *endAction; |
|
364 |
|
365 QPointF computeTransformOrigin(QDeclarativeItem::TransformOrigin origin, qreal width, qreal height) const; |
|
366 }; |
|
367 |
|
368 class QDeclarativeAnchorAnimationPrivate : public QDeclarativeAbstractAnimationPrivate |
|
369 { |
|
370 Q_DECLARE_PUBLIC(QDeclarativeAnchorAnimation) |
|
371 public: |
|
372 QDeclarativeAnchorAnimationPrivate() : rangeIsSet(false), va(0), |
|
373 interpolator(QVariantAnimationPrivate::getInterpolator(QMetaType::QReal)) {} |
|
374 |
|
375 bool rangeIsSet; |
|
376 QDeclarativeBulkValueAnimator *va; |
|
377 QVariantAnimation::Interpolator interpolator; |
|
378 QList<QDeclarativeItem*> targets; |
|
379 }; |
|
380 |
|
381 QT_END_NAMESPACE |
|
382 |
|
383 #endif // QDECLARATIVEANIMATION_P_H |