use the function QmlObject::createObjectFromSource

This commit is contained in:
Marco Martin 2013-02-19 20:20:32 +01:00
parent 31c442f39a
commit 222e132ecd
4 changed files with 44 additions and 26 deletions

View File

@ -50,7 +50,7 @@ public:
delete root.data(); delete root.data();
} }
void errorPrint(); void errorPrint(QQmlComponent *component);
void execute(const QUrl &source); void execute(const QUrl &source);
void scheduleExecutionEnd(); void scheduleExecutionEnd();
void minimumWidthChanged(); void minimumWidthChanged();
@ -71,7 +71,7 @@ public:
bool delay : 1; bool delay : 1;
}; };
void QmlObjectPrivate::errorPrint() void QmlObjectPrivate::errorPrint(QQmlComponent *component)
{ {
QString errorStr = "Error loading QML file.\n"; QString errorStr = "Error loading QML file.\n";
if(component->isError()){ if(component->isError()){
@ -181,7 +181,7 @@ void QmlObject::completeInitialization()
return; return;
} }
if (d->component->status() != QQmlComponent::Ready || d->component->isError()) { if (d->component->status() != QQmlComponent::Ready || d->component->isError()) {
d->errorPrint(); d->errorPrint(d->component);
return; return;
} }
@ -195,7 +195,7 @@ void QmlObject::completeInitialization()
//d->root = d->component->create(); //d->root = d->component->create();
if (!d->root) { if (!d->root) {
d->errorPrint(); d->errorPrint(d->component);
} }
#ifndef NDEBUG #ifndef NDEBUG
@ -205,6 +205,29 @@ void QmlObject::completeInitialization()
emit finished(); 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" #include "moc_qmlobject.cpp"

View File

@ -158,6 +158,14 @@ public:
*/ */
QQmlComponent *mainComponent() const; 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: public Q_SLOTS:
/** /**
* Finishes the process of initialization. * Finishes the process of initialization.

View File

@ -137,20 +137,15 @@ void AppletInterface::init()
pkg.setPath("org.kde.toolbox"); pkg.setPath("org.kde.toolbox");
if (pkg.isValid()) { if (pkg.isValid()) {
QQmlComponent *toolBoxComponent = new QQmlComponent(m_qmlObject->engine(), this); QObject *toolBoxObject = m_qmlObject->createObjectFromSource(QUrl::fromLocalFile(pkg.filePath("mainscript")));
toolBoxComponent->loadUrl(QUrl::fromLocalFile(pkg.filePath("mainscript")));
QObject *toolBoxObject = toolBoxComponent->create(engine->rootContext());
QObject *containmentGraphicObject = m_qmlObject->rootObject(); QObject *containmentGraphicObject = m_qmlObject->rootObject();
if (containmentGraphicObject && toolBoxObject) { if (containmentGraphicObject && toolBoxObject) {
//memory management
toolBoxComponent->setParent(toolBoxObject);
toolBoxObject->setProperty("parent", QVariant::fromValue(containmentGraphicObject)); toolBoxObject->setProperty("parent", QVariant::fromValue(containmentGraphicObject));
containmentGraphicObject->setProperty("toolBox", QVariant::fromValue(toolBoxObject)); containmentGraphicObject->setProperty("toolBox", QVariant::fromValue(toolBoxObject));
} else { } else {
delete toolBoxComponent;
delete toolBoxObject; delete toolBoxObject;
} }
} else { } else {
@ -515,9 +510,7 @@ void AppletInterface::geometryChanged(const QRectF &newGeometry, const QRectF &o
return; return;
} }
QQmlComponent *component = new QQmlComponent(m_qmlObject->engine(), this); m_compactUiObject = m_qmlObject->createObjectFromSource(QUrl::fromLocalFile(applet()->containment()->corona()->package().filePath("ui", "CompactApplet.qml")));
component->loadUrl(QUrl::fromLocalFile(applet()->containment()->corona()->package().filePath("ui", "CompactApplet.qml")));
m_compactUiObject = component->create(m_qmlObject->engine()->rootContext());
QObject *compactRepresentation = 0; QObject *compactRepresentation = 0;
@ -525,12 +518,13 @@ void AppletInterface::geometryChanged(const QRectF &newGeometry, const QRectF &o
if (m_compactUiObject) { if (m_compactUiObject) {
QQmlComponent *compactComponent = m_qmlObject->rootObject()->property("compactRepresentation").value<QQmlComponent *>(); QQmlComponent *compactComponent = m_qmlObject->rootObject()->property("compactRepresentation").value<QQmlComponent *>();
if (!compactComponent) { if (compactComponent) {
compactComponent = new QQmlComponent(m_qmlObject->engine(), this);
compactComponent->loadUrl(QUrl::fromLocalFile(applet()->containment()->corona()->package().filePath("ui", "DefaultCompactRepresentation.qml")));
}
compactRepresentation = compactComponent->create(m_qmlObject->engine()->rootContext()); compactRepresentation = compactComponent->create(m_qmlObject->engine()->rootContext());
if (compactRepresentation) { } else {
compactRepresentation = m_qmlObject->createObjectFromSource(QUrl::fromLocalFile(applet()->containment()->corona()->package().filePath("ui", "DefaultCompactRepresentation.qml")));
}
if (compactRepresentation && compactComponent) {
compactComponent->setParent(compactRepresentation); compactComponent->setParent(compactRepresentation);
} else { } else {
delete compactComponent; delete compactComponent;
@ -538,9 +532,6 @@ void AppletInterface::geometryChanged(const QRectF &newGeometry, const QRectF &o
} }
if (m_compactUiObject && compactRepresentation) { if (m_compactUiObject && compactRepresentation) {
//for memory management
component->setParent(m_compactUiObject.data());
//put compactRepresentation in the icon place //put compactRepresentation in the icon place
compactRepresentation->setProperty("parent", QVariant::fromValue(m_compactUiObject.data())); compactRepresentation->setProperty("parent", QVariant::fromValue(m_compactUiObject.data()));
m_compactUiObject.data()->setProperty("compactRepresentation", QVariant::fromValue(compactRepresentation)); 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 //failed to create UI, don't do anything, return in expanded status
} else { } else {
qWarning() << component->errors();
m_expanded = true; m_expanded = true;
delete component;
} }
emit expandedChanged(); emit expandedChanged();

View File

@ -135,9 +135,7 @@ void ContainmentInterface::appletAddedForward(Plasma::Applet *applet)
//if an appletGraphicObject is not set, we have to display some error message //if an appletGraphicObject is not set, we have to display some error message
} else if (applet && contGraphicObject) { } else if (applet && contGraphicObject) {
QQmlComponent *component = new QQmlComponent(qmlObject()->engine(), applet); QObject *errorUi = qmlObject()->createObjectFromSource(QUrl::fromLocalFile(containment()->corona()->package().filePath("ui", "AppletError.qml")));
component->loadUrl(QUrl::fromLocalFile(containment()->corona()->package().filePath("ui", "AppletError.qml")));
QObject *errorUi = component->create();
if (errorUi) { if (errorUi) {
errorUi->setProperty("visible", false); errorUi->setProperty("visible", false);