Merge branch 'master' into qtquick2
[profile/ivi/qtdeclarative.git] / src / declarative / qml / qdeclarativepropertycache.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 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/qdeclarativepropertycache_p.h"
43
44 #include "private/qdeclarativeengine_p.h"
45 #include "private/qdeclarativebinding_p.h"
46 #include <QtCore/qdebug.h>
47
48 Q_DECLARE_METATYPE(QScriptValue)
49
50 QT_BEGIN_NAMESPACE
51
52 QDeclarativePropertyCache::Data::Flags QDeclarativePropertyCache::Data::flagsForProperty(const QMetaProperty &p, QDeclarativeEngine *engine) 
53 {
54     int propType = p.userType();
55
56     Flags flags;
57
58     if (p.isConstant())
59         flags |= Data::IsConstant;
60     if (p.isWritable())
61         flags |= Data::IsWritable;
62     if (p.isResettable())
63         flags |= Data::IsResettable;
64     if (p.isFinal())
65         flags |= Data::IsFinal;
66
67     if (propType == qMetaTypeId<QDeclarativeBinding *>()) {
68         flags |= Data::IsQmlBinding;
69     } else if (propType == qMetaTypeId<QScriptValue>()) {
70         flags |= Data::IsQScriptValue;
71     } else if (p.isEnumType()) {
72         flags |= Data::IsEnumType;
73     } else {
74         QDeclarativeMetaType::TypeCategory cat = engine ? QDeclarativeEnginePrivate::get(engine)->typeCategory(propType)
75                                                : QDeclarativeMetaType::typeCategory(propType);
76         if (cat == QDeclarativeMetaType::Object)
77             flags |= Data::IsQObjectDerived;
78         else if (cat == QDeclarativeMetaType::List)
79             flags |= Data::IsQList;
80     }
81
82     return flags;
83 }
84
85 void QDeclarativePropertyCache::Data::load(const QMetaProperty &p, QDeclarativeEngine *engine)
86 {
87     propType = p.userType();
88     if (QVariant::Type(propType) == QVariant::LastType)
89         propType = qMetaTypeId<QVariant>();
90     coreIndex = p.propertyIndex();
91     notifyIndex = p.notifySignalIndex();
92     flags = flagsForProperty(p, engine);
93     revision = p.revision();
94 }
95
96 void QDeclarativePropertyCache::Data::load(const QMetaMethod &m)
97 {
98     coreIndex = m.methodIndex();
99     relatedIndex = -1;
100     flags |= Data::IsFunction;
101     if (m.methodType() == QMetaMethod::Signal)
102         flags |= Data::IsSignal;
103     propType = QVariant::Invalid;
104
105     const char *returnType = m.typeName();
106     if (returnType) 
107         propType = QMetaType::type(returnType);
108
109     QList<QByteArray> params = m.parameterTypes();
110     if (!params.isEmpty())
111         flags |= Data::HasArguments;
112     revision = m.revision();
113 }
114
115
116 /*!
117 Creates a new empty QDeclarativePropertyCache.
118 */
119 QDeclarativePropertyCache::QDeclarativePropertyCache(QDeclarativeEngine *e)
120 : QDeclarativeCleanup(e), engine(e)
121 {
122     Q_ASSERT(engine);
123 }
124
125 /*!
126 Creates a new QDeclarativePropertyCache of \a metaObject.
127 */
128 QDeclarativePropertyCache::QDeclarativePropertyCache(QDeclarativeEngine *e, const QMetaObject *metaObject)
129 : QDeclarativeCleanup(e), engine(e)
130 {
131     Q_ASSERT(engine);
132     Q_ASSERT(metaObject);
133
134     update(engine, metaObject);
135 }
136
137 QDeclarativePropertyCache::~QDeclarativePropertyCache()
138 {
139     clear();
140 }
141
142 void QDeclarativePropertyCache::clear()
143 {
144     for (int ii = 0; ii < indexCache.count(); ++ii) {
145         if (indexCache.at(ii)) indexCache.at(ii)->release();
146     }
147
148     for (int ii = 0; ii < methodIndexCache.count(); ++ii) {
149         RData *data = methodIndexCache.at(ii);
150         if (data) data->release(); 
151     }
152
153     for (StringCache::ConstIterator iter = stringCache.begin(); 
154             iter != stringCache.end(); ++iter) {
155         RData *data = (*iter);
156         data->release(); 
157     }
158
159     for (IdentifierCache::ConstIterator iter = identifierCache.begin(); 
160             iter != identifierCache.end(); ++iter) {
161         RData *data = (*iter);
162         data->release(); 
163     }
164
165     indexCache.clear();
166     methodIndexCache.clear();
167     stringCache.clear();
168     identifierCache.clear();
169 }
170
171 QDeclarativePropertyCache::Data QDeclarativePropertyCache::create(const QMetaObject *metaObject, 
172                                                                   const QString &property)
173 {
174     Q_ASSERT(metaObject);
175
176     QDeclarativePropertyCache::Data rv;
177     {
178         const QMetaObject *cmo = metaObject;
179         while (cmo) {
180             int idx = metaObject->indexOfProperty(property.toUtf8());
181             if (idx != -1) {
182                 QMetaProperty p = metaObject->property(idx);
183                 if (p.isScriptable()) {
184                     rv.load(metaObject->property(idx));
185                     return rv;
186                 } else {
187                     while (cmo && cmo->propertyOffset() >= idx)
188                         cmo = cmo->superClass();
189                 }
190             } else {
191                 cmo = 0;
192             }
193         }
194     }
195
196     int methodCount = metaObject->methodCount();
197     for (int ii = methodCount - 1; ii >= 3; --ii) { // >=3 to block the destroyed signal and deleteLater() slot
198         QMetaMethod m = metaObject->method(ii);
199         if (m.access() == QMetaMethod::Private)
200             continue;
201         QString methodName = QString::fromUtf8(m.signature());
202
203         int parenIdx = methodName.indexOf(QLatin1Char('('));
204         Q_ASSERT(parenIdx != -1);
205         QStringRef methodNameRef = methodName.leftRef(parenIdx);
206
207         if (methodNameRef == property) {
208             rv.load(m);
209             return rv;
210         }
211     }
212
213     return rv;
214 }
215
216 QDeclarativePropertyCache *QDeclarativePropertyCache::copy() const
217 {
218     QDeclarativePropertyCache *cache = new QDeclarativePropertyCache(engine);
219     cache->indexCache = indexCache;
220     cache->methodIndexCache = methodIndexCache;
221     cache->stringCache = stringCache;
222     cache->identifierCache = identifierCache;
223     cache->allowedRevisionCache = allowedRevisionCache;
224
225     for (int ii = 0; ii < indexCache.count(); ++ii) {
226         if (indexCache.at(ii)) indexCache.at(ii)->addref();
227     }
228     for (int ii = 0; ii < methodIndexCache.count(); ++ii) {
229         if (methodIndexCache.at(ii)) methodIndexCache.at(ii)->addref();
230     }
231     for (StringCache::ConstIterator iter = stringCache.begin(); iter != stringCache.end(); ++iter)
232         (*iter)->addref();
233     for (IdentifierCache::ConstIterator iter = identifierCache.begin(); iter != identifierCache.end(); ++iter)
234         (*iter)->addref();
235
236     return cache;
237 }
238
239 void QDeclarativePropertyCache::append(QDeclarativeEngine *engine, const QMetaObject *metaObject, 
240                                        Data::Flag propertyFlags, Data::Flag methodFlags, Data::Flag signalFlags)
241 {
242     append(engine, metaObject, -1, propertyFlags, methodFlags, signalFlags);
243 }
244
245 void QDeclarativePropertyCache::append(QDeclarativeEngine *engine, const QMetaObject *metaObject, 
246                                        int revision, 
247                                        Data::Flag propertyFlags, Data::Flag methodFlags, Data::Flag signalFlags)
248 {
249     Q_UNUSED(revision);
250
251     allowedRevisionCache.append(0);
252
253     QDeclarativeEnginePrivate *enginePriv = QDeclarativeEnginePrivate::get(engine);
254     int methodCount = metaObject->methodCount();
255     // 3 to block the destroyed signal and the deleteLater() slot
256     int methodOffset = qMax(3, metaObject->methodOffset()); 
257
258     methodIndexCache.resize(methodCount);
259     for (int ii = methodOffset; ii < methodCount; ++ii) {
260         QMetaMethod m = metaObject->method(ii);
261         if (m.access() == QMetaMethod::Private) 
262             continue;
263         QString methodName = QString::fromUtf8(m.signature());
264
265         int parenIdx = methodName.indexOf(QLatin1Char('('));
266         Q_ASSERT(parenIdx != -1);
267         methodName = methodName.left(parenIdx);
268
269         RData *data = new RData;
270         data->identifier = enginePriv->objectClass->createPersistentIdentifier(methodName);
271         methodIndexCache[ii] = data;  
272
273         data->load(m);
274         if (m.methodType() == QMetaMethod::Slot || m.methodType() == QMetaMethod::Method) 
275             data->flags |= methodFlags;
276         else if (m.methodType() == QMetaMethod::Signal)
277             data->flags |= signalFlags;
278
279         data->metaObjectOffset = allowedRevisionCache.count() - 1;
280
281         if (stringCache.contains(methodName)) {
282             RData *old = stringCache[methodName];
283             // We only overload methods in the same class, exactly like C++
284             if (old->flags & Data::IsFunction && old->coreIndex >= methodOffset)
285                 data->relatedIndex = old->coreIndex;
286             data->overrideIndexIsProperty = !bool(old->flags & Data::IsFunction);
287             data->overrideIndex = old->coreIndex;
288             stringCache[methodName]->release();
289             identifierCache[data->identifier.identifier]->release();
290         }
291
292         stringCache.insert(methodName, data);
293         identifierCache.insert(data->identifier.identifier, data);
294         data->addref();
295         data->addref();
296     }
297
298     int propCount = metaObject->propertyCount();
299     int propOffset = metaObject->propertyOffset();
300
301     indexCache.resize(propCount);
302     for (int ii = propOffset; ii < propCount; ++ii) {
303         QMetaProperty p = metaObject->property(ii);
304         if (!p.isScriptable())
305             continue;
306
307         QString propName = QString::fromUtf8(p.name());
308
309         RData *data = new RData;
310         data->identifier = enginePriv->objectClass->createPersistentIdentifier(propName);
311         indexCache[ii] = data;
312
313         data->load(p, engine);
314         data->flags |= propertyFlags;
315
316         data->metaObjectOffset = allowedRevisionCache.count() - 1;
317
318         if (stringCache.contains(propName)) {
319             RData *old = stringCache[propName];
320             data->overrideIndexIsProperty = !bool(old->flags & Data::IsFunction);
321             data->overrideIndex = old->coreIndex;
322             stringCache[propName]->release();
323             identifierCache[data->identifier.identifier]->release();
324         }
325
326         stringCache.insert(propName, data);
327         identifierCache.insert(data->identifier.identifier, data);
328         data->addref();
329         data->addref();
330     }
331 }
332
333 void QDeclarativePropertyCache::updateRecur(QDeclarativeEngine *engine, const QMetaObject *metaObject)
334 {
335     if (!metaObject)
336         return;
337
338     updateRecur(engine, metaObject->superClass());
339
340     append(engine, metaObject);
341 }
342
343 void QDeclarativePropertyCache::update(QDeclarativeEngine *engine, const QMetaObject *metaObject)
344 {
345     Q_ASSERT(engine);
346     Q_ASSERT(metaObject);
347
348     clear();
349
350     // Optimization to prevent unnecessary reallocation of lists
351     indexCache.reserve(metaObject->propertyCount());
352     methodIndexCache.reserve(metaObject->methodCount());
353
354     updateRecur(engine,metaObject);
355 }
356
357 QDeclarativePropertyCache::Data *
358 QDeclarativePropertyCache::property(int index) const
359 {
360     if (index < 0 || index >= indexCache.count())
361         return 0;
362
363     return indexCache.at(index);
364 }
365
366 QDeclarativePropertyCache::Data *
367 QDeclarativePropertyCache::method(int index) const
368 {
369     if (index < 0 || index >= methodIndexCache.count())
370         return 0;
371
372     return methodIndexCache.at(index);
373 }
374
375 QDeclarativePropertyCache::Data *
376 QDeclarativePropertyCache::property(const QString &str) const
377 {
378     return stringCache.value(str);
379 }
380
381 QString QDeclarativePropertyCache::Data::name(QObject *object)
382 {
383     if (!object)
384         return QString();
385
386     return name(object->metaObject());
387 }
388
389 QString QDeclarativePropertyCache::Data::name(const QMetaObject *metaObject)
390 {
391     if (!metaObject || coreIndex == -1)
392         return QString();
393
394     if (flags & IsFunction) {
395         QMetaMethod m = metaObject->method(coreIndex);
396
397         QString name = QString::fromUtf8(m.signature());
398         int parenIdx = name.indexOf(QLatin1Char('('));
399         if (parenIdx != -1)
400             name = name.left(parenIdx);
401         return name;
402     } else {
403         QMetaProperty p = metaObject->property(coreIndex);
404         return QString::fromUtf8(p.name());
405     }
406 }
407
408 QStringList QDeclarativePropertyCache::propertyNames() const
409 {
410     return stringCache.keys();
411 }
412
413 QDeclarativePropertyCache::Data *QDeclarativePropertyCache::property(QDeclarativeEngine *engine, QObject *obj, 
414                                                    const QScriptDeclarativeClass::Identifier &name, Data &local)
415 {
416     QDeclarativePropertyCache::Data *rv = 0;
417
418     QDeclarativeEnginePrivate *enginePrivate = QDeclarativeEnginePrivate::get(engine);
419
420     QDeclarativePropertyCache *cache = 0;
421     QDeclarativeData *ddata = QDeclarativeData::get(obj);
422     if (ddata && ddata->propertyCache && ddata->propertyCache->qmlEngine() == engine)
423         cache = ddata->propertyCache;
424     if (!cache) {
425         cache = enginePrivate->cache(obj);
426         if (cache && ddata && !ddata->propertyCache) { cache->addref(); ddata->propertyCache = cache; }
427     }
428
429     if (cache) {
430         rv = cache->property(name);
431     } else {
432         local = QDeclarativePropertyCache::create(obj->metaObject(), enginePrivate->objectClass->toString(name));
433         if (local.isValid())
434             rv = &local;
435     }
436
437     return rv;
438 }
439
440 QDeclarativePropertyCache::Data *QDeclarativePropertyCache::property(QDeclarativeEngine *engine, QObject *obj, 
441                                                    const QString &name, Data &local)
442 {
443     QDeclarativePropertyCache::Data *rv = 0;
444
445     if (!engine) {
446         local = QDeclarativePropertyCache::create(obj->metaObject(), name);
447         if (local.isValid())
448             rv = &local;
449     } else {
450         QDeclarativeEnginePrivate *enginePrivate = QDeclarativeEnginePrivate::get(engine);
451
452         QDeclarativePropertyCache *cache = 0;
453         QDeclarativeData *ddata = QDeclarativeData::get(obj);
454         if (ddata && ddata->propertyCache && ddata->propertyCache->qmlEngine() == engine)
455             cache = ddata->propertyCache;
456         if (!cache) {
457             cache = enginePrivate->cache(obj);
458             if (cache && ddata && !ddata->propertyCache) { cache->addref(); ddata->propertyCache = cache; }
459         }
460
461         if (cache) {
462             rv = cache->property(name);
463         } else {
464             local = QDeclarativePropertyCache::create(obj->metaObject(), name);
465             if (local.isValid())
466                 rv = &local;
467         }
468     }
469
470     return rv;
471 }
472
473 QT_END_NAMESPACE