diff options
Diffstat (limited to 'src/declarative/util/qdeclarativebind.cpp')
-rw-r--r-- | src/declarative/util/qdeclarativebind.cpp | 148 |
1 files changed, 120 insertions, 28 deletions
diff --git a/src/declarative/util/qdeclarativebind.cpp b/src/declarative/util/qdeclarativebind.cpp index 862c9dcf79..6038aca8d5 100644 --- a/src/declarative/util/qdeclarativebind.cpp +++ b/src/declarative/util/qdeclarativebind.cpp @@ -7,29 +7,29 @@ ** This file is part of the QtDeclarative module of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:LGPL$ -** No Commercial Usage -** This file contains pre-release code and may not be distributed. -** You may use this file in accordance with the terms and conditions -** contained in the Technology Preview License Agreement accompanying -** this package. -** ** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** ** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception +** rights. These rights are described in the Nokia Qt LGPL Exception ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. ** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. ** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. ** ** ** @@ -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 <qdeclarativeengine.h> #include <qdeclarativecontext.h> #include <qdeclarativeproperty.h> +#include <qdeclarativeinfo.h> #include <QtCore/qfile.h> #include <QtCore/qdebug.h> @@ -60,13 +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<bool> when; + bool componentComplete; QObject *obj; - QString prop; + QString propName; QDeclarativeNullableValue<QVariant> value; + QDeclarativeProperty prop; + QDeclarativeAbstractBinding *prevBind; }; @@ -76,6 +82,8 @@ public: \since 4.7 \brief The Binding element allows arbitrary property bindings to be created. + \section1 Binding to an inaccessible property + Sometimes it is necessary to bind to a property of an object that wasn't directly instantiated by QML - generally a property of a class exported to QML by C++. In these cases, regular property binding doesn't work. Binding @@ -91,6 +99,44 @@ public: Whenever the text in the TextEdit is updated, the C++ property will be updated also. + \section1 "Single-branch" conditional binding + + In some circumstances you may want to control the value of a property + only when a certain condition is true (and relinquish control in all + other cirumstances). This often isn't possible to accomplish with a direct + binding, as you need to supply values for all possible branches. + + \qml + // warning: "Unable to assign [undefined] to double value" + value: if (mouse.pressed) mouse.mouseX + \endqml + + The above example will produce a warning whenever we release the mouse, as the value + of the binding is undefined when the mouse isn't pressed. We can use the Binding + element to rewrite the above code and avoid the warning. + + \qml + Binding on value { + when: mouse.pressed + value: mouse.mouseX + } + \endqml + + The Binding element will also restore any previously set direct bindings on + the property. In that sense, it functions much like a simplified State. + + \qml + // this is equivilant to the above Binding + State { + name: "pressed" + when: mouse.pressed + PropertyChanges { + target: obj + value: mouse.mouseX + } + } + \endqml + If the binding target or binding property is changed, the bound value is immediately pushed onto the new target. @@ -117,6 +163,9 @@ QDeclarativeBind::~QDeclarativeBind() value: name; when: list.ListView.isCurrentItem } \endcode + + When the binding becomes inactive again, any direct bindings that were previously + set on the property will be restored. */ bool QDeclarativeBind::when() const { @@ -127,6 +176,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(); } @@ -145,7 +197,13 @@ 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); eval(); } @@ -157,13 +215,19 @@ void QDeclarativeBind::setObject(QObject *obj) QString QDeclarativeBind::property() const { Q_D(const QDeclarativeBind); - return d->prop; + return d->propName; } void QDeclarativeBind::setProperty(const QString &p) { Q_D(QDeclarativeBind); - d->prop = p; + 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); eval(); } @@ -182,11 +246,16 @@ 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(); } +void QDeclarativeBind::setTarget(const QDeclarativeProperty &p) +{ + Q_D(QDeclarativeBind); + d->prop = p; +} + void QDeclarativeBind::classBegin() { Q_D(QDeclarativeBind); @@ -197,17 +266,40 @@ void QDeclarativeBind::componentComplete() { Q_D(QDeclarativeBind); d->componentComplete = true; + if (!d->prop.isValid()) + d->prop = QDeclarativeProperty(d->obj, d->propName); eval(); } void QDeclarativeBind::eval() { Q_D(QDeclarativeBind); - if (!d->obj || d->value.isNull || !d->when || !d->componentComplete) + if (!d->prop.isValid() || d->value.isNull || !d->componentComplete) return; - QDeclarativeProperty prop(d->obj, d->prop); - prop.write(d->value.value); + 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); } QT_END_NAMESPACE |