|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the QtDeclarative module of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
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 |
|
48 QT_BEGIN_NAMESPACE |
|
49 |
|
50 |
|
51 class QDeclarativeOpenMetaObjectTypePrivate |
|
52 { |
|
53 public: |
|
54 QDeclarativeOpenMetaObjectTypePrivate() : mem(0), cache(0), engine(0) {} |
|
55 |
|
56 void init(const QMetaObject *metaObj); |
|
57 |
|
58 int propertyOffset; |
|
59 int signalOffset; |
|
60 QHash<QByteArray, int> names; |
|
61 QMetaObjectBuilder mob; |
|
62 QMetaObject *mem; |
|
63 QDeclarativePropertyCache *cache; |
|
64 QDeclarativeEngine *engine; |
|
65 QSet<QDeclarativeOpenMetaObject*> referers; |
|
66 }; |
|
67 |
|
68 QDeclarativeOpenMetaObjectType::QDeclarativeOpenMetaObjectType(const QMetaObject *base, QDeclarativeEngine *engine) |
|
69 : d(new QDeclarativeOpenMetaObjectTypePrivate) |
|
70 { |
|
71 d->engine = engine; |
|
72 d->init(base); |
|
73 } |
|
74 |
|
75 QDeclarativeOpenMetaObjectType::~QDeclarativeOpenMetaObjectType() |
|
76 { |
|
77 if (d->mem) |
|
78 qFree(d->mem); |
|
79 if (d->cache) |
|
80 d->cache->release(); |
|
81 delete d; |
|
82 } |
|
83 |
|
84 |
|
85 int QDeclarativeOpenMetaObjectType::propertyOffset() const |
|
86 { |
|
87 return d->propertyOffset; |
|
88 } |
|
89 |
|
90 int QDeclarativeOpenMetaObjectType::signalOffset() const |
|
91 { |
|
92 return d->signalOffset; |
|
93 } |
|
94 |
|
95 int QDeclarativeOpenMetaObjectType::createProperty(const QByteArray &name) |
|
96 { |
|
97 int id = d->mob.propertyCount(); |
|
98 d->mob.addSignal("__" + QByteArray::number(id) + "()"); |
|
99 QMetaPropertyBuilder build = d->mob.addProperty(name, "QVariant", id); |
|
100 build.setDynamic(true); |
|
101 propertyCreated(id, build); |
|
102 qFree(d->mem); |
|
103 d->mem = d->mob.toMetaObject(); |
|
104 d->names.insert(name, id); |
|
105 QSet<QDeclarativeOpenMetaObject*>::iterator it = d->referers.begin(); |
|
106 while (it != d->referers.end()) { |
|
107 QDeclarativeOpenMetaObject *omo = *it; |
|
108 *static_cast<QMetaObject *>(omo) = *d->mem; |
|
109 if (d->cache) |
|
110 d->cache->update(d->engine, omo); |
|
111 ++it; |
|
112 } |
|
113 |
|
114 return d->propertyOffset + id; |
|
115 } |
|
116 |
|
117 void QDeclarativeOpenMetaObjectType::propertyCreated(int id, QMetaPropertyBuilder &builder) |
|
118 { |
|
119 if (d->referers.count()) |
|
120 (*d->referers.begin())->propertyCreated(id, builder); |
|
121 } |
|
122 |
|
123 void QDeclarativeOpenMetaObjectTypePrivate::init(const QMetaObject *metaObj) |
|
124 { |
|
125 if (!mem) { |
|
126 mob.setSuperClass(metaObj); |
|
127 mob.setClassName(metaObj->className()); |
|
128 mob.setFlags(QMetaObjectBuilder::DynamicMetaObject); |
|
129 |
|
130 mem = mob.toMetaObject(); |
|
131 |
|
132 propertyOffset = mem->propertyOffset(); |
|
133 signalOffset = mem->methodOffset(); |
|
134 } |
|
135 } |
|
136 |
|
137 //---------------------------------------------------------------------------- |
|
138 |
|
139 class QDeclarativeOpenMetaObjectPrivate |
|
140 { |
|
141 public: |
|
142 QDeclarativeOpenMetaObjectPrivate(QDeclarativeOpenMetaObject *_q) |
|
143 : q(_q), parent(0), type(0), cacheProperties(false) {} |
|
144 |
|
145 inline QVariant &getData(int idx) { |
|
146 while (data.count() <= idx) |
|
147 data << QPair<QVariant, bool>(QVariant(), false); |
|
148 QPair<QVariant, bool> &prop = data[idx]; |
|
149 if (!prop.second) { |
|
150 prop.first = q->initialValue(idx); |
|
151 prop.second = true; |
|
152 } |
|
153 return prop.first; |
|
154 } |
|
155 |
|
156 inline void writeData(int idx, const QVariant &value) { |
|
157 while (data.count() <= idx) |
|
158 data << QPair<QVariant, bool>(QVariant(), false); |
|
159 QPair<QVariant, bool> &prop = data[idx]; |
|
160 prop.first = value; |
|
161 prop.second = true; |
|
162 } |
|
163 |
|
164 bool autoCreate; |
|
165 QDeclarativeOpenMetaObject *q; |
|
166 QAbstractDynamicMetaObject *parent; |
|
167 QList<QPair<QVariant, bool> > data; |
|
168 QObject *object; |
|
169 QDeclarativeOpenMetaObjectType *type; |
|
170 bool cacheProperties; |
|
171 }; |
|
172 |
|
173 QDeclarativeOpenMetaObject::QDeclarativeOpenMetaObject(QObject *obj, bool automatic) |
|
174 : d(new QDeclarativeOpenMetaObjectPrivate(this)) |
|
175 { |
|
176 d->autoCreate = automatic; |
|
177 d->object = obj; |
|
178 |
|
179 d->type = new QDeclarativeOpenMetaObjectType(obj->metaObject(), 0); |
|
180 d->type->d->referers.insert(this); |
|
181 |
|
182 QObjectPrivate *op = QObjectPrivate::get(obj); |
|
183 *static_cast<QMetaObject *>(this) = *d->type->d->mem; |
|
184 op->metaObject = this; |
|
185 } |
|
186 |
|
187 QDeclarativeOpenMetaObject::QDeclarativeOpenMetaObject(QObject *obj, QDeclarativeOpenMetaObjectType *type, bool automatic) |
|
188 : d(new QDeclarativeOpenMetaObjectPrivate(this)) |
|
189 { |
|
190 d->autoCreate = automatic; |
|
191 d->object = obj; |
|
192 |
|
193 d->type = type; |
|
194 d->type->addref(); |
|
195 d->type->d->referers.insert(this); |
|
196 |
|
197 QObjectPrivate *op = QObjectPrivate::get(obj); |
|
198 *static_cast<QMetaObject *>(this) = *d->type->d->mem; |
|
199 op->metaObject = this; |
|
200 } |
|
201 |
|
202 QDeclarativeOpenMetaObject::~QDeclarativeOpenMetaObject() |
|
203 { |
|
204 if (d->parent) |
|
205 delete d->parent; |
|
206 d->type->d->referers.remove(this); |
|
207 d->type->release(); |
|
208 delete d; |
|
209 } |
|
210 |
|
211 QDeclarativeOpenMetaObjectType *QDeclarativeOpenMetaObject::type() const |
|
212 { |
|
213 return d->type; |
|
214 } |
|
215 |
|
216 int QDeclarativeOpenMetaObject::metaCall(QMetaObject::Call c, int id, void **a) |
|
217 { |
|
218 if (( c == QMetaObject::ReadProperty || c == QMetaObject::WriteProperty) |
|
219 && id >= d->type->d->propertyOffset) { |
|
220 int propId = id - d->type->d->propertyOffset; |
|
221 if (c == QMetaObject::ReadProperty) { |
|
222 propertyRead(propId); |
|
223 *reinterpret_cast<QVariant *>(a[0]) = d->getData(propId); |
|
224 } else if (c == QMetaObject::WriteProperty) { |
|
225 if (propId <= d->data.count() || d->data[propId].first != *reinterpret_cast<QVariant *>(a[0])) { |
|
226 propertyWrite(propId); |
|
227 d->writeData(propId, *reinterpret_cast<QVariant *>(a[0])); |
|
228 propertyWritten(propId); |
|
229 activate(d->object, d->type->d->signalOffset + propId, 0); |
|
230 } |
|
231 } |
|
232 return -1; |
|
233 } else { |
|
234 if (d->parent) |
|
235 return d->parent->metaCall(c, id, a); |
|
236 else |
|
237 return d->object->qt_metacall(c, id, a); |
|
238 } |
|
239 } |
|
240 |
|
241 QAbstractDynamicMetaObject *QDeclarativeOpenMetaObject::parent() const |
|
242 { |
|
243 return d->parent; |
|
244 } |
|
245 |
|
246 QVariant QDeclarativeOpenMetaObject::value(int id) const |
|
247 { |
|
248 return d->getData(id); |
|
249 } |
|
250 |
|
251 void QDeclarativeOpenMetaObject::setValue(int id, const QVariant &value) |
|
252 { |
|
253 d->writeData(id, value); |
|
254 activate(d->object, id + d->type->d->signalOffset, 0); |
|
255 } |
|
256 |
|
257 QVariant QDeclarativeOpenMetaObject::value(const QByteArray &name) const |
|
258 { |
|
259 QHash<QByteArray, int>::ConstIterator iter = d->type->d->names.find(name); |
|
260 if (iter == d->type->d->names.end()) |
|
261 return QVariant(); |
|
262 |
|
263 return d->getData(*iter); |
|
264 } |
|
265 |
|
266 QVariant &QDeclarativeOpenMetaObject::operator[](const QByteArray &name) |
|
267 { |
|
268 QHash<QByteArray, int>::ConstIterator iter = d->type->d->names.find(name); |
|
269 Q_ASSERT(iter != d->type->d->names.end()); |
|
270 |
|
271 return d->getData(*iter); |
|
272 } |
|
273 |
|
274 QVariant &QDeclarativeOpenMetaObject::operator[](int id) |
|
275 { |
|
276 return d->getData(id); |
|
277 } |
|
278 |
|
279 void QDeclarativeOpenMetaObject::setValue(const QByteArray &name, const QVariant &val) |
|
280 { |
|
281 QHash<QByteArray, int>::ConstIterator iter = d->type->d->names.find(name); |
|
282 |
|
283 int id = -1; |
|
284 if (iter == d->type->d->names.end()) { |
|
285 id = d->type->createProperty(name.constData()) - d->type->d->propertyOffset; |
|
286 } else { |
|
287 id = *iter; |
|
288 } |
|
289 |
|
290 QVariant &dataVal = d->getData(id); |
|
291 if (dataVal == val) |
|
292 return; |
|
293 |
|
294 dataVal = val; |
|
295 activate(d->object, id + d->type->d->signalOffset, 0); |
|
296 } |
|
297 |
|
298 void QDeclarativeOpenMetaObject::setCached(bool c) |
|
299 { |
|
300 if (c == d->cacheProperties || !d->type->d->engine) |
|
301 return; |
|
302 |
|
303 d->cacheProperties = c; |
|
304 |
|
305 QDeclarativeData *qmldata = QDeclarativeData::get(d->object, true); |
|
306 if (d->cacheProperties) { |
|
307 if (!d->type->d->cache) |
|
308 d->type->d->cache = new QDeclarativePropertyCache(d->type->d->engine, this); |
|
309 qmldata->propertyCache = d->type->d->cache; |
|
310 d->type->d->cache->addref(); |
|
311 } else { |
|
312 if (d->type->d->cache) |
|
313 d->type->d->cache->release(); |
|
314 qmldata->propertyCache = 0; |
|
315 } |
|
316 } |
|
317 |
|
318 |
|
319 int QDeclarativeOpenMetaObject::createProperty(const char *name, const char *) |
|
320 { |
|
321 if (d->autoCreate) |
|
322 return d->type->createProperty(name); |
|
323 else |
|
324 return -1; |
|
325 } |
|
326 |
|
327 void QDeclarativeOpenMetaObject::propertyRead(int) |
|
328 { |
|
329 } |
|
330 |
|
331 void QDeclarativeOpenMetaObject::propertyWrite(int) |
|
332 { |
|
333 } |
|
334 |
|
335 void QDeclarativeOpenMetaObject::propertyWritten(int) |
|
336 { |
|
337 } |
|
338 |
|
339 void QDeclarativeOpenMetaObject::propertyCreated(int, QMetaPropertyBuilder &) |
|
340 { |
|
341 } |
|
342 |
|
343 QVariant QDeclarativeOpenMetaObject::initialValue(int) |
|
344 { |
|
345 return QVariant(); |
|
346 } |
|
347 |
|
348 int QDeclarativeOpenMetaObject::count() const |
|
349 { |
|
350 return d->type->d->names.count(); |
|
351 } |
|
352 |
|
353 QByteArray QDeclarativeOpenMetaObject::name(int idx) const |
|
354 { |
|
355 Q_ASSERT(idx >= 0 && idx < d->type->d->names.count()); |
|
356 |
|
357 return d->type->d->mob.property(idx).name(); |
|
358 } |
|
359 |
|
360 QObject *QDeclarativeOpenMetaObject::object() const |
|
361 { |
|
362 return d->object; |
|
363 } |
|
364 |
|
365 QT_END_NAMESPACE |