Qt 4.8
qdeclarativeopenmetaobject.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/qdeclarativeopenmetaobject_p.h"
43 #include "private/qdeclarativepropertycache_p.h"
44 #include "private/qdeclarativedata_p.h"
45 #include <qmetaobjectbuilder_p.h>
46 #include <qdebug.h>
47 
49 
50 
52 {
53 public:
55 
56  void init(const QMetaObject *metaObj);
57 
66 };
67 
70 {
71  d->engine = engine;
72  d->init(base);
73 }
74 
76 {
77  if (d->mem)
78  qFree(d->mem);
79  if (d->cache)
80  d->cache->release();
81  delete d;
82 }
83 
84 
86 {
87  return d->propertyOffset;
88 }
89 
91 {
92  return d->signalOffset;
93 }
94 
96 {
97  int id = d->mob.propertyCount();
98  d->mob.addSignal("__" + QByteArray::number(id) + "()");
99  QMetaPropertyBuilder build = d->mob.addProperty(name, "QVariant", id);
100  propertyCreated(id, build);
101  qFree(d->mem);
102  d->mem = d->mob.toMetaObject();
103  d->names.insert(name, id);
105  while (it != d->referers.end()) {
107  *static_cast<QMetaObject *>(omo) = *d->mem;
108  if (d->cache)
109  d->cache->update(d->engine, omo);
110  ++it;
111  }
112 
113  return d->propertyOffset + id;
114 }
115 
117 {
118  if (d->referers.count())
119  (*d->referers.begin())->propertyCreated(id, builder);
120 }
121 
123 {
124  if (!mem) {
125  mob.setSuperClass(metaObj);
126  mob.setClassName(metaObj->className());
128 
129  mem = mob.toMetaObject();
130 
131  propertyOffset = mem->propertyOffset();
132  signalOffset = mem->methodOffset();
133  }
134 }
135 
136 //----------------------------------------------------------------------------
137 
139 {
140 public:
142  : q(_q), parent(0), type(0), cacheProperties(false) {}
143 
144  inline QVariant &getData(int idx) {
145  while (data.count() <= idx)
146  data << QPair<QVariant, bool>(QVariant(), false);
147  QPair<QVariant, bool> &prop = data[idx];
148  if (!prop.second) {
149  prop.first = q->initialValue(idx);
150  prop.second = true;
151  }
152  return prop.first;
153  }
154 
155  inline void writeData(int idx, const QVariant &value) {
156  while (data.count() <= idx)
157  data << QPair<QVariant, bool>(QVariant(), false);
158  QPair<QVariant, bool> &prop = data[idx];
159  prop.first = value;
160  prop.second = true;
161  }
162 
163  inline bool hasData(int idx) const {
164  if (idx >= data.count())
165  return false;
166  return data[idx].second;
167  }
168 
176 };
177 
180 {
182  d->object = obj;
183 
185  d->type->d->referers.insert(this);
186 
188  d->parent = static_cast<QAbstractDynamicMetaObject *>(op->metaObject);
189  *static_cast<QMetaObject *>(this) = *d->type->d->mem;
190  op->metaObject = this;
191 }
192 
195 {
197  d->object = obj;
198 
199  d->type = type;
200  d->type->addref();
201  d->type->d->referers.insert(this);
202 
204  d->parent = static_cast<QAbstractDynamicMetaObject *>(op->metaObject);
205  *static_cast<QMetaObject *>(this) = *d->type->d->mem;
206  op->metaObject = this;
207 }
208 
210 {
211  if (d->parent)
212  delete d->parent;
213  d->type->d->referers.remove(this);
214  d->type->release();
215  delete d;
216 }
217 
219 {
220  return d->type;
221 }
222 
224 {
226  && id >= d->type->d->propertyOffset) {
227  int propId = id - d->type->d->propertyOffset;
228  if (c == QMetaObject::ReadProperty) {
229  propertyRead(propId);
230  *reinterpret_cast<QVariant *>(a[0]) = d->getData(propId);
231  } else if (c == QMetaObject::WriteProperty) {
232  if (propId <= d->data.count() || d->data[propId].first != *reinterpret_cast<QVariant *>(a[0])) {
233  propertyWrite(propId);
234  d->writeData(propId, *reinterpret_cast<QVariant *>(a[0]));
235  propertyWritten(propId);
236  activate(d->object, d->type->d->signalOffset + propId, 0);
237  }
238  }
239  return -1;
240  } else {
241  if (d->parent)
242  return d->parent->metaCall(c, id, a);
243  else
244  return d->object->qt_metacall(c, id, a);
245  }
246 }
247 
249 {
250  return d->parent;
251 }
252 
254 {
255  return d->getData(id);
256 }
257 
259 {
260  d->writeData(id, value);
261  activate(d->object, id + d->type->d->signalOffset, 0);
262 }
263 
265 {
267  if (iter == d->type->d->names.end())
268  return QVariant();
269 
270  return d->getData(*iter);
271 }
272 
274 {
276  Q_ASSERT(iter != d->type->d->names.end());
277 
278  return d->getData(*iter);
279 }
280 
282 {
283  return d->getData(id);
284 }
285 
287 {
289 
290  int id = -1;
291  if (iter == d->type->d->names.end()) {
292  id = createProperty(name.constData(), "") - d->type->d->propertyOffset;
293  } else {
294  id = *iter;
295  }
296 
297  if (id >= 0) {
298  QVariant &dataVal = d->getData(id);
299  if (dataVal == val)
300  return;
301 
302  dataVal = val;
303  activate(d->object, id + d->type->d->signalOffset, 0);
304  }
305 }
306 
307 // returns true if this value has been initialized by a call to either value() or setValue()
309 {
310  return d->hasData(id);
311 }
312 
314 {
315  if (c == d->cacheProperties || !d->type->d->engine)
316  return;
317 
318  d->cacheProperties = c;
319 
320  QDeclarativeData *qmldata = QDeclarativeData::get(d->object, true);
321  if (d->cacheProperties) {
322  if (!d->type->d->cache)
323  d->type->d->cache = new QDeclarativePropertyCache(d->type->d->engine, this);
324  qmldata->propertyCache = d->type->d->cache;
325  d->type->d->cache->addref();
326  } else {
327  if (d->type->d->cache)
328  d->type->d->cache->release();
329  qmldata->propertyCache = 0;
330  }
331 }
332 
333 
334 int QDeclarativeOpenMetaObject::createProperty(const char *name, const char *)
335 {
336  if (d->autoCreate)
337  return d->type->createProperty(name);
338  else
339  return -1;
340 }
341 
343 {
344 }
345 
347 {
348 }
349 
351 {
352 }
353 
355 {
356 }
357 
359 {
360  return QVariant();
361 }
362 
364 {
365  return d->type->d->names.count();
366 }
367 
369 {
370  Q_ASSERT(idx >= 0 && idx < d->type->d->names.count());
371 
372  return d->type->d->mob.property(idx).name();
373 }
374 
376 {
377  return d->object;
378 }
379 
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
virtual void propertyCreated(int, QMetaPropertyBuilder &)
static bool automatic(Engine *driver, int token)
double d
Definition: qnumeric_p.h:62
The QMetaObject class contains meta-information about Qt objects.
Definition: qobjectdefs.h:304
QDeclarativePropertyCache * propertyCache
static QDeclarativeData * get(const QObject *object, bool create=false)
int type
Definition: qmetatype.cpp:239
unsigned char c[8]
Definition: qnumeric_p.h:62
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void setValue(const QByteArray &, const QVariant &)
int createProperty(const QByteArray &name)
QByteArray name() const
Returns the name associated with this property.
#define it(className, varName)
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
bool remove(const T &value)
Definition: qset.h:89
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
T1 first
Definition: qpair.h:65
T2 second
Definition: qpair.h:66
QDeclarativeOpenMetaObjectType * type
void update(QDeclarativeEngine *, const QMetaObject *)
long ASN1_INTEGER_get ASN1_INTEGER * a
QDeclarativeOpenMetaObjectType * type() const
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
iterator begin()
Definition: qset.h:166
QSet< QDeclarativeOpenMetaObject * > referers
static const uint base
Definition: qurl.cpp:268
static QObjectPrivate * get(QObject *o)
Definition: qobject_p.h:177
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition: qhash.h:753
virtual int metaCall(QMetaObject::Call, int _id, void **)
Definition: qobject_p.h:336
virtual int createProperty(const char *, const char *)
QVariant value(const QByteArray &) const
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
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
iterator end()
Definition: qset.h:169
const_iterator insert(const T &value)
Definition: qset.h:179
static const char * data(const QByteArray &arr)
int count() const
Definition: qset.h:178
void writeData(int idx, const QVariant &value)
const uint * data
Definition: qobjectdefs.h:471
QDeclarativeOpenMetaObjectPrivate(QDeclarativeOpenMetaObject *_q)
T & first()
Returns a reference to the first item in the list.
Definition: qlist.h:282
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
QDeclarativeOpenMetaObject(QObject *, bool=true)
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the hash...
Definition: qhash.h:467
The QDeclarativeEngine class provides an environment for instantiating QML components.
static void activate(QObject *sender, int signal_index, void **argv)
Definition: qobject.cpp:3690
QDeclarativeOpenMetaObjectType(const QMetaObject *base, QDeclarativeEngine *engine)
const char * className() const
Returns the class name.
Definition: qobjectdefs.h:491
QMetaObject * metaObject
Definition: qobject.h:107
if(void) toggleToolbarShown
QDeclarativeOpenMetaObjectTypePrivate * d
int count(const Key &key) const
Returns the number of items associated with the key.
Definition: qhash.h:719
QList< QPair< QVariant, bool > > data
QDeclarativeOpenMetaObjectPrivate * d
QMetaPropertyBuilder property(int index) const
Returns the property at index in this class.
iterator find(const Key &key)
Returns an iterator pointing to the item with the key in the hash.
Definition: qhash.h:865
virtual void propertyCreated(int, QMetaPropertyBuilder &)
QMetaObject * toMetaObject() const
Converts this meta object builder into a concrete QMetaObject.
QVariant & operator[](const QByteArray &)
QMetaMethodBuilder addSignal(const QByteArray &signature)
Adds a new signal to this class with the specified signature.
static QByteArray number(int, int base=10)
Returns a byte array containing the string equivalent of the number n to base base (10 by default)...
virtual int metaCall(QMetaObject::Call _c, int _id, void **_a)
int propertyCount() const
Returns the number of properties in this class, excluding the number of properties in the base class...
virtual const QMetaObject * metaObject() const
Returns a pointer to the meta-object of this object.
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
QAbstractDynamicMetaObject * parent() const