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
4#include "qanimationgroup.h"
5#include "Qt3DAnimation/private/qanimationgroup_p.h"
6
7QT_BEGIN_NAMESPACE
8
9namespace Qt3DAnimation {
10
11/*!
12 \class Qt3DAnimation::QAnimationGroup
13 \brief A class grouping animations together.
14 \inmodule Qt3DAnimation
15 \since 5.9
16 \inherits QObject
17
18 Qt3DAnimation::QAnimationGroup class is used to group multiple animations so that
19 they can act as one animation. The position set to the group is also set to
20 all animations in a group. The duration is the maximum of the individual animations.
21 The animations can be any supported animation type and do not have to have the same name.
22*/
23/*!
24 \qmltype AnimationGroup
25 \brief A type grouping animations together.
26 \inqmlmodule Qt3D.Animation
27 \since 5.9
28 \inherits QObject
29
30 AnimationGroup type is used to group multiple animations so that
31 they can act as one animation. The position set to the group is also set to
32 all animations in a group. The duration is the maximum of the individual animations.
33 The animations can be any supported animation type and do not have to have the same name.
34*/
35
36/*!
37 \property Qt3DAnimation::QAnimationGroup::name
38 Holds the name of the animation group.
39*/
40/*!
41 \property Qt3DAnimation::QAnimationGroup::position
42 Holds the animation position.
43*/
44/*!
45 \property Qt3DAnimation::QAnimationGroup::duration
46 Holds the maximum duration of the animations in the group.
47 \readonly
48*/
49
50/*!
51 \qmlproperty string AnimationGroup::name
52 Holds the name of the animation group.
53*/
54/*!
55 \qmlproperty real AnimationGroup::position
56 Holds the animation position.
57*/
58/*!
59 \qmlproperty real AnimationGroup::duration
60 Holds the maximum duration of the animations in the group.
61 \readonly
62*/
63/*!
64 \qmlproperty list<AbstractAnimation> AnimationGroup::animations
65 Holds the list of animations in the animation group.
66*/
67
68QAnimationGroupPrivate::QAnimationGroupPrivate()
69 : QObjectPrivate()
70 , m_position(0.0f)
71 , m_duration(0.0f)
72{
73
74}
75
76void QAnimationGroupPrivate::updatePosition(float position)
77{
78 m_position = position;
79 for (QAbstractAnimation *aa : std::as_const(t&: m_animations))
80 aa->setPosition(position);
81}
82
83/*!
84 Constructs an QAnimationGroup with \a parent.
85*/
86QAnimationGroup::QAnimationGroup(QObject *parent)
87 : QObject(*new QAnimationGroupPrivate, parent)
88{
89
90}
91
92QString QAnimationGroup::name() const
93{
94 Q_D(const QAnimationGroup);
95 return d->m_name;
96}
97
98/*!
99 Returns the list of animations in the group.
100 */
101QList<Qt3DAnimation::QAbstractAnimation *> QAnimationGroup::animationList()
102{
103 Q_D(QAnimationGroup);
104 return d->m_animations;
105}
106
107float QAnimationGroup::position() const
108{
109 Q_D(const QAnimationGroup);
110 return d->m_position;
111}
112
113float QAnimationGroup::duration() const
114{
115 Q_D(const QAnimationGroup);
116 return d->m_duration;
117}
118
119void QAnimationGroup::setName(const QString &name)
120{
121 Q_D(QAnimationGroup);
122 if (d->m_name != name) {
123 d->m_name = name;
124 emit nameChanged(name);
125 }
126}
127
128/*!
129 Sets the \a animations to the group. Old animations are removed.
130 */
131void QAnimationGroup::setAnimations(const QList<Qt3DAnimation::QAbstractAnimation *> &animations)
132{
133 Q_D(QAnimationGroup);
134 d->m_animations = animations;
135 d->m_duration = 0.0f;
136 for (const Qt3DAnimation::QAbstractAnimation *a : animations)
137 d->m_duration = qMax(a: d->m_duration, b: a->duration());
138}
139
140/*!
141 Adds new \a animation to the group.
142 */
143void QAnimationGroup::addAnimation(Qt3DAnimation::QAbstractAnimation *animation)
144{
145 Q_D(QAnimationGroup);
146 if (!d->m_animations.contains(t: animation)) {
147 d->m_animations.push_back(t: animation);
148 d->m_duration = qMax(a: d->m_duration, b: animation->duration());
149 }
150}
151
152/*!
153 Removes \a animation from the group.
154 */
155void QAnimationGroup::removeAnimation(Qt3DAnimation::QAbstractAnimation *animation)
156{
157 Q_D(QAnimationGroup);
158 if (!d->m_animations.contains(t: animation)) {
159 d->m_animations.removeAll(t: animation);
160 if (qFuzzyCompare(p1: d->m_duration, p2: animation->duration())) {
161 d->m_duration = 0.0f;
162 for (const Qt3DAnimation::QAbstractAnimation *a : std::as_const(t&: d->m_animations))
163 d->m_duration = qMax(a: d->m_duration, b: a->duration());
164 }
165 }
166}
167
168void QAnimationGroup::setPosition(float position)
169{
170 Q_D(QAnimationGroup);
171 if (!qFuzzyCompare(p1: d->m_position, p2: position)) {
172 d->updatePosition(position);
173 emit positionChanged(position);
174 }
175}
176
177} // Qt3DAnimation
178
179QT_END_NAMESPACE
180
181#include "moc_qanimationgroup.cpp"
182

source code of qt3d/src/animation/frontend/qanimationgroup.cpp