diff options
author | Aaron Kennedy <aaron.kennedy@nokia.com> | 2009-04-27 05:36:11 (GMT) |
---|---|---|
committer | Aaron Kennedy <aaron.kennedy@nokia.com> | 2009-04-27 05:36:11 (GMT) |
commit | 9f941fd65083ae6d3f2f5441bf60346821932218 (patch) | |
tree | 4ccd25dd9de4835a551534de37a931e2105fa4d3 /src/declarative/qml | |
parent | f75b524b060682a896ac7a3951ac677d29e15727 (diff) | |
download | Qt-9f941fd65083ae6d3f2f5441bf60346821932218.zip Qt-9f941fd65083ae6d3f2f5441bf60346821932218.tar.gz Qt-9f941fd65083ae6d3f2f5441bf60346821932218.tar.bz2 |
Add a declarative data ptr to QObjectPrivate
This data ptr does not increase the size of the QObject, as we take advantage of space only used during destruction. Currently this data is only used to store an object's QmlContext, but it will be used for more later.
Diffstat (limited to 'src/declarative/qml')
-rw-r--r-- | src/declarative/qml/qml.h | 5 | ||||
-rw-r--r-- | src/declarative/qml/qmlcompiler.cpp | 9 | ||||
-rw-r--r-- | src/declarative/qml/qmlcompiler_p.h | 3 | ||||
-rw-r--r-- | src/declarative/qml/qmlcontext.cpp | 2 | ||||
-rw-r--r-- | src/declarative/qml/qmlcontext_p.h | 7 | ||||
-rw-r--r-- | src/declarative/qml/qmlengine.cpp | 33 | ||||
-rw-r--r-- | src/declarative/qml/qmlengine.h | 2 | ||||
-rw-r--r-- | src/declarative/qml/qmlvme.cpp | 3 |
8 files changed, 59 insertions, 5 deletions
diff --git a/src/declarative/qml/qml.h b/src/declarative/qml/qml.h index bf7b6bc..fb288b7 100644 --- a/src/declarative/qml/qml.h +++ b/src/declarative/qml/qml.h @@ -88,6 +88,11 @@ QT_MODULE(Declarative) #define QML_DEFINE_NOCREATE_TYPE(TYPE) \ template<> QmlPrivate::InstanceType QmlPrivate::Define<TYPE *>::instance(qmlRegisterType<TYPE>(#TYPE)); +class QmlContext; +class QmlEngine; +Q_DECLARATIVE_EXPORT QmlContext *qmlContext(const QObject *); +Q_DECLARATIVE_EXPORT QmlEngine *qmlEngine(const QObject *); + QML_DECLARE_TYPE(QObject); Q_DECLARE_METATYPE(QVariant); diff --git a/src/declarative/qml/qmlcompiler.cpp b/src/declarative/qml/qmlcompiler.cpp index f03ce20..5436227 100644 --- a/src/declarative/qml/qmlcompiler.cpp +++ b/src/declarative/qml/qmlcompiler.cpp @@ -1647,12 +1647,15 @@ QmlCompiledData &QmlCompiledData::operator=(const QmlCompiledData &other) return *this; } -QObject *QmlCompiledData::TypeReference::createInstance() const +QObject *QmlCompiledData::TypeReference::createInstance(QmlContext *ctxt) const { if(type) { - return type->create(); + QObject *rv = type->create(); + if(rv) + QmlEngine::setContextForObject(rv, ctxt); + return rv; } else if(component) { - return component->create(QmlContext::activeContext()); + return component->create(ctxt); } else { return 0; } diff --git a/src/declarative/qml/qmlcompiler_p.h b/src/declarative/qml/qmlcompiler_p.h index 754e284..286cda8 100644 --- a/src/declarative/qml/qmlcompiler_p.h +++ b/src/declarative/qml/qmlcompiler_p.h @@ -54,6 +54,7 @@ class QmlXmlParser; class QmlEngine; class QmlComponent; class QmlCompiledComponent; +class QmlContext; namespace QmlParser { class Object; @@ -83,7 +84,7 @@ public: QmlCustomParser *parser; QmlRefCount *ref; - QObject *createInstance() const; + QObject *createInstance(QmlContext *) const; }; QList<TypeReference> types; struct CustomTypeData diff --git a/src/declarative/qml/qmlcontext.cpp b/src/declarative/qml/qmlcontext.cpp index 104f460..40e33c2 100644 --- a/src/declarative/qml/qmlcontext.cpp +++ b/src/declarative/qml/qmlcontext.cpp @@ -96,6 +96,8 @@ void QmlContextPrivate::init() else scopeChain = parent->d_func()->scopeChain; scopeChain.prepend(scopeObj); + + contextData.context = q; } void QmlContextPrivate::addDefaultObject(QObject *object, Priority priority) diff --git a/src/declarative/qml/qmlcontext_p.h b/src/declarative/qml/qmlcontext_p.h index 8c51a6b..aa0bf3e 100644 --- a/src/declarative/qml/qmlcontext_p.h +++ b/src/declarative/qml/qmlcontext_p.h @@ -81,6 +81,13 @@ public: NormalPriority }; void addDefaultObject(QObject *, Priority); + + class ContextData : public QDeclarativeData + { + public: + QmlContext *context; + }; + ContextData contextData; }; QT_END_NAMESPACE diff --git a/src/declarative/qml/qmlengine.cpp b/src/declarative/qml/qmlengine.cpp index 5cbd02b..66c41f2 100644 --- a/src/declarative/qml/qmlengine.cpp +++ b/src/declarative/qml/qmlengine.cpp @@ -42,6 +42,7 @@ #include <QMetaProperty> #include <private/qmlengine_p.h> #include <private/qmlcontext_p.h> +#include <private/qobject_p.h> #ifdef QT_SCRIPTTOOLS_LIB #include <QScriptEngineDebugger> @@ -595,6 +596,38 @@ QNetworkAccessManager *QmlEngine::networkAccessManager() const return d->networkAccessManager; } +QmlContext *QmlEngine::contextForObject(const QObject *object) +{ + QObjectPrivate *priv = QObjectPrivate::get(const_cast<QObject *>(object)); + if(priv->declarativeData) { + return static_cast<QmlContextPrivate::ContextData *>(priv->declarativeData)->context; + } else { + return 0; + } +} + +QmlContext *qmlContext(const QObject *obj) +{ + return QmlEngine::contextForObject(obj); +} + +QmlEngine *qmlEngine(const QObject *obj) +{ + QmlContext *context = QmlEngine::contextForObject(obj); + return context?context->engine():0; +} + + +void QmlEngine::setContextForObject(QObject *object, QmlContext *context) +{ + QObjectPrivate *priv = QObjectPrivate::get(object); + if(priv->declarativeData) { + qWarning("QmlEngine::setContextForObject(): Object already has a QmlContext"); + return; + } + priv->declarativeData = &context->d_func()->contextData; +} + /*! \internal */ QScriptEngine *QmlEngine::scriptEngine() { diff --git a/src/declarative/qml/qmlengine.h b/src/declarative/qml/qmlengine.h index 086595a..fde84d4 100644 --- a/src/declarative/qml/qmlengine.h +++ b/src/declarative/qml/qmlengine.h @@ -81,6 +81,8 @@ public: void setNetworkAccessManager(QNetworkAccessManager *); QNetworkAccessManager *networkAccessManager() const; + static QmlContext *contextForObject(const QObject *); + static void setContextForObject(QObject *, QmlContext *); private: // LK: move to the private class QScriptEngine *scriptEngine(); diff --git a/src/declarative/qml/qmlvme.cpp b/src/declarative/qml/qmlvme.cpp index 966ef8a..b6d9173 100644 --- a/src/declarative/qml/qmlvme.cpp +++ b/src/declarative/qml/qmlvme.cpp @@ -253,7 +253,7 @@ QObject *QmlVME::run(QmlContext *ctxt, QmlCompiledComponent *comp, int start, in #ifdef Q_ENABLE_PERFORMANCE_LOG QFxCompilerTimer<QFxCompiler::InstrCreateObject> cc; #endif - QObject *o = types.at(instr.create.type).createInstance(); + QObject *o = types.at(instr.create.type).createInstance(QmlContext::activeContext()); if(!o) VME_EXCEPTION("Unable to create object of type" << types.at(instr.create.type).className); @@ -276,6 +276,7 @@ QObject *QmlVME::run(QmlContext *ctxt, QmlCompiledComponent *comp, int start, in QObject *o = QmlMetaType::toQObject(v); if(!o) VME_EXCEPTION("Unable to create" << types.at(instr.create.type).className); + QmlEngine::setContextForObject(o, QmlContext::activeContext()); if(!stack.isEmpty()) { QObject *parent = stack.top(); |