Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquickanimator.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// Qt-Security score:significant reason:default
4
7
8#include <private/qquickitem_p.h>
9
11
12/*!
13 \qmltype Animator
14 \nativetype QQuickAnimator
15 \inqmlmodule QtQuick
16 \since 5.2
17 \ingroup qtquick-transitions-animations
18 \inherits Animation
19 \brief Is the base of all QML animators.
20
21 Animator types are a special type of animation which operate
22 directly on Qt Quick's scene graph, rather than the QML objects and their
23 properties like regular Animation types do. This has the benefit that
24 Animator based animations can animate on the \l
25 {Threaded Render Loop ('threaded')}{scene graph's rendering thread} even when the
26 UI thread is blocked.
27
28 The value of the QML property will be updated after the animation has
29 finished. The property is not updated while the animation is running.
30
31 The Animator types can be used just like any other Animation type.
32
33 \snippet qml/animators.qml mixed
34
35 If all sub-animations of ParallelAnimation and SequentialAnimation
36 are Animator types, the ParallelAnimation and SequentialAnimation will
37 also be treated as an Animator and be run on the scene graph's rendering
38 thread when possible.
39
40 The Animator types can be used for animations during transitions, but
41 they do not support the \l {Transition::reversible}{reversible}
42 property.
43
44 The Animator type cannot be used directly in a QML file. It exists
45 to provide a set of common properties and methods, available across all the
46 other animator types that inherit from it. Attempting to use the Animator
47 type directly will result in an error.
48
49 \include animatornotes.qdocinc
50 */
51
52QQuickAnimator::QQuickAnimator(QQuickAnimatorPrivate &dd, QObject *parent)
53 : QQuickAbstractAnimation(dd, parent)
54{
55}
56
57QQuickAnimator::QQuickAnimator(QObject *parent)
58 : QQuickAbstractAnimation(*new QQuickAnimatorPrivate, parent)
59{
60}
61
62/*!
63 \qmlproperty QtQuick::Item QtQuick::Animator::target
64
65 This property holds the target item of the animator.
66
67 \note Animator targets must be Item based types.
68 */
69
70void QQuickAnimator::setTargetItem(QQuickItem *target)
71{
72 Q_D(QQuickAnimator);
73 if (target == d->target)
74 return;
75 d->target = target;
76 Q_EMIT targetItemChanged(d->target);
77}
78
79QQuickItem *QQuickAnimator::targetItem() const
80{
81 Q_D(const QQuickAnimator);
82 return d->target;
83}
84
85/*!
86 \qmlproperty int QtQuick::Animator::duration
87 This property holds the duration of the animation in milliseconds.
88
89 The default value is 250.
90*/
91void QQuickAnimator::setDuration(int duration)
92{
93 Q_D(QQuickAnimator);
94 if (duration == d->duration)
95 return;
96 d->duration = duration;
97 Q_EMIT durationChanged(duration);
98}
99
100int QQuickAnimator::duration() const
101{
102 Q_D(const QQuickAnimator);
103 return d->duration;
104}
105
106/*!
107 \qmlpropertygroup QtQuick::Animator::easing
108 \qmlproperty enumeration QtQuick::Animator::easing.type
109 \qmlproperty real QtQuick::Animator::easing.amplitude
110 \qmlproperty real QtQuick::Animator::easing.overshoot
111 \qmlproperty real QtQuick::Animator::easing.period
112 \qmlproperty list<real> QtQuick::Animator::easing.bezierCurve
113 \include qquickanimation.cpp propertyanimation.easing
114*/
115
116void QQuickAnimator::setEasing(const QEasingCurve &easing)
117{
118 Q_D(QQuickAnimator);
119 if (easing == d->easing)
120 return;
121 d->easing = easing;
122 Q_EMIT easingChanged(d->easing);
123}
124
125QEasingCurve QQuickAnimator::easing() const
126{
127 Q_D(const QQuickAnimator);
128 return d->easing;
129}
130
131/*!
132 \qmlproperty real QtQuick::Animator::to
133 This property holds the end value for the animation.
134
135 If the Animator is defined within a \l Transition or \l Behavior,
136 this value defaults to the value defined in the end state of the
137 \l Transition, or the value of the property change that triggered the
138 \l Behavior.
139 */
140
141void QQuickAnimator::setTo(qreal to)
142{
143 Q_D(QQuickAnimator);
144 if (to == d->to)
145 return;
146 d->toIsDefined = true;
147 d->to = to;
148 Q_EMIT toChanged(d->to);
149}
150
151qreal QQuickAnimator::to() const
152{
153 Q_D(const QQuickAnimator);
154 return d->to;
155}
156
157/*!
158 \qmlproperty real QtQuick::Animator::from
159 This property holds the starting value for the animation.
160
161 If the Animator is defined within a \l Transition or \l Behavior,
162 this value defaults to the value defined in the starting state of the
163 \l Transition, or the current value of the property at the moment the
164 \l Behavior is triggered.
165
166 \sa {Animation and Transitions in Qt Quick}
167*/
168
169void QQuickAnimator::setFrom(qreal from)
170{
171 Q_D(QQuickAnimator);
172 d->fromIsDefined = true;
173 if (from == d->from)
174 return;
175 d->from = from;
176 Q_EMIT fromChanged(d->from);
177}
178
179qreal QQuickAnimator::from() const
180{
181 Q_D(const QQuickAnimator);
182 return d->from;
183}
184
185void QQuickAnimatorPrivate::apply(QQuickAnimatorJob *job,
186 const QString &propertyName,
187 QQuickStateActions &actions,
188 QQmlProperties &modified,
189 QObject *defaultTarget)
190{
191
192 if (actions.size()) {
193 for (int i=0; i<actions.size(); ++i) {
194 QQuickStateAction &action = actions[i];
195 if (action.property.name() != propertyName)
196 continue;
197 modified << action.property;
198
199 job->setTarget(qobject_cast<QQuickItem *>(action.property.object()));
200
201 if (fromIsDefined)
202 job->setFrom(from);
203 else if (action.fromValue.isValid())
204 job->setFrom(action.fromValue.toReal());
205 else
206 job->setFrom(action.property.read().toReal());
207
208 if (toIsDefined)
209 job->setTo(to);
210 else if (action.toValue.isValid())
211 job->setTo(action.toValue.toReal());
212 else
213 job->setTo(action.property.read().toReal());
214
215 // This magic line is in sync with what PropertyAnimation does
216 // and prevents the animation to end up in the "completeList"
217 // which forces action.toValue to be written directly to
218 // the item when a transition is cancelled.
219 action.fromValue = action.toValue;
220 }
221 }
222
223 if (modified.isEmpty()) {
224 job->setTarget(target);
225 if (fromIsDefined)
226 job->setFrom(from);
227 job->setTo(to);
228 }
229
230 if (!job->target()) {
231 if (defaultProperty.object())
232 job->setTarget(qobject_cast<QQuickItem *>(defaultProperty.object()));
233 else
234 job->setTarget(qobject_cast<QQuickItem *>(defaultTarget));
235 }
236
237 if (modified.isEmpty() && !fromIsDefined && job->target())
238 job->setFrom(job->target()->property(propertyName.toLatin1()).toReal());
239
240 job->setDuration(duration);
241 job->setLoopCount(loopCount);
242 job->setEasingCurve(easing);
243}
244
245QAbstractAnimationJob *QQuickAnimator::transition(QQuickStateActions &actions,
246 QQmlProperties &modified,
247 TransitionDirection direction,
248 QObject *defaultTarget)
249{
250 Q_D(QQuickAnimator);
251
252 if (d->defaultProperty.isValid() && propertyName() != d->defaultProperty.name()) {
253 qmlWarning(this) << "property name conflict: \""
254 << propertyName() << "\" != \"" << d->defaultProperty.name() << "\"";
255 return nullptr;
256 }
257
258 // The animation system cannot handle backwards uncontrolled animations.
259 if (direction == Backward)
260 return nullptr;
261
262 QQuickAnimatorJob *job = createJob();
263 if (!job)
264 return nullptr;
265
266 d->apply(job, propertyName(), actions, modified, defaultTarget);
267
268 if (!job->target()) {
269 delete job;
270 return nullptr;
271 }
272
273 return job;
274}
275
276/*!
277 \qmltype XAnimator
278 \nativetype QQuickXAnimator
279 \inqmlmodule QtQuick
280 \since 5.2
281 \ingroup qtquick-transitions-animations
282 \inherits Animator
283 \brief The XAnimator type animates the x position of an Item.
284
285 \l{Animator} types are different from normal Animation types. When
286 using an Animator, the animation can be run in the render thread
287 and the property value will jump to the end when the animation is
288 complete.
289
290 The value of Item::x is updated after the animation has finished.
291
292 The following snippet shows how to use a XAnimator together
293 with a Rectangle item.
294
295 \snippet qml/animators.qml x target
296
297 It is also possible to use the \c on keyword to tie the
298 XAnimator directly to an Item instance.
299
300 \snippet qml/animators.qml x on
301
302 \include animatornotes.qdocinc
303 */
304
305QQuickXAnimator::QQuickXAnimator(QObject *parent) : QQuickAnimator(parent) {}
306
307QQuickAnimatorJob *QQuickXAnimator::createJob() const { return new QQuickXAnimatorJob(); }
308
309/*!
310 \qmltype YAnimator
311 \nativetype QQuickYAnimator
312 \inqmlmodule QtQuick
313 \since 5.2
314 \ingroup qtquick-transitions-animations
315 \inherits Animator
316 \brief The YAnimator type animates the y position of an Item.
317
318 \l{Animator} types are different from normal Animation types. When
319 using an Animator, the animation can be run in the render thread
320 and the property value will jump to the end when the animation is
321 complete.
322
323 The value of Item::y is updated after the animation has finished.
324
325 The following snippet shows how to use a YAnimator together
326 with a Rectangle item.
327
328 \snippet qml/animators.qml y target
329
330 It is also possible to use the \c on keyword to tie the
331 YAnimator directly to an Item instance.
332
333 \snippet qml/animators.qml y on
334
335 \include animatornotes.qdocinc
336 */
337
338QQuickYAnimator::QQuickYAnimator(QObject *parent) : QQuickAnimator(parent) {}
339
340QQuickAnimatorJob *QQuickYAnimator::createJob() const { return new QQuickYAnimatorJob(); }
341
342/*!
343 \qmltype ScaleAnimator
344 \nativetype QQuickScaleAnimator
345 \inqmlmodule QtQuick
346 \since 5.2
347 \ingroup qtquick-transitions-animations
348 \inherits Animator
349 \brief The ScaleAnimator type animates the scale factor of an Item.
350
351 \l{Animator} types are different from normal Animation types. When
352 using an Animator, the animation can be run in the render thread
353 and the property value will jump to the end when the animation is
354 complete.
355
356 The value of Item::scale is updated after the animation has finished.
357
358 The following snippet shows how to use a ScaleAnimator together
359 with a Rectangle item.
360
361 \snippet qml/animators.qml scale target
362
363 It is also possible to use the \c on keyword to tie the
364 ScaleAnimator directly to an Item instance.
365
366 \snippet qml/animators.qml scale on
367
368 \include animatornotes.qdocinc
369
370 \sa Item::transformOrigin, RotationAnimator
371 */
372
373QQuickScaleAnimator::QQuickScaleAnimator(QObject *parent) : QQuickAnimator(parent) {}
374
375QQuickAnimatorJob *QQuickScaleAnimator::createJob() const { return new QQuickScaleAnimatorJob(); }
376
377/*!
378 \qmltype OpacityAnimator
379 \nativetype QQuickOpacityAnimator
380 \inqmlmodule QtQuick
381 \since 5.2
382 \ingroup qtquick-transitions-animations
383 \inherits Animator
384 \brief The OpacityAnimator type animates the opacity of an Item.
385
386 \l{Animator} types are different from normal Animation types. When
387 using an Animator, the animation can be run in the render thread
388 and the property value will jump to the end when the animation is
389 complete.
390
391 The value of Item::opacity is updated after the animation has finished.
392
393 The following snippet shows how to use a OpacityAnimator together
394 with a Rectangle item.
395
396 \snippet qml/animators.qml opacity target
397
398 It is also possible to use the \c on keyword to tie the
399 OpacityAnimator directly to an Item instance.
400
401 \snippet qml/animators.qml opacity on
402
403 \include animatornotes.qdocinc
404 */
405
406QQuickOpacityAnimator::QQuickOpacityAnimator(QObject *parent) : QQuickAnimator(parent) {}
407
408QQuickAnimatorJob *QQuickOpacityAnimator::createJob() const { return new QQuickOpacityAnimatorJob(); }
409
410/*!
411 \qmltype RotationAnimator
412 \nativetype QQuickRotationAnimator
413 \inqmlmodule QtQuick
414 \since 5.2
415 \ingroup qtquick-transitions-animations
416 \inherits Animator
417 \brief The RotationAnimator type animates the rotation of an Item.
418
419 \l{Animator} types are different from normal Animation types. When
420 using an Animator, the animation can be run in the render thread
421 and the property value will jump to the end when the animation is
422 complete.
423
424 The value of Item::rotation is updated after the animation has finished.
425
426 The following snippet shows how to use a RotationAnimator together
427 with a Rectangle item.
428
429 \snippet qml/animators.qml rotation target
430
431 It is also possible to use the \c on keyword to tie the
432 RotationAnimator directly to the \c rotation property of an Item
433 instance.
434
435 \snippet qml/animators.qml rotation on
436
437 \include animatornotes.qdocinc
438
439 \sa Item::transformOrigin, ScaleAnimator
440 */
441
442QQuickRotationAnimator::QQuickRotationAnimator(QObject *parent)
443 : QQuickAnimator(*new QQuickRotationAnimatorPrivate, parent)
444{
445}
446
447QQuickAnimatorJob *QQuickRotationAnimator::createJob() const {
448 Q_D(const QQuickRotationAnimator);
449 QQuickRotationAnimatorJob *job = new QQuickRotationAnimatorJob();
450 job->setDirection(d->direction);
451 return job;
452}
453
454/*!
455 \qmlproperty enumeration QtQuick::RotationAnimator::direction
456 This property holds the direction of the rotation.
457
458 Possible values are:
459
460 \value RotationAnimator.Numerical
461 (default) Rotate by linearly interpolating between the two numbers.
462 A rotation from 10 to 350 will rotate 340 degrees clockwise.
463 \value RotationAnimator.Clockwise
464 Rotate clockwise between the two values
465 \value RotationAnimator.Counterclockwise
466 Rotate counterclockwise between the two values
467 \value RotationAnimator.Shortest
468 Rotate in the direction that produces the shortest animation path.
469 A rotation from 10 to 350 will rotate 20 degrees counterclockwise.
470*/
471void QQuickRotationAnimator::setDirection(RotationDirection dir)
472{
473 Q_D(QQuickRotationAnimator);
474 if (d->direction == dir)
475 return;
476 d->direction = dir;
477 Q_EMIT directionChanged(d->direction);
478}
479
480QQuickRotationAnimator::RotationDirection QQuickRotationAnimator::direction() const
481{
482 Q_D(const QQuickRotationAnimator);
483 return d->direction;
484}
485
486#if QT_CONFIG(quick_shadereffect)
487/*!
488 \qmltype UniformAnimator
489 \nativetype QQuickUniformAnimator
490 \inqmlmodule QtQuick
491 \since 5.2
492 \ingroup qtquick-transitions-animations
493 \inherits Animator
494 \brief The UniformAnimator type animates a uniform of a ShaderEffect.
495
496 \l{Animator} types are different from normal Animation types. When
497 using an Animator, the animation can be run in the render thread
498 and the property value will jump to the end when the animation is
499 complete.
500
501 The value of the QML property defining the uniform is updated after
502 the animation has finished.
503
504 The following snippet shows how to use a UniformAnimator together
505 with a ShaderEffect item.
506
507 \snippet qml/animators.qml shader target
508
509 It is also possible to use the \c on keyword to tie the
510 UniformAnimator directly to a uniform of a ShaderEffect
511 instance.
512
513 \snippet qml/animators.qml shader on
514
515 \include animatornotes.qdocinc
516
517 \sa ShaderEffect, ShaderEffectSource
518 */
519
520QQuickUniformAnimator::QQuickUniformAnimator(QObject *parent)
521 : QQuickAnimator(*new QQuickUniformAnimatorPrivate, parent)
522{
523}
524
525/*!
526 \qmlproperty string QtQuick::UniformAnimator::uniform
527 This property holds the name of the uniform to animate.
528
529 The value of the uniform must correspond to both a property
530 on the target ShaderEffect and must be a uniform of type
531 \c float in the fragment or vertex shader.
532 */
533void QQuickUniformAnimator::setUniform(const QString &uniform)
534{
535 Q_D(QQuickUniformAnimator);
536 if (d->uniform == uniform)
537 return;
538 d->uniform = uniform;
539 Q_EMIT uniformChanged(d->uniform);
540}
541
542QString QQuickUniformAnimator::uniform() const
543{
544 Q_D(const QQuickUniformAnimator);
545 return d->uniform;
546}
547
548QString QQuickUniformAnimator::propertyName() const
549{
550 Q_D(const QQuickUniformAnimator);
551 if (!d->uniform.isEmpty())
552 return d->uniform;
553 return d->defaultProperty.name();
554}
555
556QQuickAnimatorJob *QQuickUniformAnimator::createJob() const
557{
558 QString u = propertyName();
559 if (u.isEmpty())
560 return nullptr;
561
562 QQuickUniformAnimatorJob *job = new QQuickUniformAnimatorJob();
563 job->setUniform(u.toLatin1());
564 return job;
565}
566#endif
567
568QT_END_NAMESPACE
569
570#include "moc_qquickanimator_p.cpp"
void apply(QQuickAnimatorJob *job, const QString &propertyName, QQuickStateActions &actions, QQmlProperties &modified, QObject *defaultTarget)
Combined button and popup list for selecting options.
QList< QQmlProperty > QQmlProperties
QQuickStateOperation::ActionList QQuickStateActions