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 QtCore module of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
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
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
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 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | /*!
|
---|
43 | \class QAnimationGroup
|
---|
44 | \brief The QAnimationGroup class is an abstract base class for groups of animations.
|
---|
45 | \since 4.6
|
---|
46 | \ingroup animation
|
---|
47 |
|
---|
48 | An animation group is a container for animations (subclasses of
|
---|
49 | QAbstractAnimation). A group is usually responsible for managing
|
---|
50 | the \l{QAbstractAnimation::State}{state} of its animations, i.e.,
|
---|
51 | it decides when to start, stop, resume, and pause them. Currently,
|
---|
52 | Qt provides two such groups: QParallelAnimationGroup and
|
---|
53 | QSequentialAnimationGroup. Look up their class descriptions for
|
---|
54 | details.
|
---|
55 |
|
---|
56 | Since QAnimationGroup inherits from QAbstractAnimation, you can
|
---|
57 | combine groups, and easily construct complex animation graphs.
|
---|
58 | You can query QAbstractAnimation for the group it belongs to
|
---|
59 | (using the \l{QAbstractAnimation::}{group()} function).
|
---|
60 |
|
---|
61 | To start a top-level animation group, you simply use the
|
---|
62 | \l{QAbstractAnimation::}{start()} function from
|
---|
63 | QAbstractAnimation. By a top-level animation group, we think of a
|
---|
64 | group that itself is not contained within another group. Starting
|
---|
65 | sub groups directly is not supported, and may lead to unexpected
|
---|
66 | behavior.
|
---|
67 |
|
---|
68 | \omit OK, we'll put in a snippet on this here \endomit
|
---|
69 |
|
---|
70 | QAnimationGroup provides methods for adding and retrieving
|
---|
71 | animations. Besides that, you can remove animations by calling
|
---|
72 | remove(), and clear the animation group by calling
|
---|
73 | clear(). You may keep track of changes in the group's
|
---|
74 | animations by listening to QEvent::ChildAdded and
|
---|
75 | QEvent::ChildRemoved events.
|
---|
76 |
|
---|
77 | \omit OK, let's find a snippet here as well. \endomit
|
---|
78 |
|
---|
79 | QAnimationGroup takes ownership of the animations it manages, and
|
---|
80 | ensures that they are deleted when the animation group is deleted.
|
---|
81 |
|
---|
82 | \sa QAbstractAnimation, QVariantAnimation, {The Animation Framework}
|
---|
83 | */
|
---|
84 |
|
---|
85 | #include "qanimationgroup.h"
|
---|
86 | #include <QtCore/qdebug.h>
|
---|
87 | #include <QtCore/qcoreevent.h>
|
---|
88 | #include "qanimationgroup_p.h"
|
---|
89 |
|
---|
90 | #ifndef QT_NO_ANIMATION
|
---|
91 |
|
---|
92 | QT_BEGIN_NAMESPACE
|
---|
93 |
|
---|
94 |
|
---|
95 | /*!
|
---|
96 | Constructs a QAnimationGroup.
|
---|
97 | \a parent is passed to QObject's constructor.
|
---|
98 | */
|
---|
99 | QAnimationGroup::QAnimationGroup(QObject *parent)
|
---|
100 | : QAbstractAnimation(*new QAnimationGroupPrivate, parent)
|
---|
101 | {
|
---|
102 | }
|
---|
103 |
|
---|
104 | /*!
|
---|
105 | \internal
|
---|
106 | */
|
---|
107 | QAnimationGroup::QAnimationGroup(QAnimationGroupPrivate &dd, QObject *parent)
|
---|
108 | : QAbstractAnimation(dd, parent)
|
---|
109 | {
|
---|
110 | }
|
---|
111 |
|
---|
112 | /*!
|
---|
113 | Destroys the animation group. It will also destroy all its animations.
|
---|
114 | */
|
---|
115 | QAnimationGroup::~QAnimationGroup()
|
---|
116 | {
|
---|
117 | }
|
---|
118 |
|
---|
119 | /*!
|
---|
120 | Returns a pointer to the animation at \a index in this group. This
|
---|
121 | function is useful when you need access to a particular animation. \a
|
---|
122 | index is between 0 and animationCount() - 1.
|
---|
123 |
|
---|
124 | \sa animationCount(), indexOfAnimation()
|
---|
125 | */
|
---|
126 | QAbstractAnimation *QAnimationGroup::animationAt(int index) const
|
---|
127 | {
|
---|
128 | Q_D(const QAnimationGroup);
|
---|
129 |
|
---|
130 | if (index < 0 || index >= d->animations.size()) {
|
---|
131 | qWarning("QAnimationGroup::animationAt: index is out of bounds");
|
---|
132 | return 0;
|
---|
133 | }
|
---|
134 |
|
---|
135 | return d->animations.at(index);
|
---|
136 | }
|
---|
137 |
|
---|
138 |
|
---|
139 | /*!
|
---|
140 | Returns the number of animations managed by this group.
|
---|
141 |
|
---|
142 | \sa indexOfAnimation(), addAnimation(), animationAt()
|
---|
143 | */
|
---|
144 | int QAnimationGroup::animationCount() const
|
---|
145 | {
|
---|
146 | Q_D(const QAnimationGroup);
|
---|
147 | return d->animations.size();
|
---|
148 | }
|
---|
149 |
|
---|
150 | /*!
|
---|
151 | Returns the index of \a animation. The returned index can be passed
|
---|
152 | to the other functions that take an index as an argument.
|
---|
153 |
|
---|
154 | \sa insertAnimation(), animationAt(), takeAnimation()
|
---|
155 | */
|
---|
156 | int QAnimationGroup::indexOfAnimation(QAbstractAnimation *animation) const
|
---|
157 | {
|
---|
158 | Q_D(const QAnimationGroup);
|
---|
159 | return d->animations.indexOf(animation);
|
---|
160 | }
|
---|
161 |
|
---|
162 | /*!
|
---|
163 | Adds \a animation to this group. This will call insertAnimation with
|
---|
164 | index equals to animationCount().
|
---|
165 |
|
---|
166 | \note The group takes ownership of the animation.
|
---|
167 |
|
---|
168 | \sa removeAnimation()
|
---|
169 | */
|
---|
170 | void QAnimationGroup::addAnimation(QAbstractAnimation *animation)
|
---|
171 | {
|
---|
172 | Q_D(QAnimationGroup);
|
---|
173 | insertAnimation(d->animations.count(), animation);
|
---|
174 | }
|
---|
175 |
|
---|
176 | /*!
|
---|
177 | Inserts \a animation into this animation group at \a index.
|
---|
178 | If \a index is 0 the animation is inserted at the beginning.
|
---|
179 | If \a index is animationCount(), the animation is inserted at the end.
|
---|
180 |
|
---|
181 | \note The group takes ownership of the animation.
|
---|
182 |
|
---|
183 | \sa takeAnimation(), addAnimation(), indexOfAnimation(), removeAnimation()
|
---|
184 | */
|
---|
185 | void QAnimationGroup::insertAnimation(int index, QAbstractAnimation *animation)
|
---|
186 | {
|
---|
187 | Q_D(QAnimationGroup);
|
---|
188 |
|
---|
189 | if (index < 0 || index > d->animations.size()) {
|
---|
190 | qWarning("QAnimationGroup::insertAnimation: index is out of bounds");
|
---|
191 | return;
|
---|
192 | }
|
---|
193 |
|
---|
194 | if (QAnimationGroup *oldGroup = animation->group())
|
---|
195 | oldGroup->removeAnimation(animation);
|
---|
196 |
|
---|
197 | d->animations.insert(index, animation);
|
---|
198 | QAbstractAnimationPrivate::get(animation)->group = this;
|
---|
199 | // this will make sure that ChildAdded event is sent to 'this'
|
---|
200 | animation->setParent(this);
|
---|
201 | d->animationInsertedAt(index);
|
---|
202 | }
|
---|
203 |
|
---|
204 | /*!
|
---|
205 | Removes \a animation from this group. The ownership of \a animation is
|
---|
206 | transferred to the caller.
|
---|
207 |
|
---|
208 | \sa takeAnimation(), insertAnimation(), addAnimation()
|
---|
209 | */
|
---|
210 | void QAnimationGroup::removeAnimation(QAbstractAnimation *animation)
|
---|
211 | {
|
---|
212 | Q_D(QAnimationGroup);
|
---|
213 |
|
---|
214 | if (!animation) {
|
---|
215 | qWarning("QAnimationGroup::remove: cannot remove null animation");
|
---|
216 | return;
|
---|
217 | }
|
---|
218 | int index = d->animations.indexOf(animation);
|
---|
219 | if (index == -1) {
|
---|
220 | qWarning("QAnimationGroup::remove: animation is not part of this group");
|
---|
221 | return;
|
---|
222 | }
|
---|
223 |
|
---|
224 | takeAnimation(index);
|
---|
225 | }
|
---|
226 |
|
---|
227 | /*!
|
---|
228 | Returns the animation at \a index and removes it from the animation group.
|
---|
229 |
|
---|
230 | \note The ownership of the animation is transferred to the caller.
|
---|
231 |
|
---|
232 | \sa removeAnimation(), addAnimation(), insertAnimation(), indexOfAnimation()
|
---|
233 | */
|
---|
234 | QAbstractAnimation *QAnimationGroup::takeAnimation(int index)
|
---|
235 | {
|
---|
236 | Q_D(QAnimationGroup);
|
---|
237 | if (index < 0 || index >= d->animations.size()) {
|
---|
238 | qWarning("QAnimationGroup::takeAnimation: no animation at index %d", index);
|
---|
239 | return 0;
|
---|
240 | }
|
---|
241 | QAbstractAnimation *animation = d->animations.at(index);
|
---|
242 | QAbstractAnimationPrivate::get(animation)->group = 0;
|
---|
243 | // ### removing from list before doing setParent to avoid inifinite recursion
|
---|
244 | // in ChildRemoved event
|
---|
245 | d->animations.removeAt(index);
|
---|
246 | animation->setParent(0);
|
---|
247 | d->animationRemoved(index, animation);
|
---|
248 | return animation;
|
---|
249 | }
|
---|
250 |
|
---|
251 | /*!
|
---|
252 | Removes and deletes all animations in this animation group, and resets the current
|
---|
253 | time to 0.
|
---|
254 |
|
---|
255 | \sa addAnimation(), removeAnimation()
|
---|
256 | */
|
---|
257 | void QAnimationGroup::clear()
|
---|
258 | {
|
---|
259 | Q_D(QAnimationGroup);
|
---|
260 | qDeleteAll(d->animations);
|
---|
261 | }
|
---|
262 |
|
---|
263 | /*!
|
---|
264 | \reimp
|
---|
265 | */
|
---|
266 | bool QAnimationGroup::event(QEvent *event)
|
---|
267 | {
|
---|
268 | Q_D(QAnimationGroup);
|
---|
269 | if (event->type() == QEvent::ChildAdded) {
|
---|
270 | QChildEvent *childEvent = static_cast<QChildEvent *>(event);
|
---|
271 | if (QAbstractAnimation *a = qobject_cast<QAbstractAnimation *>(childEvent->child())) {
|
---|
272 | if (a->group() != this)
|
---|
273 | addAnimation(a);
|
---|
274 | }
|
---|
275 | } else if (event->type() == QEvent::ChildRemoved) {
|
---|
276 | QChildEvent *childEvent = static_cast<QChildEvent *>(event);
|
---|
277 | QAbstractAnimation *a = static_cast<QAbstractAnimation *>(childEvent->child());
|
---|
278 | // You can only rely on the child being a QObject because in the QEvent::ChildRemoved
|
---|
279 | // case it might be called from the destructor.
|
---|
280 | int index = d->animations.indexOf(a);
|
---|
281 | if (index != -1)
|
---|
282 | takeAnimation(index);
|
---|
283 | }
|
---|
284 | return QAbstractAnimation::event(event);
|
---|
285 | }
|
---|
286 |
|
---|
287 |
|
---|
288 | void QAnimationGroupPrivate::animationRemoved(int index, QAbstractAnimation *)
|
---|
289 | {
|
---|
290 | Q_Q(QAnimationGroup);
|
---|
291 | Q_UNUSED(index);
|
---|
292 | if (animations.isEmpty()) {
|
---|
293 | currentTime = 0;
|
---|
294 | q->stop();
|
---|
295 | }
|
---|
296 | }
|
---|
297 |
|
---|
298 | QT_END_NAMESPACE
|
---|
299 |
|
---|
300 | #include "moc_qanimationgroup.cpp"
|
---|
301 |
|
---|
302 | #endif //QT_NO_ANIMATION
|
---|