From 303730a3fdd399a3e0a65b08338b4db75aa2acee Mon Sep 17 00:00:00 2001 From: Aaron Kennedy Date: Thu, 11 Mar 2010 17:53:42 +1000 Subject: Replace QDeclarativeContext::addDefaultObject() -> setContextObject() It is faster and easier to use to just support a single context object. --- demos/declarative/minehunt/minehunt.cpp | 2 +- .../graphicsitems/qdeclarativeloader.cpp | 2 +- .../graphicsitems/qdeclarativevisualitemmodel.cpp | 6 ++-- .../qml/qdeclarativecompiledbindings.cpp | 4 +-- src/declarative/qml/qdeclarativecontext.cpp | 29 ++++++++++++-------- src/declarative/qml/qdeclarativecontext.h | 7 +++-- src/declarative/qml/qdeclarativecontext_p.h | 3 +- .../qml/qdeclarativecontextscriptclass.cpp | 26 ++++++------------ .../qml/qdeclarativecontextscriptclass_p.h | 1 - src/declarative/qml/qdeclarativevme.cpp | 2 +- .../tst_qdeclarativecontext.cpp | 32 ++++------------------ .../declarative/qdeclarativeecmascript/testtypes.h | 10 +------ .../tst_qdeclarativeecmascript.cpp | 12 ++++---- .../tst_qdeclarativelistmodel.cpp | 2 +- 14 files changed, 50 insertions(+), 88 deletions(-) diff --git a/demos/declarative/minehunt/minehunt.cpp b/demos/declarative/minehunt/minehunt.cpp index 2e1b5b3..5e44d1b 100644 --- a/demos/declarative/minehunt/minehunt.cpp +++ b/demos/declarative/minehunt/minehunt.cpp @@ -303,7 +303,7 @@ class MinehuntExtensionPlugin : public QDeclarativeExtensionPlugin MinehuntGame* game = new MinehuntGame(); - engine->rootContext()->addDefaultObject(game); + engine->rootContext()->setContextObject(game); } }; diff --git a/src/declarative/graphicsitems/qdeclarativeloader.cpp b/src/declarative/graphicsitems/qdeclarativeloader.cpp index c9e80ee..61a32e5 100644 --- a/src/declarative/graphicsitems/qdeclarativeloader.cpp +++ b/src/declarative/graphicsitems/qdeclarativeloader.cpp @@ -273,7 +273,7 @@ void QDeclarativeLoaderPrivate::_q_sourceLoaded() if (component) { QDeclarativeContext *ctxt = new QDeclarativeContext(qmlContext(q)); - ctxt->addDefaultObject(q); + ctxt->setContextObject(q); if (!component->errors().isEmpty()) { qWarning() << component->errors(); diff --git a/src/declarative/graphicsitems/qdeclarativevisualitemmodel.cpp b/src/declarative/graphicsitems/qdeclarativevisualitemmodel.cpp index 6bad4da..6341764 100644 --- a/src/declarative/graphicsitems/qdeclarativevisualitemmodel.cpp +++ b/src/declarative/graphicsitems/qdeclarativevisualitemmodel.cpp @@ -196,7 +196,7 @@ QVariant QDeclarativeVisualItemModel::evaluate(int index, const QString &express return QVariant(); QDeclarativeContext *ccontext = qmlContext(this); QDeclarativeContext *ctxt = new QDeclarativeContext(ccontext); - ctxt->addDefaultObject(d->children.at(index)); + ctxt->setContextObject(d->children.at(index)); QDeclarativeExpression e(ctxt, expression, objectContext); QVariant value = e.value(); delete ctxt; @@ -991,7 +991,7 @@ QDeclarativeItem *QDeclarativeVisualDataModel::item(int index, const QByteArray QDeclarativeContext *ctxt = new QDeclarativeContext(ccontext); QDeclarativeVisualDataModelData *data = new QDeclarativeVisualDataModelData(index, this); ctxt->setContextProperty(QLatin1String("model"), data); - ctxt->addDefaultObject(data); + ctxt->setContextObject(data); nobj = d->m_delegate->beginCreate(ctxt); if (complete) d->m_delegate->completeCreate(); @@ -1104,7 +1104,7 @@ QVariant QDeclarativeVisualDataModel::evaluate(int index, const QString &express if (!ccontext) ccontext = qmlContext(this); QDeclarativeContext *ctxt = new QDeclarativeContext(ccontext); QDeclarativeVisualDataModelData *data = new QDeclarativeVisualDataModelData(index, this); - ctxt->addDefaultObject(data); + ctxt->setContextObject(data); QDeclarativeExpression e(ctxt, expression, objectContext); value = e.value(); delete data; diff --git a/src/declarative/qml/qdeclarativecompiledbindings.cpp b/src/declarative/qml/qdeclarativecompiledbindings.cpp index b35b5b5..c66a417 100644 --- a/src/declarative/qml/qdeclarativecompiledbindings.cpp +++ b/src/declarative/qml/qdeclarativecompiledbindings.cpp @@ -845,7 +845,7 @@ void QDeclarativeCompiledBindingsPrivate::findgeneric(Register *output, } } - if (QObject *root = context->defaultObjects.isEmpty()?0:context->defaultObjects.first()) { + if (QObject *root = context->contextObject) { if (findproperty(root, output, enginePriv, subIdx, name, isTerminal)) return; @@ -1086,7 +1086,7 @@ void QDeclarativeCompiledBindingsPrivate::run(int instrIndex, break; case Instr::LoadRoot: - registers[instr->load.reg].setQObject(context->defaultObjects.at(0)); + registers[instr->load.reg].setQObject(context->contextObject); break; case Instr::LoadAttached: diff --git a/src/declarative/qml/qdeclarativecontext.cpp b/src/declarative/qml/qdeclarativecontext.cpp index 356a4ab..58de1a7 100644 --- a/src/declarative/qml/qdeclarativecontext.cpp +++ b/src/declarative/qml/qdeclarativecontext.cpp @@ -59,7 +59,7 @@ QT_BEGIN_NAMESPACE QDeclarativeContextPrivate::QDeclarativeContextPrivate() : parent(0), engine(0), isInternal(false), propertyNames(0), - notifyIndex(-1), highPriorityCount(0), imports(0), expressions(0), contextObjects(0), + notifyIndex(-1), contextObject(0), imports(0), expressions(0), contextObjects(0), idValues(0), idValueCount(0), optimizedBindings(0) { } @@ -172,7 +172,7 @@ void QDeclarativeContextPrivate::init() MyDataSet myDataSet; QDeclarativeEngine engine; QDeclarativeContext context(engine.rootContext()); - context.addDefaultObject(&myDataSet); + context.setContextObject(&myDataSet); QDeclarativeComponent component(&engine, "ListView { model=myModel }"); component.create(&context); @@ -365,13 +365,21 @@ QDeclarativeContext *QDeclarativeContext::parentContext() const } /*! - Add \a defaultObject to this context. The object will be added after - any existing default objects. + Return the context object, or 0 if there is no context object. */ -void QDeclarativeContext::addDefaultObject(QObject *defaultObject) +QObject *QDeclarativeContext::contextObject() const +{ + Q_D(const QDeclarativeContext); + return d->contextObject; +} + +/*! + Set the context \a object. +*/ +void QDeclarativeContext::setContextObject(QObject *object) { Q_D(QDeclarativeContext); - d->defaultObjects.prepend(defaultObject); + d->contextObject = object; } /*! @@ -466,15 +474,12 @@ QVariant QDeclarativeContext::contextProperty(const QString &name) const if (idx == -1) { QByteArray utf8Name = name.toUtf8(); - for (int ii = d->defaultObjects.count() - 1; ii >= 0; --ii) { - QObject *obj = d->defaultObjects.at(ii); + if (d->contextObject) { + QObject *obj = d->contextObject; QDeclarativePropertyCache::Data local; QDeclarativePropertyCache::Data *property = QDeclarativePropertyCache::property(d->engine, obj, name, local); - if (property) { - value = obj->metaObject()->property(property->coreIndex).read(obj); - break; - } + if (property) value = obj->metaObject()->property(property->coreIndex).read(obj); } if (!value.isValid() && parentContext()) value = parentContext()->contextProperty(name); diff --git a/src/declarative/qml/qdeclarativecontext.h b/src/declarative/qml/qdeclarativecontext.h index 0fb9bee..3ad9863 100644 --- a/src/declarative/qml/qdeclarativecontext.h +++ b/src/declarative/qml/qdeclarativecontext.h @@ -72,11 +72,12 @@ public: QDeclarativeEngine *engine() const; QDeclarativeContext *parentContext() const; - void addDefaultObject(QObject *); - void setContextProperty(const QString &, QObject *); - void setContextProperty(const QString &, const QVariant &); + QObject *contextObject() const; + void setContextObject(QObject *); QVariant contextProperty(const QString &) const; + void setContextProperty(const QString &, QObject *); + void setContextProperty(const QString &, const QVariant &); QUrl resolvedUrl(const QUrl &); diff --git a/src/declarative/qml/qdeclarativecontext_p.h b/src/declarative/qml/qdeclarativecontext_p.h index 5b597fa..ba9aa41 100644 --- a/src/declarative/qml/qdeclarativecontext_p.h +++ b/src/declarative/qml/qdeclarativecontext_p.h @@ -94,8 +94,7 @@ public: QList propertyValues; int notifyIndex; - QObjectList defaultObjects; - int highPriorityCount; + QObject *contextObject; QList scripts; void addScript(const QDeclarativeParser::Object::ScriptBlock &, QObject *); diff --git a/src/declarative/qml/qdeclarativecontextscriptclass.cpp b/src/declarative/qml/qdeclarativecontextscriptclass.cpp index 5fcf4e2..874eeac 100644 --- a/src/declarative/qml/qdeclarativecontextscriptclass.cpp +++ b/src/declarative/qml/qdeclarativecontextscriptclass.cpp @@ -80,7 +80,7 @@ struct ContextData : public QScriptDeclarativeClass::Object { */ QDeclarativeContextScriptClass::QDeclarativeContextScriptClass(QDeclarativeEngine *bindEngine) : QDeclarativeScriptClass(QDeclarativeEnginePrivate::getScriptEngine(bindEngine)), engine(bindEngine), - lastScopeObject(0), lastContext(0), lastData(0), lastPropertyIndex(-1), lastDefaultObject(-1) + lastScopeObject(0), lastContext(0), lastData(0), lastPropertyIndex(-1) { } @@ -132,7 +132,6 @@ QDeclarativeContextScriptClass::queryProperty(Object *object, const Identifier & lastContext = 0; lastData = 0; lastPropertyIndex = -1; - lastDefaultObject = -1; QDeclarativeContext *bindContext = ((ContextData *)object)->getContext(engine); QObject *scopeObject = ((ContextData *)object)->getScope(engine); @@ -210,13 +209,13 @@ QDeclarativeContextScriptClass::queryProperty(QDeclarativeContext *bindContext, } } - for (int ii = cp->defaultObjects.count() - 1; ii >= 0; --ii) { + if (cp->contextObject) { QScriptClass::QueryFlags rv = - ep->objectClass->queryProperty(cp->defaultObjects.at(ii), name, flags, bindContext, + ep->objectClass->queryProperty(cp->contextObject, name, flags, bindContext, QDeclarativeObjectScriptClass::ImplicitObject | QDeclarativeObjectScriptClass::SkipAttachedProperties); if (rv) { - lastDefaultObject = ii; + lastScopeObject = cp->contextObject; lastContext = bindContext; return rv; } @@ -244,9 +243,9 @@ QDeclarativeContextScriptClass::property(Object *object, const Identifier &name) } else if (lastData) { if (lastData->type) - return Value(scriptEngine, ep->typeNameClass->newObject(cp->defaultObjects.at(0), lastData->type)); + return Value(scriptEngine, ep->typeNameClass->newObject(cp->contextObject, lastData->type)); else - return Value(scriptEngine, ep->typeNameClass->newObject(cp->defaultObjects.at(0), lastData->typeNamespace)); + return Value(scriptEngine, ep->typeNameClass->newObject(cp->contextObject, lastData->typeNamespace)); } else if (lastPropertyIndex != -1) { @@ -267,10 +266,6 @@ QDeclarativeContextScriptClass::property(Object *object, const Identifier &name) return Value(scriptEngine, rv); - } else if(lastDefaultObject != -1) { - - // Default object property - return ep->objectClass->property(cp->defaultObjects.at(lastDefaultObject), name); } else { @@ -283,7 +278,7 @@ void QDeclarativeContextScriptClass::setProperty(Object *object, const Identifie const QScriptValue &value) { Q_UNUSED(object); - Q_ASSERT(lastScopeObject || lastDefaultObject != -1); + Q_ASSERT(lastScopeObject); QDeclarativeContext *bindContext = lastContext; Q_ASSERT(bindContext); @@ -291,12 +286,7 @@ void QDeclarativeContextScriptClass::setProperty(Object *object, const Identifie QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(engine); QDeclarativeContextPrivate *cp = QDeclarativeContextPrivate::get(bindContext); - if (lastScopeObject) { - ep->objectClass->setProperty(lastScopeObject, name, value, bindContext); - } else { - ep->objectClass->setProperty(cp->defaultObjects.at(lastDefaultObject), name, value, - bindContext); - } + ep->objectClass->setProperty(lastScopeObject, name, value, bindContext); } QT_END_NAMESPACE diff --git a/src/declarative/qml/qdeclarativecontextscriptclass_p.h b/src/declarative/qml/qdeclarativecontextscriptclass_p.h index 4b0dca0..32c117c 100644 --- a/src/declarative/qml/qdeclarativecontextscriptclass_p.h +++ b/src/declarative/qml/qdeclarativecontextscriptclass_p.h @@ -90,7 +90,6 @@ private: QDeclarativeContext *lastContext; QDeclarativeTypeNameCache::Data *lastData; int lastPropertyIndex; - int lastDefaultObject; QScriptValue lastFunction; uint m_id; diff --git a/src/declarative/qml/qdeclarativevme.cpp b/src/declarative/qml/qdeclarativevme.cpp index d916900..055a857 100644 --- a/src/declarative/qml/qdeclarativevme.cpp +++ b/src/declarative/qml/qdeclarativevme.cpp @@ -237,7 +237,7 @@ QObject *QDeclarativeVME::run(QDeclarativeVMEStack &stack, QDeclarati case QDeclarativeInstruction::SetDefault: { QObject *target = stack.top(); - ctxt->addDefaultObject(target); + ctxt->setContextObject(target); } break; diff --git a/tests/auto/declarative/qdeclarativecontext/tst_qdeclarativecontext.cpp b/tests/auto/declarative/qdeclarativecontext/tst_qdeclarativecontext.cpp index f0117f5..b212820 100644 --- a/tests/auto/declarative/qdeclarativecontext/tst_qdeclarativecontext.cpp +++ b/tests/auto/declarative/qdeclarativecontext/tst_qdeclarativecontext.cpp @@ -58,7 +58,7 @@ private slots: void engineMethod(); void parentContext(); void setContextProperty(); - void addDefaultObject(); + void setContextObject(); void destruction(); void idAsContextProperty(); @@ -224,24 +224,6 @@ private: int _c; }; -class TestObject2 : public QObject -{ - Q_OBJECT - Q_PROPERTY(int b READ b NOTIFY bChanged) - -public: - TestObject2() : _b(10) {} - - int b() const { return _b; } - void setB(int b) { _b = b; emit bChanged(); } - -signals: - void bChanged(); - -private: - int _b; -}; - #define TEST_CONTEXT_PROPERTY(ctxt, name, value) \ { \ QDeclarativeComponent component(&engine); \ @@ -367,35 +349,31 @@ void tst_qdeclarativecontext::setContextProperty() } } -void tst_qdeclarativecontext::addDefaultObject() +void tst_qdeclarativecontext::setContextObject() { QDeclarativeContext ctxt(&engine); TestObject to; - TestObject2 to2; to.setA(2); to.setB(192); to.setC(18); - to2.setB(111999); - ctxt.addDefaultObject(&to2); - ctxt.addDefaultObject(&to); + ctxt.setContextObject(&to); ctxt.setContextProperty("c", QVariant(9)); // Static context properties TEST_CONTEXT_PROPERTY(&ctxt, a, QVariant(2)); - TEST_CONTEXT_PROPERTY(&ctxt, b, QVariant(111999)); + TEST_CONTEXT_PROPERTY(&ctxt, b, QVariant(192)); TEST_CONTEXT_PROPERTY(&ctxt, c, QVariant(9)); to.setA(12); to.setB(100); to.setC(7); - to2.setB(1612); ctxt.setContextProperty("c", QVariant(3)); TEST_CONTEXT_PROPERTY(&ctxt, a, QVariant(12)); - TEST_CONTEXT_PROPERTY(&ctxt, b, QVariant(1612)); + TEST_CONTEXT_PROPERTY(&ctxt, b, QVariant(100)); TEST_CONTEXT_PROPERTY(&ctxt, c, QVariant(3)); // Changes in context properties diff --git a/tests/auto/declarative/qdeclarativeecmascript/testtypes.h b/tests/auto/declarative/qdeclarativeecmascript/testtypes.h index a283e3f..78a5e0f 100644 --- a/tests/auto/declarative/qdeclarativeecmascript/testtypes.h +++ b/tests/auto/declarative/qdeclarativeecmascript/testtypes.h @@ -207,18 +207,10 @@ class MyDefaultObject1 : public QObject Q_OBJECT Q_PROPERTY(int horseLegs READ horseLegs CONSTANT); Q_PROPERTY(int antLegs READ antLegs CONSTANT); + Q_PROPERTY(int emuLegs READ emuLegs CONSTANT); public: int horseLegs() const { return 4; } int antLegs() const { return 6; } -}; - -class MyDefaultObject2 : public QObject -{ - Q_OBJECT - Q_PROPERTY(int antLegs READ antLegs CONSTANT); - Q_PROPERTY(int emuLegs READ emuLegs CONSTANT); -public: - int antLegs() const { return 5; } // Had an accident int emuLegs() const { return 2; } }; diff --git a/tests/auto/declarative/qdeclarativeecmascript/tst_qdeclarativeecmascript.cpp b/tests/auto/declarative/qdeclarativeecmascript/tst_qdeclarativeecmascript.cpp index bdd2c93..caefdbf 100644 --- a/tests/auto/declarative/qdeclarativeecmascript/tst_qdeclarativeecmascript.cpp +++ b/tests/auto/declarative/qdeclarativeecmascript/tst_qdeclarativeecmascript.cpp @@ -350,7 +350,6 @@ void tst_qdeclarativeecmascript::basicExpressions() MyQmlObject object2; MyQmlObject object3; MyDefaultObject1 default1; - MyDefaultObject2 default2; MyDefaultObject3 default3; object1.setStringProperty("Object1"); object2.setStringProperty("Object2"); @@ -359,13 +358,12 @@ void tst_qdeclarativeecmascript::basicExpressions() QDeclarativeContext context(engine.rootContext()); QDeclarativeContext nestedContext(&context); - context.addDefaultObject(&default1); - context.addDefaultObject(&default2); + context.setContextObject(&default1); context.setContextProperty("a", QVariant(1944)); context.setContextProperty("b", QVariant("Milk")); context.setContextProperty("object", &object1); context.setContextProperty("objectOverride", &object2); - nestedContext.addDefaultObject(&default3); + nestedContext.setContextObject(&default3); nestedContext.setContextProperty("b", QVariant("Cow")); nestedContext.setContextProperty("objectOverride", &object3); nestedContext.setContextProperty("millipedeLegs", QVariant(100)); @@ -1639,7 +1637,7 @@ void tst_qdeclarativeecmascript::listToVariant() MyQmlContainer container; QDeclarativeContext context(engine.rootContext()); - context.addDefaultObject(&container); + context.setContextObject(&container); QObject *object = component.create(&context); QVERIFY(object != 0); @@ -1911,7 +1909,7 @@ void tst_qdeclarativeecmascript::ownership() { OwnershipObject own; QDeclarativeContext *context = new QDeclarativeContext(engine.rootContext()); - context->addDefaultObject(&own); + context->setContextObject(&own); { QDeclarativeComponent component(&engine, TEST_FILE("ownership.qml")); @@ -1971,7 +1969,7 @@ void tst_qdeclarativeecmascript::qlistqobjectMethods() { QListQObjectMethodsObject obj; QDeclarativeContext *context = new QDeclarativeContext(engine.rootContext()); - context->addDefaultObject(&obj); + context->setContextObject(&obj); QDeclarativeComponent component(&engine, TEST_FILE("qlistqobjectMethods.qml")); diff --git a/tests/auto/declarative/qdeclarativelistmodel/tst_qdeclarativelistmodel.cpp b/tests/auto/declarative/qdeclarativelistmodel/tst_qdeclarativelistmodel.cpp index a1e6d6b..1b59608 100644 --- a/tests/auto/declarative/qdeclarativelistmodel/tst_qdeclarativelistmodel.cpp +++ b/tests/auto/declarative/qdeclarativelistmodel/tst_qdeclarativelistmodel.cpp @@ -214,7 +214,7 @@ void tst_QDeclarativeListModel::dynamic() QDeclarativeEngine engine; QDeclarativeListModel model; QDeclarativeEngine::setContextForObject(&model,engine.rootContext()); - engine.rootContext()->addDefaultObject(&model); + engine.rootContext()->setContextObject(&model); QDeclarativeExpression e(engine.rootContext(), script, &model); if (!warning.isEmpty()) QTest::ignoreMessage(QtWarningMsg, warning.toLatin1()); -- cgit v0.12