From 68e3cab8a8183a5a88e5be092471a05692e05afe Mon Sep 17 00:00:00 2001 From: Michael Brasser Date: Mon, 30 Nov 2009 11:29:39 +1000 Subject: Support disabling a Behavior. --- src/declarative/util/qmlbehavior.cpp | 34 ++++++++++++++++++++-- src/declarative/util/qmlbehavior_p.h | 7 +++++ tests/auto/declarative/behaviors/data/disabled.qml | 27 +++++++++++++++++ tests/auto/declarative/behaviors/tst_behaviors.cpp | 15 ++++++++++ 4 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 tests/auto/declarative/behaviors/data/disabled.qml diff --git a/src/declarative/util/qmlbehavior.cpp b/src/declarative/util/qmlbehavior.cpp index 711c70d..23d3838 100644 --- a/src/declarative/util/qmlbehavior.cpp +++ b/src/declarative/util/qmlbehavior.cpp @@ -55,18 +55,21 @@ class QmlBehaviorPrivate : public QObjectPrivate { Q_DECLARE_PUBLIC(QmlBehavior) public: - QmlBehaviorPrivate() : animation(0) {} + QmlBehaviorPrivate() : animation(0), enabled(true) {} QmlMetaProperty property; QVariant currentValue; QmlAbstractAnimation *animation; + bool enabled; }; /*! \qmlclass Behavior QmlBehavior \brief The Behavior element allows you to specify a default animation for a property change. - In example below, the rect will use a bounce easing curve over 200 millisecond for any changes to its y property: + Behaviors provide one way to specify \l{qmlanimation.html}{animations} in QML. + + In the example below, the rect will use a bounce easing curve over 200 millisecond for any changes to its y property: \code Rectangle { width: 20; height: 20 @@ -80,6 +83,9 @@ public: } } \endcode + + Currently only a single Behavior may be specified for a property; + this Behavior can be enabled and disabled via the \l{enabled} property. */ @@ -118,10 +124,32 @@ void QmlBehavior::setAnimation(QmlAbstractAnimation *animation) d->animation->setTarget(d->property); } +/*! + \qmlproperty bool Behavior::enabled + Whether the Behavior will be triggered when the property it is tracking changes. + + By default a Behavior is enabled. +*/ + +bool QmlBehavior::enabled() const +{ + Q_D(const QmlBehavior); + return d->enabled; +} + +void QmlBehavior::setEnabled(bool enabled) +{ + Q_D(QmlBehavior); + if (d->enabled == enabled) + return; + d->enabled = enabled; + emit enabledChanged(); +} + void QmlBehavior::write(const QVariant &value) { Q_D(QmlBehavior); - if (!d->animation) { + if (!d->animation || !d->enabled) { d->property.write(value, QmlMetaProperty::BypassInterceptor | QmlMetaProperty::DontRemoveBinding); return; } diff --git a/src/declarative/util/qmlbehavior_p.h b/src/declarative/util/qmlbehavior_p.h index b61df32..581a0a8 100644 --- a/src/declarative/util/qmlbehavior_p.h +++ b/src/declarative/util/qmlbehavior_p.h @@ -63,6 +63,7 @@ class Q_DECLARATIVE_EXPORT QmlBehavior : public QObject, public QmlPropertyValue Q_INTERFACES(QmlPropertyValueInterceptor) Q_CLASSINFO("DefaultProperty", "animation") Q_PROPERTY(QmlAbstractAnimation *animation READ animation WRITE setAnimation) + Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged) public: QmlBehavior(QObject *parent=0); @@ -73,6 +74,12 @@ public: QmlAbstractAnimation *animation(); void setAnimation(QmlAbstractAnimation *); + + bool enabled() const; + void setEnabled(bool enabled); + +Q_SIGNALS: + void enabledChanged(); }; QT_END_NAMESPACE diff --git a/tests/auto/declarative/behaviors/data/disabled.qml b/tests/auto/declarative/behaviors/data/disabled.qml new file mode 100644 index 0000000..e7b8d51 --- /dev/null +++ b/tests/auto/declarative/behaviors/data/disabled.qml @@ -0,0 +1,27 @@ +import Qt 4.6 +Rectangle { + width: 400 + height: 400 + Rectangle { + id: rect + objectName: "MyRect" + width: 100; height: 100; color: "green" + x: Behavior { + objectName: "MyBehavior"; + enabled: false + NumberAnimation { duration: 200; } + } + } + MouseRegion { + id: clicker + anchors.fill: parent + } + states: State { + name: "moved" + when: clicker.pressed + PropertyChanges { + target: rect + x: 200 + } + } +} diff --git a/tests/auto/declarative/behaviors/tst_behaviors.cpp b/tests/auto/declarative/behaviors/tst_behaviors.cpp index 6343968..e1376ce 100644 --- a/tests/auto/declarative/behaviors/tst_behaviors.cpp +++ b/tests/auto/declarative/behaviors/tst_behaviors.cpp @@ -64,6 +64,7 @@ private slots: void emptyBehavior(); void nonSelectingBehavior(); void reassignedAnimation(); + void disabled(); }; void tst_behaviors::simpleBehavior() @@ -243,6 +244,20 @@ void tst_behaviors::reassignedAnimation() rect->findChild("MyBehavior"))->animation())->duration(), 200); } +void tst_behaviors::disabled() +{ + QmlEngine engine; + QmlComponent c(&engine, QUrl("file://" SRCDIR "/data/disabled.qml")); + QmlGraphicsRectangle *rect = qobject_cast(c.create()); + QVERIFY(rect); + QCOMPARE(rect->findChild("MyBehavior")->enabled(), false); + + rect->setState("moved"); + qreal x = qobject_cast(rect->findChild("MyRect"))->x(); + QCOMPARE(x, qreal(200)); //should change immediately + +} + QTEST_MAIN(tst_behaviors) #include "tst_behaviors.moc" -- cgit v0.12