From 0c375ebc59e68121cff43d9dd65aec4593c34154 Mon Sep 17 00:00:00 2001 From: Michael Brasser Date: Mon, 14 Mar 2011 09:45:58 +1000 Subject: [PATCH] Restore original binding when Binding's when clause becomes false. Change-Id: I7e2ba1f6e3cd1e049ec2666ab8191cb91acbfe50 Reviewed-by: Martin Jones --- src/declarative/util/qdeclarativebind.cpp | 49 +++++++++++++++++++++++++--- 1 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/declarative/util/qdeclarativebind.cpp b/src/declarative/util/qdeclarativebind.cpp index ff30336..252dd65 100644 --- a/src/declarative/util/qdeclarativebind.cpp +++ b/src/declarative/util/qdeclarativebind.cpp @@ -42,10 +42,13 @@ #include "private/qdeclarativebind_p.h" #include "private/qdeclarativenullablevalue_p_p.h" +#include "private/qdeclarativeproperty_p.h" +#include "private/qdeclarativebinding_p.h" #include #include #include +#include #include #include @@ -60,14 +63,16 @@ QT_BEGIN_NAMESPACE class QDeclarativeBindPrivate : public QObjectPrivate { public: - QDeclarativeBindPrivate() : when(true), componentComplete(true), obj(0) {} + QDeclarativeBindPrivate() : componentComplete(true), obj(0), prevBind(0) {} + ~QDeclarativeBindPrivate() { if (prevBind) prevBind->destroy(); } - bool when : 1; - bool componentComplete : 1; + QDeclarativeNullableValue when; + bool componentComplete; QObject *obj; QString propName; QDeclarativeNullableValue value; QDeclarativeProperty prop; + QDeclarativeAbstractBinding *prevBind; }; @@ -128,6 +133,9 @@ bool QDeclarativeBind::when() const void QDeclarativeBind::setWhen(bool v) { Q_D(QDeclarativeBind); + if (!d->when.isNull && d->when == v) + return; + d->when = v; eval(); } @@ -146,6 +154,10 @@ QObject *QDeclarativeBind::object() void QDeclarativeBind::setObject(QObject *obj) { Q_D(QDeclarativeBind); + if (d->obj && d->obj != obj) { + qmlInfo(this) << tr("Cannot change the object assigned to a Binding."); + return; + } d->obj = obj; if (d->componentComplete) d->prop = QDeclarativeProperty(d->obj, d->propName); @@ -166,6 +178,10 @@ QString QDeclarativeBind::property() const void QDeclarativeBind::setProperty(const QString &p) { Q_D(QDeclarativeBind); + if (!d->propName.isEmpty() && d->propName != p) { + qmlInfo(this) << tr("Cannot change the property assigned to a Binding."); + return; + } d->propName = p; if (d->componentComplete) d->prop = QDeclarativeProperty(d->obj, d->propName); @@ -187,8 +203,7 @@ QVariant QDeclarativeBind::value() const void QDeclarativeBind::setValue(const QVariant &v) { Q_D(QDeclarativeBind); - d->value.value = v; - d->value.isNull = false; + d->value = v; eval(); } @@ -216,9 +231,31 @@ void QDeclarativeBind::componentComplete() void QDeclarativeBind::eval() { Q_D(QDeclarativeBind); - if (!d->prop.isValid() || d->value.isNull || !d->when || !d->componentComplete) + if (!d->prop.isValid() || d->value.isNull || !d->componentComplete) return; + if (d->when.isValid()) { + if (!d->when) { + //restore any previous binding + if (d->prevBind) { + QDeclarativeAbstractBinding *tmp; + tmp = QDeclarativePropertyPrivate::setBinding(d->prop, d->prevBind); + if (tmp) //should this ever be true? + tmp->destroy(); + d->prevBind = 0; + } + return; + } + + //save any set binding for restoration + QDeclarativeAbstractBinding *tmp; + tmp = QDeclarativePropertyPrivate::setBinding(d->prop, 0); + if (tmp && d->prevBind) + d->prevBind->destroy(); + else if (!d->prevBind) + d->prevBind = tmp; + } + d->prop.write(d->value.value); } -- 1.7.2.5