File indexing completed on 2024-05-19 04:56:32
0001 /** 0002 * \file qmlcommandplugin.h 0003 * Starter for QML scripts. 0004 * 0005 * \b Project: Kid3 0006 * \author Urs Fleisch 0007 * \date 15 Feb 2015 0008 * 0009 * Copyright (C) 2015-2024 Urs Fleisch 0010 * 0011 * This file is part of Kid3. 0012 * 0013 * Kid3 is free software; you can redistribute it and/or modify 0014 * it under the terms of the GNU General Public License as published by 0015 * the Free Software Foundation; either version 2 of the License, or 0016 * (at your option) any later version. 0017 * 0018 * Kid3 is distributed in the hope that it will be useful, 0019 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0020 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0021 * GNU General Public License for more details. 0022 * 0023 * You should have received a copy of the GNU General Public License 0024 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0025 */ 0026 0027 #pragma once 0028 0029 #include <QObject> 0030 #include <QQmlError> 0031 #include "iusercommandprocessor.h" 0032 0033 class Kid3Application; 0034 class QQuickView; 0035 class QQmlEngine; 0036 0037 /** 0038 * Starter for QML scripts. 0039 */ 0040 class KID3_PLUGIN_EXPORT QmlCommandPlugin 0041 : public QObject, public IUserCommandProcessor { 0042 Q_OBJECT 0043 Q_PLUGIN_METADATA(IID "org.kde.kid3.IUserCommandProcessor") 0044 Q_INTERFACES(IUserCommandProcessor) 0045 public: 0046 /** 0047 * Constructor. 0048 * 0049 * @param parent parent object 0050 */ 0051 explicit QmlCommandPlugin(QObject* parent = nullptr); 0052 0053 /** 0054 * Destructor. 0055 */ 0056 ~QmlCommandPlugin() override = default; 0057 0058 /** 0059 * Get keys of available user commands. 0060 * @return list of keys, ["qml", "qmlview"]. 0061 */ 0062 QStringList userCommandKeys() const override; 0063 0064 /** 0065 * Initialize processor. 0066 * This method must be invoked before the first call to startUserCommand() 0067 * to set the application context. 0068 * @param app application context 0069 */ 0070 void initialize(Kid3Application* app) override; 0071 0072 /** 0073 * Cleanup processor. 0074 * This method must be invoked to close and delete the QML resources. 0075 */ 0076 void cleanup() override; 0077 0078 /** 0079 * Start a QML script. 0080 * @param key user command name, "qml" or "qmlview" 0081 * @param arguments arguments to pass to script 0082 * @param showOutput true to enable output in output viewer, using signal 0083 * commandOutput(). 0084 * @return true if command is started. 0085 */ 0086 bool startUserCommand( 0087 const QString& key, const QStringList& arguments, bool showOutput) override; 0088 0089 /** 0090 * Return object which emits commandOutput() signal. 0091 * @return this. 0092 */ 0093 QObject* qobject() override; 0094 0095 signals: 0096 /** 0097 * Emitted when output is enabled and a QML message is generated. 0098 * @param msg message from QML, error or console output 0099 */ 0100 void commandOutput(const QString& msg); 0101 0102 /** 0103 * Emitted when the command finishes. 0104 * @param exitCode exit code of command 0105 */ 0106 void finished(int exitCode); 0107 0108 private slots: 0109 void onEngineError(const QList<QQmlError>& errors); 0110 void onQmlViewClosing(); 0111 void onQmlViewFinished(); 0112 void onQmlEngineQuit(); 0113 void onEngineFinished(); 0114 0115 private: 0116 void setupQmlEngine(QQmlEngine* engine); 0117 void onEngineReady(); 0118 0119 static void messageHandler(QtMsgType type, const QMessageLogContext& context, 0120 const QString& msg); 0121 0122 Kid3Application* m_app; 0123 QQuickView* m_qmlView; 0124 QQmlEngine* m_qmlEngine; 0125 bool m_showOutput; 0126 0127 static QmlCommandPlugin* s_messageHandlerInstance; 0128 };