#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>
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 propName;
QDeclarativeNullableValue<QVariant> value;
QDeclarativeProperty prop;
+ QDeclarativeAbstractBinding *prevBind;
};
void QDeclarativeBind::setWhen(bool v)
{
Q_D(QDeclarativeBind);
+ if (!d->when.isNull && d->when == v)
+ return;
+
d->when = v;
eval();
}
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);
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);
void QDeclarativeBind::setValue(const QVariant &v)
{
Q_D(QDeclarativeBind);
- d->value.value = v;
- d->value.isNull = false;
+ d->value = v;
eval();
}
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);
}