src/declarative/qml/qdeclarativepropertycache.cpp
changeset 30 5dc02b23752f
child 37 758a864f9613
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
       
     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/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 }
       
    92 
       
    93 void QDeclarativePropertyCache::Data::load(const QMetaMethod &m)
       
    94 {
       
    95     coreIndex = m.methodIndex();
       
    96     flags |= Data::IsFunction;
       
    97     propType = QVariant::Invalid;
       
    98 
       
    99     const char *returnType = m.typeName();
       
   100     if (returnType) 
       
   101         propType = QMetaType::type(returnType);
       
   102 
       
   103     QList<QByteArray> params = m.parameterTypes();
       
   104     if (!params.isEmpty())
       
   105         flags |= Data::HasArguments;
       
   106 }
       
   107 
       
   108 
       
   109 /*!
       
   110 Creates a new empty QDeclarativePropertyCache.
       
   111 */
       
   112 QDeclarativePropertyCache::QDeclarativePropertyCache(QDeclarativeEngine *e)
       
   113 : QDeclarativeCleanup(e), engine(e)
       
   114 {
       
   115     Q_ASSERT(engine);
       
   116 }
       
   117 
       
   118 /*!
       
   119 Creates a new QDeclarativePropertyCache of \a metaObject.
       
   120 */
       
   121 QDeclarativePropertyCache::QDeclarativePropertyCache(QDeclarativeEngine *e, const QMetaObject *metaObject)
       
   122 : QDeclarativeCleanup(e), engine(e)
       
   123 {
       
   124     Q_ASSERT(engine);
       
   125     Q_ASSERT(metaObject);
       
   126 
       
   127     update(engine, metaObject);
       
   128 }
       
   129 
       
   130 QDeclarativePropertyCache::~QDeclarativePropertyCache()
       
   131 {
       
   132     clear();
       
   133 }
       
   134 
       
   135 void QDeclarativePropertyCache::clear()
       
   136 {
       
   137     for (int ii = 0; ii < indexCache.count(); ++ii) 
       
   138         indexCache.at(ii)->release();
       
   139 
       
   140     for (StringCache::ConstIterator iter = stringCache.begin(); 
       
   141             iter != stringCache.end(); ++iter)
       
   142         (*iter)->release();
       
   143 
       
   144     for (IdentifierCache::ConstIterator iter = identifierCache.begin(); 
       
   145             iter != identifierCache.end(); ++iter)
       
   146         (*iter)->release();
       
   147 
       
   148     indexCache.clear();
       
   149     stringCache.clear();
       
   150     identifierCache.clear();
       
   151 }
       
   152 
       
   153 QDeclarativePropertyCache::Data QDeclarativePropertyCache::create(const QMetaObject *metaObject, 
       
   154                                                                   const QString &property)
       
   155 {
       
   156     Q_ASSERT(metaObject);
       
   157 
       
   158     QDeclarativePropertyCache::Data rv;
       
   159     int idx = metaObject->indexOfProperty(property.toUtf8());
       
   160     if (idx != -1) {
       
   161         rv.load(metaObject->property(idx));
       
   162         return rv;
       
   163     }
       
   164 
       
   165     int methodCount = metaObject->methodCount();
       
   166     for (int ii = methodCount - 1; ii >= 2; --ii) { // >=2 to block the destroyed signal
       
   167         QMetaMethod m = metaObject->method(ii);
       
   168         if (m.access() == QMetaMethod::Private)
       
   169             continue;
       
   170         QString methodName = QString::fromUtf8(m.signature());
       
   171 
       
   172         int parenIdx = methodName.indexOf(QLatin1Char('('));
       
   173         Q_ASSERT(parenIdx != -1);
       
   174         QStringRef methodNameRef = methodName.leftRef(parenIdx);
       
   175 
       
   176         if (methodNameRef == property) {
       
   177             rv.load(m);
       
   178             return rv;
       
   179         }
       
   180     }
       
   181 
       
   182     return rv;
       
   183 }
       
   184 
       
   185 QDeclarativePropertyCache *QDeclarativePropertyCache::copy() const
       
   186 {
       
   187     QDeclarativePropertyCache *cache = new QDeclarativePropertyCache(engine);
       
   188     cache->indexCache = indexCache;
       
   189     cache->stringCache = stringCache;
       
   190     cache->identifierCache = identifierCache;
       
   191 
       
   192     for (int ii = 0; ii < indexCache.count(); ++ii)
       
   193         indexCache.at(ii)->addref();
       
   194     for (StringCache::ConstIterator iter = stringCache.begin(); iter != stringCache.end(); ++iter)
       
   195         (*iter)->addref();
       
   196     for (IdentifierCache::ConstIterator iter = identifierCache.begin(); iter != identifierCache.end(); ++iter)
       
   197         (*iter)->addref();
       
   198 
       
   199     return cache;
       
   200 }
       
   201 
       
   202 void QDeclarativePropertyCache::append(QDeclarativeEngine *engine, const QMetaObject *metaObject, 
       
   203                               Data::Flag propertyFlags, Data::Flag methodFlags)
       
   204 {
       
   205     QDeclarativeEnginePrivate *enginePriv = QDeclarativeEnginePrivate::get(engine);
       
   206 
       
   207     int propCount = metaObject->propertyCount();
       
   208     int propOffset = metaObject->propertyOffset();
       
   209 
       
   210     indexCache.resize(propCount);
       
   211     for (int ii = propOffset; ii < propCount; ++ii) {
       
   212         QMetaProperty p = metaObject->property(ii);
       
   213         QString propName = QString::fromUtf8(p.name());
       
   214 
       
   215         RData *data = new RData;
       
   216         data->identifier = enginePriv->objectClass->createPersistentIdentifier(propName);
       
   217 
       
   218         data->load(p, engine);
       
   219         data->flags |= propertyFlags;
       
   220 
       
   221         indexCache[ii] = data;
       
   222 
       
   223         if (stringCache.contains(propName)) {
       
   224             stringCache[propName]->release();
       
   225             identifierCache[data->identifier.identifier]->release();
       
   226         }
       
   227 
       
   228         stringCache.insert(propName, data);
       
   229         identifierCache.insert(data->identifier.identifier, data);
       
   230         data->addref();
       
   231         data->addref();
       
   232     }
       
   233 
       
   234     int methodCount = metaObject->methodCount();
       
   235     int methodOffset = qMax(2, metaObject->methodOffset()); // 2 to block the destroyed signal
       
   236     for (int ii = methodOffset; ii < methodCount; ++ii) {
       
   237         QMetaMethod m = metaObject->method(ii);
       
   238         if (m.access() == QMetaMethod::Private)
       
   239             continue;
       
   240         QString methodName = QString::fromUtf8(m.signature());
       
   241 
       
   242         int parenIdx = methodName.indexOf(QLatin1Char('('));
       
   243         Q_ASSERT(parenIdx != -1);
       
   244         methodName = methodName.left(parenIdx);
       
   245 
       
   246         RData *data = new RData;
       
   247         data->identifier = enginePriv->objectClass->createPersistentIdentifier(methodName);
       
   248 
       
   249         if (stringCache.contains(methodName)) {
       
   250             stringCache[methodName]->release();
       
   251             identifierCache[data->identifier.identifier]->release();
       
   252         }
       
   253 
       
   254         data->load(m);
       
   255         if (m.methodType() == QMetaMethod::Slot || m.methodType() == QMetaMethod::Method) 
       
   256             data->flags |= methodFlags;
       
   257 
       
   258         stringCache.insert(methodName, data);
       
   259         identifierCache.insert(data->identifier.identifier, data);
       
   260         data->addref();
       
   261     }
       
   262 }
       
   263 
       
   264 void QDeclarativePropertyCache::update(QDeclarativeEngine *engine, const QMetaObject *metaObject)
       
   265 {
       
   266     Q_ASSERT(engine);
       
   267     Q_ASSERT(metaObject);
       
   268     QDeclarativeEnginePrivate *enginePriv = QDeclarativeEnginePrivate::get(engine);
       
   269 
       
   270     clear();
       
   271 
       
   272     // ### The properties/methods should probably be spliced on a per-metaobject basis
       
   273     int propCount = metaObject->propertyCount();
       
   274 
       
   275     indexCache.resize(propCount);
       
   276     for (int ii = propCount - 1; ii >= 0; --ii) {
       
   277         QMetaProperty p = metaObject->property(ii);
       
   278         QString propName = QString::fromUtf8(p.name());
       
   279 
       
   280         RData *data = new RData;
       
   281         data->identifier = enginePriv->objectClass->createPersistentIdentifier(propName);
       
   282 
       
   283         data->load(p, engine);
       
   284 
       
   285         indexCache[ii] = data;
       
   286 
       
   287         if (stringCache.contains(propName))
       
   288             continue;
       
   289 
       
   290         stringCache.insert(propName, data);
       
   291         identifierCache.insert(data->identifier.identifier, data);
       
   292         data->addref();
       
   293         data->addref();
       
   294     }
       
   295 
       
   296     int methodCount = metaObject->methodCount();
       
   297     for (int ii = methodCount - 1; ii >= 2; --ii) { // >=2 to block the destroyed signal
       
   298         QMetaMethod m = metaObject->method(ii);
       
   299         if (m.access() == QMetaMethod::Private)
       
   300             continue;
       
   301         QString methodName = QString::fromUtf8(m.signature());
       
   302 
       
   303         int parenIdx = methodName.indexOf(QLatin1Char('('));
       
   304         Q_ASSERT(parenIdx != -1);
       
   305         methodName = methodName.left(parenIdx);
       
   306 
       
   307         if (stringCache.contains(methodName))
       
   308             continue;
       
   309 
       
   310         RData *data = new RData;
       
   311         data->identifier = enginePriv->objectClass->createPersistentIdentifier(methodName);
       
   312 
       
   313         data->load(m);
       
   314 
       
   315         stringCache.insert(methodName, data);
       
   316         identifierCache.insert(data->identifier.identifier, data);
       
   317         data->addref();
       
   318     }
       
   319 }
       
   320 
       
   321 QDeclarativePropertyCache::Data *
       
   322 QDeclarativePropertyCache::property(int index) const
       
   323 {
       
   324     if (index < 0 || index >= indexCache.count())
       
   325         return 0;
       
   326 
       
   327     return indexCache.at(index);
       
   328 }
       
   329 
       
   330 QDeclarativePropertyCache::Data *
       
   331 QDeclarativePropertyCache::property(const QString &str) const
       
   332 {
       
   333     return stringCache.value(str);
       
   334 }
       
   335 
       
   336 QString QDeclarativePropertyCache::Data::name(QObject *object)
       
   337 {
       
   338     if (!object)
       
   339         return QString();
       
   340 
       
   341     return name(object->metaObject());
       
   342 }
       
   343 
       
   344 QString QDeclarativePropertyCache::Data::name(const QMetaObject *metaObject)
       
   345 {
       
   346     if (!metaObject || coreIndex == -1)
       
   347         return QString();
       
   348 
       
   349     if (flags & IsFunction) {
       
   350         QMetaMethod m = metaObject->method(coreIndex);
       
   351 
       
   352         QString name = QString::fromUtf8(m.signature());
       
   353         int parenIdx = name.indexOf(QLatin1Char('('));
       
   354         if (parenIdx != -1)
       
   355             name = name.left(parenIdx);
       
   356         return name;
       
   357     } else {
       
   358         QMetaProperty p = metaObject->property(coreIndex);
       
   359         return QString::fromUtf8(p.name());
       
   360     }
       
   361 }
       
   362 
       
   363 QStringList QDeclarativePropertyCache::propertyNames() const
       
   364 {
       
   365     return stringCache.keys();
       
   366 }
       
   367 
       
   368 QDeclarativePropertyCache::Data *QDeclarativePropertyCache::property(QDeclarativeEngine *engine, QObject *obj, 
       
   369                                                    const QScriptDeclarativeClass::Identifier &name, Data &local)
       
   370 {
       
   371     QDeclarativePropertyCache::Data *rv = 0;
       
   372 
       
   373     QDeclarativeEnginePrivate *enginePrivate = QDeclarativeEnginePrivate::get(engine);
       
   374 
       
   375     QDeclarativePropertyCache *cache = 0;
       
   376     QDeclarativeData *ddata = QDeclarativeData::get(obj);
       
   377     if (ddata && ddata->propertyCache && ddata->propertyCache->qmlEngine() == engine)
       
   378         cache = ddata->propertyCache;
       
   379     if (!cache) {
       
   380         cache = enginePrivate->cache(obj);
       
   381         if (cache && ddata && !ddata->propertyCache) { cache->addref(); ddata->propertyCache = cache; }
       
   382     }
       
   383 
       
   384     if (cache) {
       
   385         rv = cache->property(name);
       
   386     } else {
       
   387         local = QDeclarativePropertyCache::create(obj->metaObject(), enginePrivate->objectClass->toString(name));
       
   388         if (local.isValid())
       
   389             rv = &local;
       
   390     }
       
   391 
       
   392     return rv;
       
   393 }
       
   394 
       
   395 QDeclarativePropertyCache::Data *QDeclarativePropertyCache::property(QDeclarativeEngine *engine, QObject *obj, 
       
   396                                                    const QString &name, Data &local)
       
   397 {
       
   398     QDeclarativePropertyCache::Data *rv = 0;
       
   399 
       
   400     if (!engine) {
       
   401         local = QDeclarativePropertyCache::create(obj->metaObject(), name);
       
   402         if (local.isValid())
       
   403             rv = &local;
       
   404     } else {
       
   405         QDeclarativeEnginePrivate *enginePrivate = QDeclarativeEnginePrivate::get(engine);
       
   406 
       
   407         QDeclarativePropertyCache *cache = 0;
       
   408         QDeclarativeData *ddata = QDeclarativeData::get(obj);
       
   409         if (ddata && ddata->propertyCache && ddata->propertyCache->qmlEngine() == engine)
       
   410             cache = ddata->propertyCache;
       
   411         if (!cache) {
       
   412             cache = enginePrivate->cache(obj);
       
   413             if (cache && ddata && !ddata->propertyCache) { cache->addref(); ddata->propertyCache = cache; }
       
   414         }
       
   415 
       
   416         if (cache) {
       
   417             rv = cache->property(name);
       
   418         } else {
       
   419             local = QDeclarativePropertyCache::create(obj->metaObject(), name);
       
   420             if (local.isValid())
       
   421                 rv = &local;
       
   422         }
       
   423     }
       
   424 
       
   425     return rv;
       
   426 }
       
   427 
       
   428 QT_END_NAMESPACE