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