source: trunk/doc/src/frameworks-technologies/animation.qdoc

Last change on this file was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

  • Property svn:eol-style set to native
File size: 13.8 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the documentation of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:FDL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in a
14** written agreement between you and Nokia.
15**
16** GNU Free Documentation License
17** Alternatively, this file may be used under the terms of the GNU Free
18** Documentation License version 1.3 as published by the Free Software
19** Foundation and appearing in the file included in the packaging of this
20** file.
21**
22** If you have questions regarding the use of this file, please contact
23** Nokia at [email protected].
24** $QT_END_LICENSE$
25**
26****************************************************************************/
27
28/*!
29 \group animation
30 \title Animation Framework
31*/
32
33/*!
34 \page animation-overview.html
35 \title The Animation Framework
36 \ingroup qt-gui-concepts
37
38 \brief An overview of the Animation Framework
39
40 \ingroup frameworks-technologies
41
42 \keyword Animation
43
44 The animation framework is part of the Kinetic project, and aims
45 to provide an easy way for creating animated and smooth GUI's. By
46 animating Qt properties, the framework provides great freedom for
47 animating widgets and other \l{QObject}s. The framework can also
48 be used with the Graphics View framework.
49
50 In this overview, we explain the basics of its architecture. We
51 also show examples of the most common techniques that the
52 framework allows for animating QObjects and graphics items.
53
54 \tableofcontents
55
56 \section1 The Animation Architecture
57
58 We will in this section take a high-level look at the animation
59 framework's architecture and how it is used to animate Qt
60 properties. The following diagram shows the most important classes
61 in the animation framework.
62
63 \image animations-architecture.png
64
65 The animation framework foundation consists of the base class
66 QAbstractAnimation, and its two subclasses QVariantAnimation and
67 QAnimationGroup. QAbstractAnimation is the ancestor of all
68 animations. It represents basic properties that are common for all
69 animations in the framework; notably, the ability to start, stop,
70 and pause an animation. It is also receives the time change
71 notifications.
72
73 The animation framework further provides the QPropertyAnimation
74 class, which inherits QVariantAnimation and performs animation of
75 a Qt property, which is part of Qt's \l{Meta-Object
76 System}{meta-object system}. The class performs an interpolation
77 over the property using an easing curve. So when you want to
78 animate a value, you can declare it as a property and make your
79 class a QObject. Note that this gives us great freedom in
80 animating already existing widgets and other \l{QObject}s.
81
82 Complex animations can be constructed by building a tree structure
83 of \l{QAbstractAnimation}s. The tree is built by using
84 \l{QAnimationGroup}s, which function as containers for other
85 animations. Note also that the groups are subclasses of
86 QAbstractAnimation, so groups can themselves contain other groups.
87
88 The animation framework can be used on its own, but is also
89 designed to be part of the state machine framework (See the
90 \l{The State Machine Framework}{state machine framework} for an
91 introduction to the Qt state machine). The state machine provides
92 a special state that can play an animation. A QState can also set
93 properties when the state is entered or exited, and this special
94 animation state will interpolate between these values when given a
95 QPropertyAnimation. We will look more closely at this later.
96
97 Behind the scenes, the animations are controlled by a global
98 timer, which sends \l{QAbstractAnimation::updateCurrentTime()}{updates} to
99 all animations that are playing.
100
101 For detailed descriptions of the classes' function and roles in
102 the framework, please look up their class descriptions.
103
104 \section1 Classes in the Animation Framework
105
106 These classes provide a framework for creating both simple and complex
107 animations.
108
109 \annotatedlist animation
110
111 \section1 Animating Qt Properties
112
113 As mentioned in the previous section, the QPropertyAnimation class
114 can interpolate over Qt properties. It is this class that should
115 be used for animation of values; in fact, its superclass,
116 QVariantAnimation, is an abstract class, and cannot be used
117 directly.
118
119 A major reason we chose to animate Qt properties is that it
120 presents us with freedom to animate already existing classes in
121 the Qt API. Notably, the QWidget class (which we can also embed in
122 a QGraphicsView) has properties for its bounds, colors, etc.
123 Let's look at a small example:
124
125 \code
126 QPushButton button("Animated Button");
127 button.show();
128
129 QPropertyAnimation animation(&button, "geometry");
130 animation.setDuration(10000);
131 animation.setStartValue(QRect(0, 0, 100, 30));
132 animation.setEndValue(QRect(250, 250, 100, 30));
133
134 animation.start();
135 \endcode
136
137 This code will move \c button from the top left corner of the
138 screen to the position (250, 250) in 10 seconds (10000 milliseconds).
139
140 The example above will do a linear interpolation between the
141 start and end value. It is also possible to set values
142 situated between the start and end value. The interpolation
143 will then go by these points.
144
145 \code
146 QPushButton button("Animated Button");
147 button.show();
148
149 QPropertyAnimation animation(&button, "geometry");
150 animation.setDuration(10000);
151
152 animation.setKeyValueAt(0, QRect(0, 0, 100, 30));
153 animation.setKeyValueAt(0.8, QRect(250, 250, 100, 30));
154 animation.setKeyValueAt(1, QRect(0, 0, 100, 30));
155
156 animation.start();
157 \endcode
158
159 In this example, the animation will take the button to (250, 250)
160 in 8 seconds, and then move it back to its original position in
161 the remaining 2 seconds. The movement will be linearly
162 interpolated between these points.
163
164 You also have the possibility to animate values of a QObject
165 that is not declared as a Qt property. The only requirement is
166 that this value has a setter. You can then subclass the class
167 containing the value and declare a property that uses this setter.
168 Note that each Qt property requires a getter, so you will need to
169 provide a getter yourself if this is not defined.
170
171 \code
172 class MyGraphicsRectItem : public QObject, public QGraphicsRectItem
173 {
174 Q_OBJECT
175 Q_PROPERTY(QRectF geometry READ geometry WRITE setGeometry)
176 };
177 \endcode
178
179 In the above code example, we subclass QGraphicsRectItem and
180 define a geometry property. We can now animate the widgets
181 geometry even if QGraphicsRectItem does not provide the geometry
182 property.
183
184 For a general introduction to the Qt property system, see its
185 \l{Qt's Property System}{overview}.
186
187 \section1 Animations and the Graphics View Framework
188
189 When you want to animate \l{QGraphicsItem}s, you also use
190 QPropertyAnimation. However, QGraphicsItem does not inherit QObject.
191 A good solution is to subclass the graphics item you wish to animate.
192 This class will then also inherit QObject.
193 This way, QPropertyAnimation can be used for \l{QGraphicsItem}s.
194 The example below shows how this is done. Another possibility is
195 to inherit QGraphicsWidget, which already is a QObject.
196
197 \code
198 class Pixmap : public QObject, public QGraphicsPixmapItem
199 {
200 Q_OBJECT
201 Q_PROPERTY(QPointF pos READ pos WRITE setPos)
202 ...
203 \endcode
204
205 As described in the previous section, we need to define
206 properties that we wish to animate.
207
208 Note that QObject must be the first class inherited as the
209 meta-object system demands this.
210
211 \section1 Easing Curves
212
213 As mentioned, QPropertyAnimation performs an interpolation between
214 the start and end property value. In addition to adding more key
215 values to the animation, you can also use an easing curve. Easing
216 curves describe a function that controls how the speed of the
217 interpolation between 0 and 1 should be, and are useful if you
218 want to control the speed of an animation without changing the
219 path of the interpolation.
220
221 \code
222 QPushButton button("Animated Button");
223 button.show();
224
225 QPropertyAnimation animation(&button, "geometry");
226 animation.setDuration(3000);
227 animation.setStartValue(QRect(0, 0, 100, 30));
228 animation.setEndValue(QRect(250, 250, 100, 30));
229
230 animation.setEasingCurve(QEasingCurve::OutBounce);
231
232 animation.start();