src/declarative/util/qdeclarativebehavior.cpp
changeset 33 3e2da88830cd
parent 30 5dc02b23752f
child 37 758a864f9613
--- a/src/declarative/util/qdeclarativebehavior.cpp	Tue Jul 06 15:10:48 2010 +0300
+++ b/src/declarative/util/qdeclarativebehavior.cpp	Wed Aug 18 10:37:55 2010 +0300
@@ -58,7 +58,8 @@
 {
     Q_DECLARE_PUBLIC(QDeclarativeBehavior)
 public:
-    QDeclarativeBehaviorPrivate() : animation(0), enabled(true), finalized(false) {}
+    QDeclarativeBehaviorPrivate() : animation(0), enabled(true), finalized(false)
+      , blockRunningChanged(false) {}
 
     QDeclarativeProperty property;
     QVariant currentValue;
@@ -66,6 +67,7 @@
     QDeclarativeGuard<QDeclarativeAbstractAnimation> animation;
     bool enabled;
     bool finalized;
+    bool blockRunningChanged;
 };
 
 /*!
@@ -73,28 +75,21 @@
     \since 4.7
     \brief The Behavior element allows you to specify a default animation for a property change.
 
-    Behaviors provide one way to specify \l{qdeclarativeanimation.html}{animations} in QML.
+    A Behavior defines the default animation to be applied whenever a 
+    particular property value changes.
+
+    For example, the following Behavior defines a NumberAnimation to be run
+    whenever the \l Rectangle's \c width value changes. When the MouseArea
+    is clicked, the \c width is changed, triggering the behavior's animation:
 
-    In the example below, the rectangle will use a bounce easing curve over 200 millisecond for any changes to its y property:
-    \code
-    Rectangle {
-        width: 20; height: 20
-        color: "#00ff00"
-        y: 200  // initial value
-        Behavior on y {
-            NumberAnimation {
-                easing.type: Easing.OutBounce
-                easing.amplitude: 100
-                duration: 200
-            }
-        }
-    }
-    \endcode
+    \snippet doc/src/snippets/declarative/behavior.qml 0
 
-    Currently only a single Behavior may be specified for a property;
-    this Behavior can be enabled and disabled via the \l{enabled} property.
+    To run multiple animations within a Behavior, use ParallelAnimation or
+    SequentialAnimation.
 
-    \sa QtDeclarative
+    Note that a property cannot have more than one assigned Behavior.
+
+    \sa {Property Behaviors}, {declarative/animation/behaviors}{Behavior example}, QtDeclarative
 */
 
 
@@ -111,7 +106,7 @@
     \qmlproperty Animation Behavior::animation
     \default
 
-    The animation to use when the behavior is triggered.
+    This property holds the animation to run when the behavior is triggered.
 */
 
 QDeclarativeAbstractAnimation *QDeclarativeBehavior::animation()
@@ -132,12 +127,27 @@
     if (d->animation) {
         d->animation->setDefaultTarget(d->property);
         d->animation->setDisableUserControl();
+        connect(d->animation->qtAnimation(),
+                SIGNAL(stateChanged(QAbstractAnimation::State,QAbstractAnimation::State)),
+                this,
+                SLOT(qtAnimationStateChanged(QAbstractAnimation::State,QAbstractAnimation::State)));
     }
 }
 
+
+void QDeclarativeBehavior::qtAnimationStateChanged(QAbstractAnimation::State newState,QAbstractAnimation::State)
+{
+    Q_D(QDeclarativeBehavior);
+    if (!d->blockRunningChanged)
+        d->animation->notifyRunningChanged(newState == QAbstractAnimation::Running);
+}
+
+
 /*!
     \qmlproperty bool Behavior::enabled
-    Whether the Behavior will be triggered when the property it is tracking changes.
+
+    This property holds whether the behavior will be triggered when the tracked
+    property changes value.
 
     By default a Behavior is enabled.
 */
@@ -167,14 +177,17 @@
         return;
     }
 
-    if (value == d->targetValue)
+    if (d->animation->isRunning() && value == d->targetValue)
         return;
 
     d->currentValue = d->property.read();
     d->targetValue = value;
 
-    if (d->animation->qtAnimation()->duration() != -1)
+    if (d->animation->qtAnimation()->duration() != -1
+            && d->animation->qtAnimation()->state() != QAbstractAnimation::Stopped) {
+        d->blockRunningChanged = true;
         d->animation->qtAnimation()->stop();
+    }
 
     QDeclarativeStateOperation::ActionList actions;
     QDeclarativeAction action;
@@ -186,6 +199,7 @@
     QList<QDeclarativeProperty> after;
     d->animation->transition(actions, after, QDeclarativeAbstractAnimation::Forward);
     d->animation->qtAnimation()->start();
+    d->blockRunningChanged = false;
     if (!after.contains(d->property))
         QDeclarativePropertyPrivate::write(d->property, value, QDeclarativePropertyPrivate::BypassInterceptor | QDeclarativePropertyPrivate::DontRemoveBinding);    
 }