Skip to content

Commit

Permalink
App: handle DialogExecScript closing properly
Browse files Browse the repository at this point in the history
  • Loading branch information
HuguesDelorme committed Jun 28, 2024
1 parent 073bea6 commit 661fb1a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/app/commands_tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ void CommandExecScript::execute()
auto strFilePath = QFileDialog::getOpenFileName(
this->widgetMain(),
Command::tr("Choose JavaScript file"),
QString(/*dir*/),
QString{}/*dir*/,
Command::tr("Script files(*.js)")
);
if (!strFilePath.isEmpty())
Expand Down
59 changes: 38 additions & 21 deletions src/app/dialog_exec_script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
#include "qtwidgets_utils.h"
#include "../qtcommon/filepath_conv.h"
#include "../qtcommon/log_message_handler.h"
#include "../qtcommon/qtcore_utils.h"
#include "../qtscripting/script_global.h"
#include "ui_dialog_exec_script.h"

#include <QtCore/QDir>
#include <QtCore/QFile>
#include <QtCore/QFileinfo>
#include <QtCore/QTimer>
#include <QtCore/QFileInfo>
#include <QtQml/QJSEngine>
#include <gsl/util>

Expand Down Expand Up @@ -43,10 +43,12 @@ DialogExecScript::DialogExecScript(QWidget* parent)
m_taskMgr.signalStarted.connectSlot(&DialogExecScript::onTaskStarted, this);
m_taskMgr.signalEnded.connectSlot(&DialogExecScript::onTaskEnded, this);
QObject::connect(
m_ui->btn_restartStop, &QAbstractButton::clicked, this, &DialogExecScript::restartOrStopScriptExec
m_ui->btn_restartStop, &QAbstractButton::clicked,
this, &DialogExecScript::restartOrStopScriptExec
);
QObject::connect(
m_ui->buttonBox->button(QDialogButtonBox::Close), &QAbstractButton::clicked, this, &QDialog::accept
m_ui->buttonBox->button(QDialogButtonBox::Close), &QAbstractButton::clicked,
this, &DialogExecScript::tryCloseDialog
);
}

Expand Down Expand Up @@ -79,7 +81,7 @@ void DialogExecScript::startScript()
});
auto fnAddConsoleOutput = [=](QtMsgType type, const QString& text, const QString& file, int line) {
const Message msg = { type, text, file, line };
QTimer::singleShot(0, this, [=]{ this->addConsoleOutput(msg); });
QtCoreUtils::runJobOnMainThread([=]{ this->addConsoleOutput(msg); });
};
logMsgHandler.setJsConsoleOutputHandler(
[=](QtMsgType type, const QMessageLogContext& context, const QString& text) {
Expand All @@ -105,16 +107,6 @@ void DialogExecScript::startScript()
m_taskMgr.run(m_scriptExecTaskId);
}

void DialogExecScript::done(int resultCode)
{
if (m_scriptExecIsRunning) {
// TODO Ask if script execution should be stopped
}
else {
QDialog::done(resultCode);
}
}

void DialogExecScript::onTaskStarted(TaskId taskId)
{
if (m_scriptExecTaskId != taskId)
Expand Down Expand Up @@ -149,15 +141,18 @@ void DialogExecScript::onTaskEnded(TaskId taskId)
m_ui->treeWidget_Output->resizeColumnToContents(col);
}

void DialogExecScript::interruptScriptExec()
{
m_wasScriptExecInterrupted = true;
m_jsEngine->setInterrupted(true);
}

void DialogExecScript::restartOrStopScriptExec()
{
if (m_scriptExecIsRunning) {
m_wasScriptExecInterrupted = true;
m_jsEngine->setInterrupted(true);
}
else {
if (m_scriptExecIsRunning)
this->interruptScriptExec();
else
this->startScript();
}
}

void DialogExecScript::recreateScriptEngine()
Expand Down Expand Up @@ -187,4 +182,26 @@ void DialogExecScript::addConsoleOutput(const Message& msg)
m_ui->treeWidget_Output->addTopLevelItem(item);
}

void DialogExecScript::tryCloseDialog()
{
if (m_scriptExecIsRunning) {
auto msgBox = QtWidgetsUtils::asyncMsgBoxWarning(
this,
tr("Warning"),
tr("Script execution isn't finished\n\nInterrupt and exit?"),
QMessageBox::Yes | QMessageBox::No
);
QObject::connect(msgBox, &QMessageBox::buttonClicked, this, [=](QAbstractButton* btn) {
if (btn == msgBox->button(QMessageBox::Yes)) {
this->interruptScriptExec();
m_taskMgr.waitForDone(m_scriptExecTaskId, 10000/*ms*/);
QtCoreUtils::runJobOnMainThread([=]{ this->tryCloseDialog(); });
}
});
}
else {
this->reject();
}
}

} // namespace Mayo
5 changes: 3 additions & 2 deletions src/app/dialog_exec_script.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,11 @@ class DialogExecScript : public QDialog {

void startScript();

void done(int resultCode) override;

private:
void onTaskStarted(TaskId taskId);
void onTaskEnded(TaskId taskId);

void interruptScriptExec();
void restartOrStopScriptExec();
void recreateScriptEngine();

Expand All @@ -46,6 +45,8 @@ class DialogExecScript : public QDialog {

void addConsoleOutput(const Message& msg);

void tryCloseDialog();

class Ui_DialogExecScript* m_ui = nullptr;
ScriptEngineCreator m_fnScriptEngineCreator;
QJSEngine* m_jsEngine = nullptr;
Expand Down
3 changes: 0 additions & 3 deletions src/app/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "../base/global.h"
#include "../gui/gui_application.h"
#include "../gui/gui_document.h"
#include "../qtscripting/script_global.h"
#include "app_context.h"
#include "app_module.h"
#include "commands_file.h"
Expand Down Expand Up @@ -99,8 +98,6 @@ void MainWindow::addPage(IAppContext::Page page, IWidgetMainPage* pageWidget)

void MainWindow::createCommands()
{
auto jsEngine = createScriptEngine(m_guiApp->application(), this);

// "File" commands
this->addCommand<CommandNewDocument>();
this->addCommand<CommandOpenDocuments>();
Expand Down

0 comments on commit 661fb1a

Please sign in to comment.