Qt 4.8
qdeclarativeboundsignal.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/qdeclarativeboundsignal_p.h"
43 
44 #include "private/qmetaobjectbuilder_p.h"
45 #include "private/qdeclarativeengine_p.h"
46 #include "private/qdeclarativeexpression_p.h"
47 #include "private/qdeclarativecontext_p.h"
48 #include "private/qdeclarativemetatype_p.h"
49 #include "qdeclarative.h"
50 #include "qdeclarativecontext.h"
51 #include "private/qdeclarativeglobal_p.h"
52 #include "private/qdeclarativedebugtrace_p.h"
53 
54 #include <QtCore/qstringbuilder.h>
55 #include <QtCore/qdebug.h>
56 
58 
60 {
62 public:
65 
66  void setValues(void **);
67  void clearValues();
68 
69 private:
70  friend class MetaObject;
71  int metaCall(QMetaObject::Call, int _id, void **);
74  : parent(b) {}
75 
76  int metaCall(QMetaObject::Call c, int id, void **a) {
77  return parent->metaCall(c, id, a);
78  }
80  };
81 
82  int *types;
83  void **values;
85 };
86 
87 static int evaluateIdx = -1;
88 
90 : QObject(parent)
91 {
92 }
93 
95 {
96 }
97 
99  QObject *parent)
100 : m_expression(0), m_signal(signal), m_paramsValid(false), m_isEvaluating(false), m_params(0),
101  m_scope(scope, this)
102 {
103  init(parent);
104 }
105 
107  QObject *scope, const QMetaMethod &signal,
108  QObject *parent)
109 : m_expression(0), m_signal(signal), m_paramsValid(false), m_isEvaluating(false), m_params(0),
110  m_scope(scope, this)
111 {
112  init(parent);
113  m_expression = new QDeclarativeExpression(ctxt, scope, val);
114 }
115 
117 {
118  // This is thread safe. Although it may be updated by two threads, they
119  // will both set it to the same value - so the worst thing that can happen
120  // is that they both do the work to figure it out. Boo hoo.
121  if (evaluateIdx == -1) evaluateIdx = metaObject()->methodCount();
122 
123  QDeclarative_setParent_noEvent(this, parent);
125 
127  data->addBoundSignal(this);
128 }
129 
131 {
133  delete m_expression;
134  m_expression = 0;
135 }
136 
138 {
141  QVarLengthArray<char> signalSignature;
142  QObjectPrivate::signalSignature(m_signal, &signalSignature);
143  priv->disconnectNotify(signalSignature.constData());
144 }
145 
147 {
148  return m_signal.methodIndex();
149 }
150 
155 {
156  return m_expression;
157 }
158 
167 {
169  m_expression = e;
171  return rv;
172 }
173 
175 {
177  return static_cast<QDeclarativeBoundSignal *>(s);
178 }
179 
181 {
182  if (c == QMetaObject::InvokeMetaMethod && id == evaluateIdx) {
183  if (!m_expression)
184  return -1;
189  }
190  m_isEvaluating = true;
191  if (!m_paramsValid) {
194  m_paramsValid = true;
195  }
196 
197  if (m_params) m_params->setValues(a);
198  if (m_expression && m_expression->engine()) {
202  }
203  if (m_params) m_params->clearValues();
204  m_isEvaluating = false;
206  return -1;
207  } else {
208  return QObject::qt_metacall(c, id, a);
209  }
210 }
211 
213 {
214  if (m_scope) {
216  if (data)
217  data->removeBoundSignal(this);
218  }
219 }
220 
222  QObject *parent)
223 : QObject(parent), types(0), values(0)
224 {
225  MetaObject *mo = new MetaObject(this);
226 
227  // ### Optimize!
228  QMetaObjectBuilder mob;
230  mob.setClassName("QDeclarativeBoundSignalParameters");
231 
232  QList<QByteArray> paramTypes = method.parameterTypes();
233  QList<QByteArray> paramNames = method.parameterNames();
234  types = new int[paramTypes.count()];
235  for (int ii = 0; ii < paramTypes.count(); ++ii) {
236  const QByteArray &type = paramTypes.at(ii);
237  const QByteArray &name = paramNames.at(ii);
238 
239  if (name.isEmpty() || type.isEmpty()) {
240  types[ii] = 0;
241  continue;
242  }
243 
247  QMetaPropertyBuilder prop = mob.addProperty(name, "QObject*");
248  prop.setWritable(false);
249  } else {
250  QByteArray propType = type;
251  if (t >= QVariant::UserType || t == QVariant::Invalid) {
252  //copy of QDeclarativeObjectScriptClass::enumType()
253  QByteArray scope;
255  int scopeIdx = propType.lastIndexOf("::");
256  if (scopeIdx != -1) {
257  scope = propType.left(scopeIdx);
258  name = propType.mid(scopeIdx + 2);
259  } else {
260  name = propType;
261  }
262  const QMetaObject *meta;
263  if (scope == "Qt")
265  else
266  meta = parent->parent()->metaObject(); //### assumes parent->parent()
267  for (int i = meta->enumeratorCount() - 1; i >= 0; --i) {
268  QMetaEnum m = meta->enumerator(i);
269  if ((m.name() == name) && (scope.isEmpty() || (m.scope() == scope))) {
270  t = QVariant::Int;
271  propType = "int";
272  break;
273  }
274  }
275  }
277  types[ii] = t;
278  QMetaPropertyBuilder prop = mob.addProperty(name, propType);
279  prop.setWritable(false);
280  } else {
281  types[ii] = 0x80000000 | t;
282  QMetaPropertyBuilder prop = mob.addProperty(name, "QVariant");
283  prop.setWritable(false);
284  }
285  }
286  }
287  myMetaObject = mob.toMetaObject();
288  *static_cast<QMetaObject *>(mo) = *myMetaObject;
289 
290  d_ptr->metaObject = mo;
291 }
292 
294 {
295  delete [] types;
297 }
298 
300 {
301  values = v;
302 }
303 
305 {
306  values = 0;
307 }
308 
310 {
311  if (!values)
312  return -1;
313 
314  if (c == QMetaObject::ReadProperty && id >= 1) {
315  if (types[id - 1] & 0x80000000) {
316  *((QVariant *)a[0]) = QVariant(types[id - 1] & 0x7FFFFFFF, values[id]);
317  } else {
318  QDeclarativeMetaType::copy(types[id - 1], a[0], values[id]);
319  }
320  return -1;
321  } else {
322  return qt_metacall(c, id, a);
323  }
324 }
325 
327 
328 #include <qdeclarativeboundsignal.moc>
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
The QMetaObject class contains meta-information about Qt objects.
Definition: qobjectdefs.h:304
const T * constData() const
static QDeclarativeData * get(const QObject *object, bool create=false)
int type
Definition: qmetatype.cpp:239
unsigned char c[8]
Definition: qnumeric_p.h:62
The QMetaEnum class provides meta-data about an enumerator.
Definition: qmetaobject.h:147
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
static void signalSignature(const QMetaMethod &signal, QVarLengthArray< char > *result)
Definition: qobject_p.h:255
int methodIndex() const
Returns this method&#39;s index.
QDeclarativeEngine * engine() const
Returns the QDeclarativeEngine this expression is associated with, or 0 if there is no association or...
static void rangeLocation(RangeType, const QString &, int)
The QMetaPropertyBuilder class enables modifications to a property definition on a meta object builde...
Q_CORE_EXPORT void qFree(void *ptr)
Definition: qmalloc.cpp:58
static QDeclarativeBoundSignal * cast(QObject *)
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
void setClassName(const QByteArray &name)
Sets the name of the class being constructed by this meta object builder.
int metaCall(QMetaObject::Call c, int id, void **a)
static bool canCopy(int type)
static bool copy(int type, void *data, const void *copy=0)
Copies copy into data, assuming they both are of type type.
static const QMetaObject staticMetaObject
This variable stores the meta-object for the class.
Definition: qobject.h:128
static bool connect(QObject *sender, int signal_index, const QObject *receiver, int method_index, int type=0, int *types=0)
Connect sender signal_index to receiver method_index with the specified type and types.
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
long ASN1_INTEGER_get ASN1_INTEGER * a
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
The QString class provides a Unicode character string.
Definition: qstring.h:83
T * qobject_cast(QObject *object)
Definition: qobject.h:375
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
const char * scope() const
Returns the scope this enumerator was declared in.
QDeclarativeBoundSignalParameters * m_params
QDeclarativeExpression * m_expression
static QObjectPrivate * get(QObject *o)
Definition: qobject_p.h:177
const char * name() const
Returns the name of the enumerator (without the scope).
void setNotifyOnValueChanged(bool)
Sets whether the valueChanged() signal is emitted when the expression&#39;s evaluated value changes...
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
QDeclarativeExpression * setExpression(QDeclarativeExpression *)
Sets the signal expression to e.
void removeBoundSignal(QDeclarativeAbstractBoundSignal *signal)
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
int lastIndexOf(char c, int from=-1) const
Returns the index position of the last occurrence of character ch in the byte array, searching backward from index position from.
int metaCall(QMetaObject::Call, int _id, void **)
QList< QByteArray > parameterNames() const
Returns a list of parameter names.
QMetaPropertyBuilder addProperty(const QByteArray &name, const QByteArray &type, int notifierId=-1)
Adds a new readable/writable property to this class with the specified name and type.
const char * name
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
QDeclarativeBoundSignal(QObject *scope, const QMetaMethod &signal, QObject *parent)
static const char * data(const QByteArray &arr)
Type
This enum type defines the types of variable that a QVariant can contain.
Definition: qvariant.h:95
static int evaluateIdx
static bool disconnect(const QObject *sender, int signal_index, const QObject *receiver, int method_index)
Definition: qobject.cpp:3276
int enumeratorCount() const
Returns the number of enumerators in this class.
QByteArray left(int len) const
Returns a byte array that contains the leftmost len bytes of this byte array.
static int type(const char *typeName)
Returns a handle to the type called typeName, or 0 if there is no such type.
Definition: qmetatype.cpp:607
quint16 values[128]
QByteArray mid(int index, int len=-1) const
Returns a byte array containing len bytes from this byte array, starting at position pos...
#define Q_OBJECT
Definition: qobjectdefs.h:157
QDeclarativeExpression * expression() const
Returns the signal expression.
static void endRange(RangeType)
static void startRange(RangeType)
QString expression() const
Returns the expression string.
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
bool hasError() const
Returns true if the last call to evaluate() resulted in an error, otherwise false.
QList< QByteArray > parameterTypes() const
Returns a list of parameter types.
int lineNumber() const
Returns the source file line number for this expression.
static const struct @32 types[]
void QDeclarative_setParent_noEvent(QObject *object, QObject *parent)
Makes the object a child of parent.
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
MetaObject(QDeclarativeBoundSignalParameters *b)
QString sourceFile() const
Returns the source file URL for this expression.
virtual int qt_metacall(QMetaObject::Call c, int id, void **a)
static const QMetaObjectPrivate * priv(const uint *data)
QMetaObject * metaObject
Definition: qobject.h:107
static void rangeData(RangeType, const QString &)
QMetaEnum enumerator(int index) const
Returns the meta-data for the enumerator with the given index.
static const QMetaObject staticQtMetaObject
Definition: qobject.h:322
The QDeclarativeContext class defines a context within a QML engine.
static QDeclarativeExpressionPrivate * get(QDeclarativeExpression *expr)
QScopedPointer< QObjectData > d_ptr
Definition: qobject.h:320
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
QVariant value(QObject *secondaryScope=0, bool *isUndefined=0)
const char * signature() const
Returns the signature of this method (e.g., setValue(double)).
void setWritable(bool value)
Sets this property to writable if value is true.
QDeclarativeError error() const
Return any error from the last call to evaluate().
The QDeclarativeExpression class evaluates JavaScript in a QML context.
QMetaObject * toMetaObject() const
Converts this meta object builder into a concrete QMetaObject.
void warning(const QDeclarativeError &)
void setSuperClass(const QMetaObject *meta)
Sets the superclass meta object of the class being constructed by this meta object builder to meta...
int methodCount() const
Returns the number of methods known to the meta-object system in this class, including the number of ...
QDeclarativeBoundSignalParameters(const QMetaMethod &, QObject *=0)
The QMetaMethod class provides meta-data about a member function.
Definition: qmetaobject.h:56
virtual const QMetaObject * metaObject() const
Returns a pointer to the meta-object of this object.