From 66f0c05a485fe4622633bc7e336c0ccae24f9fd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Du=C5=A1an=20Poizl?= Date: Sat, 8 Jun 2024 20:11:25 +0200 Subject: [PATCH] Fix calling GUI methods from script thread --- batchprocessing.cpp | 29 +++++++++++++++++++++++++++++ batchprocessing.h | 7 ++++++- loadrunable.cpp | 8 +++++--- scriptengine.cpp | 29 +++++++++++++++-------------- scriptengine.h | 8 +++++--- 5 files changed, 60 insertions(+), 21 deletions(-) diff --git a/batchprocessing.cpp b/batchprocessing.cpp index d736351..cd043b9 100644 --- a/batchprocessing.cpp +++ b/batchprocessing.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #endif @@ -235,3 +236,31 @@ void BatchProcessing::newMessage(const QString &message, bool error) else _ui->log->setTextColor(_textColor); _ui->log->append(message); } + +QJSValue BatchProcessing::getString(const QString &label, const QString &text) +{ + bool ok = false; + QString ret = QInputDialog::getText(this, tr("Enter text"), label, QLineEdit::Normal, text, &ok); + return ok ? ret : QJSValue(); +} + +QJSValue BatchProcessing::getInt(const QString &label, int value) +{ + bool ok = false; + int ret = QInputDialog::getInt(this, tr("Enter integer number"), label, value, INT_MIN, INT_MAX, 1, &ok); + return ok ? ret : QJSValue(); +} + +QJSValue BatchProcessing::getFloat(const QString &label, double value, int decimals) +{ + bool ok = false; + double ret = QInputDialog::getDouble(this, tr("Enter float number"), label, value, -INFINITY, INFINITY, decimals, &ok); + return ok ? ret : QJSValue(); +} + +QJSValue BatchProcessing::getItem(const QStringList &items, const QString &label, int current) +{ + bool ok = false; + QString ret = QInputDialog::getItem(this, tr("Select item"), label, items, current, false, &ok); + return ok ? ret : QJSValue(); +} diff --git a/batchprocessing.h b/batchprocessing.h index 3e938d7..55e5227 100644 --- a/batchprocessing.h +++ b/batchprocessing.h @@ -9,6 +9,7 @@ namespace Ui { class BatchProcessing; } class BatchProcessing : public QDialog { + Q_OBJECT Ui::BatchProcessing *_ui; QString _scriptBasePath; QFileSystemWatcher _fileWatcher; @@ -22,7 +23,6 @@ public: protected: void closeEvent(QCloseEvent *event); public slots: - void scriptDirChanged(); void addFiles(); void addDir(); void removePath(); @@ -33,6 +33,11 @@ public slots: void stopScript(); void scriptFinished(); void newMessage(const QString &message, bool error); + + QJSValue getString(const QString &label, const QString &text); + QJSValue getInt(const QString &label, int value); + QJSValue getFloat(const QString &label, double value, int decimals); + QJSValue getItem(const QStringList &items, const QString &label, int current); }; #endif // BATCHPROCESSING_H diff --git a/loadrunable.cpp b/loadrunable.cpp index 91591cd..8b17119 100644 --- a/loadrunable.cpp +++ b/loadrunable.cpp @@ -615,7 +615,7 @@ void ConvertRunable::run() if(rawimage) { - if(m_format == "XISF") + if(m_format == "xisf") { try { @@ -674,9 +674,10 @@ void ConvertRunable::run() { qDebug() << "Failed to save XISF image" << err.what(); } + return; } - if(m_format == "FITS") + if(m_format == "fits") { int status = 0; fitsfile *fw; @@ -691,9 +692,10 @@ void ConvertRunable::run() } writeFITSImage(fw, rawimage, imageinfo); fits_close_file(fw, &status); + return; } - if(m_format == "QIMAGE") + // if nothing else try QImage { QImage::Format format = QImage::Format_Invalid; int width = rawimage->widthBytes(); diff --git a/scriptengine.cpp b/scriptengine.cpp index 54defa0..3d8f8ac 100644 --- a/scriptengine.cpp +++ b/scriptengine.cpp @@ -6,13 +6,14 @@ #include "loadrunable.h" #include "rawimage.h" #include "loadrunable.h" +#include "batchprocessing.h" #include #include "libXISF/libxisf.h" namespace Script { -ScriptEngine::ScriptEngine(QDialog *parent) +ScriptEngine::ScriptEngine(BatchProcessing *parent) : _jsEngine(new QJSEngine(this)) , _database(new Database(this)) , _parent(parent) @@ -91,30 +92,30 @@ void ScriptEngine::sync() QJSValue ScriptEngine::getString(const QString &label, const QString &text) const { - bool ok = false; - QString ret = QInputDialog::getText(_parent, tr("Enter text"), label, QLineEdit::Normal, text, &ok); - return ok ? ret : QJSValue(); + QJSValue ret; + QMetaObject::invokeMethod(_parent, "getString", Qt::BlockingQueuedConnection, Q_RETURN_ARG(QJSValue, ret), Q_ARG(QString, label), Q_ARG(QString, text)); + return ret; } QJSValue ScriptEngine::getInt(const QString &label, int value) { - bool ok = false; - int ret = QInputDialog::getInt(_parent, tr("Enter integer number"), label, value, INT_MIN, INT_MAX, 1, &ok); - return ok ? ret : QJSValue(); + QJSValue ret; + QMetaObject::invokeMethod(_parent, "getInt", Qt::BlockingQueuedConnection, Q_RETURN_ARG(QJSValue, ret), Q_ARG(QString, label), Q_ARG(int, value)); + return ret; } QJSValue ScriptEngine::getFloat(const QString &label, double value, int decimals) const { - bool ok = false; - double ret = QInputDialog::getDouble(_parent, tr("Enter float number"), label, value, -INFINITY, INFINITY, decimals, &ok); - return ok ? ret : QJSValue(); + QJSValue ret; + QMetaObject::invokeMethod(_parent, "getFloat", Qt::BlockingQueuedConnection, Q_RETURN_ARG(QJSValue, ret), Q_ARG(QString, label), Q_ARG(double, value), Q_ARG(int, decimals)); + return ret; } QJSValue ScriptEngine::getItem(const QStringList &items, const QString &label, int current) const { - bool ok = false; - QString ret = QInputDialog::getItem(_parent, tr("Select item"), label, items, current, false, &ok); - return ok ? ret : QJSValue(); + QJSValue ret; + QMetaObject::invokeMethod(_parent, "getItem", Qt::BlockingQueuedConnection, Q_RETURN_ARG(QJSValue, ret), Q_ARG(QStringList, items), Q_ARG(QString, label), Q_ARG(int, current)); + return ret; } bool ScriptEngine::convert(File *file, QString &outpath, const QString &format, const QVariantMap ¶ms, bool async) @@ -565,7 +566,7 @@ QJSValue File::stats() return _stats; } -ScriptEngineThread::ScriptEngineThread(QDialog *parent) : QObject(parent) +ScriptEngineThread::ScriptEngineThread(BatchProcessing *parent) : QObject(parent) { _thread = new QThread(); _thread->setObjectName("ScriptEngine"); diff --git a/scriptengine.h b/scriptengine.h index 9c53108..8c1b497 100644 --- a/scriptengine.h +++ b/scriptengine.h @@ -10,6 +10,8 @@ #include "database.h" #include "imageinfo.h" +class BatchProcessing; + namespace Script { @@ -20,14 +22,14 @@ class ScriptEngine : public QObject Q_OBJECT QJSEngine *_jsEngine; Database *_database; - QDialog *_parent; + BatchProcessing *_parent; QThreadPool *_pool; QSemaphore _semaphore; QString _scriptPath; QString _outputDir; QList> _paths; public: - explicit ScriptEngine(QDialog *parent = nullptr); + explicit ScriptEngine(BatchProcessing *parent = nullptr); void setParams(const QString &scriptPath, const QList> &paths, const QString &outputDir); void reportError(const QString &message); const QString& outputDir() const; @@ -59,7 +61,7 @@ class ScriptEngineThread : public QObject QThread *_thread; ScriptEngine *_engine; public: - ScriptEngineThread(QDialog *parent = nullptr); + ScriptEngineThread(BatchProcessing *parent = nullptr); ~ScriptEngineThread(); void setParams(const QString &scriptPath, const QList> &paths, const QString &outputDir); void start();