1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQml module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
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::createProperty(const QByteArray &name)
101 int id = d->mob.propertyCount();
102 d->mob.addSignal("__" + QByteArray::number(id) + "()");
103 QMetaPropertyBuilder build = d->mob.addProperty(name, "QVariant", id);
104 propertyCreated(id, build);
106 d->mem = d->mob.toMetaObject();
107 d->names.insert(name, id);
108 QSet<QQmlOpenMetaObject*>::iterator it = d->referers.begin();
109 while (it != d->referers.end()) {
110 QQmlOpenMetaObject *omo = *it;
111 *static_cast<QMetaObject *>(omo) = *d->mem;
113 d->cache->update(d->engine, omo);
117 return d->propertyOffset + id;
120 void QQmlOpenMetaObjectType::propertyCreated(int id, QMetaPropertyBuilder &builder)
122 if (d->referers.count())
123 (*d->referers.begin())->propertyCreated(id, builder);
126 void QQmlOpenMetaObjectTypePrivate::init(const QMetaObject *metaObj)
129 mob.setSuperClass(metaObj);
130 mob.setClassName(metaObj->className());
131 mob.setFlags(QMetaObjectBuilder::DynamicMetaObject);
133 mem = mob.toMetaObject();
135 propertyOffset = mem->propertyOffset();
136 signalOffset = mem->methodOffset();
140 //----------------------------------------------------------------------------
142 class QQmlOpenMetaObjectPrivate
145 QQmlOpenMetaObjectPrivate(QQmlOpenMetaObject *_q)
146 : q(_q), parent(0), type(0), cacheProperties(false) {}
148 inline QPair<QVariant, bool> &getDataRef(int idx) {
149 while (data.count() <= idx)
150 data << QPair<QVariant, bool>(QVariant(), false);
154 inline QVariant &getData(int idx) {
155 QPair<QVariant, bool> &prop = getDataRef(idx);
157 prop.first = q->initialValue(idx);
163 inline bool hasData(int idx) const {
164 if (idx >= data.count())
166 return data[idx].second;
170 QQmlOpenMetaObject *q;
171 QAbstractDynamicMetaObject *parent;
172 QList<QPair<QVariant, bool> > data;
174 QQmlOpenMetaObjectType *type;
175 bool cacheProperties;
178 QQmlOpenMetaObject::QQmlOpenMetaObject(QObject *obj, const QMetaObject *base, bool automatic)
179 : d(new QQmlOpenMetaObjectPrivate(this))
181 d->autoCreate = automatic;
184 d->type = new QQmlOpenMetaObjectType(base ? base : obj->metaObject(), 0);
185 d->type->d->referers.insert(this);
187 QObjectPrivate *op = QObjectPrivate::get(obj);
188 d->parent = static_cast<QAbstractDynamicMetaObject *>(op->metaObject);
189 *static_cast<QMetaObject *>(this) = *d->type->d->mem;
190 op->metaObject = this;
193 QQmlOpenMetaObject::QQmlOpenMetaObject(QObject *obj, QQmlOpenMetaObjectType *type, bool automatic)
194 : d(new QQmlOpenMetaObjectPrivate(this))
196 d->autoCreate = automatic;
201 d->type->d->referers.insert(this);
203 QObjectPrivate *op = QObjectPrivate::get(obj);
204 d->parent = static_cast<QAbstractDynamicMetaObject *>(op->metaObject);
205 *static_cast<QMetaObject *>(this) = *d->type->d->mem;
206 op->metaObject = this;
209 QQmlOpenMetaObject::~QQmlOpenMetaObject()
213 d->type->d->referers.remove(this);
218 QQmlOpenMetaObjectType *QQmlOpenMetaObject::type() const
223 int QQmlOpenMetaObject::metaCall(QMetaObject::Call c, int id, void **a)
225 if (( c == QMetaObject::ReadProperty || c == QMetaObject::WriteProperty)
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 QPair<QVariant, bool> &prop = d->getDataRef(propId);
235 prop.first = propertyWriteValue(propId, *reinterpret_cast<QVariant *>(a[0]));
237 propertyWritten(propId);
238 activate(d->object, d->type->d->signalOffset + propId, 0);
244 return d->parent->metaCall(c, id, a);
246 return d->object->qt_metacall(c, id, a);
250 QAbstractDynamicMetaObject *QQmlOpenMetaObject::parent() const
255 QVariant QQmlOpenMetaObject::value(int id) const
257 return d->getData(id);
260 void QQmlOpenMetaObject::setValue(int id, const QVariant &value)
262 QPair<QVariant, bool> &prop = d->getDataRef(id);
263 prop.first = propertyWriteValue(id, value);
265 activate(d->object, id + d->type->d->signalOffset, 0);
268 QVariant QQmlOpenMetaObject::value(const QByteArray &name) const
270 QHash<QByteArray, int>::ConstIterator iter = d->type->d->names.find(name);
271 if (iter == d->type->d->names.end())
274 return d->getData(*iter);
277 QVariant &QQmlOpenMetaObject::operator[](const QByteArray &name)
279 QHash<QByteArray, int>::ConstIterator iter = d->type->d->names.find(name);
280 Q_ASSERT(iter != d->type->d->names.end());
282 return d->getData(*iter);
285 QVariant &QQmlOpenMetaObject::operator[](int id)
287 return d->getData(id);
290 bool QQmlOpenMetaObject::setValue(const QByteArray &name, const QVariant &val)
292 QHash<QByteArray, int>::ConstIterator iter = d->type->d->names.find(name);
295 if (iter == d->type->d->names.end()) {
296 id = createProperty(name.constData(), "") - d->type->d->propertyOffset;
302 QVariant &dataVal = d->getData(id);
307 activate(d->object, id + d->type->d->signalOffset, 0);
314 // returns true if this value has been initialized by a call to either value() or setValue()
315 bool QQmlOpenMetaObject::hasValue(int id) const
317 return d->hasData(id);
320 void QQmlOpenMetaObject::setCached(bool c)
322 if (c == d->cacheProperties || !d->type->d->engine)
325 d->cacheProperties = c;
327 QQmlData *qmldata = QQmlData::get(d->object, true);
328 if (d->cacheProperties) {
329 if (!d->type->d->cache)
330 d->type->d->cache = new QQmlPropertyCache(d->type->d->engine, this);
331 qmldata->propertyCache = d->type->d->cache;
332 d->type->d->cache->addref();
334 if (d->type->d->cache)
335 d->type->d->cache->release();
336 qmldata->propertyCache = 0;
341 int QQmlOpenMetaObject::createProperty(const char *name, const char *)
344 return d->type->createProperty(name);
349 void QQmlOpenMetaObject::propertyRead(int)
353 void QQmlOpenMetaObject::propertyWrite(int)
357 QVariant QQmlOpenMetaObject::propertyWriteValue(int, const QVariant &value)
362 void QQmlOpenMetaObject::propertyWritten(int)
366 void QQmlOpenMetaObject::propertyCreated(int, QMetaPropertyBuilder &)
370 QVariant QQmlOpenMetaObject::initialValue(int)
375 int QQmlOpenMetaObject::count() const
377 return d->type->d->names.count();
380 QByteArray QQmlOpenMetaObject::name(int idx) const
382 Q_ASSERT(idx >= 0 && idx < d->type->d->names.count());
384 return d->type->d->mob.property(idx).name();
387 QObject *QQmlOpenMetaObject::object() const