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
qanimationgroup.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
48#include "qanimationgroup.h"
49#include <QtCore/qdebug.h>
50#include <QtCore/qcoreevent.h>
51#include "qanimationgroup_p.h"
52
53#include <algorithm>
54
56
57
66
74
79{
80 Q_D(QAnimationGroup);
81 // We need to clear the animations now while we are still a valid QAnimationGroup.
82 // If we wait until ~QObject() the QAbstractAnimation's pointer back to us would
83 // point to a QObject, not a valid QAnimationGroup.
84 d->clear(true);
85}
86
95{
96 Q_D(const QAnimationGroup);
97
98 if (index < 0 || index >= d->animations.size()) {
99 qWarning("QAnimationGroup::animationAt: index is out of bounds");
100 return nullptr;
101 }
102
103 return d->animations.at(index);
104}
105
106
113{
114 Q_D(const QAnimationGroup);
115 return d->animations.size();
116}
117
125{
126 Q_D(const QAnimationGroup);
127 return d->animations.indexOf(animation);
128}
129
143
154{
155 Q_D(QAnimationGroup);
156
157 if (index < 0 || index > d->animations.size()) {
158 qWarning("QAnimationGroup::insertAnimation: index is out of bounds");
159 return;
160 }
161
162 if (QAnimationGroup *oldGroup = animation->group()) {
163 oldGroup->removeAnimation(animation);
164 // ensure we don't insert out of bounds if oldGroup == this
165 index = qMin(index, d->animations.size());
166 }
167
168 d->animations.insert(index, animation);
170 // this will make sure that ChildAdded event is sent to 'this'
171 animation->setParent(this);
172 d->animationInsertedAt(index);
173}
174
182{
183 Q_D(QAnimationGroup);
184
185 if (!animation) {
186 qWarning("QAnimationGroup::remove: cannot remove null animation");
187 return;
188 }
189 qsizetype index = d->animations.indexOf(animation);
190 if (index == -1) {
191 qWarning("QAnimationGroup::remove: animation is not part of this group");
192 return;
193 }
194
196}
197
206{
207 Q_D(QAnimationGroup);
208 if (index < 0 || index >= d->animations.size()) {
209 qWarning("QAnimationGroup::takeAnimation: no animation at index %d", index);
210 return nullptr;
211 }
212 QAbstractAnimation *animation = d->animations.at(index);
214 // ### removing from list before doing setParent to avoid infinite recursion
215 // in ChildRemoved event
216 d->animations.removeAt(index);
217 animation->setParent(nullptr);
218 d->animationRemoved(index, animation);
219 return animation;
220}
221
229{
230 Q_D(QAnimationGroup);
231 d->clear(false);
232}
233
238{
239 Q_D(QAnimationGroup);
240 if (event->type() == QEvent::ChildAdded) {
241 QChildEvent *childEvent = static_cast<QChildEvent *>(event);
242 if (QAbstractAnimation *a = qobject_cast<QAbstractAnimation *>(childEvent->child())) {
243 if (a->group() != this)
245 }
246 } else if (event->type() == QEvent::ChildRemoved) {
247 QChildEvent *childEvent = static_cast<QChildEvent *>(event);
248 // You can only rely on the child being a QObject because in the QEvent::ChildRemoved
249 // case it might be called from the destructor. Casting down to QAbstractAnimation then
250 // entails undefined behavior, so compare items as QObjects (which std::find does internally):
252 = std::find(d->animations.cbegin(), d->animations.cend(), childEvent->child());
253 if (it != d->animations.cend())
254 takeAnimation(it - d->animations.cbegin());
255 }
257}
258
259void QAnimationGroupPrivate::clear(bool onDestruction)
260{
261 const QList<QAbstractAnimation *> animationsCopy = animations; // taking a copy
263 // Clearing backwards so the indices doesn't change while we remove animations.
264 for (qsizetype i = animationsCopy.size() - 1; i >= 0; --i) {
265 QAbstractAnimation *animation = animationsCopy.at(i);
266 animation->setParent(nullptr);
268 // If we are in ~QAnimationGroup() it is not safe to called the virtual
269 // animationRemoved method, which can still be a method in a
270 // QAnimationGroupPrivate derived class that assumes q_ptr is still
271 // a valid derived class of QAnimationGroup.
272 if (!onDestruction)
274 delete animation;
275 }
276}
277
287
289
290#include "moc_qanimationgroup.cpp"
static QAbstractAnimationPrivate * get(QAbstractAnimation *q)
bool event(QEvent *event) override
\reimp
QAnimationGroup * group() const
If this animation is part of a QAnimationGroup, this function returns a pointer to the group; otherwi...
QList< QAbstractAnimation * > animations
void clear(bool onDestruction)
virtual void animationRemoved(qsizetype, QAbstractAnimation *)
\inmodule QtCore
void addAnimation(QAbstractAnimation *animation)
Adds animation to this group.
QAnimationGroup(QObject *parent=nullptr)
Constructs a QAnimationGroup.
void clear()
Removes and deletes all animations in this animation group, and resets the current time to 0.
void insertAnimation(int index, QAbstractAnimation *animation)
Inserts animation into this animation group at index.
QAbstractAnimation * takeAnimation(int index)
Returns the animation at index and removes it from the animation group.
~QAnimationGroup()
Destroys the animation group.
QAbstractAnimation * animationAt(int index) const
Returns a pointer to the animation at index in this group.
int indexOfAnimation(QAbstractAnimation *animation) const
Returns the index of animation.
int animationCount() const
Returns the number of animations managed by this group.
void removeAnimation(QAbstractAnimation *animation)
Removes animation from this group.
bool event(QEvent *event) override
\reimp
\inmodule QtCore
Definition qcoreevent.h:379
\inmodule QtCore
Definition qcoreevent.h:45
@ ChildRemoved
Definition qcoreevent.h:108
@ ChildAdded
Definition qcoreevent.h:106
bool isEmpty() const noexcept
Definition qlist.h:401
void clear()
Definition qlist.h:434
\inmodule QtCore
Definition qobject.h:103
virtual void childEvent(QChildEvent *event)
This event handler can be reimplemented in a subclass to receive child events.
Definition qobject.cpp:1508
void setParent(QObject *parent)
Makes the object a child of parent.
Definition qobject.cpp:2195
const_iterator cend() const noexcept
Definition qset.h:142
const_iterator cbegin() const noexcept
Definition qset.h:138
QSet< QString >::iterator it
Combined button and popup list for selecting options.
#define qWarning
Definition qlogging.h:166
constexpr const T & qMin(const T &a, const T &b)
Definition qminmax.h:40
GLboolean GLboolean GLboolean GLboolean a
[7]
GLuint index
[2]
struct _cl_event * event
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
#define Q_UNUSED(x)
ptrdiff_t qsizetype
Definition qtypes.h:165
static double currentTime()
QPropertyAnimation animation
[0]