1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "QtQuick1/private/qdeclarativeanimation_p.h"
43 #include "QtQuick1/private/qdeclarativeanimation_p_p.h"
45 #include "QtQuick1/private/qdeclarativebehavior_p.h"
46 #include "QtQuick1/private/qdeclarativestateoperations_p.h"
47 #include "QtDeclarative/private/qdeclarativecontext_p.h"
49 #include <QtDeclarative/qdeclarativepropertyvaluesource.h>
50 #include <QtDeclarative/qdeclarative.h>
51 #include <QtDeclarative/qdeclarativeinfo.h>
52 #include <QtDeclarative/qdeclarativeexpression.h>
53 #include <QtDeclarative/private/qdeclarativestringconverters_p.h>
54 #include <QtDeclarative/private/qdeclarativeglobal_p.h>
55 #include <QtDeclarative/private/qdeclarativemetatype_p.h>
56 #include <QtDeclarative/private/qdeclarativevaluetype_p.h>
57 #include <QtDeclarative/private/qdeclarativeproperty_p.h>
58 #include <QtDeclarative/private/qdeclarativeengine_p.h>
63 #include <QParallelAnimationGroup>
64 #include <QSequentialAnimationGroup>
65 #include <QtCore/qset.h>
66 #include <QtCore/qrect.h>
67 #include <QtCore/qpoint.h>
68 #include <QtCore/qsize.h>
69 #include <QtCore/qmath.h>
71 #include <private/qvariantanimation_p.h>
78 \qmlclass Animation QDeclarative1AbstractAnimation
79 \inqmlmodule QtQuick 1
80 \ingroup qml-animation-transition
82 \brief The Animation element is the base of all QML animations.
84 The Animation element cannot be used directly in a QML file. It exists
85 to provide a set of common properties and methods, available across all the
86 other animation types that inherit from it. Attempting to use the Animation
87 element directly will result in an error.
90 QDeclarative1AbstractAnimation::QDeclarative1AbstractAnimation(QObject *parent)
91 : QObject(*(new QDeclarative1AbstractAnimationPrivate), parent)
95 QDeclarative1AbstractAnimation::~QDeclarative1AbstractAnimation()
99 QDeclarative1AbstractAnimation::QDeclarative1AbstractAnimation(QDeclarative1AbstractAnimationPrivate &dd, QObject *parent)
100 : QObject(dd, parent)
105 \qmlproperty bool QtQuick1::Animation::running
106 This property holds whether the animation is currently running.
108 The \c running property can be set to declaratively control whether or not
109 an animation is running. The following example will animate a rectangle
110 whenever the \l MouseArea is pressed.
114 width: 100; height: 100
115 NumberAnimation on x {
116 running: myMouse.pressed
119 MouseArea { id: myMouse }
123 Likewise, the \c running property can be read to determine if the animation
124 is running. In the following example the text element will indicate whether
125 or not the animation is running.
128 NumberAnimation { id: myAnimation }
129 Text { text: myAnimation.running ? "Animation is running" : "Animation is not running" }
132 Animations can also be started and stopped imperatively from JavaScript
133 using the \c start() and \c stop() methods.
135 By default, animations are not running. Though, when the animations are assigned to properties,
136 as property value sources using the \e on syntax, they are set to running by default.
138 bool QDeclarative1AbstractAnimation::isRunning() const
140 Q_D(const QDeclarative1AbstractAnimation);
144 // the behavior calls this function
145 void QDeclarative1AbstractAnimation::notifyRunningChanged(bool running)
147 Q_D(QDeclarative1AbstractAnimation);
148 if (d->disableUserControl && d->running != running) {
149 d->running = running;
150 emit runningChanged(running);
154 //commence is called to start an animation when it is used as a
155 //simple animation, and not as part of a transition
156 void QDeclarative1AbstractAnimationPrivate::commence()
158 Q_Q(QDeclarative1AbstractAnimation);
160 QDeclarative1StateActions actions;
161 QDeclarativeProperties properties;
162 q->transition(actions, properties, QDeclarative1AbstractAnimation::Forward);
164 q->qtAnimation()->start();
165 if (q->qtAnimation()->state() == QAbstractAnimation::Stopped) {
171 QDeclarativeProperty QDeclarative1AbstractAnimationPrivate::createProperty(QObject *obj, const QString &str, QObject *infoObj)
173 QDeclarativeProperty prop(obj, str, qmlContext(infoObj));
174 if (!prop.isValid()) {
175 qmlInfo(infoObj) << QDeclarative1AbstractAnimation::tr("Cannot animate non-existent property \"%1\"").arg(str);
176 return QDeclarativeProperty();
177 } else if (!prop.isWritable()) {
178 qmlInfo(infoObj) << QDeclarative1AbstractAnimation::tr("Cannot animate read-only property \"%1\"").arg(str);
179 return QDeclarativeProperty();
184 void QDeclarative1AbstractAnimation::setRunning(bool r)
186 Q_D(QDeclarative1AbstractAnimation);
187 if (!d->componentComplete) {
190 d->avoidPropertyValueSourceStart = true;
191 else if (!d->registered) {
192 d->registered = true;
193 QDeclarativeEnginePrivate *engPriv = QDeclarativeEnginePrivate::get(qmlEngine(this));
194 engPriv->registerFinalizeCallback(this, this->metaObject()->indexOfSlot("componentFinalized()"));
202 if (d->group || d->disableUserControl) {
203 qmlInfo(this) << "setRunning() cannot be used on non-root animation nodes.";
209 bool supressStart = false;
210 if (d->alwaysRunToEnd && d->loopCount != 1
211 && qtAnimation()->state() == QAbstractAnimation::Running) {
212 //we've restarted before the final loop finished; restore proper loop count
213 if (d->loopCount == -1)
214 qtAnimation()->setLoopCount(d->loopCount);
216 qtAnimation()->setLoopCount(qtAnimation()->currentLoop() + d->loopCount);
217 supressStart = true; //we want the animation to continue, rather than restart
220 if (!d->connectedTimeLine) {
221 QObject::connect(qtAnimation(), SIGNAL(finished()),
222 this, SLOT(timelineComplete()));
223 d->connectedTimeLine = true;
229 if (d->alwaysRunToEnd) {
230 if (d->loopCount != 1)
231 qtAnimation()->setLoopCount(qtAnimation()->currentLoop()+1); //finish the current loop
233 qtAnimation()->stop();
238 emit runningChanged(d->running);
242 \qmlproperty bool QtQuick1::Animation::paused
243 This property holds whether the animation is currently paused.
245 The \c paused property can be set to declaratively control whether or not
246 an animation is paused.
248 Animations can also be paused and resumed imperatively from JavaScript
249 using the \c pause() and \c resume() methods.
251 By default, animations are not paused.
253 bool QDeclarative1AbstractAnimation::isPaused() const
255 Q_D(const QDeclarative1AbstractAnimation);
259 void QDeclarative1AbstractAnimation::setPaused(bool p)
261 Q_D(QDeclarative1AbstractAnimation);
265 if (d->group || d->disableUserControl) {
266 qmlInfo(this) << "setPaused() cannot be used on non-root animation nodes.";
272 qtAnimation()->pause();
274 qtAnimation()->resume();
276 emit pausedChanged(d->paused);
279 void QDeclarative1AbstractAnimation::classBegin()
281 Q_D(QDeclarative1AbstractAnimation);
282 d->componentComplete = false;
285 void QDeclarative1AbstractAnimation::componentComplete()
287 Q_D(QDeclarative1AbstractAnimation);
288 d->componentComplete = true;
291 void QDeclarative1AbstractAnimation::componentFinalized()
293 Q_D(QDeclarative1AbstractAnimation);
301 \qmlproperty bool QtQuick1::Animation::alwaysRunToEnd
302 This property holds whether the animation should run to completion when it is stopped.
304 If this true the animation will complete its current iteration when it
305 is stopped - either by setting the \c running property to false, or by
306 calling the \c stop() method. The \c complete() method is not effected
309 This behavior is most useful when the \c repeat property is set, as the
310 animation will finish playing normally but not restart.
312 By default, the alwaysRunToEnd property is not set.
314 \note alwaysRunToEnd has no effect on animations in a Transition.
316 bool QDeclarative1AbstractAnimation::alwaysRunToEnd() const
318 Q_D(const QDeclarative1AbstractAnimation);
319 return d->alwaysRunToEnd;
322 void QDeclarative1AbstractAnimation::setAlwaysRunToEnd(bool f)
324 Q_D(QDeclarative1AbstractAnimation);
325 if (d->alwaysRunToEnd == f)
328 d->alwaysRunToEnd = f;
329 emit alwaysRunToEndChanged(f);
333 \qmlproperty int QtQuick1::Animation::loops
334 This property holds the number of times the animation should play.
336 By default, \c loops is 1: the animation will play through once and then stop.
338 If set to Animation.Infinite, the animation will continuously repeat until it is explicitly
339 stopped - either by setting the \c running property to false, or by calling
340 the \c stop() method.
342 In the following example, the rectangle will spin indefinitely.
346 width: 100; height: 100; color: "green"
347 RotationAnimation on rotation {
348 loops: Animation.Infinite
355 int QDeclarative1AbstractAnimation::loops() const
357 Q_D(const QDeclarative1AbstractAnimation);
361 void QDeclarative1AbstractAnimation::setLoops(int loops)
363 Q_D(QDeclarative1AbstractAnimation);
367 if (loops == d->loopCount)
370 d->loopCount = loops;
371 qtAnimation()->setLoopCount(loops);
372 emit loopCountChanged(loops);
376 int QDeclarative1AbstractAnimation::currentTime()
378 return qtAnimation()->currentLoopTime();
381 void QDeclarative1AbstractAnimation::setCurrentTime(int time)
383 qtAnimation()->setCurrentTime(time);
386 QDeclarative1AnimationGroup *QDeclarative1AbstractAnimation::group() const
388 Q_D(const QDeclarative1AbstractAnimation);
392 void QDeclarative1AbstractAnimation::setGroup(QDeclarative1AnimationGroup *g)
394 Q_D(QDeclarative1AbstractAnimation);
398 static_cast<QDeclarative1AnimationGroupPrivate *>(d->group->d_func())->animations.removeAll(this);
402 if (d->group && !static_cast<QDeclarative1AnimationGroupPrivate *>(d->group->d_func())->animations.contains(this))
403 static_cast<QDeclarative1AnimationGroupPrivate *>(d->group->d_func())->animations.append(this);
405 //if (g) //if removed from a group, then the group should no longer be the parent
410 \qmlmethod QtQuick1::Animation::start()
411 \brief Starts the animation.
413 If the animation is already running, calling this method has no effect. The
414 \c running property will be true following a call to \c start().
416 void QDeclarative1AbstractAnimation::start()
422 \qmlmethod QtQuick1::Animation::pause()
423 \brief Pauses the animation.
425 If the animation is already paused, calling this method has no effect. The
426 \c paused property will be true following a call to \c pause().
428 void QDeclarative1AbstractAnimation::pause()
434 \qmlmethod QtQuick1::Animation::resume()
435 \brief Resumes a paused animation.
437 If the animation is not paused, calling this method has no effect. The
438 \c paused property will be false following a call to \c resume().
440 void QDeclarative1AbstractAnimation::resume()
446 \qmlmethod QtQuick1::Animation::stop()
447 \brief Stops the animation.
449 If the animation is not running, calling this method has no effect. The
450 \c running property will be false following a call to \c stop().
452 Normally \c stop() stops the animation immediately, and the animation has
453 no further influence on property values. In this example animation
456 NumberAnimation on x { from: 0; to: 100; duration: 500 }
459 was stopped at time 250ms, the \c x property will have a value of 50.
461 However, if the \c alwaysRunToEnd property is set, the animation will
462 continue running until it completes and then stop. The \c running property
463 will still become false immediately.
465 void QDeclarative1AbstractAnimation::stop()
471 \qmlmethod QtQuick1::Animation::restart()
472 \brief Restarts the animation.
474 This is a convenience method, and is equivalent to calling \c stop() and
477 void QDeclarative1AbstractAnimation::restart()
484 \qmlmethod QtQuick1::Animation::complete()
485 \brief Stops the animation, jumping to the final property values.
487 If the animation is not running, calling this method has no effect. The
488 \c running property will be false following a call to \c complete().
490 Unlike \c stop(), \c complete() immediately fast-forwards the animation to
491 its end. In the following example,
494 NumberAnimation on x { from: 0; to: 100; duration: 500 }
497 calling \c stop() at time 250ms will result in the \c x property having
498 a value of 50, while calling \c complete() will set the \c x property to
499 100, exactly as though the animation had played the whole way through.
501 void QDeclarative1AbstractAnimation::complete()
504 qtAnimation()->setCurrentTime(qtAnimation()->duration());
508 void QDeclarative1AbstractAnimation::setTarget(const QDeclarativeProperty &p)
510 Q_D(QDeclarative1AbstractAnimation);
511 d->defaultProperty = p;
513 if (!d->avoidPropertyValueSourceStart)
518 we rely on setTarget only being called when used as a value source
519 so this function allows us to do the same thing as setTarget without
522 void QDeclarative1AbstractAnimation::setDefaultTarget(const QDeclarativeProperty &p)
524 Q_D(QDeclarative1AbstractAnimation);
525 d->defaultProperty = p;
529 don't allow start/stop/pause/resume to be manually invoked,
530 because something else (like a Behavior) already has control
533 void QDeclarative1AbstractAnimation::setDisableUserControl()
535 Q_D(QDeclarative1AbstractAnimation);
536 d->disableUserControl = true;
539 void QDeclarative1AbstractAnimation::transition(QDeclarative1StateActions &actions,
540 QDeclarativeProperties &modified,
541 TransitionDirection direction)
548 void QDeclarative1AbstractAnimation::timelineComplete()
550 Q_D(QDeclarative1AbstractAnimation);
552 if (d->alwaysRunToEnd && d->loopCount != 1) {
553 //restore the proper loopCount for the next run
554 qtAnimation()->setLoopCount(d->loopCount);
559 \qmlclass PauseAnimation QDeclarative1PauseAnimation
560 \inqmlmodule QtQuick 1
561 \ingroup qml-animation-transition
564 \brief The PauseAnimation element provides a pause for an animation.
566 When used in a SequentialAnimation, PauseAnimation is a step when
567 nothing happens, for a specified duration.
569 A 500ms animation sequence, with a 100ms pause between two animations:
571 SequentialAnimation {
572 NumberAnimation { ... duration: 200 }
573 PauseAnimation { duration: 100 }
574 NumberAnimation { ... duration: 200 }
578 \sa {QML Animation and Transitions}, {declarative/animation/basics}{Animation basics example}
580 QDeclarative1PauseAnimation::QDeclarative1PauseAnimation(QObject *parent)
581 : QDeclarative1AbstractAnimation(*(new QDeclarative1PauseAnimationPrivate), parent)
583 Q_D(QDeclarative1PauseAnimation);
587 QDeclarative1PauseAnimation::~QDeclarative1PauseAnimation()
591 void QDeclarative1PauseAnimationPrivate::init()
593 Q_Q(QDeclarative1PauseAnimation);
594 pa = new QPauseAnimation;
595 QDeclarative_setParent_noEvent(pa, q);
599 \qmlproperty int QtQuick1::PauseAnimation::duration
600 This property holds the duration of the pause in milliseconds
602 The default value is 250.
604 int QDeclarative1PauseAnimation::duration() const
606 Q_D(const QDeclarative1PauseAnimation);
607 return d->pa->duration();
610 void QDeclarative1PauseAnimation::setDuration(int duration)
613 qmlInfo(this) << tr("Cannot set a duration of < 0");
617 Q_D(QDeclarative1PauseAnimation);
618 if (d->pa->duration() == duration)
620 d->pa->setDuration(duration);
621 emit durationChanged(duration);
624 QAbstractAnimation *QDeclarative1PauseAnimation::qtAnimation()
626 Q_D(QDeclarative1PauseAnimation);
631 \qmlclass ColorAnimation QDeclarative1ColorAnimation
632 \inqmlmodule QtQuick 1
633 \ingroup qml-animation-transition
635 \inherits PropertyAnimation
636 \brief The ColorAnimation element animates changes in color values.
638 ColorAnimation is a specialized PropertyAnimation that defines an
639 animation to be applied when a color value changes.
641 Here is a ColorAnimation applied to the \c color property of a \l Rectangle
642 as a property value source. It animates the \c color property's value from
643 its current value to a value of "red", over 1000 milliseconds:
645 \snippet doc/src/snippets/qtquick1/coloranimation.qml 0
647 Like any other animation element, a ColorAnimation can be applied in a
648 number of ways, including transitions, behaviors and property value
649 sources. The \l {QML Animation and Transitions} documentation shows a
650 variety of methods for creating animations.
652 For convenience, when a ColorAnimation is used in a \l Transition, it will
653 animate any \c color properties that have been modified during the state
654 change. If a \l{PropertyAnimation::}{property} or
655 \l{PropertyAnimation::}{properties} are explicitly set for the animation,
656 then those are used instead.
658 \sa {QML Animation and Transitions}, {declarative/animation/basics}{Animation basics example}
660 QDeclarative1ColorAnimation::QDeclarative1ColorAnimation(QObject *parent)
661 : QDeclarative1PropertyAnimation(parent)
663 Q_D(QDeclarative1PropertyAnimation);
664 d->interpolatorType = QMetaType::QColor;
665 d->interpolator = QVariantAnimationPrivate::getInterpolator(d->interpolatorType);
666 d->defaultToInterpolatorType = true;
669 QDeclarative1ColorAnimation::~QDeclarative1ColorAnimation()
674 \qmlproperty color QtQuick1::ColorAnimation::from
675 This property holds the color value at which the animation should begin.
677 For example, the following animation is not applied until a color value
678 has reached "#c0c0c0":
683 // States are defined here...
686 transition: Transition {
687 NumberAnimation { from: "#c0c0c0"; duration: 2000 }
692 If the ColorAnimation is defined within a \l Transition or \l Behavior,
693 this value defaults to the value defined in the starting state of the
694 \l Transition, or the current value of the property at the moment the
695 \l Behavior is triggered.
697 \sa {QML Animation and Transitions}
699 QColor QDeclarative1ColorAnimation::from() const
701 Q_D(const QDeclarative1PropertyAnimation);
702 return d->from.value<QColor>();
705 void QDeclarative1ColorAnimation::setFrom(const QColor &f)
707 QDeclarative1PropertyAnimation::setFrom(f);
711 \qmlproperty color QtQuick1::ColorAnimation::to
713 This property holds the color value at which the animation should end.
715 If the ColorAnimation is defined within a \l Transition or \l Behavior,
716 this value defaults to the value defined in the end state of the
717 \l Transition, or the value of the property change that triggered the
720 \sa {QML Animation and Transitions}
722 QColor QDeclarative1ColorAnimation::to() const
724 Q_D(const QDeclarative1PropertyAnimation);
725 return d->to.value<QColor>();
728 void QDeclarative1ColorAnimation::setTo(const QColor &t)
730 QDeclarative1PropertyAnimation::setTo(t);
736 \qmlclass ScriptAction QDeclarative1ScriptAction
737 \inqmlmodule QtQuick 1
738 \ingroup qml-animation-transition
741 \brief The ScriptAction element allows scripts to be run during an animation.
743 ScriptAction can be used to run a script at a specific point in an animation.
746 SequentialAnimation {
750 ScriptAction { script: doSomething(); }
757 When used as part of a Transition, you can also target a specific
758 StateChangeScript to run using the \c scriptName property.
760 \snippet doc/src/snippets/qtquick1/states/statechangescript.qml state and transition
762 \sa StateChangeScript
764 QDeclarative1ScriptAction::QDeclarative1ScriptAction(QObject *parent)
765 :QDeclarative1AbstractAnimation(*(new QDeclarative1ScriptActionPrivate), parent)
767 Q_D(QDeclarative1ScriptAction);
771 QDeclarative1ScriptAction::~QDeclarative1ScriptAction()
775 void QDeclarative1ScriptActionPrivate::init()
777 Q_Q(QDeclarative1ScriptAction);
778 rsa = new QActionAnimation_1(&proxy);
779 QDeclarative_setParent_noEvent(rsa, q);
783 \qmlproperty script QtQuick1::ScriptAction::script
784 This property holds the script to run.
786 QDeclarativeScriptString QDeclarative1ScriptAction::script() const
788 Q_D(const QDeclarative1ScriptAction);
792 void QDeclarative1ScriptAction::setScript(const QDeclarativeScriptString &script)
794 Q_D(QDeclarative1ScriptAction);
799 \qmlproperty string QtQuick1::ScriptAction::scriptName
800 This property holds the the name of the StateChangeScript to run.
802 This property is only valid when ScriptAction is used as part of a transition.
803 If both script and scriptName are set, scriptName will be used.
805 \note When using scriptName in a reversible transition, the script will only
806 be run when the transition is being run forwards.
808 QString QDeclarative1ScriptAction::stateChangeScriptName() const
810 Q_D(const QDeclarative1ScriptAction);
814 void QDeclarative1ScriptAction::setStateChangeScriptName(const QString &name)
816 Q_D(QDeclarative1ScriptAction);
820 void QDeclarative1ScriptActionPrivate::execute()
822 Q_Q(QDeclarative1ScriptAction);
823 if (hasRunScriptScript && reversing)
826 QDeclarativeScriptString scriptStr = hasRunScriptScript ? runScriptScript : script;
828 const QString &str = scriptStr.script();
829 if (!str.isEmpty()) {
830 QDeclarativeExpression expr(scriptStr.context(), scriptStr.scopeObject(), str);
831 QDeclarativeData *ddata = QDeclarativeData::get(q);
832 if (ddata && ddata->outerContext && !ddata->outerContext->url.isEmpty())
833 expr.setSourceLocation(ddata->outerContext->url.toString(), ddata->lineNumber, ddata->columnNumber);
836 qmlInfo(q) << expr.error();
840 void QDeclarative1ScriptAction::transition(QDeclarative1StateActions &actions,
841 QDeclarativeProperties &modified,
842 TransitionDirection direction)
844 Q_D(QDeclarative1ScriptAction);
847 d->hasRunScriptScript = false;
848 d->reversing = (direction == Backward);
849 for (int ii = 0; ii < actions.count(); ++ii) {
850 QDeclarative1Action &action = actions[ii];
852 if (action.event && action.event->typeName() == QLatin1String("StateChangeScript")
853 && static_cast<QDeclarative1StateChangeScript*>(action.event)->name() == d->name) {
854 d->runScriptScript = static_cast<QDeclarative1StateChangeScript*>(action.event)->script();
855 d->hasRunScriptScript = true;
856 action.actionDone = true;
857 break; //only match one (names should be unique)
862 QAbstractAnimation *QDeclarative1ScriptAction::qtAnimation()
864 Q_D(QDeclarative1ScriptAction);
871 \qmlclass PropertyAction QDeclarative1PropertyAction
872 \inqmlmodule QtQuick 1
873 \ingroup qml-animation-transition
876 \brief The PropertyAction element allows immediate property changes during animation.
878 PropertyAction is used to specify an immediate property change during an
879 animation. The property change is not animated.
881 It is useful for setting non-animated property values during an animation.
883 For example, here is a SequentialAnimation that sets the image's
884 \l {Image::}{smooth} property to \c true, animates the width of the image,
885 then sets \l {Image::}{smooth} back to \c false:
887 \snippet doc/src/snippets/qtquick1/propertyaction.qml standalone
889 PropertyAction is also useful for setting the exact point at which a property
890 change should occur during a \l Transition. For example, if PropertyChanges
891 was used in a \l State to rotate an item around a particular
892 \l {Item::}{transformOrigin}, it might be implemented like this:
894 \snippet doc/src/snippets/qtquick1/propertyaction.qml transition
896 However, with this code, the \c transformOrigin is not set until \e after
897 the animation, as a \l State is taken to define the values at the \e end of
898 a transition. The animation would rotate at the default \c transformOrigin,
899 then jump to \c Item.BottomRight. To fix this, insert a PropertyAction
900 before the RotationAnimation begins:
902 \snippet doc/src/snippets/qtquick1/propertyaction-sequential.qml sequential
904 This immediately sets the \c transformOrigin property to the value defined
905 in the end state of the \l Transition (i.e. the value defined in the
906 PropertyAction object) so that the rotation animation begins with the
907 correct transform origin.
909 \sa {QML Animation and Transitions}, QtDeclarative
911 QDeclarative1PropertyAction::QDeclarative1PropertyAction(QObject *parent)
912 : QDeclarative1AbstractAnimation(*(new QDeclarative1PropertyActionPrivate), parent)
914 Q_D(QDeclarative1PropertyAction);
918 QDeclarative1PropertyAction::~QDeclarative1PropertyAction()
922 void QDeclarative1PropertyActionPrivate::init()
924 Q_Q(QDeclarative1PropertyAction);
925 spa = new QActionAnimation_1;
926 QDeclarative_setParent_noEvent(spa, q);
929 QObject *QDeclarative1PropertyAction::target() const
931 Q_D(const QDeclarative1PropertyAction);
935 void QDeclarative1PropertyAction::setTarget(QObject *o)
937 Q_D(QDeclarative1PropertyAction);
941 emit targetChanged();
944 QString QDeclarative1PropertyAction::property() const
946 Q_D(const QDeclarative1PropertyAction);
947 return d->propertyName;
950 void QDeclarative1PropertyAction::setProperty(const QString &n)
952 Q_D(QDeclarative1PropertyAction);
953 if (d->propertyName == n)
956 emit propertyChanged();
960 \qmlproperty Object QtQuick1::PropertyAction::target
961 \qmlproperty list<Object> QtQuick1::PropertyAction::targets
962 \qmlproperty string QtQuick1::PropertyAction::property
963 \qmlproperty string QtQuick1::PropertyAction::properties
965 These properties determine the items and their properties that are
966 affected by this action.
968 The details of how these properties are interpreted in different situations
969 is covered in the \l{PropertyAnimation::properties}{corresponding} PropertyAnimation
974 QString QDeclarative1PropertyAction::properties() const
976 Q_D(const QDeclarative1PropertyAction);
977 return d->properties;
980 void QDeclarative1PropertyAction::setProperties(const QString &p)
982 Q_D(QDeclarative1PropertyAction);
983 if (d->properties == p)
986 emit propertiesChanged(p);
989 QDeclarativeListProperty<QObject> QDeclarative1PropertyAction::targets()
991 Q_D(QDeclarative1PropertyAction);
992 return QDeclarativeListProperty<QObject>(this, d->targets);
996 \qmlproperty list<Object> QtQuick1::PropertyAction::exclude
997 This property holds the objects that should not be affected by this action.
1001 QDeclarativeListProperty<QObject> QDeclarative1PropertyAction::exclude()
1003 Q_D(QDeclarative1PropertyAction);
1004 return QDeclarativeListProperty<QObject>(this, d->exclude);
1008 \qmlproperty any QtQuick1::PropertyAction::value
1009 This property holds the value to be set on the property.
1011 If the PropertyAction is defined within a \l Transition or \l Behavior,
1012 this value defaults to the value defined in the end state of the
1013 \l Transition, or the value of the property change that triggered the
1016 QVariant QDeclarative1PropertyAction::value() const
1018 Q_D(const QDeclarative1PropertyAction);
1022 void QDeclarative1PropertyAction::setValue(const QVariant &v)
1024 Q_D(QDeclarative1PropertyAction);
1025 if (d->value.isNull || d->value != v) {
1027 emit valueChanged(v);
1031 QAbstractAnimation *QDeclarative1PropertyAction::qtAnimation()
1033 Q_D(QDeclarative1PropertyAction);
1037 void QDeclarative1PropertyAction::transition(QDeclarative1StateActions &actions,
1038 QDeclarativeProperties &modified,
1039 TransitionDirection direction)
1041 Q_D(QDeclarative1PropertyAction);
1042 Q_UNUSED(direction);
1044 struct QDeclarative1SetPropertyAnimationAction : public QAbstractAnimationAction
1046 QDeclarative1StateActions actions;
1047 virtual void doAction()
1049 for (int ii = 0; ii < actions.count(); ++ii) {
1050 const QDeclarative1Action &action = actions.at(ii);
1051 QDeclarativePropertyPrivate::write(action.property, action.toValue, QDeclarativePropertyPrivate::BypassInterceptor | QDeclarativePropertyPrivate::DontRemoveBinding);
1056 QStringList props = d->properties.isEmpty() ? QStringList() : d->properties.split(QLatin1Char(','));
1057 for (int ii = 0; ii < props.count(); ++ii)
1058 props[ii] = props.at(ii).trimmed();
1059 if (!d->propertyName.isEmpty())
1060 props << d->propertyName;
1062 QList<QObject*> targets = d->targets;
1064 targets.append(d->target);
1066 bool hasSelectors = !props.isEmpty() || !targets.isEmpty() || !d->exclude.isEmpty();
1068 if (d->defaultProperty.isValid() && !hasSelectors) {
1069 props << d->defaultProperty.name();
1070 targets << d->defaultProperty.object();
1073 QDeclarative1SetPropertyAnimationAction *data = new QDeclarative1SetPropertyAnimationAction;
1075 bool hasExplicit = false;
1076 //an explicit animation has been specified
1077 if (d->value.isValid()) {
1078 for (int i = 0; i < props.count(); ++i) {
1079 for (int j = 0; j < targets.count(); ++j) {
1080 QDeclarative1Action myAction;
1081 myAction.property = d->createProperty(targets.at(j), props.at(i), this);
1082 if (myAction.property.isValid()) {
1083 myAction.toValue = d->value;
1084 QDeclarative1PropertyAnimationPrivate::convertVariant(myAction.toValue, myAction.property.propertyType());
1085 data->actions << myAction;
1087 for (int ii = 0; ii < actions.count(); ++ii) {
1088 QDeclarative1Action &action = actions[ii];
1089 if (action.property.object() == myAction.property.object() &&
1090 myAction.property.name() == action.property.name()) {
1091 modified << action.property;
1092 break; //### any chance there could be multiples?
1101 for (int ii = 0; ii < actions.count(); ++ii) {
1102 QDeclarative1Action &action = actions[ii];
1104 QObject *obj = action.property.object();
1105 QString propertyName = action.property.name();
1106 QObject *sObj = action.specifiedObject;
1107 QString sPropertyName = action.specifiedProperty;
1108 bool same = (obj == sObj);
1110 if ((targets.isEmpty() || targets.contains(obj) || (!same && targets.contains(sObj))) &&
1111 (!d->exclude.contains(obj)) && (same || (!d->exclude.contains(sObj))) &&
1112 (props.contains(propertyName) || (!same && props.contains(sPropertyName)))) {
1113 QDeclarative1Action myAction = action;
1115 if (d->value.isValid())
1116 myAction.toValue = d->value;
1117 QDeclarative1PropertyAnimationPrivate::convertVariant(myAction.toValue, myAction.property.propertyType());
1119 modified << action.property;
1120 data->actions << myAction;
1121 action.fromValue = myAction.toValue;
1125 if (data->actions.count()) {
1126 d->spa->setAnimAction(data, QAbstractAnimation::DeleteWhenStopped);
1133 \qmlclass NumberAnimation QDeclarative1NumberAnimation
1134 \inqmlmodule QtQuick 1
1135 \ingroup qml-animation-transition
1137 \inherits PropertyAnimation
1138 \brief The NumberAnimation element animates changes in qreal-type values.
1140 NumberAnimation is a specialized PropertyAnimation that defines an
1141 animation to be applied when a numerical value changes.
1143 Here is a NumberAnimation applied to the \c x property of a \l Rectangle
1144 as a property value source. It animates the \c x value from its current
1145 value to a value of 50, over 1000 milliseconds:
1147 \snippet doc/src/snippets/qtquick1/numberanimation.qml 0
1149 Like any other animation element, a NumberAnimation can be applied in a
1150 number of ways, including transitions, behaviors and property value
1151 sources. The \l {QML Animation and Transitions} documentation shows a
1152 variety of methods for creating animations.
1154 Note that NumberAnimation may not animate smoothly if there are irregular
1155 changes in the number value that it is tracking. If this is the case, use
1156 SmoothedAnimation instead.
1158 \sa {QML Animation and Transitions}, {declarative/animation/basics}{Animation basics example}
1160 QDeclarative1NumberAnimation::QDeclarative1NumberAnimation(QObject *parent)
1161 : QDeclarative1PropertyAnimation(parent)
1166 QDeclarative1NumberAnimation::QDeclarative1NumberAnimation(QDeclarative1PropertyAnimationPrivate &dd, QObject *parent)
1167 : QDeclarative1PropertyAnimation(dd, parent)
1172 QDeclarative1NumberAnimation::~QDeclarative1NumberAnimation()
1176 void QDeclarative1NumberAnimation::init()
1178 Q_D(QDeclarative1PropertyAnimation);
1179 d->interpolatorType = QMetaType::QReal;
1180 d->interpolator = QVariantAnimationPrivate::getInterpolator(d->interpolatorType);
1184 \qmlproperty real QtQuick1::NumberAnimation::from
1185 This property holds the starting value for the animation.
1187 For example, the following animation is not applied until the \c x value
1196 transition: Transition {
1197 NumberAnimation { properties: "x"; from: 100; duration: 200 }
1202 If the NumberAnimation is defined within a \l Transition or \l Behavior,
1203 this value defaults to the value defined in the starting state of the
1204 \l Transition, or the current value of the property at the moment the
1205 \l Behavior is triggered.
1207 \sa {QML Animation and Transitions}
1210 qreal QDeclarative1NumberAnimation::from() const
1212 Q_D(const QDeclarative1PropertyAnimation);
1213 return d->from.toReal();
1216 void QDeclarative1NumberAnimation::setFrom(qreal f)
1218 QDeclarative1PropertyAnimation::setFrom(f);
1222 \qmlproperty real QtQuick1::NumberAnimation::to
1223 This property holds the end value for the animation.
1225 If the NumberAnimation is defined within a \l Transition or \l Behavior,
1226 this value defaults to the value defined in the end state of the
1227 \l Transition, or the value of the property change that triggered the
1230 \sa {QML Animation and Transitions}
1232 qreal QDeclarative1NumberAnimation::to() const
1234 Q_D(const QDeclarative1PropertyAnimation);
1235 return d->to.toReal();
1238 void QDeclarative1NumberAnimation::setTo(qreal t)
1240 QDeclarative1PropertyAnimation::setTo(t);
1246 \qmlclass Vector3dAnimation QDeclarative1Vector3dAnimation
1247 \inqmlmodule QtQuick 1
1248 \ingroup qml-animation-transition
1250 \inherits PropertyAnimation
1251 \brief The Vector3dAnimation element animates changes in QVector3d values.
1253 Vector3dAnimation is a specialized PropertyAnimation that defines an
1254 animation to be applied when a Vector3d value changes.
1256 Like any other animation element, a Vector3dAnimation can be applied in a
1257 number of ways, including transitions, behaviors and property value
1258 sources. The \l {QML Animation and Transitions} documentation shows a
1259 variety of methods for creating animations.
1261 \sa {QML Animation and Transitions}, {declarative/animation/basics}{Animation basics example}
1263 QDeclarative1Vector3dAnimation::QDeclarative1Vector3dAnimation(QObject *parent)
1264 : QDeclarative1PropertyAnimation(parent)
1266 Q_D(QDeclarative1PropertyAnimation);
1267 d->interpolatorType = QMetaType::QVector3D;
1268 d->interpolator = QVariantAnimationPrivate::getInterpolator(d->interpolatorType);
1269 d->defaultToInterpolatorType = true;
1272 QDeclarative1Vector3dAnimation::~QDeclarative1Vector3dAnimation()
1277 \qmlproperty real QtQuick1::Vector3dAnimation::from
1278 This property holds the starting value for the animation.
1280 If the Vector3dAnimation is defined within a \l Transition or \l Behavior,
1281 this value defaults to the value defined in the starting state of the
1282 \l Transition, or the current value of the property at the moment the
1283 \l Behavior is triggered.
1285 \sa {QML Animation and Transitions}
1287 QVector3D QDeclarative1Vector3dAnimation::from() const
1289 Q_D(const QDeclarative1PropertyAnimation);
1290 return d->from.value<QVector3D>();
1293 void QDeclarative1Vector3dAnimation::setFrom(QVector3D f)
1295 QDeclarative1PropertyAnimation::setFrom(f);
1299 \qmlproperty real QtQuick1::Vector3dAnimation::to
1300 This property holds the end value for the animation.
1302 If the Vector3dAnimation is defined within a \l Transition or \l Behavior,
1303 this value defaults to the value defined in the end state of the
1304 \l Transition, or the value of the property change that triggered the
1307 \sa {QML Animation and Transitions}
1309 QVector3D QDeclarative1Vector3dAnimation::to() const
1311 Q_D(const QDeclarative1PropertyAnimation);
1312 return d->to.value<QVector3D>();
1315 void QDeclarative1Vector3dAnimation::setTo(QVector3D t)
1317 QDeclarative1PropertyAnimation::setTo(t);
1323 \qmlclass RotationAnimation QDeclarative1RotationAnimation
1324 \inqmlmodule QtQuick 1
1325 \ingroup qml-animation-transition
1327 \inherits PropertyAnimation
1328 \brief The RotationAnimation element animates changes in rotation values.
1330 RotationAnimation is a specialized PropertyAnimation that gives control
1331 over the direction of rotation during an animation.
1333 By default, it rotates in the direction
1334 of the numerical change; a rotation from 0 to 240 will rotate 240 degrees
1335 clockwise, while a rotation from 240 to 0 will rotate 240 degrees
1336 counterclockwise. The \l direction property can be set to specify the
1337 direction in which the rotation should occur.
1339 In the following example we use RotationAnimation to animate the rotation
1340 between states via the shortest path:
1342 \snippet doc/src/snippets/qtquick1/rotationanimation.qml 0
1344 Notice the RotationAnimation did not need to set a \l target
1345 value. As a convenience, when used in a transition, RotationAnimation will rotate all
1346 properties named "rotation" or "angle". You can override this by providing
1347 your own properties via \l {PropertyAnimation::properties}{properties} or
1348 \l {PropertyAnimation::property}{property}.
1350 Also, note the \l Rectangle will be rotated around its default
1351 \l {Item::}{transformOrigin} (which is \c Item.Center). To use a different
1352 transform origin, set the origin in the PropertyChanges object and apply
1353 the change at the start of the animation using PropertyAction. See the
1354 PropertyAction documentation for more details.
1356 Like any other animation element, a RotationAnimation can be applied in a
1357 number of ways, including transitions, behaviors and property value
1358 sources. The \l {QML Animation and Transitions} documentation shows a
1359 variety of methods for creating animations.
1361 \sa {QML Animation and Transitions}, {declarative/animation/basics}{Animation basics example}
1363 QVariant _q_interpolateShortestRotation(qreal &f, qreal &t, qreal progress)
1367 while(diff > 180.0){
1371 while(diff < -180.0){
1375 return QVariant(f + (newt - f) * progress);
1378 QVariant _q_interpolateClockwiseRotation(qreal &f, qreal &t, qreal progress)
1386 return QVariant(f + (newt - f) * progress);
1389 QVariant _q_interpolateCounterclockwiseRotation(qreal &f, qreal &t, qreal progress)
1397 return QVariant(f + (newt - f) * progress);
1400 QDeclarative1RotationAnimation::QDeclarative1RotationAnimation(QObject *parent)
1401 : QDeclarative1PropertyAnimation(*(new QDeclarative1RotationAnimationPrivate), parent)
1403 Q_D(QDeclarative1RotationAnimation);
1404 d->interpolatorType = QMetaType::QReal;
1405 d->interpolator = QVariantAnimationPrivate::getInterpolator(d->interpolatorType);
1406 d->defaultProperties = QLatin1String("rotation,angle");
1409 QDeclarative1RotationAnimation::~QDeclarative1RotationAnimation()
1414 \qmlproperty real QtQuick1::RotationAnimation::from
1415 This property holds the starting value for the animation.
1417 For example, the following animation is not applied until the \c angle value
1426 transition: Transition {
1427 RotationAnimation { properties: "angle"; from: 100; duration: 2000 }
1432 If the RotationAnimation is defined within a \l Transition or \l Behavior,
1433 this value defaults to the value defined in the starting state of the
1434 \l Transition, or the current value of the property at the moment the
1435 \l Behavior is triggered.
1437 \sa {QML Animation and Transitions}
1439 qreal QDeclarative1RotationAnimation::from() const
1441 Q_D(const QDeclarative1RotationAnimation);
1442 return d->from.toReal();
1445 void QDeclarative1RotationAnimation::setFrom(qreal f)
1447 QDeclarative1PropertyAnimation::setFrom(f);
1451 \qmlproperty real QtQuick1::RotationAnimation::to
1452 This property holds the end value for the animation..
1454 If the RotationAnimation is defined within a \l Transition or \l Behavior,
1455 this value defaults to the value defined in the end state of the
1456 \l Transition, or the value of the property change that triggered the
1459 \sa {QML Animation and Transitions}
1461 qreal QDeclarative1RotationAnimation::to() const
1463 Q_D(const QDeclarative1RotationAnimation);
1464 return d->to.toReal();
1467 void QDeclarative1RotationAnimation::setTo(qreal t)
1469 QDeclarative1PropertyAnimation::setTo(t);
1473 \qmlproperty enumeration QtQuick1::RotationAnimation::direction
1474 This property holds the direction of the rotation.
1476 Possible values are:
1479 \o RotationAnimation.Numerical (default) - Rotate by linearly interpolating between the two numbers.
1480 A rotation from 10 to 350 will rotate 340 degrees clockwise.
1481 \o RotationAnimation.Clockwise - Rotate clockwise between the two values
1482 \o RotationAnimation.Counterclockwise - Rotate counterclockwise between the two values
1483 \o RotationAnimation.Shortest - Rotate in the direction that produces the shortest animation path.
1484 A rotation from 10 to 350 will rotate 20 degrees counterclockwise.
1487 QDeclarative1RotationAnimation::RotationDirection QDeclarative1RotationAnimation::direction() const
1489 Q_D(const QDeclarative1RotationAnimation);
1490 return d->direction;
1493 void QDeclarative1RotationAnimation::setDirection(QDeclarative1RotationAnimation::RotationDirection direction)
1495 Q_D(QDeclarative1RotationAnimation);
1496 if (d->direction == direction)
1499 d->direction = direction;
1500 switch(d->direction) {
1502 d->interpolator = reinterpret_cast<QVariantAnimation::Interpolator>(&_q_interpolateClockwiseRotation);
1504 case Counterclockwise:
1505 d->interpolator = reinterpret_cast<QVariantAnimation::Interpolator>(&_q_interpolateCounterclockwiseRotation);
1508 d->interpolator = reinterpret_cast<QVariantAnimation::Interpolator>(&_q_interpolateShortestRotation);
1511 d->interpolator = QVariantAnimationPrivate::getInterpolator(d->interpolatorType);
1515 emit directionChanged();
1520 QDeclarative1AnimationGroup::QDeclarative1AnimationGroup(QObject *parent)
1521 : QDeclarative1AbstractAnimation(*(new QDeclarative1AnimationGroupPrivate), parent)
1525 QDeclarative1AnimationGroup::QDeclarative1AnimationGroup(QDeclarative1AnimationGroupPrivate &dd, QObject *parent)
1526 : QDeclarative1AbstractAnimation(dd, parent)
1530 void QDeclarative1AnimationGroupPrivate::append_animation(QDeclarativeListProperty<QDeclarative1AbstractAnimation> *list, QDeclarative1AbstractAnimation *a)
1532 QDeclarative1AnimationGroup *q = qobject_cast<QDeclarative1AnimationGroup *>(list->object);
1535 // This is an optimization for the parenting that already occurs via addAnimation
1536 QDeclarative_setParent_noEvent(a->qtAnimation(), q->d_func()->ag);
1537 q->d_func()->ag->addAnimation(a->qtAnimation());
1541 void QDeclarative1AnimationGroupPrivate::clear_animation(QDeclarativeListProperty<QDeclarative1AbstractAnimation> *list)
1543 QDeclarative1AnimationGroup *q = qobject_cast<QDeclarative1AnimationGroup *>(list->object);
1545 while (q->d_func()->animations.count()) {
1546 QDeclarative1AbstractAnimation *firstAnim = q->d_func()->animations.at(0);
1547 QDeclarative_setParent_noEvent(firstAnim->qtAnimation(), 0);
1548 q->d_func()->ag->removeAnimation(firstAnim->qtAnimation());
1549 firstAnim->setGroup(0);
1554 QDeclarative1AnimationGroup::~QDeclarative1AnimationGroup()
1558 QDeclarativeListProperty<QDeclarative1AbstractAnimation> QDeclarative1AnimationGroup::animations()
1560 Q_D(QDeclarative1AnimationGroup);
1561 QDeclarativeListProperty<QDeclarative1AbstractAnimation> list(this, d->animations);
1562 list.append = &QDeclarative1AnimationGroupPrivate::append_animation;
1563 list.clear = &QDeclarative1AnimationGroupPrivate::clear_animation;
1568 \qmlclass SequentialAnimation QDeclarative1SequentialAnimation
1569 \inqmlmodule QtQuick 1
1570 \ingroup qml-animation-transition
1573 \brief The SequentialAnimation element allows animations to be run sequentially.
1575 The SequentialAnimation and ParallelAnimation elements allow multiple
1576 animations to be run together. Animations defined in a SequentialAnimation
1577 are run one after the other, while animations defined in a ParallelAnimation
1578 are run at the same time.
1580 The following example runs two number animations in a sequence. The \l Rectangle
1581 animates to a \c x position of 50, then to a \c y position of 50.
1583 \snippet doc/src/snippets/qtquick1/sequentialanimation.qml 0
1585 Animations defined within a \l Transition are automatically run in parallel,
1586 so SequentialAnimation can be used to enclose the animations in a \l Transition
1587 if this is the preferred behavior.
1589 Like any other animation element, a SequentialAnimation can be applied in a
1590 number of ways, including transitions, behaviors and property value
1591 sources. The \l {QML Animation and Transitions} documentation shows a
1592 variety of methods for creating animations.
1594 \note Once an animation has been grouped into a SequentialAnimation or
1595 ParallelAnimation, it cannot be individually started and stopped; the
1596 SequentialAnimation or ParallelAnimation must be started and stopped as a group.
1598 \sa ParallelAnimation, {QML Animation and Transitions}, {declarative/animation/basics}{Animation basics example}
1601 QDeclarative1SequentialAnimation::QDeclarative1SequentialAnimation(QObject *parent) :
1602 QDeclarative1AnimationGroup(parent)
1604 Q_D(QDeclarative1AnimationGroup);
1605 d->ag = new QSequentialAnimationGroup;
1606 QDeclarative_setParent_noEvent(d->ag, this);
1609 QDeclarative1SequentialAnimation::~QDeclarative1SequentialAnimation()
1613 QAbstractAnimation *QDeclarative1SequentialAnimation::qtAnimation()
1615 Q_D(QDeclarative1AnimationGroup);
1619 void QDeclarative1SequentialAnimation::transition(QDeclarative1StateActions &actions,
1620 QDeclarativeProperties &modified,
1621 TransitionDirection direction)
1623 Q_D(QDeclarative1AnimationGroup);
1627 if (direction == Backward) {
1629 from = d->animations.count() - 1;
1632 bool valid = d->defaultProperty.isValid();
1633 for (int ii = from; ii < d->animations.count() && ii >= 0; ii += inc) {
1635 d->animations.at(ii)->setDefaultTarget(d->defaultProperty);
1636 d->animations.at(ii)->transition(actions, modified, direction);
1643 \qmlclass ParallelAnimation QDeclarative1ParallelAnimation
1644 \inqmlmodule QtQuick 1
1645 \ingroup qml-animation-transition
1648 \brief The ParallelAnimation element allows animations to be run in parallel.
1650 The SequentialAnimation and ParallelAnimation elements allow multiple
1651 animations to be run together. Animations defined in a SequentialAnimation
1652 are run one after the other, while animations defined in a ParallelAnimation
1653 are run at the same time.
1655 The following animation runs two number animations in parallel. The \l Rectangle
1656 moves to (50,50) by animating its \c x and \c y properties at the same time.
1658 \snippet doc/src/snippets/qtquick1/parallelanimation.qml 0
1660 Like any other animation element, a ParallelAnimation can be applied in a
1661 number of ways, including transitions, behaviors and property value
1662 sources. The \l {QML Animation and Transitions} documentation shows a
1663 variety of methods for creating animations.
1665 \note Once an animation has been grouped into a SequentialAnimation or
1666 ParallelAnimation, it cannot be individually started and stopped; the
1667 SequentialAnimation or ParallelAnimation must be started and stopped as a group.
1669 \sa SequentialAnimation, {QML Animation and Transitions}, {declarative/animation/basics}{Animation basics example}
1671 QDeclarative1ParallelAnimation::QDeclarative1ParallelAnimation(QObject *parent) :
1672 QDeclarative1AnimationGroup(parent)
1674 Q_D(QDeclarative1AnimationGroup);
1675 d->ag = new QParallelAnimationGroup;
1676 QDeclarative_setParent_noEvent(d->ag, this);
1679 QDeclarative1ParallelAnimation::~QDeclarative1ParallelAnimation()
1683 QAbstractAnimation *QDeclarative1ParallelAnimation::qtAnimation()
1685 Q_D(QDeclarative1AnimationGroup);
1689 void QDeclarative1ParallelAnimation::transition(QDeclarative1StateActions &actions,
1690 QDeclarativeProperties &modified,
1691 TransitionDirection direction)
1693 Q_D(QDeclarative1AnimationGroup);
1694 bool valid = d->defaultProperty.isValid();
1695 for (int ii = 0; ii < d->animations.count(); ++ii) {
1697 d->animations.at(ii)->setDefaultTarget(d->defaultProperty);
1698 d->animations.at(ii)->transition(actions, modified, direction);
1704 //convert a variant from string type to another animatable type
1705 void QDeclarative1PropertyAnimationPrivate::convertVariant(QVariant &variant, int type)
1707 if (variant.userType() != QVariant::String) {
1708 variant.convert((QVariant::Type)type);
1713 case QVariant::Rect: {
1714 variant.setValue(QDeclarativeStringConverters::rectFFromString(variant.toString()).toRect());
1717 case QVariant::RectF: {
1718 variant.setValue(QDeclarativeStringConverters::rectFFromString(variant.toString()));
1721 case QVariant::Point: {
1722 variant.setValue(QDeclarativeStringConverters::pointFFromString(variant.toString()).toPoint());
1725 case QVariant::PointF: {
1726 variant.setValue(QDeclarativeStringConverters::pointFFromString(variant.toString()));
1729 case QVariant::Size: {
1730 variant.setValue(QDeclarativeStringConverters::sizeFFromString(variant.toString()).toSize());
1733 case QVariant::SizeF: {
1734 variant.setValue(QDeclarativeStringConverters::sizeFFromString(variant.toString()));
1737 case QVariant::Color: {
1738 variant.setValue(QDeclarativeStringConverters::colorFromString(variant.toString()));
1741 case QVariant::Vector3D: {
1742 variant.setValue(QDeclarativeStringConverters::vector3DFromString(variant.toString()));
1746 if (QDeclarativeValueTypeFactory::isValueType((uint)type)) {
1747 variant.convert((QVariant::Type)type);
1749 QDeclarativeMetaType::StringConverter converter = QDeclarativeMetaType::customStringConverter(type);
1751 variant = converter(variant.toString());
1758 \qmlclass PropertyAnimation QDeclarative1PropertyAnimation
1759 \inqmlmodule QtQuick 1
1760 \ingroup qml-animation-transition
1763 \brief The PropertyAnimation element animates changes in property values.
1765 PropertyAnimation provides a way to animate changes to a property's value.
1767 It can be used to define animations in a number of ways:
1770 \o In a \l Transition
1772 For example, to animate any objects that have changed their \c x or \c y properties
1773 as a result of a state change, using an \c InOutQuad easing curve:
1775 \snippet doc/src/snippets/qtquick1/propertyanimation.qml transition
1780 For example, to animate all changes to a rectangle's \c x property:
1782 \snippet doc/src/snippets/qtquick1/propertyanimation.qml behavior
1785 \o As a property value source
1787 For example, to repeatedly animate the rectangle's \c x property:
1789 \snippet doc/src/snippets/qtquick1/propertyanimation.qml propertyvaluesource
1792 \o In a signal handler
1794 For example, to fade out \c theObject when clicked:
1797 anchors.fill: theObject
1798 onClicked: PropertyAnimation { target: theObject; property: "opacity"; to: 0 }
1804 For example, to animate \c rect's \c width property over 500ms, from its current width to 30:
1806 \snippet doc/src/snippets/qtquick1/propertyanimation.qml standalone
1810 Depending on how the animation is used, the set of properties normally used will be
1811 different. For more information see the individual property documentation, as well
1812 as the \l{QML Animation and Transitions} introduction.
1814 Note that PropertyAnimation inherits the abstract \l Animation element.
1815 This includes additional properties and methods for controlling the animation.
1817 \sa {QML Animation and Transitions}, {declarative/animation/basics}{Animation basics example}
1820 QDeclarative1PropertyAnimation::QDeclarative1PropertyAnimation(QObject *parent)
1821 : QDeclarative1AbstractAnimation(*(new QDeclarative1PropertyAnimationPrivate), parent)
1823 Q_D(QDeclarative1PropertyAnimation);
1827 QDeclarative1PropertyAnimation::QDeclarative1PropertyAnimation(QDeclarative1PropertyAnimationPrivate &dd, QObject *parent)
1828 : QDeclarative1AbstractAnimation(dd, parent)
1830 Q_D(QDeclarative1PropertyAnimation);
1834 QDeclarative1PropertyAnimation::~QDeclarative1PropertyAnimation()
1838 void QDeclarative1PropertyAnimationPrivate::init()
1840 Q_Q(QDeclarative1PropertyAnimation);
1841 va = new QDeclarative1BulkValueAnimator;
1842 QDeclarative_setParent_noEvent(va, q);
1846 \qmlproperty int QtQuick1::PropertyAnimation::duration
1847 This property holds the duration of the animation, in milliseconds.
1849 The default value is 250.
1851 int QDeclarative1PropertyAnimation::duration() const
1853 Q_D(const QDeclarative1PropertyAnimation);
1854 return d->va->duration();
1857 void QDeclarative1PropertyAnimation::setDuration(int duration)
1860 qmlInfo(this) << tr("Cannot set a duration of < 0");
1864 Q_D(QDeclarative1PropertyAnimation);
1865 if (d->va->duration() == duration)
1867 d->va->setDuration(duration);
1868 emit durationChanged(duration);
1872 \qmlproperty real QtQuick1::PropertyAnimation::from
1873 This property holds the starting value for the animation.
1875 If the PropertyAnimation is defined within a \l Transition or \l Behavior,
1876 this value defaults to the value defined in the starting state of the
1877 \l Transition, or the current value of the property at the moment the
1878 \l Behavior is triggered.
1880 \sa {QML Animation and Transitions}
1882 QVariant QDeclarative1PropertyAnimation::from() const
1884 Q_D(const QDeclarative1PropertyAnimation);
1888 void QDeclarative1PropertyAnimation::setFrom(const QVariant &f)
1890 Q_D(QDeclarative1PropertyAnimation);
1891 if (d->fromIsDefined && f == d->from)
1894 d->fromIsDefined = f.isValid();
1895 emit fromChanged(f);
1899 \qmlproperty real QtQuick1::PropertyAnimation::to
1900 This property holds the end value for the animation.
1902 If the PropertyAnimation is defined within a \l Transition or \l Behavior,
1903 this value defaults to the value defined in the end state of the
1904 \l Transition, or the value of the property change that triggered the
1907 \sa {QML Animation and Transitions}
1909 QVariant QDeclarative1PropertyAnimation::to() const
1911 Q_D(const QDeclarative1PropertyAnimation);
1915 void QDeclarative1PropertyAnimation::setTo(const QVariant &t)
1917 Q_D(QDeclarative1PropertyAnimation);
1918 if (d->toIsDefined && t == d->to)
1921 d->toIsDefined = t.isValid();
1926 \qmlproperty enumeration QtQuick1::PropertyAnimation::easing.type
1927 \qmlproperty real QtQuick1::PropertyAnimation::easing.amplitude
1928 \qmlproperty real QtQuick1::PropertyAnimation::easing.overshoot
1929 \qmlproperty real QtQuick1::PropertyAnimation::easing.period
1930 \brief the easing curve used for the animation.
1932 To specify an easing curve you need to specify at least the type. For some curves you can also specify
1933 amplitude, period and/or overshoot (more details provided after the table). The default easing curve is
1937 PropertyAnimation { properties: "y"; easing.type: Easing.InOutElastic; easing.amplitude: 2.0; easing.period: 1.5 }
1940 Available types are:
1945 \o Easing curve for a linear (t) function: velocity is constant.
1946 \o \inlineimage qeasingcurve-linear.png
1949 \o Easing curve for a quadratic (t^2) function: accelerating from zero velocity.
1950 \o \inlineimage qeasingcurve-inquad.png
1952 \o \c Easing.OutQuad
1953 \o Easing curve for a quadratic (t^2) function: decelerating to zero velocity.
1954 \o \inlineimage qeasingcurve-outquad.png
1956 \o \c Easing.InOutQuad
1957 \o Easing curve for a quadratic (t^2) function: acceleration until halfway, then deceleration.
1958 \o \inlineimage qeasingcurve-inoutquad.png
1960 \o \c Easing.OutInQuad
1961 \o Easing curve for a quadratic (t^2) function: deceleration until halfway, then acceleration.
1962 \o \inlineimage qeasingcurve-outinquad.png
1964 \o \c Easing.InCubic
1965 \o Easing curve for a cubic (t^3) function: accelerating from zero velocity.
1966 \o \inlineimage qeasingcurve-incubic.png
1968 \o \c Easing.OutCubic
1969 \o Easing curve for a cubic (t^3) function: decelerating from zero velocity.
1970 \o \inlineimage qeasingcurve-outcubic.png
1972 \o \c Easing.InOutCubic
1973 \o Easing curve for a cubic (t^3) function: acceleration until halfway, then deceleration.
1974 \o \inlineimage qeasingcurve-inoutcubic.png
1976 \o \c Easing.OutInCubic
1977 \o Easing curve for a cubic (t^3) function: deceleration until halfway, then acceleration.
1978 \o \inlineimage qeasingcurve-outincubic.png
1980 \o \c Easing.InQuart
1981 \o Easing curve for a quartic (t^4) function: accelerating from zero velocity.
1982 \o \inlineimage qeasingcurve-inquart.png
1984 \o \c Easing.OutQuart
1985 \o Easing curve for a quartic (t^4) function: decelerating from zero velocity.
1986 \o \inlineimage qeasingcurve-outquart.png
1988 \o \c Easing.InOutQuart
1989 \o Easing curve for a quartic (t^4) function: acceleration until halfway, then deceleration.
1990 \o \inlineimage qeasingcurve-inoutquart.png
1992 \o \c Easing.OutInQuart
1993 \o Easing curve for a quartic (t^4) function: deceleration until halfway, then acceleration.
1994 \o \inlineimage qeasingcurve-outinquart.png
1996 \o \c Easing.InQuint
1997 \o Easing curve for a quintic (t^5) function: accelerating from zero velocity.
1998 \o \inlineimage qeasingcurve-inquint.png
2000 \o \c Easing.OutQuint
2001 \o Easing curve for a quintic (t^5) function: decelerating from zero velocity.
2002 \o \inlineimage qeasingcurve-outquint.png
2004 \o \c Easing.InOutQuint
2005 \o Easing curve for a quintic (t^5) function: acceleration until halfway, then deceleration.
2006 \o \inlineimage qeasingcurve-inoutquint.png
2008 \o \c Easing.OutInQuint
2009 \o Easing curve for a quintic (t^5) function: deceleration until halfway, then acceleration.
2010 \o \inlineimage qeasingcurve-outinquint.png
2013 \o Easing curve for a sinusoidal (sin(t)) function: accelerating from zero velocity.
2014 \o \inlineimage qeasingcurve-insine.png
2016 \o \c Easing.OutSine
2017 \o Easing curve for a sinusoidal (sin(t)) function: decelerating from zero velocity.
2018 \o \inlineimage qeasingcurve-outsine.png
2020 \o \c Easing.InOutSine
2021 \o Easing curve for a sinusoidal (sin(t)) function: acceleration until halfway, then deceleration.
2022 \o \inlineimage qeasingcurve-inoutsine.png
2024 \o \c Easing.OutInSine
2025 \o Easing curve for a sinusoidal (sin(t)) function: deceleration until halfway, then acceleration.
2026 \o \inlineimage qeasingcurve-outinsine.png
2029 \o Easing curve for an exponential (2^t) function: accelerating from zero velocity.
2030 \o \inlineimage qeasingcurve-inexpo.png
2032 \o \c Easing.OutExpo
2033 \o Easing curve for an exponential (2^t) function: decelerating from zero velocity.
2034 \o \inlineimage qeasingcurve-outexpo.png
2036 \o \c Easing.InOutExpo
2037 \o Easing curve for an exponential (2^t) function: acceleration until halfway, then deceleration.
2038 \o \inlineimage qeasingcurve-inoutexpo.png
2040 \o \c Easing.OutInExpo
2041 \o Easing curve for an exponential (2^t) function: deceleration until halfway, then acceleration.
2042 \o \inlineimage qeasingcurve-outinexpo.png
2045 \o Easing curve for a circular (sqrt(1-t^2)) function: accelerating from zero velocity.
2046 \o \inlineimage qeasingcurve-incirc.png
2048 \o \c Easing.OutCirc
2049 \o Easing curve for a circular (sqrt(1-t^2)) function: decelerating from zero velocity.
2050 \o \inlineimage qeasingcurve-outcirc.png
2052 \o \c Easing.InOutCirc
2053 \o Easing curve for a circular (sqrt(1-t^2)) function: acceleration until halfway, then deceleration.
2054 \o \inlineimage qeasingcurve-inoutcirc.png
2056 \o \c Easing.OutInCirc
2057 \o Easing curve for a circular (sqrt(1-t^2)) function: deceleration until halfway, then acceleration.
2058 \o \inlineimage qeasingcurve-outincirc.png
2060 \o \c Easing.InElastic
2061 \o Easing curve for an elastic (exponentially decaying sine wave) function: accelerating from zero velocity.
2062 \br The peak amplitude can be set with the \e amplitude parameter, and the period of decay by the \e period parameter.
2063 \o \inlineimage qeasingcurve-inelastic.png
2065 \o \c Easing.OutElastic
2066 \o Easing curve for an elastic (exponentially decaying sine wave) function: decelerating from zero velocity.
2067 \br The peak amplitude can be set with the \e amplitude parameter, and the period of decay by the \e period parameter.
2068 \o \inlineimage qeasingcurve-outelastic.png
2070 \o \c Easing.InOutElastic
2071 \o Easing curve for an elastic (exponentially decaying sine wave) function: acceleration until halfway, then deceleration.
2072 \o \inlineimage qeasingcurve-inoutelastic.png
2074 \o \c Easing.OutInElastic
2075 \o Easing curve for an elastic (exponentially decaying sine wave) function: deceleration until halfway, then acceleration.
2076 \o \inlineimage qeasingcurve-outinelastic.png
2079 \o Easing curve for a back (overshooting cubic function: (s+1)*t^3 - s*t^2) easing in: accelerating from zero velocity.
2080 \o \inlineimage qeasingcurve-inback.png
2082 \o \c Easing.OutBack
2083 \o Easing curve for a back (overshooting cubic function: (s+1)*t^3 - s*t^2) easing out: decelerating to zero velocity.
2084 \o \inlineimage qeasingcurve-outback.png
2086 \o \c Easing.InOutBack
2087 \o Easing curve for a back (overshooting cubic function: (s+1)*t^3 - s*t^2) easing in/out: acceleration until halfway, then deceleration.
2088 \o \inlineimage qeasingcurve-inoutback.png
2090 \o \c Easing.OutInBack
2091 \o Easing curve for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing out/in: deceleration until halfway, then acceleration.
2092 \o \inlineimage qeasingcurve-outinback.png
2094 \o \c Easing.InBounce
2095 \o Easing curve for a bounce (exponentially decaying parabolic bounce) function: accelerating from zero velocity.
2096 \o \inlineimage qeasingcurve-inbounce.png
2098 \o \c Easing.OutBounce
2099 \o Easing curve for a bounce (exponentially decaying parabolic bounce) function: decelerating from zero velocity.
2100 \o \inlineimage qeasingcurve-outbounce.png
2102 \o \c Easing.InOutBounce
2103 \o Easing curve for a bounce (exponentially decaying parabolic bounce) function easing in/out: acceleration until halfway, then deceleration.
2104 \o \inlineimage qeasingcurve-inoutbounce.png
2106 \o \c Easing.OutInBounce
2107 \o Easing curve for a bounce (exponentially decaying parabolic bounce) function easing out/in: deceleration until halfway, then acceleration.
2108 \o \inlineimage qeasingcurve-outinbounce.png
2111 \c easing.amplitude is only applicable for bounce and elastic curves (curves of type
2112 \c Easing.InBounce, \c Easing.OutBounce, \c Easing.InOutBounce, \c Easing.OutInBounce, \c Easing.InElastic,
2113 \c Easing.OutElastic, \c Easing.InOutElastic or \c Easing.OutInElastic).
2115 \c easing.overshoot is only applicable if \c easing.type is: \c Easing.InBack, \c Easing.OutBack,
2116 \c Easing.InOutBack or \c Easing.OutInBack.
2118 \c easing.period is only applicable if easing.type is: \c Easing.InElastic, \c Easing.OutElastic,
2119 \c Easing.InOutElastic or \c Easing.OutInElastic.
2121 See the \l {declarative/animation/easing}{easing} example for a demonstration of
2122 the different easing settings.
2124 QEasingCurve QDeclarative1PropertyAnimation::easing() const
2126 Q_D(const QDeclarative1PropertyAnimation);
2127 return d->va->easingCurve();
2130 void QDeclarative1PropertyAnimation::setEasing(const QEasingCurve &e)
2132 Q_D(QDeclarative1PropertyAnimation);
2133 if (d->va->easingCurve() == e)
2136 d->va->setEasingCurve(e);
2137 emit easingChanged(e);
2140 QObject *QDeclarative1PropertyAnimation::target() const
2142 Q_D(const QDeclarative1PropertyAnimation);
2146 void QDeclarative1PropertyAnimation::setTarget(QObject *o)
2148 Q_D(QDeclarative1PropertyAnimation);
2152 emit targetChanged();
2155 QString QDeclarative1PropertyAnimation::property() const
2157 Q_D(const QDeclarative1PropertyAnimation);
2158 return d->propertyName;
2161 void QDeclarative1PropertyAnimation::setProperty(const QString &n)
2163 Q_D(QDeclarative1PropertyAnimation);
2164 if (d->propertyName == n)
2166 d->propertyName = n;
2167 emit propertyChanged();
2170 QString QDeclarative1PropertyAnimation::properties() const
2172 Q_D(const QDeclarative1PropertyAnimation);
2173 return d->properties;
2176 void QDeclarative1PropertyAnimation::setProperties(const QString &prop)
2178 Q_D(QDeclarative1PropertyAnimation);
2179 if (d->properties == prop)
2182 d->properties = prop;
2183 emit propertiesChanged(prop);
2187 \qmlproperty string QtQuick1::PropertyAnimation::properties
2188 \qmlproperty list<Object> QtQuick1::PropertyAnimation::targets
2189 \qmlproperty string QtQuick1::PropertyAnimation::property
2190 \qmlproperty Object QtQuick1::PropertyAnimation::target
2192 These properties are used as a set to determine which properties should be animated.
2193 The singular and plural forms are functionally identical, e.g.
2195 NumberAnimation { target: theItem; property: "x"; to: 500 }
2197 has the same meaning as
2199 NumberAnimation { targets: theItem; properties: "x"; to: 500 }
2201 The singular forms are slightly optimized, so if you do have only a single target/property
2202 to animate you should try to use them.
2204 The \c targets property allows multiple targets to be set. For example, this animates the
2205 \c x property of both \c itemA and \c itemB:
2208 NumberAnimation { targets: [itemA, itemB]; properties: "x"; to: 500 }
2211 In many cases these properties do not need to be explicitly specified, as they can be
2212 inferred from the animation framework:
2216 \o Value Source / Behavior
2217 \o When an animation is used as a value source or in a Behavior, the default target and property
2218 name to be animated can both be inferred.
2222 width: 100; height: 100
2223 color: Qt.rgba(0,0,1)
2224 NumberAnimation on x { to: 500; loops: Animation.Infinite } //animate theRect's x property
2225 Behavior on y { NumberAnimation {} } //animate theRect's y property
2230 \o When used in a transition, a property animation is assumed to match \e all targets
2231 but \e no properties. In practice, that means you need to specify at least the properties
2232 in order for the animation to do anything.
2236 width: 100; height: 100
2237 color: Qt.rgba(0,0,1)
2238 Item { id: uselessItem }
2241 PropertyChanges { target: theRect; x: 200; y: 200; z: 4 }
2242 PropertyChanges { target: uselessItem; x: 10; y: 10; z: 2 }
2244 transitions: Transition {
2245 //animate both theRect's and uselessItem's x and y to their final values
2246 NumberAnimation { properties: "x,y" }
2248 //animate theRect's z to its final value
2249 NumberAnimation { target: theRect; property: "z" }
2255 \o When an animation is used standalone, both the target and property need to be
2256 explicitly specified.
2260 width: 100; height: 100
2261 color: Qt.rgba(0,0,1)
2262 //need to explicitly specify target and property
2263 NumberAnimation { id: theAnim; target: theRect; property: "x"; to: 500 }
2265 anchors.fill: parent
2266 onClicked: theAnim.start()
2272 As seen in the above example, properties is specified as a comma-separated string of property names to animate.
2274 \sa exclude, {QML Animation and Transitions}
2276 QDeclarativeListProperty<QObject> QDeclarative1PropertyAnimation::targets()
2278 Q_D(QDeclarative1PropertyAnimation);
2279 return QDeclarativeListProperty<QObject>(this, d->targets);
2283 \qmlproperty list<Object> QtQuick1::PropertyAnimation::exclude
2284 This property holds the items not to be affected by this animation.
2285 \sa PropertyAnimation::targets
2287 QDeclarativeListProperty<QObject> QDeclarative1PropertyAnimation::exclude()
2289 Q_D(QDeclarative1PropertyAnimation);
2290 return QDeclarativeListProperty<QObject>(this, d->exclude);
2293 QAbstractAnimation *QDeclarative1PropertyAnimation::qtAnimation()
2295 Q_D(QDeclarative1PropertyAnimation);
2299 void QDeclarative1AnimationPropertyUpdater::setValue(qreal v)
2301 bool deleted = false;
2302 wasDeleted = &deleted;
2303 if (reverse) //QVariantAnimation sends us 1->0 when reversed, but we are expecting 0->1
2305 for (int ii = 0; ii < actions.count(); ++ii) {
2306 QDeclarative1Action &action = actions[ii];
2309 QDeclarativePropertyPrivate::write(action.property, action.toValue, QDeclarativePropertyPrivate::BypassInterceptor | QDeclarativePropertyPrivate::DontRemoveBinding);
2311 if (!fromSourced && !fromDefined) {
2312 action.fromValue = action.property.read();
2313 if (interpolatorType)
2314 QDeclarative1PropertyAnimationPrivate::convertVariant(action.fromValue, interpolatorType);
2316 if (!interpolatorType) {
2317 int propType = action.property.propertyType();
2318 if (!prevInterpolatorType || prevInterpolatorType != propType) {
2319 prevInterpolatorType = propType;
2320 interpolator = QVariantAnimationPrivate::getInterpolator(prevInterpolatorType);
2324 QDeclarativePropertyPrivate::write(action.property, interpolator(action.fromValue.constData(), action.toValue.constData(), v), QDeclarativePropertyPrivate::BypassInterceptor | QDeclarativePropertyPrivate::DontRemoveBinding);
2333 void QDeclarative1PropertyAnimation::transition(QDeclarative1StateActions &actions,
2334 QDeclarativeProperties &modified,
2335 TransitionDirection direction)
2337 Q_D(QDeclarative1PropertyAnimation);
2339 QStringList props = d->properties.isEmpty() ? QStringList() : d->properties.split(QLatin1Char(','));
2340 for (int ii = 0; ii < props.count(); ++ii)
2341 props[ii] = props.at(ii).trimmed();
2342 if (!d->propertyName.isEmpty())
2343 props << d->propertyName;
2345 QList<QObject*> targets = d->targets;
2347 targets.append(d->target);
2349 bool hasSelectors = !props.isEmpty() || !targets.isEmpty() || !d->exclude.isEmpty();
2350 bool useType = (props.isEmpty() && d->defaultToInterpolatorType) ? true : false;
2352 if (d->defaultProperty.isValid() && !hasSelectors) {
2353 props << d->defaultProperty.name();
2354 targets << d->defaultProperty.object();
2357 if (props.isEmpty() && !d->defaultProperties.isEmpty()) {
2358 props << d->defaultProperties.split(QLatin1Char(','));
2361 QDeclarative1AnimationPropertyUpdater *data = new QDeclarative1AnimationPropertyUpdater;
2362 data->interpolatorType = d->interpolatorType;
2363 data->interpolator = d->interpolator;
2364 data->reverse = direction == Backward ? true : false;
2365 data->fromSourced = false;
2366 data->fromDefined = d->fromIsDefined;
2368 bool hasExplicit = false;
2369 //an explicit animation has been specified
2370 if (d->toIsDefined) {
2371 for (int i = 0; i < props.count(); ++i) {
2372 for (int j = 0; j < targets.count(); ++j) {
2373 QDeclarative1Action myAction;
2374 myAction.property = d->createProperty(targets.at(j), props.at(i), this);
2375 if (myAction.property.isValid()) {
2376 if (d->fromIsDefined) {
2377 myAction.fromValue = d->from;
2378 d->convertVariant(myAction.fromValue, d->interpolatorType ? d->interpolatorType : myAction.property.propertyType());
2380 myAction.toValue = d->to;
2381 d->convertVariant(myAction.toValue, d->interpolatorType ? d->interpolatorType : myAction.property.propertyType());
2382 data->actions << myAction;
2384 for (int ii = 0; ii < actions.count(); ++ii) {
2385 QDeclarative1Action &action = actions[ii];
2386 if (action.property.object() == myAction.property.object() &&
2387 myAction.property.name() == action.property.name()) {
2388 modified << action.property;
2389 break; //### any chance there could be multiples?
2398 for (int ii = 0; ii < actions.count(); ++ii) {
2399 QDeclarative1Action &action = actions[ii];
2401 QObject *obj = action.property.object();
2402 QString propertyName = action.property.name();
2403 QObject *sObj = action.specifiedObject;
2404 QString sPropertyName = action.specifiedProperty;
2405 bool same = (obj == sObj);
2407 if ((targets.isEmpty() || targets.contains(obj) || (!same && targets.contains(sObj))) &&
2408 (!d->exclude.contains(obj)) && (same || (!d->exclude.contains(sObj))) &&
2409 (props.contains(propertyName) || (!same && props.contains(sPropertyName))
2410 || (useType && action.property.propertyType() == d->interpolatorType))) {
2411 QDeclarative1Action myAction = action;
2413 if (d->fromIsDefined)
2414 myAction.fromValue = d->from;
2416 myAction.fromValue = QVariant();
2418 myAction.toValue = d->to;
2420 d->convertVariant(myAction.fromValue, d->interpolatorType ? d->interpolatorType : myAction.property.propertyType());
2421 d->convertVariant(myAction.toValue, d->interpolatorType ? d->interpolatorType : myAction.property.propertyType());
2423 modified << action.property;
2425 data->actions << myAction;
2426 action.fromValue = myAction.toValue;
2430 if (data->actions.count()) {
2431 if (!d->rangeIsSet) {
2432 d->va->setStartValue(qreal(0));
2433 d->va->setEndValue(qreal(1));
2434 d->rangeIsSet = true;
2436 d->va->setAnimValue(data, QAbstractAnimation::DeleteWhenStopped);
2437 d->va->setFromSourcedValue(&data->fromSourced);
2438 d->actions = &data->actions;
2441 d->va->setFromSourcedValue(0); //clear previous data
2442 d->va->setAnimValue(0, QAbstractAnimation::DeleteWhenStopped); //clear previous data
2448 \qmlclass ParentAnimation QDeclarative1ParentAnimation
2449 \inqmlmodule QtQuick 1
2450 \ingroup qml-animation-transition
2453 \brief The ParentAnimation element animates changes in parent values.
2455 ParentAnimation is used to animate a parent change for an \l Item.
2457 For example, the following ParentChange changes \c blueRect to become
2458 a child of \c redRect when it is clicked. The inclusion of the
2459 ParentAnimation, which defines a NumberAnimation to be applied during
2460 the transition, ensures the item animates smoothly as it moves to
2463 \snippet doc/src/snippets/qtquick1/parentanimation.qml 0
2465 A ParentAnimation can contain any number of animations. These animations will
2466 be run in parallel; to run them sequentially, define them within a
2467 SequentialAnimation.
2469 In some cases, such as when reparenting between items with clipping enabled, it is useful
2470 to animate the parent change via another item that does not have clipping
2471 enabled. Such an item can be set using the \l via property.
2473 For convenience, when a ParentAnimation is used in a \l Transition, it will
2474 animate any ParentChange that has occurred during the state change.
2475 This can be overridden by setting a specific target item using the
2478 Like any other animation element, a ParentAnimation can be applied in a
2479 number of ways, including transitions, behaviors and property value
2480 sources. The \l {QML Animation and Transitions} documentation shows a
2481 variety of methods for creating animations.
2483 \sa {QML Animation and Transitions}, {declarative/animation/basics}{Animation basics example}
2485 QDeclarative1ParentAnimation::QDeclarative1ParentAnimation(QObject *parent)
2486 : QDeclarative1AnimationGroup(*(new QDeclarative1ParentAnimationPrivate), parent)
2488 Q_D(QDeclarative1ParentAnimation);
2489 d->topLevelGroup = new QSequentialAnimationGroup;
2490 QDeclarative_setParent_noEvent(d->topLevelGroup, this);
2492 d->startAction = new QActionAnimation_1;
2493 QDeclarative_setParent_noEvent(d->startAction, d->topLevelGroup);
2494 d->topLevelGroup->addAnimation(d->startAction);
2496 d->ag = new QParallelAnimationGroup;
2497 QDeclarative_setParent_noEvent(d->ag, d->topLevelGroup);
2498 d->topLevelGroup->addAnimation(d->ag);
2500 d->endAction = new QActionAnimation_1;
2501 QDeclarative_setParent_noEvent(d->endAction, d->topLevelGroup);
2502 d->topLevelGroup->addAnimation(d->endAction);
2505 QDeclarative1ParentAnimation::~QDeclarative1ParentAnimation()
2510 \qmlproperty Item QtQuick1::ParentAnimation::target
2511 The item to reparent.
2513 When used in a transition, if no target is specified, all
2514 ParentChange occurrences are animated by the ParentAnimation.
2516 QDeclarativeItem *QDeclarative1ParentAnimation::target() const
2518 Q_D(const QDeclarative1ParentAnimation);
2522 void QDeclarative1ParentAnimation::setTarget(QDeclarativeItem *target)
2524 Q_D(QDeclarative1ParentAnimation);
2525 if (target == d->target)
2529 emit targetChanged();
2533 \qmlproperty Item QtQuick1::ParentAnimation::newParent
2534 The new parent to animate to.
2536 If the ParentAnimation is defined within a \l Transition or \l Behavior,
2537 this value defaults to the value defined in the end state of the
2538 \l Transition, or the value of the property change that triggered the
2541 QDeclarativeItem *QDeclarative1ParentAnimation::newParent() const
2543 Q_D(const QDeclarative1ParentAnimation);
2544 return d->newParent;
2547 void QDeclarative1ParentAnimation::setNewParent(QDeclarativeItem *newParent)
2549 Q_D(QDeclarative1ParentAnimation);
2550 if (newParent == d->newParent)
2553 d->newParent = newParent;
2554 emit newParentChanged();
2558 \qmlproperty Item QtQuick1::ParentAnimation::via
2559 The item to reparent via. This provides a way to do an unclipped animation
2560 when both the old parent and new parent are clipped.
2570 QDeclarativeItem *QDeclarative1ParentAnimation::via() const
2572 Q_D(const QDeclarative1ParentAnimation);
2576 void QDeclarative1ParentAnimation::setVia(QDeclarativeItem *via)
2578 Q_D(QDeclarative1ParentAnimation);
2586 //### mirrors same-named function in QDeclarativeItem
2587 QPointF QDeclarative1ParentAnimationPrivate::computeTransformOrigin(QDeclarativeItem::TransformOrigin origin, qreal width, qreal height) const
2591 case QDeclarativeItem::TopLeft:
2592 return QPointF(0, 0);
2593 case QDeclarativeItem::Top:
2594 return QPointF(width / 2., 0);
2595 case QDeclarativeItem::TopRight:
2596 return QPointF(width, 0);
2597 case QDeclarativeItem::Left:
2598 return QPointF(0, height / 2.);
2599 case QDeclarativeItem::Center:
2600 return QPointF(width / 2., height / 2.);
2601 case QDeclarativeItem::Right:
2602 return QPointF(width, height / 2.);
2603 case QDeclarativeItem::BottomLeft:
2604 return QPointF(0, height);
2605 case QDeclarativeItem::Bottom:
2606 return QPointF(width / 2., height);
2607 case QDeclarativeItem::BottomRight:
2608 return QPointF(width, height);
2612 void QDeclarative1ParentAnimation::transition(QDeclarative1StateActions &actions,
2613 QDeclarativeProperties &modified,
2614 TransitionDirection direction)
2616 Q_D(QDeclarative1ParentAnimation);
2618 struct QDeclarative1ParentAnimationData : public QAbstractAnimationAction
2620 QDeclarative1ParentAnimationData() {}
2621 ~QDeclarative1ParentAnimationData() { qDeleteAll(pc); }
2623 QDeclarative1StateActions actions;
2624 //### reverse should probably apply on a per-action basis
2626 QList<QDeclarative1ParentChange *> pc;
2627 virtual void doAction()
2629 for (int ii = 0; ii < actions.count(); ++ii) {
2630 const QDeclarative1Action &action = actions.at(ii);
2632 action.event->reverse();
2634 action.event->execute();
2639 QDeclarative1ParentAnimationData *data = new QDeclarative1ParentAnimationData;
2640 QDeclarative1ParentAnimationData *viaData = new QDeclarative1ParentAnimationData;
2642 bool hasExplicit = false;
2643 if (d->target && d->newParent) {
2644 data->reverse = false;
2645 QDeclarative1Action myAction;
2646 QDeclarative1ParentChange *pc = new QDeclarative1ParentChange;
2647 pc->setObject(d->target);
2648 pc->setParent(d->newParent);
2649 myAction.event = pc;
2651 data->actions << myAction;
2654 viaData->reverse = false;
2655 QDeclarative1Action myVAction;
2656 QDeclarative1ParentChange *vpc = new QDeclarative1ParentChange;
2657 vpc->setObject(d->target);
2658 vpc->setParent(d->via);
2659 myVAction.event = vpc;
2661 viaData->actions << myVAction;
2663 //### once actions have concept of modified,
2664 // loop to match appropriate ParentChanges and mark as modified
2668 for (int i = 0; i < actions.size(); ++i) {
2669 QDeclarative1Action &action = actions[i];
2670 if (action.event && action.event->typeName() == QLatin1String("ParentChange")
2671 && (!d->target || static_cast<QDeclarative1ParentChange*>(action.event)->object() == d->target)) {
2673 QDeclarative1ParentChange *pc = static_cast<QDeclarative1ParentChange*>(action.event);
2674 QDeclarative1Action myAction = action;
2675 data->reverse = action.reverseEvent;
2677 //### this logic differs from PropertyAnimation
2678 // (probably a result of modified vs. done)
2680 QDeclarative1ParentChange *epc = new QDeclarative1ParentChange;
2681 epc->setObject(static_cast<QDeclarative1ParentChange*>(action.event)->object());
2682 epc->setParent(d->newParent);
2683 myAction.event = epc;
2685 data->actions << myAction;
2688 action.actionDone = true;
2689 data->actions << myAction;
2693 viaData->reverse = false;
2694 QDeclarative1Action myAction;
2695 QDeclarative1ParentChange *vpc = new QDeclarative1ParentChange;
2696 vpc->setObject(pc->object());
2697 vpc->setParent(d->via);
2698 myAction.event = vpc;
2700 viaData->actions << myAction;
2701 QDeclarative1Action dummyAction;
2702 QDeclarative1Action &xAction = pc->xIsSet() && i < actions.size()-1 ? actions[++i] : dummyAction;
2703 QDeclarative1Action &yAction = pc->yIsSet() && i < actions.size()-1 ? actions[++i] : dummyAction;
2704 QDeclarative1Action &sAction = pc->scaleIsSet() && i < actions.size()-1 ? actions[++i] : dummyAction;
2705 QDeclarative1Action &rAction = pc->rotationIsSet() && i < actions.size()-1 ? actions[++i] : dummyAction;
2706 QDeclarativeItem *target = pc->object();
2707 QDeclarativeItem *targetParent = action.reverseEvent ? pc->originalParent() : pc->parent();
2709 //### this mirrors the logic in QDeclarative1ParentChange.
2711 const QTransform &transform = targetParent->itemTransform(d->via, &ok);
2712 if (transform.type() >= QTransform::TxShear || !ok) {
2713 qmlInfo(this) << QDeclarative1ParentAnimation::tr("Unable to preserve appearance under complex transform");
2719 bool isRotate = (transform.type() == QTransform::TxRotate) || (transform.m11() < 0);
2720 if (ok && !isRotate) {
2721 if (transform.m11() == transform.m22())
2722 scale = transform.m11();
2724 qmlInfo(this) << QDeclarative1ParentAnimation::tr("Unable to preserve appearance under non-uniform scale");
2727 } else if (ok && isRotate) {
2728 if (transform.m11() == transform.m22())
2729 scale = qSqrt(transform.m11()*transform.m11() + transform.m12()*transform.m12());
2731 qmlInfo(this) << QDeclarative1ParentAnimation::tr("Unable to preserve appearance under non-uniform scale");
2736 rotation = atan2(transform.m12()/scale, transform.m11()/scale) * 180/M_PI;
2738 qmlInfo(this) << QDeclarative1ParentAnimation::tr("Unable to preserve appearance under scale of 0");
2743 const QPointF &point = transform.map(QPointF(xAction.toValue.toReal(),yAction.toValue.toReal()));
2744 qreal x = point.x();
2745 qreal y = point.y();
2746 if (ok && target->transformOrigin() != QDeclarativeItem::TopLeft) {
2747 qreal w = target->width();
2748 qreal h = target->height();
2749 if (pc->widthIsSet() && i < actions.size() - 1)
2750 w = actions[++i].toValue.toReal();
2751 if (pc->heightIsSet() && i < actions.size() - 1)
2752 h = actions[++i].toValue.toReal();
2753 const QPointF &transformOrigin
2754 = d->computeTransformOrigin(target->transformOrigin(), w,h);
2755 qreal tempxt = transformOrigin.x();
2756 qreal tempyt = transformOrigin.y();
2758 t.translate(-tempxt, -tempyt);
2760 t.scale(scale, scale);
2761 t.translate(tempxt, tempyt);
2762 const QPointF &offset = t.map(QPointF(0,0));
2768 //qDebug() << x << y << rotation << scale;
2769 xAction.toValue = x;
2770 yAction.toValue = y;
2771 sAction.toValue = sAction.toValue.toReal() * scale;
2772 rAction.toValue = rAction.toValue.toReal() + rotation;
2778 if (data->actions.count()) {
2779 if (direction == QDeclarative1AbstractAnimation::Forward) {
2780 d->startAction->setAnimAction(d->via ? viaData : data, QActionAnimation_1::DeleteWhenStopped);
2781 d->endAction->setAnimAction(d->via ? data : 0, QActionAnimation_1::DeleteWhenStopped);
2783 d->endAction->setAnimAction(d->via ? viaData : data, QActionAnimation_1::DeleteWhenStopped);
2784 d->startAction->setAnimAction(d->via ? data : 0, QActionAnimation_1::DeleteWhenStopped);
2793 //take care of any child animations
2794 bool valid = d->defaultProperty.isValid();
2795 for (int ii = 0; ii < d->animations.count(); ++ii) {
2797 d->animations.at(ii)->setDefaultTarget(d->defaultProperty);
2798 d->animations.at(ii)->transition(actions, modified, direction);
2803 QAbstractAnimation *QDeclarative1ParentAnimation::qtAnimation()
2805 Q_D(QDeclarative1ParentAnimation);
2806 return d->topLevelGroup;
2810 \qmlclass AnchorAnimation QDeclarative1AnchorAnimation
2811 \inqmlmodule QtQuick 1
2812 \ingroup qml-animation-transition
2815 \brief The AnchorAnimation element animates changes in anchor values.
2817 AnchorAnimation is used to animate an anchor change.
2819 In the following snippet we animate the addition of a right anchor to a \l Rectangle:
2821 \snippet doc/src/snippets/qtquick1/anchoranimation.qml 0
2823 For convenience, when an AnchorAnimation is used in a \l Transition, it will
2824 animate any AnchorChanges that have occurred during the state change.
2825 This can be overridden by setting a specific target item using the
2828 Like any other animation element, an AnchorAnimation can be applied in a
2829 number of ways, including transitions, behaviors and property value
2830 sources. The \l {QML Animation and Transitions} documentation shows a
2831 variety of methods for creating animations.
2833 \sa {QML Animation and Transitions}, AnchorChanges
2836 QDeclarative1AnchorAnimation::QDeclarative1AnchorAnimation(QObject *parent)
2837 : QDeclarative1AbstractAnimation(*(new QDeclarative1AnchorAnimationPrivate), parent)
2839 Q_D(QDeclarative1AnchorAnimation);
2840 d->va = new QDeclarative1BulkValueAnimator;
2841 QDeclarative_setParent_noEvent(d->va, this);
2844 QDeclarative1AnchorAnimation::~QDeclarative1AnchorAnimation()
2848 QAbstractAnimation *QDeclarative1AnchorAnimation::qtAnimation()
2850 Q_D(QDeclarative1AnchorAnimation);
2855 \qmlproperty list<Item> QtQuick1::AnchorAnimation::targets
2856 The items to reanchor.
2858 If no targets are specified all AnchorChanges will be
2859 animated by the AnchorAnimation.
2861 QDeclarativeListProperty<QDeclarativeItem> QDeclarative1AnchorAnimation::targets()
2863 Q_D(QDeclarative1AnchorAnimation);
2864 return QDeclarativeListProperty<QDeclarativeItem>(this, d->targets);
2868 \qmlproperty int QtQuick1::AnchorAnimation::duration
2869 This property holds the duration of the animation, in milliseconds.
2871 The default value is 250.
2873 int QDeclarative1AnchorAnimation::duration() const
2875 Q_D(const QDeclarative1AnchorAnimation);
2876 return d->va->duration();
2879 void QDeclarative1AnchorAnimation::setDuration(int duration)
2882 qmlInfo(this) << tr("Cannot set a duration of < 0");
2886 Q_D(QDeclarative1AnchorAnimation);
2887 if (d->va->duration() == duration)
2889 d->va->setDuration(duration);
2890 emit durationChanged(duration);
2894 \qmlproperty enumeration QtQuick1::AnchorAnimation::easing.type
2895 \qmlproperty real QtQuick1::AnchorAnimation::easing.amplitude
2896 \qmlproperty real QtQuick1::AnchorAnimation::easing.overshoot
2897 \qmlproperty real QtQuick1::AnchorAnimation::easing.period
2898 \brief the easing curve used for the animation.
2900 To specify an easing curve you need to specify at least the type. For some curves you can also specify
2901 amplitude, period and/or overshoot. The default easing curve is
2905 AnchorAnimation { easing.type: Easing.InOutQuad }
2908 See the \l{PropertyAnimation::easing.type} documentation for information
2909 about the different types of easing curves.
2912 QEasingCurve QDeclarative1AnchorAnimation::easing() const
2914 Q_D(const QDeclarative1AnchorAnimation);
2915 return d->va->easingCurve();
2918 void QDeclarative1AnchorAnimation::setEasing(const QEasingCurve &e)
2920 Q_D(QDeclarative1AnchorAnimation);
2921 if (d->va->easingCurve() == e)
2924 d->va->setEasingCurve(e);
2925 emit easingChanged(e);
2928 void QDeclarative1AnchorAnimation::transition(QDeclarative1StateActions &actions,
2929 QDeclarativeProperties &modified,
2930 TransitionDirection direction)
2933 Q_D(QDeclarative1AnchorAnimation);
2934 QDeclarative1AnimationPropertyUpdater *data = new QDeclarative1AnimationPropertyUpdater;
2935 data->interpolatorType = QMetaType::QReal;
2936 data->interpolator = d->interpolator;
2938 data->reverse = direction == Backward ? true : false;
2939 data->fromSourced = false;
2940 data->fromDefined = false;
2942 for (int ii = 0; ii < actions.count(); ++ii) {
2943 QDeclarative1Action &action = actions[ii];
2944 if (action.event && action.event->typeName() == QLatin1String("AnchorChanges")
2945 && (d->targets.isEmpty() || d->targets.contains(static_cast<QDeclarative1AnchorChanges*>(action.event)->object()))) {
2946 data->actions << static_cast<QDeclarative1AnchorChanges*>(action.event)->additionalActions();
2950 if (data->actions.count()) {
2951 if (!d->rangeIsSet) {
2952 d->va->setStartValue(qreal(0));
2953 d->va->setEndValue(qreal(1));
2954 d->rangeIsSet = true;
2956 d->va->setAnimValue(data, QAbstractAnimation::DeleteWhenStopped);
2957 d->va->setFromSourcedValue(&data->fromSourced);
2963 QDeclarative1ScriptActionPrivate::QDeclarative1ScriptActionPrivate()
2964 : QDeclarative1AbstractAnimationPrivate(), hasRunScriptScript(false), reversing(false), proxy(this), rsa(0) {}