From 222e132ecd5cc6322d92e50a3ecc1e427eb1c70e Mon Sep 17 00:00:00 2001 From: Marco Martin Date: Tue, 19 Feb 2013 20:20:32 +0100 Subject: [PATCH] use the function QmlObject::createObjectFromSource --- .../qml/declarative/qmlobject.cpp | 31 ++++++++++++++++--- src/scriptengines/qml/declarative/qmlobject.h | 8 +++++ .../qml/plasmoid/appletinterface.cpp | 27 +++++----------- .../qml/plasmoid/containmentinterface.cpp | 4 +-- 4 files changed, 44 insertions(+), 26 deletions(-) diff --git a/src/scriptengines/qml/declarative/qmlobject.cpp b/src/scriptengines/qml/declarative/qmlobject.cpp index abf89cb56..e6377d943 100644 --- a/src/scriptengines/qml/declarative/qmlobject.cpp +++ b/src/scriptengines/qml/declarative/qmlobject.cpp @@ -50,7 +50,7 @@ public: delete root.data(); } - void errorPrint(); + void errorPrint(QQmlComponent *component); void execute(const QUrl &source); void scheduleExecutionEnd(); void minimumWidthChanged(); @@ -71,7 +71,7 @@ public: bool delay : 1; }; -void QmlObjectPrivate::errorPrint() +void QmlObjectPrivate::errorPrint(QQmlComponent *component) { QString errorStr = "Error loading QML file.\n"; if(component->isError()){ @@ -181,7 +181,7 @@ void QmlObject::completeInitialization() return; } if (d->component->status() != QQmlComponent::Ready || d->component->isError()) { - d->errorPrint(); + d->errorPrint(d->component); return; } @@ -195,7 +195,7 @@ void QmlObject::completeInitialization() //d->root = d->component->create(); if (!d->root) { - d->errorPrint(); + d->errorPrint(d->component); } #ifndef NDEBUG @@ -205,6 +205,29 @@ void QmlObject::completeInitialization() emit finished(); } +QObject *QmlObject::createObjectFromSource(const QUrl &source) +{ + QQmlComponent *component = new QQmlComponent(d->engine, this); + component->loadUrl(source); + component->create(d->incubator, d->engine->rootContext()); + while (!d->incubator.isReady() && d->incubator.status() != QQmlIncubator::Error) { + QCoreApplication::processEvents(QEventLoop::AllEvents, 50); + } + QObject *object = d->incubator.object(); + + if (!component->isError() && d->root && object) { + //memory management + component->setParent(object); + object->setProperty("parent", QVariant::fromValue(d->root.data())); + return object; + + } else { + d->errorPrint(component); + delete component; + delete object; + return 0; + } +} #include "moc_qmlobject.cpp" diff --git a/src/scriptengines/qml/declarative/qmlobject.h b/src/scriptengines/qml/declarative/qmlobject.h index 35662a219..7c4d1e7f5 100644 --- a/src/scriptengines/qml/declarative/qmlobject.h +++ b/src/scriptengines/qml/declarative/qmlobject.h @@ -158,6 +158,14 @@ public: */ QQmlComponent *mainComponent() const; + /** + * Creates and returns an object based on the provided url to a Qml file + * with the same QQmlEngine and the same root context as the amin object, + * that will be the parent of the newly created object + * @param source url where the QML file is located + */ + QObject *createObjectFromSource(const QUrl &source); + public Q_SLOTS: /** * Finishes the process of initialization. diff --git a/src/scriptengines/qml/plasmoid/appletinterface.cpp b/src/scriptengines/qml/plasmoid/appletinterface.cpp index dcc713d3b..453a42476 100644 --- a/src/scriptengines/qml/plasmoid/appletinterface.cpp +++ b/src/scriptengines/qml/plasmoid/appletinterface.cpp @@ -137,20 +137,15 @@ void AppletInterface::init() pkg.setPath("org.kde.toolbox"); if (pkg.isValid()) { - QQmlComponent *toolBoxComponent = new QQmlComponent(m_qmlObject->engine(), this); - toolBoxComponent->loadUrl(QUrl::fromLocalFile(pkg.filePath("mainscript"))); - QObject *toolBoxObject = toolBoxComponent->create(engine->rootContext()); + QObject *toolBoxObject = m_qmlObject->createObjectFromSource(QUrl::fromLocalFile(pkg.filePath("mainscript"))); QObject *containmentGraphicObject = m_qmlObject->rootObject(); if (containmentGraphicObject && toolBoxObject) { - //memory management - toolBoxComponent->setParent(toolBoxObject); toolBoxObject->setProperty("parent", QVariant::fromValue(containmentGraphicObject)); containmentGraphicObject->setProperty("toolBox", QVariant::fromValue(toolBoxObject)); } else { - delete toolBoxComponent; delete toolBoxObject; } } else { @@ -515,9 +510,7 @@ void AppletInterface::geometryChanged(const QRectF &newGeometry, const QRectF &o return; } - QQmlComponent *component = new QQmlComponent(m_qmlObject->engine(), this); - component->loadUrl(QUrl::fromLocalFile(applet()->containment()->corona()->package().filePath("ui", "CompactApplet.qml"))); - m_compactUiObject = component->create(m_qmlObject->engine()->rootContext()); + m_compactUiObject = m_qmlObject->createObjectFromSource(QUrl::fromLocalFile(applet()->containment()->corona()->package().filePath("ui", "CompactApplet.qml"))); QObject *compactRepresentation = 0; @@ -525,12 +518,13 @@ void AppletInterface::geometryChanged(const QRectF &newGeometry, const QRectF &o if (m_compactUiObject) { QQmlComponent *compactComponent = m_qmlObject->rootObject()->property("compactRepresentation").value(); - if (!compactComponent) { - compactComponent = new QQmlComponent(m_qmlObject->engine(), this); - compactComponent->loadUrl(QUrl::fromLocalFile(applet()->containment()->corona()->package().filePath("ui", "DefaultCompactRepresentation.qml"))); + if (compactComponent) { + compactRepresentation = compactComponent->create(m_qmlObject->engine()->rootContext()); + } else { + compactRepresentation = m_qmlObject->createObjectFromSource(QUrl::fromLocalFile(applet()->containment()->corona()->package().filePath("ui", "DefaultCompactRepresentation.qml"))); } - compactRepresentation = compactComponent->create(m_qmlObject->engine()->rootContext()); - if (compactRepresentation) { + + if (compactRepresentation && compactComponent) { compactComponent->setParent(compactRepresentation); } else { delete compactComponent; @@ -538,9 +532,6 @@ void AppletInterface::geometryChanged(const QRectF &newGeometry, const QRectF &o } if (m_compactUiObject && compactRepresentation) { - //for memory management - component->setParent(m_compactUiObject.data()); - //put compactRepresentation in the icon place compactRepresentation->setProperty("parent", QVariant::fromValue(m_compactUiObject.data())); m_compactUiObject.data()->setProperty("compactRepresentation", QVariant::fromValue(compactRepresentation)); @@ -558,9 +549,7 @@ void AppletInterface::geometryChanged(const QRectF &newGeometry, const QRectF &o //failed to create UI, don't do anything, return in expanded status } else { - qWarning() << component->errors(); m_expanded = true; - delete component; } emit expandedChanged(); diff --git a/src/scriptengines/qml/plasmoid/containmentinterface.cpp b/src/scriptengines/qml/plasmoid/containmentinterface.cpp index 353984611..5d1d9a0c1 100644 --- a/src/scriptengines/qml/plasmoid/containmentinterface.cpp +++ b/src/scriptengines/qml/plasmoid/containmentinterface.cpp @@ -135,9 +135,7 @@ void ContainmentInterface::appletAddedForward(Plasma::Applet *applet) //if an appletGraphicObject is not set, we have to display some error message } else if (applet && contGraphicObject) { - QQmlComponent *component = new QQmlComponent(qmlObject()->engine(), applet); - component->loadUrl(QUrl::fromLocalFile(containment()->corona()->package().filePath("ui", "AppletError.qml"))); - QObject *errorUi = component->create(); + QObject *errorUi = qmlObject()->createObjectFromSource(QUrl::fromLocalFile(containment()->corona()->package().filePath("ui", "AppletError.qml"))); if (errorUi) { errorUi->setProperty("visible", false);