Commit 3165dc6c authored by Urs Fleisch's avatar Urs Fleisch
Browse files

QML: Terminate QML scripts when error occurs.

parent 26d85fae
......@@ -55,25 +55,6 @@
Q_EXPORT_PLUGIN2(QmlCommandPlugin, QmlCommandPlugin)
#endif
namespace {
/**
* Set import path and app property in QML engine.
* @param engine QML engine
* @param app application context
*/
void setupQmlEngine(QQmlEngine* engine, Kid3Application* app)
{
QDir pluginsDir;
if (Kid3Application::findPluginsDirectory(pluginsDir) &&
pluginsDir.cd(QLatin1String("imports"))) {
engine->addImportPath(pluginsDir.absolutePath());
}
engine->rootContext()->setContextProperty(QLatin1String("app"), app);
}
}
/**
* Constructor.
*
......@@ -147,7 +128,7 @@ bool QmlCommandPlugin::startUserCommand(
if (!m_qmlView) {
m_qmlView = new QQuickView;
m_qmlView->setResizeMode(QQuickView::SizeRootObjectToView);
setupQmlEngine(m_qmlView->engine(), m_app);
setupQmlEngine(m_qmlView->engine());
connect(m_qmlView, SIGNAL(closing(QQuickCloseEvent*)),
this, SLOT(onQmlViewClosing()));
connect(m_qmlView->engine(), SIGNAL(quit()),
......@@ -175,7 +156,7 @@ bool QmlCommandPlugin::startUserCommand(
if (!m_qmlEngine) {
m_qmlEngine = new QQmlEngine;
connect(m_qmlEngine, SIGNAL(quit()), this, SLOT(onQmlEngineQuit()));
setupQmlEngine(m_qmlEngine, m_app);
setupQmlEngine(m_qmlEngine);
}
m_qmlEngine->rootContext()->setContextProperty(QLatin1String("args"),
arguments);
......@@ -198,6 +179,29 @@ bool QmlCommandPlugin::startUserCommand(
return false;
}
/**
* Set import path and app property in QML engine.
* @param engine QML engine
*/
void QmlCommandPlugin::setupQmlEngine(QQmlEngine* engine)
{
QDir pluginsDir;
if (Kid3Application::findPluginsDirectory(pluginsDir) &&
pluginsDir.cd(QLatin1String("imports"))) {
engine->addImportPath(pluginsDir.absolutePath());
}
engine->rootContext()->setContextProperty(QLatin1String("app"), m_app);
#if QT_VERSION >= 0x050000
connect(engine, SIGNAL(warnings(QList<QQmlError>)),
this, SLOT(onEngineError(QList<QQmlError>)),
Qt::UniqueConnection);
#else
connect(engine, SIGNAL(warnings(QList<QDeclarativeError>)),
this, SLOT(onEngineError(QList<QDeclarativeError>)),
Qt::UniqueConnection);
#endif
}
/**
* Return object which emits commandOutput() signal.
* @return this.
......@@ -207,6 +211,20 @@ QObject* QmlCommandPlugin::qobject()
return this;
}
/**
* Called when an error is reported by the QML engine.
*/
void QmlCommandPlugin::onEngineError(const QList<QQmlError>& errors)
{
if (QQmlEngine* engine = qobject_cast<QQmlEngine*>(sender())) {
foreach (const QQmlError& err, errors) {
emit commandOutput(err.toString());
}
engine->clearComponentCache();
onEngineFinished();
}
}
/**
* Called when the QML view is closing.
*/
......
......@@ -37,6 +37,7 @@ class Kid3Application;
#if QT_VERSION >= 0x050000
class QQuickView;
class QQmlEngine;
class QQmlError;
#else
class QDeclarativeEngine;
class QQuickCloseEvent;
......@@ -110,12 +111,22 @@ signals:
void commandOutput(const QString& msg);
private slots:
#if QT_VERSION >= 0x050000
void onEngineError(const QList<QQmlError>& errors);
#else
void onEngineError(const QList<QDeclarativeError>& errors);
#endif
void onQmlViewClosing();
void onQmlViewFinished();
void onQmlEngineQuit();
void onEngineFinished();
private:
#if QT_VERSION >= 0x050000
void setupQmlEngine(QQmlEngine* engine);
#else
void setupQmlEngine(QDeclarativeEngine* engine);
#endif
void onEngineReady();
#if QT_VERSION >= 0x050000
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment