Initialize VisualDataModel v8 object constructor on demand.
authorAndrew den Exter <andrew.den-exter@nokia.com>
Wed, 28 Mar 2012 05:28:33 +0000 (15:28 +1000)
committerQt by Nokia <qt-info@nokia.com>
Mon, 21 May 2012 05:52:05 +0000 (07:52 +0200)
Avoid allocating data that isn't needed.

Change-Id: I6f1d55f535599516a8b5e711f977e81afd337bd6
Reviewed-by: Martin Jones <martin.jones@nokia.com>

src/quick/items/qquickvisualdatamodel.cpp
src/quick/items/qquickvisualdatamodel_p_p.h

index 8d809c1..417d50e 100644 (file)
@@ -1409,21 +1409,22 @@ QQuickVisualDataModelItemMetaType::QQuickVisualDataModelItemMetaType(
     , metaObject(0)
     , groupNames(groupNames)
 {
+    initializeMetaObject();
+}
+
+QQuickVisualDataModelItemMetaType::~QQuickVisualDataModelItemMetaType()
+{
+    free(metaObject);
+    qPersistentDispose(constructor);
+}
+
+void QQuickVisualDataModelItemMetaType::initializeMetaObject()
+{
     QMetaObjectBuilder builder;
     builder.setFlags(QMetaObjectBuilder::DynamicMetaObject);
     builder.setClassName(QQuickVisualDataModelAttached::staticMetaObject.className());
     builder.setSuperClass(&QQuickVisualDataModelAttached::staticMetaObject);
 
-    v8::HandleScope handleScope;
-    v8::Context::Scope contextScope(engine->context());
-
-    constructor = qPersistentNew(v8::ObjectTemplate::New());
-
-    constructor->SetHasExternalResource(true);
-    constructor->SetAccessor(v8::String::New("model"), get_model);
-    constructor->SetAccessor(v8::String::New("groups"), get_groups, set_groups);
-    constructor->SetAccessor(v8::String::New("isUnresolved"), get_member, 0, v8::Int32::New(30));
-
     int notifierId = 0;
     for (int i = 0; i < groupNames.count(); ++i, ++notifierId) {
         QString propertyName = QStringLiteral("in") + groupNames.at(i);
@@ -1432,9 +1433,6 @@ QQuickVisualDataModelItemMetaType::QQuickVisualDataModelItemMetaType(
         QMetaPropertyBuilder propertyBuilder = builder.addProperty(
                 propertyName.toUtf8(), "bool", notifierId);
         propertyBuilder.setWritable(true);
-
-        constructor->SetAccessor(
-                engine->toString(propertyName), get_member, set_member, v8::Int32::New(i + 1));
     }
     for (int i = 0; i < groupNames.count(); ++i, ++notifierId) {
         const QString propertyName = groupNames.at(i) + QStringLiteral("Index");
@@ -1442,18 +1440,35 @@ QQuickVisualDataModelItemMetaType::QQuickVisualDataModelItemMetaType(
         QMetaPropertyBuilder propertyBuilder = builder.addProperty(
                 propertyName.toUtf8(), "int", notifierId);
         propertyBuilder.setWritable(true);
-
-        constructor->SetAccessor(
-                engine->toString(propertyName), get_index, 0, v8::Int32::New(i + 1));
     }
 
     metaObject = builder.toMetaObject();
 }
 
-QQuickVisualDataModelItemMetaType::~QQuickVisualDataModelItemMetaType()
+void QQuickVisualDataModelItemMetaType::initializeConstructor()
 {
-    free(metaObject);
-    qPersistentDispose(constructor);
+    v8::HandleScope handleScope;
+    v8::Context::Scope contextScope(v8Engine->context());
+
+    constructor = qPersistentNew(v8::ObjectTemplate::New());
+
+    constructor->SetHasExternalResource(true);
+    constructor->SetAccessor(v8::String::New("model"), get_model);
+    constructor->SetAccessor(v8::String::New("groups"), get_groups, set_groups);
+    constructor->SetAccessor(v8::String::New("isUnresolved"), get_member, 0, v8::Int32::New(30));
+
+    int notifierId = 0;
+    for (int i = 0; i < groupNames.count(); ++i, ++notifierId) {
+        QString propertyName = QStringLiteral("in") + groupNames.at(i);
+        propertyName.replace(2, 1, propertyName.at(2).toUpper());
+        constructor->SetAccessor(
+                v8Engine->toString(propertyName), get_member, set_member, v8::Int32::New(i + 1));
+    }
+    for (int i = 0; i < groupNames.count(); ++i, ++notifierId) {
+        const QString propertyName = groupNames.at(i) + QStringLiteral("Index");
+        constructor->SetAccessor(
+                v8Engine->toString(propertyName), get_index, 0, v8::Int32::New(i + 1));
+    }
 }
 
 int QQuickVisualDataModelItemMetaType::parseGroups(const QStringList &groups) const
@@ -2049,6 +2064,8 @@ QQmlV8Handle QQuickVisualDataGroup::get(int index)
     }
 
     if (cacheItem->indexHandle.IsEmpty()) {
+        if (model->m_cacheMetaType->constructor.IsEmpty())
+            model->m_cacheMetaType->initializeConstructor();
         cacheItem->indexHandle = qPersistentNew(model->m_cacheMetaType->constructor->NewInstance());
         cacheItem->indexHandle->SetExternalResource(cacheItem);
         cacheItem->indexHandle.MakeWeak(cacheItem, QQuickVisualDataModelItemMetaType::release_index);
index ecfe1da..d539c49 100644 (file)
@@ -72,6 +72,9 @@ public:
     QQuickVisualDataModelItemMetaType(QV8Engine *engine, QQuickVisualDataModel *model, const QStringList &groupNames);
     ~QQuickVisualDataModelItemMetaType();
 
+    void initializeMetaObject();
+    void initializeConstructor();
+
     int parseGroups(const QStringList &groupNames) const;
     int parseGroups(const v8::Local<v8::Value> &groupNames) const;