diff options
author | Michael Brasser <michael.brasser@nokia.com> | 2009-11-30 01:29:39 (GMT) |
---|---|---|
committer | Michael Brasser <michael.brasser@nokia.com> | 2009-11-30 01:30:36 (GMT) |
commit | 68e3cab8a8183a5a88e5be092471a05692e05afe (patch) | |
tree | be1dabee49f3d8444298778fd440daa5a2a7c052 | |
parent | 85b3e8fab4a644f94a2953885f1e68369b070e64 (diff) | |
download | Qt-68e3cab8a8183a5a88e5be092471a05692e05afe.zip Qt-68e3cab8a8183a5a88e5be092471a05692e05afe.tar.gz Qt-68e3cab8a8183a5a88e5be092471a05692e05afe.tar.bz2 |
Support disabling a Behavior.
-rw-r--r-- | src/declarative/util/qmlbehavior.cpp | 34 | ||||
-rw-r--r-- | src/declarative/util/qmlbehavior_p.h | 7 | ||||
-rw-r--r-- | tests/auto/declarative/behaviors/data/disabled.qml | 27 | ||||
-rw-r--r-- | tests/auto/declarative/behaviors/tst_behaviors.cpp | 15 |
4 files changed, 80 insertions, 3 deletions
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<QmlBehavior*>("MyBehavior"))->animation())->duration(), 200); } +void tst_behaviors::disabled() +{ + QmlEngine engine; + QmlComponent c(&engine, QUrl("file://" SRCDIR "/data/disabled.qml")); + QmlGraphicsRectangle *rect = qobject_cast<QmlGraphicsRectangle*>(c.create()); + QVERIFY(rect); + QCOMPARE(rect->findChild<QmlBehavior*>("MyBehavior")->enabled(), false); + + rect->setState("moved"); + qreal x = qobject_cast<QmlGraphicsRectangle*>(rect->findChild<QmlGraphicsRectangle*>("MyRect"))->x(); + QCOMPARE(x, qreal(200)); //should change immediately + +} + QTEST_MAIN(tst_behaviors) #include "tst_behaviors.moc" |