Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qparallelanimationgroup.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
31//#define QANIMATION_DEBUG
32
34
38
47
56
63
68{
69 Q_D(const QParallelAnimationGroup);
70 int ret = 0;
71
72 for (AnimationListConstIt it = d->animations.constBegin(), cend = d->animations.constEnd(); it != cend; ++it) {
73 const int currentDuration = (*it)->totalDuration();
74 if (currentDuration == -1)
75 return -1; // Undetermined length
76
77 ret = qMax(ret, currentDuration);
78 }
79
80 return ret;
81}
82
87{
89 if (d->animations.isEmpty())
90 return;
91
92 if (d->currentLoop > d->lastLoop) {
93 // simulate completion of the loop
94 int dura = duration();
95 if (dura > 0) {
96 for (AnimationListConstIt it = d->animations.constBegin(), cend = d->animations.constEnd(); it != cend; ++it) {
99 animation->setCurrentTime(dura); // will stop
100 }
101 }
102 } else if (d->currentLoop < d->lastLoop) {
103 // simulate completion of the loop seeking backwards
104 for (AnimationListConstIt it = d->animations.constBegin(), cend = d->animations.constEnd(); it != cend; ++it) {
106 //we need to make sure the animation is in the right state
107 //and then rewind it
108 d->applyGroupState(animation);
110 animation->stop();
111 }
112 }
113
114#ifdef QANIMATION_DEBUG
115 qDebug("QParallellAnimationGroup %5d: setCurrentTime(%d), loop:%d, last:%d, timeFwd:%d, lastcurrent:%d, %d",
116 __LINE__, d->currentTime, d->currentLoop, d->lastLoop, timeFwd, d->lastCurrentTime, state());
117#endif
118 // finally move into the actual time of the current loop
119 for (AnimationListConstIt it = d->animations.constBegin(), cend = d->animations.constEnd(); it != cend; ++it) {
121 const int dura = animation->totalDuration();
122 //if the loopcount is bigger we should always start all animations
123 if (d->currentLoop > d->lastLoop
124 //if we're at the end of the animation, we need to start it if it wasn't already started in this loop
125 //this happens in Backward direction where not all animations are started at the same time
126 || d->shouldAnimationStart(animation, d->lastCurrentTime > dura /*startIfAtEnd*/)) {
127 d->applyGroupState(animation);
128 }
129
130 if (animation->state() == state()) {
132 if (dura > 0 && currentTime > dura)
133 animation->stop();
134 }
135 }
136 d->lastLoop = d->currentLoop;
137 d->lastCurrentTime = currentTime;
138}
139
145{
148
149 switch (newState) {
150 case Stopped:
151 for (AnimationListConstIt it = d->animations.constBegin(), cend = d->animations.constEnd(); it != cend; ++it)
152 (*it)->stop();
153 d->disconnectUncontrolledAnimations();
154 break;
155 case Paused:
156 for (AnimationListConstIt it = d->animations.constBegin(), cend = d->animations.constEnd(); it != cend; ++it) {
157 if ((*it)->state() == Running)
158 (*it)->pause();
159 }
160 break;
161 case Running:
162 d->connectUncontrolledAnimations();
163 for (AnimationListConstIt it = d->animations.constBegin(), cend = d->animations.constEnd(); it != cend; ++it) {
165 if (oldState == Stopped)
166 animation->stop();
167 animation->setDirection(d->direction);
168 if (d->shouldAnimationStart(animation, oldState == Stopped))
169 animation->start();
170 }
171 break;
172 }
173}
174
176{
178
179 QAbstractAnimation *animation = qobject_cast<QAbstractAnimation *>(q->sender());
181
182 int uncontrolledRunningCount = 0;
183 if (animation->duration() == -1 || animation->loopCount() < 0) {
185 if (it.key() == animation) {
187 }
188 if (it.value() == -1)
189 ++uncontrolledRunningCount;
190 }
191 }
192
193 if (uncontrolledRunningCount > 0)
194 return;
195
196 int maxDuration = 0;
197 for (AnimationListConstIt it = animations.constBegin(), cend = animations.constEnd(); it != cend; ++it)
198 maxDuration = qMax(maxDuration, (*it)->totalDuration());
199
200 if (currentTime >= maxDuration)
201 q->stop();
202}
203
211
222
224{
225 const int dura = animation->totalDuration();
226 if (dura == -1)
228 if (startIfAtEnd)
229 return currentTime <= dura;
231 return currentTime < dura;
232 else //direction == QAbstractAnimation::Backward
233 return currentTime && currentTime <= dura;
234}
235
237{
238 switch (state)
239 {
241 animation->start();
242 break;
244 animation->pause();
245 break;
247 default:
248 break;
249 }
250}
251
252
257
264
269{
271 //we need to update the direction of the current animation
272 if (state() != Stopped) {
273 for (AnimationListConstIt it = d->animations.constBegin(), cend = d->animations.constEnd(); it != cend; ++it)
274 (*it)->setDirection(direction);
275 } else {
276 if (direction == Forward) {
277 d->lastLoop = 0;
278 d->lastCurrentTime = 0;
279 } else {
280 // Looping backwards with loopCount == -1 does not really work well...
281 d->lastLoop = (d->loopCount == -1 ? 0 : d->loopCount - 1);
282 d->lastCurrentTime = duration();
283 }
284 }
285}
286
294
296
297#include "moc_qparallelanimationgroup.cpp"
State state
state of the animation.
Direction direction
the direction of the animation when it is in \l Running state.
State
This enum describes the state of the animation.
void stop()
Stops the animation.
void start(QAbstractAnimation::DeletionPolicy policy=KeepWhenStopped)
Starts the animation.
int loopCount
the loop count of the animation
int totalDuration() const
Returns the total and effective duration of the animation, including the loop count.
void setDirection(Direction direction)
int currentTime
the current time and progress of the animation
void setCurrentTime(int msecs)
virtual void updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState)
This virtual function is called by QAbstractAnimation when the state of the animation is changed from...
Direction
This enum describes the direction of the animation when in \l Running state.
void pause()
Pauses the animation.
QList< QAbstractAnimation * > animations
void disconnectUncontrolledAnimation(QAbstractAnimation *anim)
void connectUncontrolledAnimation(QAbstractAnimation *anim)
virtual void animationRemoved(qsizetype, QAbstractAnimation *)
\inmodule QtCore
bool event(QEvent *event) override
\reimp
\inmodule QtCore
Definition qcoreevent.h:45
\inmodule QtCore
Definition qhash.h:1145
\inmodule QtCore
Definition qhash.h:1103
bool remove(const Key &key)
Removes the item that has the key from the hash.
Definition qhash.h:958
iterator begin()
Returns an \l{STL-style iterators}{STL-style iterator} pointing to the first item in the hash.
Definition qhash.h:1212
const_iterator constEnd() const noexcept
Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the ...
Definition qhash.h:1219
const_iterator constBegin() const noexcept
Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first item in the hash.
Definition qhash.h:1215
T value(const Key &key) const noexcept
Definition qhash.h:1054
iterator end() noexcept
Returns an \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the last ...
Definition qhash.h:1216
void clear() noexcept(std::is_nothrow_destructible< Node >::value)
Removes all items from the hash and frees up all memory used by it.
Definition qhash.h:951
const_iterator constBegin() const noexcept
Definition qlist.h:632
const_iterator constEnd() const noexcept
Definition qlist.h:633
\inmodule QtCore
Definition qobject.h:103
void applyGroupState(QAbstractAnimation *animation)
bool isUncontrolledAnimationFinished(QAbstractAnimation *anim) const
bool shouldAnimationStart(QAbstractAnimation *animation, bool startIfAtEnd) const
void animationRemoved(qsizetype index, QAbstractAnimation *) override
QHash< QAbstractAnimation *, int > uncontrolledFinishTime
void updateDirection(QAbstractAnimation::Direction direction) override
\reimp
bool event(QEvent *event) override
\reimp
~QParallelAnimationGroup()
Destroys the animation group.
int duration() const override
\reimp
QParallelAnimationGroup(QObject *parent=nullptr)
Constructs a QParallelAnimationGroup.
void updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState) override
\reimp
void updateCurrentTime(int currentTime) override
\reimp
const_iterator constBegin() const noexcept
Definition qset.h:139
int duration
the duration of the animation
QSet< QString >::iterator it
direction
else opt state
[0]
void newState(QList< State > &states, const char *token, const char *lexem, bool pre)
Combined button and popup list for selecting options.
QList< QAbstractAnimation * >::ConstIterator AnimationListConstIt
#define qDebug
[1]
Definition qlogging.h:164
return ret
constexpr const T & qMax(const T &a, const T &b)
Definition qminmax.h:42
GLuint index
[2]
struct _cl_event * event
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
QT_BEGIN_NAMESPACE typedef QList< QAbstractAnimation * >::ConstIterator AnimationListConstIt
QHash< QAbstractAnimation *, int >::ConstIterator AnimationTimeHashConstIt
QHash< QAbstractAnimation *, int >::Iterator AnimationTimeHashIt
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
ptrdiff_t qsizetype
Definition qtypes.h:165
static double currentTime()
QPropertyAnimation animation
[0]