1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "QtQuick1/private/qdeclarativebind_p.h"
44 #include "QtDeclarative/private/qdeclarativenullablevalue_p_p.h"
45 #include "QtDeclarative/private/qdeclarativeguard_p.h"
47 #include <QtDeclarative/qdeclarativeengine.h>
48 #include <QtDeclarative/qdeclarativecontext.h>
49 #include <QtDeclarative/qdeclarativeproperty.h>
51 #include <QtCore/qfile.h>
52 #include <QtCore/qdebug.h>
54 #include <private/qobject_p.h>
60 class QDeclarative1BindPrivate : public QObjectPrivate
63 QDeclarative1BindPrivate() : when(true), componentComplete(true), obj(0) {}
66 bool componentComplete : 1;
67 QDeclarativeGuard<QObject> obj;
69 QDeclarativeNullableValue<QVariant> value;
74 \qmlclass Binding QDeclarative1Bind
75 \inqmlmodule QtQuick 1
76 \ingroup qml-working-with-data
78 \brief The Binding element allows arbitrary property bindings to be created.
80 Sometimes it is necessary to bind to a property of an object that wasn't
81 directly instantiated by QML - generally a property of a class exported
82 to QML by C++. In these cases, regular property binding doesn't work. Binding
83 allows you to bind any value to any property.
85 For example, imagine a C++ application that maps an "app.enteredText"
86 property into QML. You could use Binding to update the enteredText property
89 TextEdit { id: myTextField; text: "Please type here..." }
90 Binding { target: app; property: "enteredText"; value: myTextField.text }
92 Whenever the text in the TextEdit is updated, the C++ property will be
95 If the binding target or binding property is changed, the bound value is
96 immediately pushed onto the new target.
100 QDeclarative1Bind::QDeclarative1Bind(QObject *parent)
101 : QObject(*(new QDeclarative1BindPrivate), parent)
105 QDeclarative1Bind::~QDeclarative1Bind()
110 \qmlproperty bool QtQuick1::Binding::when
112 This property holds when the binding is active.
113 This should be set to an expression that evaluates to true when you want the binding to be active.
117 target: contactName; property: 'text'
118 value: name; when: list.ListView.isCurrentItem
122 bool QDeclarative1Bind::when() const
124 Q_D(const QDeclarative1Bind);
128 void QDeclarative1Bind::setWhen(bool v)
130 Q_D(QDeclarative1Bind);
136 \qmlproperty Object QtQuick1::Binding::target
138 The object to be updated.
140 QObject *QDeclarative1Bind::object()
142 Q_D(const QDeclarative1Bind);
146 void QDeclarative1Bind::setObject(QObject *obj)
148 Q_D(QDeclarative1Bind);
154 \qmlproperty string QtQuick1::Binding::property
156 The property to be updated.
158 QString QDeclarative1Bind::property() const
160 Q_D(const QDeclarative1Bind);
164 void QDeclarative1Bind::setProperty(const QString &p)
166 Q_D(QDeclarative1Bind);
172 \qmlproperty any QtQuick1::Binding::value
174 The value to be set on the target object and property. This can be a
175 constant (which isn't very useful), or a bound expression.
177 QVariant QDeclarative1Bind::value() const
179 Q_D(const QDeclarative1Bind);
180 return d->value.value;
183 void QDeclarative1Bind::setValue(const QVariant &v)
185 Q_D(QDeclarative1Bind);
187 d->value.isNull = false;
191 void QDeclarative1Bind::classBegin()
193 Q_D(QDeclarative1Bind);
194 d->componentComplete = false;
197 void QDeclarative1Bind::componentComplete()
199 Q_D(QDeclarative1Bind);
200 d->componentComplete = true;
204 void QDeclarative1Bind::eval()
206 Q_D(QDeclarative1Bind);
207 if (!d->obj || d->value.isNull || !d->when || !d->componentComplete)
210 QDeclarativeProperty prop(d->obj, d->prop);
211 prop.write(d->value.value);