1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "private/qdeclarativebind_p.h"
43 
44 #include "private/qdeclarativenullablevalue_p_p.h"
45 #include "private/qdeclarativeguard_p.h"
46 
47 #include <qdeclarativeengine.h>
48 #include <qdeclarativecontext.h>
49 #include <qdeclarativeproperty.h>
50 
51 #include <QtCore/qfile.h>
52 #include <QtCore/qdebug.h>
53 #include <QtScript/qscriptvalue.h>
54 #include <QtScript/qscriptcontext.h>
55 #include <QtScript/qscriptengine.h>
56 
57 #include <private/qobject_p.h>
58 
59 QT_BEGIN_NAMESPACE
60 
61 class QDeclarativeBindPrivate : public QObjectPrivate
62 {
63 public:
QDeclarativeBindPrivate()64     QDeclarativeBindPrivate() : when(true), componentComplete(true), obj(0) {}
65 
66     bool when : 1;
67     bool componentComplete : 1;
68     QDeclarativeGuard<QObject> obj;
69     QString prop;
70     QDeclarativeNullableValue<QVariant> value;
71 };
72 
73 
74 /*!
75     \qmlclass Binding QDeclarativeBind
76     \ingroup qml-working-with-data
77     \since 4.7
78     \brief The Binding element allows arbitrary property bindings to be created.
79 
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.
84 
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
87     like this.
88     \code
89     TextEdit { id: myTextField; text: "Please type here..." }
90     Binding { target: app; property: "enteredText"; value: myTextField.text }
91     \endcode
92     Whenever the text in the TextEdit is updated, the C++ property will be
93     updated also.
94 
95     If the binding target or binding property is changed, the bound value is
96     immediately pushed onto the new target.
97 
98     \sa QtDeclarative
99 */
QDeclarativeBind(QObject * parent)100 QDeclarativeBind::QDeclarativeBind(QObject *parent)
101     : QObject(*(new QDeclarativeBindPrivate), parent)
102 {
103 }
104 
~QDeclarativeBind()105 QDeclarativeBind::~QDeclarativeBind()
106 {
107 }
108 
109 /*!
110     \qmlproperty bool Binding::when
111 
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.
114 
115     \code
116     Binding {
117         target: contactName; property: 'text'
118         value: name; when: list.ListView.isCurrentItem
119     }
120     \endcode
121 */
when() const122 bool QDeclarativeBind::when() const
123 {
124     Q_D(const QDeclarativeBind);
125     return d->when;
126 }
127 
setWhen(bool v)128 void QDeclarativeBind::setWhen(bool v)
129 {
130     Q_D(QDeclarativeBind);
131     d->when = v;
132     eval();
133 }
134 
135 /*!
136     \qmlproperty Object Binding::target
137 
138     The object to be updated.
139 */
object()140 QObject *QDeclarativeBind::object()
141 {
142     Q_D(const QDeclarativeBind);
143     return d->obj;
144 }
145 
setObject(QObject * obj)146 void QDeclarativeBind::setObject(QObject *obj)
147 {
148     Q_D(QDeclarativeBind);
149     d->obj = obj;
150     eval();
151 }
152 
153 /*!
154     \qmlproperty string Binding::property
155 
156     The property to be updated.
157 */
property() const158 QString QDeclarativeBind::property() const
159 {
160     Q_D(const QDeclarativeBind);
161     return d->prop;
162 }
163 
setProperty(const QString & p)164 void QDeclarativeBind::setProperty(const QString &p)
165 {
166     Q_D(QDeclarativeBind);
167     d->prop = p;
168     eval();
169 }
170 
171 /*!
172     \qmlproperty any Binding::value
173 
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.
176 */
value() const177 QVariant QDeclarativeBind::value() const
178 {
179     Q_D(const QDeclarativeBind);
180     return d->value.value;
181 }
182 
setValue(const QVariant & v)183 void QDeclarativeBind::setValue(const QVariant &v)
184 {
185     Q_D(QDeclarativeBind);
186     d->value.value = v;
187     d->value.isNull = false;
188     eval();
189 }
190 
classBegin()191 void QDeclarativeBind::classBegin()
192 {
193     Q_D(QDeclarativeBind);
194     d->componentComplete = false;
195 }
196 
componentComplete()197 void QDeclarativeBind::componentComplete()
198 {
199     Q_D(QDeclarativeBind);
200     d->componentComplete = true;
201     eval();
202 }
203 
eval()204 void QDeclarativeBind::eval()
205 {
206     Q_D(QDeclarativeBind);
207     if (!d->obj || d->value.isNull || !d->when || !d->componentComplete)
208         return;
209 
210     QDeclarativeProperty prop(d->obj, d->prop);
211     prop.write(d->value.value);
212 }
213 
214 QT_END_NAMESPACE
215