source: trunk/src/declarative/util/qdeclarativebind.cpp@ 986

Last change on this file since 986 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 5.7 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 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** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at qt-info@nokia.com.
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
58QT_BEGIN_NAMESPACE
59
60class QDeclarativeBindPrivate : public QObjectPrivate
61{
62public:
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 \ingroup qml-working-with-data
76 \since 4.7
77 \brief The Binding element allows arbitrary property bindings to be created.
78
79 Sometimes it is necessary to bind to a property of an object that wasn't
80 directly instantiated by QML - generally a property of a class exported
81 to QML by C++. In these cases, regular property binding doesn't work. Binding
82 allows you to bind any value to any property.
83
84 For example, imagine a C++ application that maps an "app.enteredText"
85 property into QML. You could use Binding to update the enteredText property
86 like this.
87 \code
88 TextEdit { id: myTextField; text: "Please type here..." }
89 Binding { target: app; property: "enteredText"; value: myTextField.text }
90 \endcode
91 Whenever the text in the TextEdit is updated, the C++ property will be
92 updated also.
93
94 If the binding target or binding property is changed, the bound value is
95 immediately pushed onto the new target.
96
97 \sa QtDeclarative
98*/
99QDeclarativeBind::QDeclarativeBind(QObject *parent)
100 : QObject(*(new QDeclarativeBindPrivate), parent)
101{
102}
103
104QDeclarativeBind::~QDeclarativeBind()
105{
106}
107
108/*!
109 \qmlproperty bool Binding::when
110
111 This property holds when the binding is active.
112 This should be set to an expression that evaluates to true when you want the binding to be active.
113
114 \code
115 Binding {
116 target: contactName; property: 'text'
117 value: name; when: list.ListView.isCurrentItem
118 }
119 \endcode
120*/
121bool QDeclarativeBind::when() const
122{
123 Q_D(const QDeclarativeBind);
124 return d->when;
125}
126
127void QDeclarativeBind::setWhen(bool v)
128{
129 Q_D(QDeclarativeBind);
130 d->when = v;
131 eval();
132}
133
134/*!
135 \qmlproperty Object Binding::target
136
137 The object to be updated.
138*/
139QObject *QDeclarativeBind::object()
140{
141 Q_D(const QDeclarativeBind);
142 return d->obj;
143}
144
145void QDeclarativeBind::setObject(QObject *obj)
146{
147 Q_D(QDeclarativeBind);
148 d->obj = obj;
149 eval();
150}
151
152/*!
153 \qmlproperty string Binding::property
154
155 The property to be updated.
156*/
157QString QDeclarativeBind::property() const
158{
159 Q_D(const QDeclarativeBind);
160 return d->prop;
161}
162
163void QDeclarativeBind::setProperty(const QString &p)
164{
165 Q_D(QDeclarativeBind);
166 d->prop = p;
167 eval();
168}
169
170/*!
171 \qmlproperty any Binding::value
172
173 The value to be set on the target object and property. This can be a
174 constant (which isn't very useful), or a bound expression.
175*/
176QVariant QDeclarativeBind::value() const
177{
178 Q_D(const QDeclarativeBind);
179 return d->value.value;
180}
181
182void QDeclarativeBind::setValue(const QVariant &v)
183{
184 Q_D(QDeclarativeBind);
185 d->value.value = v;
186 d->value.isNull = false;
187 eval();
188}
189
190void QDeclarativeBind::classBegin()
191{
192 Q_D(QDeclarativeBind);
193 d->componentComplete = false;
194}
195
196void QDeclarativeBind::componentComplete()
197{
198 Q_D(QDeclarativeBind);
199 d->componentComplete = true;
200 eval();
201}
202
203void QDeclarativeBind::eval()
204{
205 Q_D(QDeclarativeBind);
206 if (!d->obj || d->value.isNull || !d->when || !d->componentComplete)
207 return;
208
209 QDeclarativeProperty prop(d->obj, d->prop);
210 prop.write(d->value.value);
211}
212
213QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.