|
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/qdeclarativeconnections_p.h" |
|
43 |
|
44 #include <qdeclarativeexpression.h> |
|
45 #include <qdeclarativeproperty_p.h> |
|
46 #include <qdeclarativeboundsignal_p.h> |
|
47 #include <qdeclarativecontext.h> |
|
48 #include <qdeclarativeinfo.h> |
|
49 |
|
50 #include <QtCore/qdebug.h> |
|
51 #include <QtCore/qstringlist.h> |
|
52 |
|
53 #include <private/qobject_p.h> |
|
54 |
|
55 QT_BEGIN_NAMESPACE |
|
56 |
|
57 class QDeclarativeConnectionsPrivate : public QObjectPrivate |
|
58 { |
|
59 public: |
|
60 QDeclarativeConnectionsPrivate() : target(0), targetSet(false), ignoreUnknownSignals(false), componentcomplete(true) {} |
|
61 |
|
62 QList<QDeclarativeBoundSignal*> boundsignals; |
|
63 QObject *target; |
|
64 |
|
65 bool targetSet; |
|
66 bool ignoreUnknownSignals; |
|
67 bool componentcomplete; |
|
68 |
|
69 QByteArray data; |
|
70 }; |
|
71 |
|
72 /*! |
|
73 \qmlclass Connections QDeclarativeConnections |
|
74 \since 4.7 |
|
75 \brief A Connections object describes generalized connections to signals. |
|
76 |
|
77 When connecting to signals in QML, the usual way is to create an |
|
78 "on<Signal>" handler that reacts when a signal is received, like this: |
|
79 |
|
80 \qml |
|
81 MouseArea { |
|
82 onClicked: { foo(...) } |
|
83 } |
|
84 \endqml |
|
85 |
|
86 However, in some cases, it is not possible to connect to a signal in this |
|
87 way, such as: |
|
88 |
|
89 \list |
|
90 \i multiple connections to the same signal |
|
91 \i connections outside the scope of the signal sender |
|
92 \i connections to targets not defined in QML |
|
93 \endlist |
|
94 |
|
95 When any of these are needed, the Connections object can be used instead. |
|
96 |
|
97 For example, the above code can be changed to use a Connections object, |
|
98 like this: |
|
99 |
|
100 \qml |
|
101 MouseArea { |
|
102 Connections { |
|
103 onClicked: foo(...) |
|
104 } |
|
105 } |
|
106 \endqml |
|
107 |
|
108 More generally, the Connections object can be a child of some other object than |
|
109 the sender of the signal: |
|
110 |
|
111 \qml |
|
112 MouseArea { |
|
113 id: area |
|
114 } |
|
115 ... |
|
116 Connections { |
|
117 target: area |
|
118 onClicked: foo(...) |
|
119 } |
|
120 \endqml |
|
121 |
|
122 \sa QtDeclarative |
|
123 */ |
|
124 |
|
125 /*! |
|
126 \internal |
|
127 \class QDeclarativeConnections |
|
128 \brief The QDeclarativeConnections class describes generalized connections to signals. |
|
129 |
|
130 */ |
|
131 QDeclarativeConnections::QDeclarativeConnections(QObject *parent) : |
|
132 QObject(*(new QDeclarativeConnectionsPrivate), parent) |
|
133 { |
|
134 } |
|
135 |
|
136 QDeclarativeConnections::~QDeclarativeConnections() |
|
137 { |
|
138 } |
|
139 |
|
140 /*! |
|
141 \qmlproperty Object Connections::target |
|
142 This property holds the object that sends the signal. |
|
143 |
|
144 If not set at all, the target defaults to be the parent of the Connections. |
|
145 |
|
146 If set to null, no connection is made and any signal handlers are ignored |
|
147 until the target is not null. |
|
148 */ |
|
149 QObject *QDeclarativeConnections::target() const |
|
150 { |
|
151 Q_D(const QDeclarativeConnections); |
|
152 return d->targetSet ? d->target : parent(); |
|
153 } |
|
154 |
|
155 void QDeclarativeConnections::setTarget(QObject *obj) |
|
156 { |
|
157 Q_D(QDeclarativeConnections); |
|
158 d->targetSet = true; // even if setting to 0, it is *set* |
|
159 if (d->target == obj) |
|
160 return; |
|
161 foreach (QDeclarativeBoundSignal *s, d->boundsignals) { |
|
162 // It is possible that target is being changed due to one of our signal |
|
163 // handlers -> use deleteLater(). |
|
164 if (s->isEvaluating()) |
|
165 s->deleteLater(); |
|
166 else |
|
167 delete s; |
|
168 } |
|
169 d->boundsignals.clear(); |
|
170 d->target = obj; |
|
171 connectSignals(); |
|
172 emit targetChanged(); |
|
173 } |
|
174 |
|
175 /*! |
|
176 \qmlproperty bool Connections::ignoreUnknownSignals |
|
177 |
|
178 Normally, you will get a runtime error if you try to connect |
|
179 to signals on an object which the object does not have. |
|
180 |
|
181 By setting this flag to true, such errors are ignored. This is |
|
182 useful if you intend to connect to different types of object, handling |
|
183 a different set of signals for each. |
|
184 */ |
|
185 bool QDeclarativeConnections::ignoreUnknownSignals() const |
|
186 { |
|
187 Q_D(const QDeclarativeConnections); |
|
188 return d->ignoreUnknownSignals; |
|
189 } |
|
190 |
|
191 void QDeclarativeConnections::setIgnoreUnknownSignals(bool ignore) |
|
192 { |
|
193 Q_D(QDeclarativeConnections); |
|
194 d->ignoreUnknownSignals = ignore; |
|
195 } |
|
196 |
|
197 |
|
198 |
|
199 QByteArray |
|
200 QDeclarativeConnectionsParser::compile(const QList<QDeclarativeCustomParserProperty> &props) |
|
201 { |
|
202 QByteArray rv; |
|
203 QDataStream ds(&rv, QIODevice::WriteOnly); |
|
204 |
|
205 for(int ii = 0; ii < props.count(); ++ii) |
|
206 { |
|
207 QString propName = QString::fromUtf8(props.at(ii).name()); |
|
208 if (!propName.startsWith(QLatin1String("on")) || !propName.at(2).isUpper()) { |
|
209 error(props.at(ii), QDeclarativeConnections::tr("Cannot assign to non-existent property \"%1\"").arg(propName)); |
|
210 return QByteArray(); |
|
211 } |
|
212 |
|
213 QList<QVariant> values = props.at(ii).assignedValues(); |
|
214 |
|
215 for (int i = 0; i < values.count(); ++i) { |
|
216 const QVariant &value = values.at(i); |
|
217 |
|
218 if (value.userType() == qMetaTypeId<QDeclarativeCustomParserNode>()) { |
|
219 error(props.at(ii), QDeclarativeConnections::tr("Connections: nested objects not allowed")); |
|
220 return QByteArray(); |
|
221 } else if (value.userType() == qMetaTypeId<QDeclarativeCustomParserProperty>()) { |
|
222 error(props.at(ii), QDeclarativeConnections::tr("Connections: syntax error")); |
|
223 return QByteArray(); |
|
224 } else { |
|
225 QDeclarativeParser::Variant v = qvariant_cast<QDeclarativeParser::Variant>(value); |
|
226 if (v.isScript()) { |
|
227 ds << propName; |
|
228 ds << v.asScript(); |
|
229 } else { |
|
230 error(props.at(ii), QDeclarativeConnections::tr("Connections: script expected")); |
|
231 return QByteArray(); |
|
232 } |
|
233 } |
|
234 } |
|
235 } |
|
236 |
|
237 return rv; |
|
238 } |
|
239 |
|
240 void QDeclarativeConnectionsParser::setCustomData(QObject *object, |
|
241 const QByteArray &data) |
|
242 { |
|
243 QDeclarativeConnectionsPrivate *p = |
|
244 static_cast<QDeclarativeConnectionsPrivate *>(QObjectPrivate::get(object)); |
|
245 p->data = data; |
|
246 } |
|
247 |
|
248 |
|
249 void QDeclarativeConnections::connectSignals() |
|
250 { |
|
251 Q_D(QDeclarativeConnections); |
|
252 if (!d->componentcomplete || (d->targetSet && !target())) |
|
253 return; |
|
254 |
|
255 QDataStream ds(d->data); |
|
256 while (!ds.atEnd()) { |
|
257 QString propName; |
|
258 ds >> propName; |
|
259 QString script; |
|
260 ds >> script; |
|
261 QDeclarativeProperty prop(target(), propName); |
|
262 if (prop.isValid() && (prop.type() & QDeclarativeProperty::SignalProperty)) { |
|
263 QDeclarativeBoundSignal *signal = |
|
264 new QDeclarativeBoundSignal(target(), prop.method(), this); |
|
265 signal->setExpression(new QDeclarativeExpression(qmlContext(this), 0, script)); |
|
266 d->boundsignals += signal; |
|
267 } else { |
|
268 if (!d->ignoreUnknownSignals) |
|
269 qmlInfo(this) << tr("Cannot assign to non-existent property \"%1\"").arg(propName); |
|
270 } |
|
271 } |
|
272 } |
|
273 |
|
274 void QDeclarativeConnections::classBegin() |
|
275 { |
|
276 Q_D(QDeclarativeConnections); |
|
277 d->componentcomplete=false; |
|
278 } |
|
279 |
|
280 void QDeclarativeConnections::componentComplete() |
|
281 { |
|
282 Q_D(QDeclarativeConnections); |
|
283 d->componentcomplete=true; |
|
284 connectSignals(); |
|
285 } |
|
286 |
|
287 QT_END_NAMESPACE |