Qt 4.8
qdeclarativeconnections.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
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>
47 #include <qdeclarativecontext.h>
48 #include <qdeclarativecontext_p.h>
49 #include <qdeclarativeinfo.h>
50 
51 #include <QtCore/qdebug.h>
52 #include <QtCore/qstringlist.h>
53 
54 #include <private/qobject_p.h>
55 
57 
59 {
60 public:
62 
65 
66  bool targetSet;
69 
71 };
72 
135 {
136 }
137 
139 {
140 }
141 
155 {
157  return d->targetSet ? d->target : parent();
158 }
159 
161 {
163  d->targetSet = true; // even if setting to 0, it is *set*
164  if (d->target == obj)
165  return;
166  foreach (QDeclarativeBoundSignal *s, d->boundsignals) {
167  // It is possible that target is being changed due to one of our signal
168  // handlers -> use deleteLater().
169  if (s->isEvaluating())
170  s->deleteLater();
171  else
172  delete s;
173  }
174  d->boundsignals.clear();
175  d->target = obj;
176  connectSignals();
178 }
179 
193 {
195  return d->ignoreUnknownSignals;
196 }
197 
199 {
201  d->ignoreUnknownSignals = ignore;
202 }
203 
204 
205 
208 {
209  QByteArray rv;
211 
212  for(int ii = 0; ii < props.count(); ++ii)
213  {
214  QString propName = QString::fromUtf8(props.at(ii).name());
215  if (!propName.startsWith(QLatin1String("on")) || !propName.at(2).isUpper()) {
216  error(props.at(ii), QDeclarativeConnections::tr("Cannot assign to non-existent property \"%1\"").arg(propName));
217  return QByteArray();
218  }
219 
220  QList<QVariant> values = props.at(ii).assignedValues();
221 
222  for (int i = 0; i < values.count(); ++i) {
223  const QVariant &value = values.at(i);
224 
225  if (value.userType() == qMetaTypeId<QDeclarativeCustomParserNode>()) {
226  error(props.at(ii), QDeclarativeConnections::tr("Connections: nested objects not allowed"));
227  return QByteArray();
228  } else if (value.userType() == qMetaTypeId<QDeclarativeCustomParserProperty>()) {
229  error(props.at(ii), QDeclarativeConnections::tr("Connections: syntax error"));
230  return QByteArray();
231  } else {
233  if (v.isScript()) {
234  ds << propName;
235  ds << v.asScript();
236  } else {
237  error(props.at(ii), QDeclarativeConnections::tr("Connections: script expected"));
238  return QByteArray();
239  }
240  }
241  }
242  }
243 
244  return rv;
245 }
246 
248  const QByteArray &data)
249 {
251  static_cast<QDeclarativeConnectionsPrivate *>(QObjectPrivate::get(object));
252  p->data = data;
253 }
254 
255 
257 {
259  if (!d->componentcomplete || (d->targetSet && !target()))
260  return;
261 
262  QDataStream ds(d->data);
263  while (!ds.atEnd()) {
264  QString propName;
265  ds >> propName;
266  QString script;
267  ds >> script;
268  QDeclarativeProperty prop(target(), propName);
269  if (prop.isValid() && (prop.type() & QDeclarativeProperty::SignalProperty)) {
270  QDeclarativeBoundSignal *signal =
271  new QDeclarativeBoundSignal(target(), prop.method(), this);
272  QDeclarativeExpression *expression = new QDeclarativeExpression(qmlContext(this), 0, script);
274  if (ddata && ddata->outerContext && !ddata->outerContext->url.isEmpty())
275  expression->setSourceLocation(ddata->outerContext->url.toString(), ddata->lineNumber);
276  signal->setExpression(expression);
277  d->boundsignals += signal;
278  } else {
279  if (!d->ignoreUnknownSignals)
280  qmlInfo(this) << tr("Cannot assign to non-existent property \"%1\"").arg(propName);
281  }
282  }
283 }
284 
286 {
288  d->componentcomplete=false;
289 }
290 
292 {
294  d->componentcomplete=true;
295  connectSignals();
296 }
297 
QDeclarativeContextData * outerContext
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
static QDeclarativeData * get(const QObject *object, bool create=false)
QList< QDeclarativeBoundSignal * > boundsignals
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QDeclarativeParserStatus ** d
const QChar at(int i) const
Returns the character at the given index position in the string.
Definition: qstring.h:698
QString toString(FormattingOptions options=None) const
Returns the human-displayable string representation of the URL.
Definition: qurl.cpp:5896
#define error(msg)
bool atEnd() const
Returns true if the I/O device has reached the end position (end of the stream or file) or if there i...
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
bool isEmpty() const
Returns true if the URL has no data; otherwise returns false.
Definition: qurl.cpp:4317
static bool ignore(const char *test, const char *const *table)
Definition: qaxserver.cpp:660
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition: qstring.cpp:3734
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
Type type() const
Returns the type of the property.
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
Q_DECLARATIVE_EXPORT QDeclarativeContext * qmlContext(const QObject *)
#define Q_D(Class)
Definition: qglobal.h:2482
bool isValid() const
Returns true if the QDeclarativeProperty refers to a valid property, otherwise false.
static QObjectPrivate * get(QObject *o)
Definition: qobject_p.h:177
void componentComplete()
Invoked after the root component that caused this instantiation has completed construction.
void classBegin()
Invoked after class creation, but before any properties have been set.
QDeclarativeExpression * setExpression(QDeclarativeExpression *)
Sets the signal expression to e.
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
virtual void setCustomData(QObject *, const QByteArray &)
QObject * target() const
QMetaMethod method() const
Return the QMetaMethod for this property if it is a SignalProperty, otherwise returns an invalid QMet...
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
#define emit
Definition: qobjectdefs.h:76
static QString fromUtf8(const char *, int size=-1)
Returns a QString initialized with the first size bytes of the UTF-8 string str.
Definition: qstring.cpp:4302
static const char * data(const QByteArray &arr)
bool ignoreUnknownSignals() const
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
QDeclarativeConnections(QObject *parent=0)
quint16 values[128]
void setIgnoreUnknownSignals(bool ignore)
virtual QByteArray compile(const QList< QDeclarativeCustomParserProperty > &)
Q_CORE_EXPORT int QT_FASTCALL script(uint ucs4)
bool isUpper() const
Returns true if the character is an uppercase letter, i.
Definition: qchar.h:273
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
int userType() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1913
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
T qvariant_cast(const QVariant &)
Definition: qvariant.h:571
QObject * parent
Definition: qobject.h:92
The QDeclarativeProperty class abstracts accessing properties on objects created from QML...
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
The QDeclarativeExpression class evaluates JavaScript in a QML context.
QDeclarativeInfo qmlInfo(const QObject *me)
void deleteLater()
Schedules this object for deletion.
Definition: qobject.cpp:2145
The QList class is a template class that provides lists.
Definition: qdatastream.h:62