* Start a QML script. * @param key user command name, "qml" or "qmlview" * @param arguments arguments to pass to script * @param showOutput true to enable output in output viewer, using signal * commandOutput() * @param vars variables to make available in command * @return true if command is started. */
| 101 | * @return true if command is started. |
| 102 | */ |
| 103 | bool QmlCommandPlugin::startUserCommand( |
| 104 | const QString& key, const QStringList& arguments, bool showOutput, |
| 105 | const QVariantMap& vars) |
| 106 | { |
| 107 | if (!arguments.isEmpty()) { |
| 108 | if (key == QLatin1String("qmlview")) { |
| 109 | m_showOutput = showOutput; |
| 110 | if (!m_qmlView) { |
| 111 | m_qmlView = new QQuickView; |
| 112 | m_qmlView->setResizeMode(QQuickView::SizeRootObjectToView); |
| 113 | setupQmlEngine(m_qmlView->engine()); |
| 114 | // New style functor based connection is not possible because |
| 115 | // QQuickCloseEvent is not public (QTBUG-36453, QTBUG-55722). |
| 116 | connect(m_qmlView, SIGNAL(closing(QQuickCloseEvent*)), // clazy:exclude=old-style-connect |
| 117 | this, SLOT(onQmlViewClosing())); |
| 118 | connect(m_qmlView->engine(), &QQmlEngine::quit, |
| 119 | this, &QmlCommandPlugin::onQmlViewFinished, Qt::QueuedConnection); |
| 120 | } |
| 121 | m_qmlView->engine()->rootContext()->setContextProperty( |
| 122 | QLatin1String("args"), arguments); |
| 123 | m_qmlView->engine()->rootContext()->setContextProperty( |
| 124 | QLatin1String("vars"), vars); |
| 125 | onEngineReady(); |
| 126 | m_qmlView->setSource(QUrl::fromLocalFile(arguments.first())); |
| 127 | if (m_qmlView->status() == QQuickView::Ready) { |
| 128 | m_qmlView->show(); |
| 129 | } else { |
| 130 | // Probably an error. |
| 131 | if (m_showOutput && m_qmlView->status() == QQuickView::Error) { |
| 132 | const auto errs = m_qmlView->errors(); |
| 133 | for (const QQmlError& err : errs) { |
| 134 | emit commandOutput(err.toString()); |
| 135 | } |
| 136 | } |
| 137 | m_qmlView->engine()->clearComponentCache(); |
| 138 | onEngineFinished(); |
| 139 | } |
| 140 | return true; |
| 141 | } |
| 142 | if (key == QLatin1String("qml")) { |
| 143 | m_showOutput = showOutput; |
| 144 | if (!m_qmlEngine) { |
| 145 | m_qmlEngine = new QQmlEngine; |
| 146 | connect(m_qmlEngine, &QQmlEngine::quit, this, &QmlCommandPlugin::onQmlEngineQuit); |
| 147 | setupQmlEngine(m_qmlEngine); |
| 148 | } |
| 149 | m_qmlEngine->rootContext()->setContextProperty(QLatin1String("args"), |
| 150 | arguments); |
| 151 | m_qmlEngine->rootContext()->setContextProperty(QLatin1String("vars"), |
| 152 | vars); |
| 153 | if (QQmlComponent component(m_qmlEngine, arguments.first()); |
| 154 | component.status() == QQmlComponent::Ready) { |
| 155 | onEngineReady(); |
| 156 | component.create(); |
| 157 | } else { |
| 158 | // Probably an error. |
| 159 | if (m_showOutput && component.isError()) { |
| 160 | const auto errs = component.errors(); |