|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the QtDeclarative module of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "private/qdeclarativebind_p.h" |
|
43 |
|
44 #include "private/qdeclarativenullablevalue_p_p.h" |
|
45 |
|
46 #include <qdeclarativeengine.h> |
|
47 #include <qdeclarativecontext.h> |
|
48 #include <qdeclarativeproperty.h> |
|
49 |
|
50 #include <QtCore/qfile.h> |
|
51 #include <QtCore/qdebug.h> |
|
52 #include <QtScript/qscriptvalue.h> |
|
53 #include <QtScript/qscriptcontext.h> |
|
54 #include <QtScript/qscriptengine.h> |
|
55 |
|
56 #include <private/qobject_p.h> |
|
57 |
|
58 QT_BEGIN_NAMESPACE |
|
59 |
|
60 class QDeclarativeBindPrivate : public QObjectPrivate |
|
61 { |
|
62 public: |
|
63 QDeclarativeBindPrivate() : when(true), componentComplete(true), obj(0) {} |
|
64 |
|
65 bool when : 1; |
|
66 bool componentComplete : 1; |
|
67 QObject *obj; |
|
68 QString prop; |
|
69 QDeclarativeNullableValue<QVariant> value; |
|
70 }; |
|
71 |
|
72 |
|
73 /*! |
|
74 \qmlclass Binding QDeclarativeBind |
|
75 \since 4.7 |
|
76 \brief The Binding element allows arbitrary property bindings to be created. |
|
77 |
|
78 Sometimes it is necessary to bind to a property of an object that wasn't |
|
79 directly instantiated by QML - generally a property of a class exported |
|
80 to QML by C++. In these cases, regular property binding doesn't work. Binding |
|
81 allows you to bind any value to any property. |
|
82 |
|
83 For example, imagine a C++ application that maps an "app.enteredText" |
|
84 property into QML. You could use Binding to update the enteredText property |
|
85 like this. |
|
86 \code |
|
87 TextEdit { id: myTextField; text: "Please type here..." } |
|
88 Binding { target: app; property: "enteredText"; value: myTextField.text } |
|
89 \endcode |
|
90 Whenever the text in the TextEdit is updated, the C++ property will be |
|
91 updated also. |
|
92 |
|
93 If the binding target or binding property is changed, the bound value is |
|
94 immediately pushed onto the new target. |
|
95 |
|
96 \sa QtDeclarative |
|
97 */ |
|
98 /*! |
|
99 \internal |
|
100 \class QDeclarativeBind |
|
101 \brief The QDeclarativeBind class allows arbitrary property bindings to be created. |
|
102 |
|
103 Simple bindings are usually earier to do in-place rather than creating a |
|
104 QDeclarativeBind item. For that reason, QDeclarativeBind is usually used to transfer property information |
|
105 from Qml to C++. |
|
106 |
|
107 \sa cppqml |
|
108 */ |
|
109 QDeclarativeBind::QDeclarativeBind(QObject *parent) |
|
110 : QObject(*(new QDeclarativeBindPrivate), parent) |
|
111 { |
|
112 } |
|
113 |
|
114 QDeclarativeBind::~QDeclarativeBind() |
|
115 { |
|
116 } |
|
117 |
|
118 /*! |
|
119 \qmlproperty bool Binding::when |
|
120 |
|
121 This property holds when the binding is active. |
|
122 This should be set to an expression that evaluates to true when you want the binding to be active. |
|
123 |
|
124 \code |
|
125 Binding { |
|
126 target: contactName; property: 'text' |
|
127 value: name; when: list.ListView.isCurrentItem |
|
128 } |
|
129 \endcode |
|
130 */ |
|
131 bool QDeclarativeBind::when() const |
|
132 { |
|
133 Q_D(const QDeclarativeBind); |
|
134 return d->when; |
|
135 } |
|
136 |
|
137 void QDeclarativeBind::setWhen(bool v) |
|
138 { |
|
139 Q_D(QDeclarativeBind); |
|
140 d->when = v; |
|
141 eval(); |
|
142 } |
|
143 |
|
144 /*! |
|
145 \qmlproperty Object Binding::target |
|
146 |
|
147 The object to be updated. |
|
148 */ |
|
149 QObject *QDeclarativeBind::object() |
|
150 { |
|
151 Q_D(const QDeclarativeBind); |
|
152 return d->obj; |
|
153 } |
|
154 |
|
155 void QDeclarativeBind::setObject(QObject *obj) |
|
156 { |
|
157 Q_D(QDeclarativeBind); |
|
158 d->obj = obj; |
|
159 eval(); |
|
160 } |
|
161 |
|
162 /*! |
|
163 \qmlproperty string Binding::property |
|
164 |
|
165 The property to be updated. |
|
166 */ |
|
167 QString QDeclarativeBind::property() const |
|
168 { |
|
169 Q_D(const QDeclarativeBind); |
|
170 return d->prop; |
|
171 } |
|
172 |
|
173 void QDeclarativeBind::setProperty(const QString &p) |
|
174 { |
|
175 Q_D(QDeclarativeBind); |
|
176 d->prop = p; |
|
177 eval(); |
|
178 } |
|
179 |
|
180 /*! |
|
181 \qmlproperty any Binding::value |
|
182 |
|
183 The value to be set on the target object and property. This can be a |
|
184 constant (which isn't very useful), or a bound expression. |
|
185 */ |
|
186 QVariant QDeclarativeBind::value() const |
|
187 { |
|
188 Q_D(const QDeclarativeBind); |
|
189 return d->value.value; |
|
190 } |
|
191 |
|
192 void QDeclarativeBind::setValue(const QVariant &v) |
|
193 { |
|
194 Q_D(QDeclarativeBind); |
|
195 d->value.value = v; |
|
196 d->value.isNull = false; |
|
197 eval(); |
|
198 } |
|
199 |
|
200 void QDeclarativeBind::classBegin() |
|
201 { |
|
202 Q_D(QDeclarativeBind); |
|
203 d->componentComplete = false; |
|
204 } |
|
205 |
|
206 void QDeclarativeBind::componentComplete() |
|
207 { |
|
208 Q_D(QDeclarativeBind); |
|
209 d->componentComplete = true; |
|
210 eval(); |
|
211 } |
|
212 |
|
213 void QDeclarativeBind::eval() |
|
214 { |
|
215 Q_D(QDeclarativeBind); |
|
216 if (!d->obj || d->value.isNull || !d->when || !d->componentComplete) |
|
217 return; |
|
218 |
|
219 QDeclarativeProperty prop(d->obj, d->prop); |
|
220 prop.write(d->value.value); |
|
221 } |
|
222 |
|
223 QT_END_NAMESPACE |