1
/****************************************************************************
3
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/
6
** This file is part of the QtQml module of the Qt Toolkit.
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.
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.
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.
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.
40
****************************************************************************/
42
#include "private/qsequentialanimationgroupjob_p.h"
43
#include "private/qpauseanimationjob_p.h"
44
#include "private/qanimationjobutil_p.h"
48
QSequentialAnimationGroupJob::QSequentialAnimationGroupJob()
49
: QAnimationGroupJob()
50
, m_currentAnimation(0)
55
QSequentialAnimationGroupJob::~QSequentialAnimationGroupJob()
59
bool QSequentialAnimationGroupJob::atEnd() const
61
// we try to detect if we're at the end of the group
62
//this is true if the following conditions are true:
63
// 1. we're in the last loop
64
// 2. the direction is forward
65
// 3. the current animation is the last one
66
// 4. the current animation has reached its end
67
const int animTotalCurrentTime = m_currentAnimation->currentTime();
68
return (m_currentLoop == m_loopCount - 1
69
&& m_direction == Forward
70
&& !m_currentAnimation->nextSibling()
71
&& animTotalCurrentTime == animationActualTotalDuration(m_currentAnimation));
74
int QSequentialAnimationGroupJob::animationActualTotalDuration(QAbstractAnimationJob *anim) const
76
int ret = anim->totalDuration();
78
ret = uncontrolledAnimationFinishTime(anim); //we can try the actual duration there
82
QSequentialAnimationGroupJob::AnimationIndex QSequentialAnimationGroupJob::indexForCurrentTime() const
84
Q_ASSERT(firstChild());
87
QAbstractAnimationJob *anim = 0;
90
for (anim = firstChild(); anim; anim = anim->nextSibling()) {
91
duration = animationActualTotalDuration(anim);
93
// 'animation' is the current animation if one of these reasons is true:
94
// 1. it's duration is undefined
95
// 2. it ends after msecs
96
// 3. it is the last animation (this can happen in case there is at least 1 uncontrolled animation)
97
// 4. it ends exactly in msecs and the direction is backwards
98
if (duration == -1 || m_currentTime < (ret.timeOffset + duration)
99
|| (m_currentTime == (ret.timeOffset + duration) && m_direction == QAbstractAnimationJob::Backward)) {
100
ret.animation = anim;
104
if (anim == m_currentAnimation)
105
ret.afterCurrent = true;
107
// 'animation' has a non-null defined duration and is not the one at time 'msecs'.
108
ret.timeOffset += duration;
111
// this can only happen when one of those conditions is true:
112
// 1. the duration of the group is undefined and we passed its actual duration
113
// 2. there are only 0-duration animations in the group
114
ret.timeOffset -= duration;
115
ret.animation = lastChild();
119
void QSequentialAnimationGroupJob::restart()
121
// restarting the group by making the first/last animation the current one
122
if (m_direction == Forward) {
124
if (m_currentAnimation == firstChild())
125
activateCurrentAnimation();
127
setCurrentAnimation(firstChild());
129
else { // direction == Backward
130
m_previousLoop = m_loopCount - 1;
131
if (m_currentAnimation == lastChild())
132
activateCurrentAnimation();
134
setCurrentAnimation(lastChild());
138
void QSequentialAnimationGroupJob::advanceForwards(const AnimationIndex &newAnimationIndex)
140
if (m_previousLoop < m_currentLoop) {
141
// we need to fast forward to the end
142
for (QAbstractAnimationJob *anim = m_currentAnimation; anim; anim = anim->nextSibling()) {
143
setCurrentAnimation(anim, true);
144
RETURN_IF_DELETED(anim->setCurrentTime(animationActualTotalDuration(anim)));
146
// this will make sure the current animation is reset to the beginning
147
if (firstChild() && !firstChild()->nextSibling()) //count == 1
148
// we need to force activation because setCurrentAnimation will have no effect
149
activateCurrentAnimation();
151
setCurrentAnimation(firstChild(), true);
154
// and now we need to fast forward from the current position to
155
for (QAbstractAnimationJob *anim = m_currentAnimation; anim && anim != newAnimationIndex.animation; anim = anim->nextSibling()) { //### WRONG,
156
setCurrentAnimation(anim, true);
157
RETURN_IF_DELETED(anim->setCurrentTime(animationActualTotalDuration(anim)));
159
// setting the new current animation will happen later
162
void QSequentialAnimationGroupJob::rewindForwards(const AnimationIndex &newAnimationIndex)
164
if (m_previousLoop > m_currentLoop) {
165
// we need to fast rewind to the beginning
166
for (QAbstractAnimationJob *anim = m_currentAnimation; anim; anim = anim->previousSibling()) {
167
setCurrentAnimation(anim, true);
168
RETURN_IF_DELETED(anim->setCurrentTime(0));
170
// this will make sure the current animation is reset to the end
171
if (lastChild() && !lastChild()->previousSibling()) //count == 1
172
// we need to force activation because setCurrentAnimation will have no effect
173
activateCurrentAnimation();
175
setCurrentAnimation(lastChild(), true);
179
// and now we need to fast rewind from the current position to
180
for (QAbstractAnimationJob *anim = m_currentAnimation; anim && anim != newAnimationIndex.animation; anim = anim->previousSibling()) {
181
setCurrentAnimation(anim, true);
182
RETURN_IF_DELETED(anim->setCurrentTime(0));
184
// setting the new current animation will happen later
187
int QSequentialAnimationGroupJob::duration() const
191
for (QAbstractAnimationJob *anim = firstChild(); anim; anim = anim->nextSibling()) {
192
const int currentDuration = anim->totalDuration();
193
if (currentDuration == -1)
194
return -1; // Undetermined length
196
ret += currentDuration;
202
void QSequentialAnimationGroupJob::updateCurrentTime(int currentTime)
204
if (!m_currentAnimation)
207
const QSequentialAnimationGroupJob::AnimationIndex newAnimationIndex = indexForCurrentTime();
209
// newAnimationIndex.index is the new current animation
210
if (m_previousLoop < m_currentLoop
211
|| (m_previousLoop == m_currentLoop && m_currentAnimation != newAnimationIndex.animation && newAnimationIndex.afterCurrent)) {
212
// advancing with forward direction is the same as rewinding with backwards direction
213
RETURN_IF_DELETED(advanceForwards(newAnimationIndex));
214
} else if (m_previousLoop > m_currentLoop
215
|| (m_previousLoop == m_currentLoop && m_currentAnimation != newAnimationIndex.animation && !newAnimationIndex.afterCurrent)) {
216
// rewinding with forward direction is the same as advancing with backwards direction
217
RETURN_IF_DELETED(rewindForwards(newAnimationIndex));
220
setCurrentAnimation(newAnimationIndex.animation);
222
const int newCurrentTime = currentTime - newAnimationIndex.timeOffset;
224
if (m_currentAnimation) {
225
RETURN_IF_DELETED(m_currentAnimation->setCurrentTime(newCurrentTime));
227
//we make sure that we don't exceed the duration here
228
m_currentTime += m_currentAnimation->currentTime() - newCurrentTime;
232
//the only case where currentAnimation could be null
233
//is when all animations have been removed
234
Q_ASSERT(!firstChild());
239
m_previousLoop = m_currentLoop;
242
void QSequentialAnimationGroupJob::updateState(QAbstractAnimationJob::State newState,
243
QAbstractAnimationJob::State oldState)
245
QAnimationGroupJob::updateState(newState, oldState);
247
if (!m_currentAnimation)
252
m_currentAnimation->stop();
255
if (oldState == m_currentAnimation->state() && oldState == Running)
256
m_currentAnimation->pause();
261
if (oldState == m_currentAnimation->state() && oldState == Paused)
262
m_currentAnimation->start();
269
void QSequentialAnimationGroupJob::updateDirection(QAbstractAnimationJob::Direction direction)
271
// we need to update the direction of the current animation
272
if (!isStopped() && m_currentAnimation)
273
m_currentAnimation->setDirection(direction);
276
void QSequentialAnimationGroupJob::setCurrentAnimation(QAbstractAnimationJob *anim, bool intermediate)
279
Q_ASSERT(!firstChild());
280
m_currentAnimation = 0;
284
if (anim == m_currentAnimation)
287
// stop the old current animation
288
if (m_currentAnimation)
289
m_currentAnimation->stop();
291
m_currentAnimation = anim;
293
activateCurrentAnimation(intermediate);
296
void QSequentialAnimationGroupJob::activateCurrentAnimation(bool intermediate)
298
if (!m_currentAnimation || isStopped())
301
m_currentAnimation->stop();
303
// we ensure the direction is consistent with the group's direction
304
m_currentAnimation->setDirection(m_direction);
306
// reset the finish time of the animation if it is uncontrolled
307
if (m_currentAnimation->totalDuration() == -1)
308
resetUncontrolledAnimationFinishTime(m_currentAnimation);
310
m_currentAnimation->start();
311
if (!intermediate && isPaused())
312
m_currentAnimation->pause();
315
void QSequentialAnimationGroupJob::uncontrolledAnimationFinished(QAbstractAnimationJob *animation)
318
Q_ASSERT(animation == m_currentAnimation);
320
setUncontrolledAnimationFinishTime(m_currentAnimation, m_currentAnimation->currentTime());
322
if ((m_direction == Forward && m_currentAnimation == lastChild())
323
|| (m_direction == Backward && m_currentAnimation == firstChild())) {
324
// we don't handle looping of a group with undefined duration
326
} else if (m_direction == Forward) {
327
// set the current animation to be the next one
328
setCurrentAnimation(m_currentAnimation->nextSibling());
330
// set the current animation to be the previous one
331
setCurrentAnimation(m_currentAnimation->previousSibling());
335
void QSequentialAnimationGroupJob::animationInserted(QAbstractAnimationJob *anim)
337
if (m_currentAnimation == 0)
338
setCurrentAnimation(firstChild()); // initialize the current animation
340
if (m_currentAnimation == anim->nextSibling()
341
&& m_currentAnimation->currentTime() == 0 && m_currentAnimation->currentLoop() == 0) {
342
//in this case we simply insert the animation before the current one has actually started
343
setCurrentAnimation(anim);
347
// if (index < m_currentAnimationIndex || m_currentLoop != 0) {
348
// qWarning("QSequentialGroup::insertAnimation only supports to add animations after the current one.");
349
// return; //we're not affected because it is added after the current one
353
void QSequentialAnimationGroupJob::animationRemoved(QAbstractAnimationJob *anim, QAbstractAnimationJob *prev, QAbstractAnimationJob *next)
355
QAnimationGroupJob::animationRemoved(anim, prev, next);
357
Q_ASSERT(m_currentAnimation); // currentAnimation should always be set
359
bool removingCurrent = anim == m_currentAnimation;
360
if (removingCurrent) {
362
setCurrentAnimation(next); //let's try to take the next one
364
setCurrentAnimation(prev);
365
else// case all animations were removed
366
setCurrentAnimation(0);
369
// duration of the previous animations up to the current animation
371
for (QAbstractAnimationJob *anim = firstChild(); anim; anim = anim->nextSibling()) {
372
if (anim == m_currentAnimation)
374
m_currentTime += animationActualTotalDuration(anim);
378
if (!removingCurrent) {
379
//the current animation is not the one being removed
380
//so we add its current time to the current time of this group
381
m_currentTime += m_currentAnimation->currentTime();
384
//let's also update the total current time
385
m_totalCurrentTime = m_currentTime + m_loopCount * duration();