From 2d0e618db203d47bfe5bda50e9cf258a19e9745c Mon Sep 17 00:00:00 2001 From: Martin Jones Date: Mon, 20 Jul 2009 13:56:48 +1000 Subject: Add an inSync property that holds whether the target has "caught up" Useful for determining when the target has finished animating. --- examples/declarative/follow/follow.qml | 17 +++++++++++++++-- examples/declarative/snow/snow.qml | 9 +++++---- src/declarative/util/qmlfollow.cpp | 29 ++++++++++++++++++++++++----- src/declarative/util/qmlfollow.h | 3 +++ 4 files changed, 47 insertions(+), 11 deletions(-) diff --git a/examples/declarative/follow/follow.qml b/examples/declarative/follow/follow.qml index 21a0309..37a953c 100644 --- a/examples/declarative/follow/follow.qml +++ b/examples/declarative/follow/follow.qml @@ -40,11 +40,24 @@ Rect { id: Mouse anchors.fill: parent Rect { + id: "Ball" width: 20; height: 20 radius: 10 color: "#0000ff" - x: Follow { source: Mouse.mouseX-10; spring: 1.0; damping: 0.05 } - y: Follow { source: Mouse.mouseY-10; spring: 1.0; damping: 0.05 } + x: Follow { id: "F1"; source: Mouse.mouseX-10; spring: 1.0; damping: 0.05; epsilon: 0.25 } + y: Follow { id: "F2"; source: Mouse.mouseY-10; spring: 1.0; damping: 0.05; epsilon: 0.25 } + states: [ + State { + name: "following" + when: !F1.inSync || !F2.inSync + SetProperties { target: Ball; color: "#ff0000" } + } + ] + transitions: [ + Transition { + ColorAnimation { duration: 200 } + } + ] } } } diff --git a/examples/declarative/snow/snow.qml b/examples/declarative/snow/snow.qml index 2b413bb..f65e0ac 100644 --- a/examples/declarative/snow/snow.qml +++ b/examples/declarative/snow/snow.qml @@ -27,11 +27,12 @@ Rect { property real targetDeform: 0 property bool slowDeform: true - property real deform - deform: Follow { source: MyLayout.targetDeform; velocity: MyLayout.slowDeform?0.1:2 } + property real deform: 0 + deform: Follow { + id: "DeformFollow"; source: MyLayout.targetDeform; velocity: MyLayout.slowDeform?0.1:2 + onSyncChanged: if(inSync) { MyLayout.slowDeform = true; MyLayout.targetDeform = 0; } + } - onDeformChanged: if(deform == targetDeform) { slowDeform = true; targetDeform = 0; } - ImageBatch { offset: 0; ref: ImagePanel } x: Follow { source: MyLayout.targetX; velocity: 1000 } diff --git a/src/declarative/util/qmlfollow.cpp b/src/declarative/util/qmlfollow.cpp index b8e6685..851d4c2 100644 --- a/src/declarative/util/qmlfollow.cpp +++ b/src/declarative/util/qmlfollow.cpp @@ -56,7 +56,7 @@ class QmlFollowPrivate : public QObjectPrivate public: QmlFollowPrivate() : sourceValue(0), maxVelocity(0), lastTime(0) - , mass(1.0), spring(0.), damping(0.), velocity(0), epsilon(0.005), modulus(0.0), enabled(true), mode(Track), clock(this) {} + , mass(1.0), spring(0.), damping(0.), velocity(0), epsilon(0.01), modulus(0.0), enabled(true), mode(Track), clock(this) {} QmlMetaProperty property; qreal currentValue; @@ -164,8 +164,10 @@ void QmlFollowPrivate::tick(int time) } lastTime = time; } - emit q->valueChanged(currentValue); property.write(currentValue); + emit q->valueChanged(currentValue); + if (clock.state() != QAbstractAnimation::Running) + emit q->syncChanged(); } void QmlFollowPrivate::updateMode() @@ -183,12 +185,14 @@ void QmlFollowPrivate::start() if (!enabled) return; + Q_Q(QmlFollow); if (mode == QmlFollowPrivate::Track) { currentValue = sourceValue; property.write(currentValue); } else if (sourceValue != currentValue && clock.state() != QAbstractAnimation::Running) { lastTime = 0; clock.start(); // infinity?? + emit q->syncChanged(); } } @@ -261,8 +265,10 @@ qreal QmlFollow::sourceValue() const void QmlFollow::setSourceValue(qreal value) { Q_D(QmlFollow); - d->sourceValue = value; - d->start(); + if (d->sourceValue != value) { + d->sourceValue = value; + d->start(); + } } /*! @@ -338,7 +344,7 @@ void QmlFollow::setDamping(qreal damping) to 0 to be considered equal to zero. This will depend on the usage of the value. For pixel positions, 0.25 would suffice. For scale, 0.005 will suffice. - The default is 0.005. Small performance improvements can result in tuning this + The default is 0.01. Small performance improvements can result in tuning this value. */ qreal QmlFollow::epsilon() const @@ -397,6 +403,19 @@ void QmlFollow::setEnabled(bool enabled) d->stop(); } +/*! + \qmlproperty bool Follow::inSync + This property is true when target is equal to the source; otherwise + false. If inSync is true the target is not being animated. + + If \l enabled is false then inSync will also be false. +*/ +bool QmlFollow::inSync() const +{ + Q_D(const QmlFollow); + return d->enabled && d->clock.state() != QAbstractAnimation::Running; +} + qreal QmlFollow::value() const { Q_D(const QmlFollow); diff --git a/src/declarative/util/qmlfollow.h b/src/declarative/util/qmlfollow.h index 07e15e9..e0a588b 100644 --- a/src/declarative/util/qmlfollow.h +++ b/src/declarative/util/qmlfollow.h @@ -68,6 +68,7 @@ class Q_DECLARATIVE_EXPORT QmlFollow : public QmlPropertyValueSource, Q_PROPERTY(bool enabled READ enabled WRITE setEnabled) Q_PROPERTY(qreal followValue READ value NOTIFY valueChanged) Q_PROPERTY(qreal modulus READ modulus WRITE setModulus NOTIFY modulusChanged) + Q_PROPERTY(bool inSync READ inSync NOTIFY syncChanged) public: QmlFollow(QObject *parent=0); @@ -89,11 +90,13 @@ public: void setModulus(qreal modulus); bool enabled() const; void setEnabled(bool enabled); + bool inSync() const; qreal value() const; Q_SIGNALS: void valueChanged(qreal); + void syncChanged(); }; QT_END_NAMESPACE -- cgit v0.12