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