From: Andrew den Exter Date: Wed, 28 Mar 2012 05:28:33 +0000 (+1000) Subject: Initialize VisualDataModel v8 object constructor on demand. X-Git-Tag: upstream/5.2.1~1846 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=cd5df3ec6fdb79260ca5bddff4f3eda9717bd70b;p=platform%2Fupstream%2Fqtdeclarative.git Initialize VisualDataModel v8 object constructor on demand. Avoid allocating data that isn't needed. Change-Id: I6f1d55f535599516a8b5e711f977e81afd337bd6 Reviewed-by: Martin Jones --- diff --git a/src/quick/items/qquickvisualdatamodel.cpp b/src/quick/items/qquickvisualdatamodel.cpp index 8d809c1..417d50e 100644 --- a/src/quick/items/qquickvisualdatamodel.cpp +++ b/src/quick/items/qquickvisualdatamodel.cpp @@ -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); diff --git a/src/quick/items/qquickvisualdatamodel_p_p.h b/src/quick/items/qquickvisualdatamodel_p_p.h index ecfe1da..d539c49 100644 --- a/src/quick/items/qquickvisualdatamodel_p_p.h +++ b/src/quick/items/qquickvisualdatamodel_p_p.h @@ -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 &groupNames) const;