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 QtQml 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 #include "private/qparallelanimationgroupjob_p.h"
41 #include "private/qanimationjobutil_p.h"
42 
43 QT_BEGIN_NAMESPACE
44 
QParallelAnimationGroupJob()45 QParallelAnimationGroupJob::QParallelAnimationGroupJob()
46     : QAnimationGroupJob()
47     , m_previousLoop(0)
48     , m_previousCurrentTime(0)
49 {
50 }
51 
~QParallelAnimationGroupJob()52 QParallelAnimationGroupJob::~QParallelAnimationGroupJob()
53 {
54 }
55 
duration() const56 int QParallelAnimationGroupJob::duration() const
57 {
58     int ret = 0;
59 
60     for (QAbstractAnimationJob *animation = firstChild(); animation; animation = animation->nextSibling()) {
61         int currentDuration = animation->totalDuration();
62         if (currentDuration == -1)
63             return -1; // Undetermined length
64         ret = qMax(ret, currentDuration);
65     }
66 
67     return ret;
68 }
69 
updateCurrentTime(int)70 void QParallelAnimationGroupJob::updateCurrentTime(int /*currentTime*/)
71 {
72     if (!firstChild())
73         return;
74 
75     if (m_currentLoop > m_previousLoop) {
76         // simulate completion of the loop
77         int dura = duration();
78         if (dura < 0) {
79             // For an uncontrolled parallel group, we need to simulate the end of running animations.
80             // As uncontrolled animation finish time is already reset for this next loop, we pick the
81             // longest of the known stop times.
82             for (QAbstractAnimationJob *animation = firstChild(); animation; animation = animation->nextSibling()) {
83                 int currentDuration = animation->totalDuration();
84                 if (currentDuration >= 0)
85                     dura = qMax(dura, currentDuration);
86             }
87         }
88         if (dura > 0) {
89             for (QAbstractAnimationJob *animation = firstChild(); animation; animation = animation->nextSibling()) {
90                 if (!animation->isStopped())
91                     RETURN_IF_DELETED(animation->setCurrentTime(dura));   // will stop
92             }
93         }
94     } else if (m_currentLoop < m_previousLoop) {
95         // simulate completion of the loop seeking backwards
96         for (QAbstractAnimationJob *animation = firstChild(); animation; animation = animation->nextSibling()) {
97             //we need to make sure the animation is in the right state
98             //and then rewind it
99             applyGroupState(animation);
100             RETURN_IF_DELETED(animation->setCurrentTime(0));
101             animation->stop();
102         }
103     }
104 
105     // finally move into the actual time of the current loop
106     for (QAbstractAnimationJob *animation = firstChild(); animation; animation = animation->nextSibling()) {
107         const int dura = animation->totalDuration();
108         //if the loopcount is bigger we should always start all animations
109         if (m_currentLoop > m_previousLoop
110             //if we're at the end of the animation, we need to start it if it wasn't already started in this loop
111             //this happens in Backward direction where not all animations are started at the same time
112             || shouldAnimationStart(animation, m_previousCurrentTime > dura /*startIfAtEnd*/)) {
113             applyGroupState(animation);
114         }
115 
116         if (animation->state() == state()) {
117             RETURN_IF_DELETED(animation->setCurrentTime(m_currentTime));
118             if (dura > 0 && m_currentTime > dura)
119                 animation->stop();
120         }
121     }
122     m_previousLoop = m_currentLoop;
123     m_previousCurrentTime = m_currentTime;
124 }
125 
updateState(QAbstractAnimationJob::State newState,QAbstractAnimationJob::State oldState)126 void QParallelAnimationGroupJob::updateState(QAbstractAnimationJob::State newState,
127                                           QAbstractAnimationJob::State oldState)
128 {
129     QAnimationGroupJob::updateState(newState, oldState);
130 
131     switch (newState) {
132     case Stopped:
133         for (QAbstractAnimationJob *animation = firstChild(); animation; animation = animation->nextSibling())
134             animation->stop();
135         break;
136     case Paused:
137         for (QAbstractAnimationJob *animation = firstChild(); animation; animation = animation->nextSibling())
138             if (animation->isRunning())
139                 animation->pause();
140         break;
141     case Running:
142         for (QAbstractAnimationJob *animation = firstChild(); animation; animation = animation->nextSibling()) {
143             if (oldState == Stopped) {
144                 animation->stop();
145                 m_previousLoop = m_direction == Forward ? 0 : m_loopCount - 1;
146             }
147             resetUncontrolledAnimationFinishTime(animation);
148             animation->setDirection(m_direction);
149             if (shouldAnimationStart(animation, oldState == Stopped))
150                 animation->start();
151         }
152         break;
153     }
154 }
155 
shouldAnimationStart(QAbstractAnimationJob * animation,bool startIfAtEnd) const156 bool QParallelAnimationGroupJob::shouldAnimationStart(QAbstractAnimationJob *animation, bool startIfAtEnd) const
157 {
158     const int dura = animation->totalDuration();
159 
160     if (dura == -1)
161         return uncontrolledAnimationFinishTime(animation) == -1;
162 
163     if (startIfAtEnd)
164         return m_currentTime <= dura;
165     if (m_direction == Forward)
166         return m_currentTime < dura;
167     else //direction == Backward
168         return m_currentTime && m_currentTime <= dura;
169 }
170 
applyGroupState(QAbstractAnimationJob * animation)171 void QParallelAnimationGroupJob::applyGroupState(QAbstractAnimationJob *animation)
172 {
173     switch (m_state)
174     {
175     case Running:
176         animation->start();
177         break;
178     case Paused:
179         animation->pause();
180         break;
181     case Stopped:
182     default:
183         break;
184     }
185 }
186 
updateDirection(QAbstractAnimationJob::Direction direction)187 void QParallelAnimationGroupJob::updateDirection(QAbstractAnimationJob::Direction direction)
188 {
189     //we need to update the direction of the current animation
190     if (!isStopped()) {
191         for (QAbstractAnimationJob *animation = firstChild(); animation; animation = animation->nextSibling()) {
192             animation->setDirection(direction);
193         }
194     } else {
195         if (direction == Forward) {
196             m_previousLoop = 0;
197             m_previousCurrentTime = 0;
198         } else {
199             // Looping backwards with loopCount == -1 does not really work well...
200             m_previousLoop = (m_loopCount == -1 ? 0 : m_loopCount - 1);
201             m_previousCurrentTime = duration();
202         }
203     }
204 }
205 
uncontrolledAnimationFinished(QAbstractAnimationJob * animation)206 void QParallelAnimationGroupJob::uncontrolledAnimationFinished(QAbstractAnimationJob *animation)
207 {
208     Q_ASSERT(animation && (animation->duration() == -1 || animation->loopCount() < 0));
209     int uncontrolledRunningCount = 0;
210 
211     for (QAbstractAnimationJob *child = firstChild(); child; child = child->nextSibling()) {
212         if (child == animation) {
213             setUncontrolledAnimationFinishTime(animation, animation->currentTime());
214         } else if (child->duration() == -1 || child->loopCount() < 0) {
215             if (uncontrolledAnimationFinishTime(child) == -1)
216                 ++uncontrolledRunningCount;
217         }
218     }
219 
220     if (uncontrolledRunningCount > 0)
221         return;
222 
223     int maxDuration = 0;
224     bool running = false;
225     for (QAbstractAnimationJob *job = firstChild(); job; job = job->nextSibling()) {
226         if (job->state() == Running)
227             running = true;
228         maxDuration = qMax(maxDuration, job->totalDuration());
229     }
230 
231     setUncontrolledAnimationFinishTime(this, qMax(maxDuration + m_currentLoopStartTime, currentTime()));
232 
233     if (!running
234             && ((m_direction == Forward && m_currentLoop == m_loopCount -1)
235                 || (m_direction == Backward && m_currentLoop == 0))) {
236         stop();
237     }
238 }
239 
debugAnimation(QDebug d) const240 void QParallelAnimationGroupJob::debugAnimation(QDebug d) const
241 {
242     d << "ParallelAnimationGroupJob(" << Qt::hex << (const void *) this << Qt::dec << ")";
243 
244     debugChildren(d);
245 }
246 
247 QT_END_NAMESPACE
248 
249