1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtQml module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #include "qqmlopenmetaobject_p.h"
43 #include <private/qqmlpropertycache_p.h>
44 #include <private/qqmldata_p.h>
45 #include <private/qmetaobjectbuilder_p.h>
51 class QQmlOpenMetaObjectTypePrivate
54 QQmlOpenMetaObjectTypePrivate() : mem(0), cache(0), engine(0) {}
56 void init(const QMetaObject *metaObj);
60 QHash<QByteArray, int> names;
61 QMetaObjectBuilder mob;
63 QQmlPropertyCache *cache;
65 QSet<QQmlOpenMetaObject*> referers;
68 QQmlOpenMetaObjectType::QQmlOpenMetaObjectType(const QMetaObject *base, QQmlEngine *engine)
69 : QQmlCleanup(engine), d(new QQmlOpenMetaObjectTypePrivate)
75 QQmlOpenMetaObjectType::~QQmlOpenMetaObjectType()
84 void QQmlOpenMetaObjectType::clear()
89 int QQmlOpenMetaObjectType::propertyOffset() const
91 return d->propertyOffset;
94 int QQmlOpenMetaObjectType::signalOffset() const
96 return d->signalOffset;
99 int QQmlOpenMetaObjectType::propertyCount() const
101 return d->names.count();
104 QByteArray QQmlOpenMetaObjectType::propertyName(int idx) const
106 Q_ASSERT(idx >= 0 && idx < d->names.count());
108 return d->mob.property(idx).name();
111 QMetaObject *QQmlOpenMetaObjectType::metaObject() const
116 int QQmlOpenMetaObjectType::createProperty(const QByteArray &name)
118 int id = d->mob.propertyCount();
119 d->mob.addSignal("__" + QByteArray::number(id) + "()");
120 QMetaPropertyBuilder build = d->mob.addProperty(name, "QVariant", id);
121 propertyCreated(id, build);
123 d->mem = d->mob.toMetaObject();
124 d->names.insert(name, id);
125 QSet<QQmlOpenMetaObject*>::iterator it = d->referers.begin();
126 while (it != d->referers.end()) {
127 QQmlOpenMetaObject *omo = *it;
128 *static_cast<QMetaObject *>(omo) = *d->mem;
130 d->cache->update(d->engine, omo);
134 return d->propertyOffset + id;
137 void QQmlOpenMetaObjectType::propertyCreated(int id, QMetaPropertyBuilder &builder)
139 if (d->referers.count())
140 (*d->referers.begin())->propertyCreated(id, builder);
143 void QQmlOpenMetaObjectTypePrivate::init(const QMetaObject *metaObj)
146 mob.setSuperClass(metaObj);
147 mob.setClassName(metaObj->className());
148 mob.setFlags(QMetaObjectBuilder::DynamicMetaObject);
150 mem = mob.toMetaObject();
152 propertyOffset = mem->propertyOffset();
153 signalOffset = mem->methodOffset();
157 //----------------------------------------------------------------------------
159 class QQmlOpenMetaObjectPrivate
162 QQmlOpenMetaObjectPrivate(QQmlOpenMetaObject *_q)
163 : q(_q), parent(0), type(0), cacheProperties(false) {}
165 inline QPair<QVariant, bool> &getDataRef(int idx) {
166 while (data.count() <= idx)
167 data << QPair<QVariant, bool>(QVariant(), false);
171 inline QVariant &getData(int idx) {
172 QPair<QVariant, bool> &prop = getDataRef(idx);
174 prop.first = q->initialValue(idx);
180 inline bool hasData(int idx) const {
181 if (idx >= data.count())
183 return data[idx].second;
187 QQmlOpenMetaObject *q;
188 QAbstractDynamicMetaObject *parent;
189 QList<QPair<QVariant, bool> > data;
191 QQmlOpenMetaObjectType *type;
192 bool cacheProperties;
195 QQmlOpenMetaObject::QQmlOpenMetaObject(QObject *obj, const QMetaObject *base, bool automatic)
196 : d(new QQmlOpenMetaObjectPrivate(this))
198 d->autoCreate = automatic;
201 d->type = new QQmlOpenMetaObjectType(base ? base : obj->metaObject(), 0);
202 d->type->d->referers.insert(this);
204 QObjectPrivate *op = QObjectPrivate::get(obj);
205 d->parent = static_cast<QAbstractDynamicMetaObject *>(op->metaObject);
206 *static_cast<QMetaObject *>(this) = *d->type->d->mem;
207 op->metaObject = this;
210 QQmlOpenMetaObject::QQmlOpenMetaObject(QObject *obj, QQmlOpenMetaObjectType *type, bool automatic)
211 : d(new QQmlOpenMetaObjectPrivate(this))
213 d->autoCreate = automatic;
218 d->type->d->referers.insert(this);
220 QObjectPrivate *op = QObjectPrivate::get(obj);
221 d->parent = static_cast<QAbstractDynamicMetaObject *>(op->metaObject);
222 *static_cast<QMetaObject *>(this) = *d->type->d->mem;
223 op->metaObject = this;
226 QQmlOpenMetaObject::~QQmlOpenMetaObject()
230 d->type->d->referers.remove(this);
235 QQmlOpenMetaObjectType *QQmlOpenMetaObject::type() const
240 int QQmlOpenMetaObject::metaCall(QMetaObject::Call c, int id, void **a)
242 if (( c == QMetaObject::ReadProperty || c == QMetaObject::WriteProperty)
243 && id >= d->type->d->propertyOffset) {
244 int propId = id - d->type->d->propertyOffset;
245 if (c == QMetaObject::ReadProperty) {
246 propertyRead(propId);
247 *reinterpret_cast<QVariant *>(a[0]) = d->getData(propId);
248 } else if (c == QMetaObject::WriteProperty) {
249 if (propId <= d->data.count() || d->data[propId].first != *reinterpret_cast<QVariant *>(a[0])) {
250 propertyWrite(propId);
251 QPair<QVariant, bool> &prop = d->getDataRef(propId);
252 prop.first = propertyWriteValue(propId, *reinterpret_cast<QVariant *>(a[0]));
254 propertyWritten(propId);
255 activate(d->object, d->type->d->signalOffset + propId, 0);
261 return d->parent->metaCall(c, id, a);
263 return d->object->qt_metacall(c, id, a);
267 QAbstractDynamicMetaObject *QQmlOpenMetaObject::parent() const
272 QVariant QQmlOpenMetaObject::value(int id) const
274 return d->getData(id);
277 void QQmlOpenMetaObject::setValue(int id, const QVariant &value)
279 QPair<QVariant, bool> &prop = d->getDataRef(id);
280 prop.first = propertyWriteValue(id, value);
282 activate(d->object, id + d->type->d->signalOffset, 0);
285 QVariant QQmlOpenMetaObject::value(const QByteArray &name) const
287 QHash<QByteArray, int>::ConstIterator iter = d->type->d->names.find(name);
288 if (iter == d->type->d->names.end())
291 return d->getData(*iter);
294 QVariant &QQmlOpenMetaObject::operator[](const QByteArray &name)
296 QHash<QByteArray, int>::ConstIterator iter = d->type->d->names.find(name);
297 Q_ASSERT(iter != d->type->d->names.end());
299 return d->getData(*iter);
302 QVariant &QQmlOpenMetaObject::operator[](int id)
304 return d->getData(id);
307 bool QQmlOpenMetaObject::setValue(const QByteArray &name, const QVariant &val)
309 QHash<QByteArray, int>::ConstIterator iter = d->type->d->names.find(name);
312 if (iter == d->type->d->names.end()) {
313 id = createProperty(name.constData(), "") - d->type->d->propertyOffset;
319 QVariant &dataVal = d->getData(id);
324 activate(d->object, id + d->type->d->signalOffset, 0);
331 // returns true if this value has been initialized by a call to either value() or setValue()
332 bool QQmlOpenMetaObject::hasValue(int id) const
334 return d->hasData(id);
337 void QQmlOpenMetaObject::setCached(bool c)
339 if (c == d->cacheProperties || !d->type->d->engine)
342 d->cacheProperties = c;
344 QQmlData *qmldata = QQmlData::get(d->object, true);
345 if (d->cacheProperties) {
346 if (!d->type->d->cache)
347 d->type->d->cache = new QQmlPropertyCache(d->type->d->engine, this);
348 qmldata->propertyCache = d->type->d->cache;
349 d->type->d->cache->addref();
351 if (d->type->d->cache)
352 d->type->d->cache->release();
353 qmldata->propertyCache = 0;
358 int QQmlOpenMetaObject::createProperty(const char *name, const char *)
361 return d->type->createProperty(name);
366 void QQmlOpenMetaObject::propertyRead(int)
370 void QQmlOpenMetaObject::propertyWrite(int)
374 QVariant QQmlOpenMetaObject::propertyWriteValue(int, const QVariant &value)
379 void QQmlOpenMetaObject::propertyWritten(int)
383 void QQmlOpenMetaObject::propertyCreated(int, QMetaPropertyBuilder &)
387 QVariant QQmlOpenMetaObject::initialValue(int)
392 int QQmlOpenMetaObject::count() const
394 return d->type->d->names.count();
397 QByteArray QQmlOpenMetaObject::name(int idx) const
399 Q_ASSERT(idx >= 0 && idx < d->type->d->names.count());
401 return d->type->d->mob.property(idx).name();
404 QObject *QQmlOpenMetaObject::object() const