Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / src / qtquick1 / util / qdeclarativebind.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "QtQuick1/private/qdeclarativebind_p.h"
43
44 #include "QtDeclarative/private/qdeclarativenullablevalue_p_p.h"
45 #include "QtDeclarative/private/qdeclarativeguard_p.h"
46
47 #include <QtDeclarative/qdeclarativeengine.h>
48 #include <QtDeclarative/qdeclarativecontext.h>
49 #include <QtDeclarative/qdeclarativeproperty.h>
50
51 #include <QtCore/qfile.h>
52 #include <QtCore/qdebug.h>
53
54 #include <private/qobject_p.h>
55
56 QT_BEGIN_NAMESPACE
57
58
59
60 class QDeclarative1BindPrivate : public QObjectPrivate
61 {
62 public:
63     QDeclarative1BindPrivate() : when(true), componentComplete(true), obj(0) {}
64
65     bool when : 1;
66     bool componentComplete : 1;
67     QDeclarativeGuard<QObject> obj;
68     QString prop;
69     QDeclarativeNullableValue<QVariant> value;
70 };
71
72
73 /*!
74     \qmlclass Binding QDeclarative1Bind
75     \inqmlmodule QtQuick 1
76     \ingroup qml-working-with-data
77     \since QtQuick 1.0
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 */
100 QDeclarative1Bind::QDeclarative1Bind(QObject *parent)
101     : QObject(*(new QDeclarative1BindPrivate), parent)
102 {
103 }
104
105 QDeclarative1Bind::~QDeclarative1Bind()
106 {
107 }
108
109 /*!
110     \qmlproperty bool QtQuick1::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 */
122 bool QDeclarative1Bind::when() const
123 {
124     Q_D(const QDeclarative1Bind);
125     return d->when;
126 }
127
128 void QDeclarative1Bind::setWhen(bool v)
129 {
130     Q_D(QDeclarative1Bind);
131     d->when = v;
132     eval();
133 }
134
135 /*!
136     \qmlproperty Object QtQuick1::Binding::target
137
138     The object to be updated.
139 */
140 QObject *QDeclarative1Bind::object()
141 {
142     Q_D(const QDeclarative1Bind);
143     return d->obj;
144 }
145
146 void QDeclarative1Bind::setObject(QObject *obj)
147 {
148     Q_D(QDeclarative1Bind);
149     d->obj = obj;
150     eval();
151 }
152
153 /*!
154     \qmlproperty string QtQuick1::Binding::property
155
156     The property to be updated.
157 */
158 QString QDeclarative1Bind::property() const
159 {
160     Q_D(const QDeclarative1Bind);
161     return d->prop;
162 }
163
164 void QDeclarative1Bind::setProperty(const QString &p)
165 {
166     Q_D(QDeclarative1Bind);
167     d->prop = p;
168     eval();
169 }
170
171 /*!
172     \qmlproperty any QtQuick1::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 */
177 QVariant QDeclarative1Bind::value() const
178 {
179     Q_D(const QDeclarative1Bind);
180     return d->value.value;
181 }
182
183 void QDeclarative1Bind::setValue(const QVariant &v)
184 {
185     Q_D(QDeclarative1Bind);
186     d->value.value = v;
187     d->value.isNull = false;
188     eval();
189 }
190
191 void QDeclarative1Bind::classBegin()
192 {
193     Q_D(QDeclarative1Bind);
194     d->componentComplete = false;
195 }
196
197 void QDeclarative1Bind::componentComplete()
198 {
199     Q_D(QDeclarative1Bind);
200     d->componentComplete = true;
201     eval();
202 }
203
204 void QDeclarative1Bind::eval()
205 {
206     Q_D(QDeclarative1Bind);
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
215
216 QT_END_NAMESPACE