From 3257c1496442b174be814438186ad5370d38cdf2 Mon Sep 17 00:00:00 2001 From: Nicolas Arnaud-Cormos Date: Wed, 20 Nov 2024 11:11:42 +0100 Subject: [PATCH] refactor: Change back the run dialog widget to a dialog The widget is not needed anymore now that we have interactive scripts. Note: need update of the photon scripts (later). Revert "Move RunScript widget as part of the main window" This reverts commit b2793abbedd25a1f26b1e46dfbbb089e2aa7614a. New features: - use a combobox to list the scripts (easier to find) - save/restore latest run script (useful particularly with IDE integration) --- src/gui/CMakeLists.txt | 6 +- src/gui/knutmain.cpp | 4 +- src/gui/mainwindow.cpp | 22 ++--- src/gui/mainwindow.h | 3 + src/gui/mainwindow.ui | 9 -- src/gui/runscriptdialog.cpp | 91 +++++++++++++++++ .../{runscriptwidget.h => runscriptdialog.h} | 15 +-- src/gui/runscriptdialog.ui | 87 +++++++++++++++++ src/gui/runscriptwidget.cpp | 88 ----------------- src/gui/runscriptwidget.ui | 97 ------------------- 10 files changed, 204 insertions(+), 218 deletions(-) create mode 100644 src/gui/runscriptdialog.cpp rename src/gui/{runscriptwidget.h => runscriptdialog.h} (63%) create mode 100644 src/gui/runscriptdialog.ui delete mode 100644 src/gui/runscriptwidget.cpp delete mode 100644 src/gui/runscriptwidget.ui diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index c08104ae..0a0bce9d 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -64,9 +64,9 @@ set(PROJECT_SOURCES rctouidialog.h rctouidialog.cpp rctouidialog.ui - runscriptwidget.h - runscriptwidget.cpp - runscriptwidget.ui + runscriptdialog.h + runscriptdialog.cpp + runscriptdialog.ui scriptpanel.h scriptpanel.cpp scriptlistpanel.cpp diff --git a/src/gui/knutmain.cpp b/src/gui/knutmain.cpp index c9824654..826b6c99 100644 --- a/src/gui/knutmain.cpp +++ b/src/gui/knutmain.cpp @@ -11,7 +11,7 @@ #include "knutmain.h" #include "mainwindow.h" #include "optionsdialog.h" -#include "runscriptwidget.h" +#include "runscriptdialog.h" namespace Gui { @@ -33,7 +33,7 @@ void KnutMain::doParse(const QCommandLineParser &parser) const { const bool runDialog = parser.isSet("gui-run"); if (runDialog) { - auto dialog = new RunScriptWidget; + auto dialog = new RunScriptDialog; dialog->show(); return; } diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp index 8f46ba23..4b3898e4 100644 --- a/src/gui/mainwindow.cpp +++ b/src/gui/mainwindow.cpp @@ -40,7 +40,7 @@ #include "rctoqrcdialog.h" #include "rctouidialog.h" #include "rcui/rcfileview.h" -#include "runscriptwidget.h" +#include "runscriptdialog.h" #include "scriptlistpanel.h" #include "scriptpanel.h" #include "shortcutmanager.h" @@ -181,11 +181,9 @@ MainWindow::MainWindow(QWidget *parent) connect(ui->actionCloseDocument, &QAction::triggered, this, &MainWindow::closeDocument); // Script - ui->runScriptWidget->hide(); - ui->runScriptWidget->installEventFilter(this); ui->apiExecutorWidget->hide(); ui->apiExecutorWidget->installEventFilter(this); - connect(ui->actionRunScript, &QAction::triggered, ui->runScriptWidget, &RunScriptWidget::open); + connect(ui->actionRunScript, &QAction::triggered, this, &MainWindow::runScript); connect(ui->actionStartRecordingScript, &QAction::triggered, m_historyPanel, &HistoryPanel::startRecording); connect(ui->actionStopRecordingScript, &QAction::triggered, m_historyPanel, &HistoryPanel::stopRecording); connect(ui->actionPlayLastScript, &QAction::triggered, m_scriptPanel, &ScriptPanel::runScript); @@ -312,16 +310,10 @@ ShortcutManager *MainWindow::shortcutManager() const bool MainWindow::eventFilter(QObject *watched, QEvent *event) { if (event->type() == QEvent::Show) { - if (qobject_cast(watched)) { + if (qobject_cast(watched)) ui->apiExecutorWidget->hide(); - ui->runScriptWidget->hide(); - } else if (qobject_cast(watched)) { + else if (qobject_cast(watched)) ui->findWidget->hide(); - ui->runScriptWidget->hide(); - } else if (qobject_cast(watched)) { - ui->apiExecutorWidget->hide(); - ui->findWidget->hide(); - } } return QMainWindow::eventFilter(watched, event); } @@ -562,6 +554,12 @@ See Wikipedia article: - @@ -535,12 +532,6 @@
apiexecutorwidget.h
1 - - Gui::RunScriptWidget - QWidget -
runscriptwidget.h
- 1 -
diff --git a/src/gui/runscriptdialog.cpp b/src/gui/runscriptdialog.cpp new file mode 100644 index 00000000..15061c7f --- /dev/null +++ b/src/gui/runscriptdialog.cpp @@ -0,0 +1,91 @@ +/* + This file is part of Knut. + + SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company + + SPDX-License-Identifier: GPL-3.0-only + + Contact KDAB at for commercial licensing options. +*/ + +#include "runscriptdialog.h" +#include "core/scriptmanager.h" +#include "ui_runscriptdialog.h" + +#include +#include +#include +#include +#include +#include + +namespace Gui { + +constexpr char LastScript[] = "lastScriptRun"; + +RunScriptDialog::RunScriptDialog(QWidget *parent) + : QDialog(parent) + , ui(new Ui::RunScriptDialog) +{ + ui->setupUi(this); + ui->comboBox->lineEdit()->setPlaceholderText("Name or path of a script"); + setWindowTitle(QApplication::applicationName() + ' ' + QApplication::applicationVersion() + " - " + windowTitle()); + + connect(ui->toolButton, &QToolButton::clicked, this, &RunScriptDialog::chooseScript); + auto okButton = ui->buttonBox->button(QDialogButtonBox::Ok); + okButton->setEnabled(false); + connect(ui->comboBox, &QComboBox::currentTextChanged, this, [okButton](const QString &str) { + okButton->setEnabled(!str.trimmed().isEmpty()); + }); + + const auto &list = Core::ScriptManager::instance()->scriptList(); + QStringList scriptNames; + scriptNames.reserve(static_cast(list.size())); + for (const auto &item : list) + scriptNames.push_back(item.name); + ui->comboBox->addItems(scriptNames); + + // Load the latest script name from the settings + QSettings settings; + const auto &lastScript = settings.value(LastScript).toString(); + if (!lastScript.isEmpty()) + ui->comboBox->setCurrentText(lastScript); +} + +RunScriptDialog::~RunScriptDialog() = default; + +void RunScriptDialog::accept() +{ + if (ui->comboBox->currentText().isEmpty()) + return; + + QString scriptName = ui->comboBox->currentText(); + // Store the latest script run from the dialog + QSettings settings; + settings.setValue(LastScript, scriptName); + + QFileInfo fi(scriptName); + if (!fi.exists()) { + const auto &list = Core::ScriptManager::instance()->scriptList(); + auto result = kdalgorithms::find_if(list, [&scriptName](const auto &item) { + return item.name == scriptName; + }); + if (!result) + return; + scriptName = result->fileName; + } + + Core::ScriptManager::instance()->runScript(scriptName); + QDialog::accept(); +} + +void RunScriptDialog::chooseScript() +{ + const QString fileName = + QFileDialog::getOpenFileName(this, QString(), ui->comboBox->currentText(), tr("Scripts (*.js *.qml)")); + if (fileName.isEmpty()) + return; + ui->comboBox->setCurrentText(fileName); +} + +} // namespace Gui diff --git a/src/gui/runscriptwidget.h b/src/gui/runscriptdialog.h similarity index 63% rename from src/gui/runscriptwidget.h rename to src/gui/runscriptdialog.h index 5ca0000f..bb3ede59 100644 --- a/src/gui/runscriptwidget.h +++ b/src/gui/runscriptdialog.h @@ -11,30 +11,31 @@ #pragma once #include + #include namespace Gui { namespace Ui { - class RunScriptWidget; + class RunScriptDialog; } -class RunScriptWidget : public QWidget +class RunScriptDialog : public QDialog { Q_OBJECT public: - explicit RunScriptWidget(QWidget *parent = nullptr); - ~RunScriptWidget() override; + explicit RunScriptDialog(QWidget *parent = nullptr); + ~RunScriptDialog() override; - void open(); +public slots: + void accept() override; private: void chooseScript(); - void run(); private: - std::unique_ptr ui; + std::unique_ptr ui; }; } // namespace Gui diff --git a/src/gui/runscriptdialog.ui b/src/gui/runscriptdialog.ui new file mode 100644 index 00000000..bb0c9b9a --- /dev/null +++ b/src/gui/runscriptdialog.ui @@ -0,0 +1,87 @@ + + + Gui::RunScriptDialog + + + + 0 + 0 + 390 + 70 + + + + Run Script Dialog + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + true + + + QComboBox::NoInsert + + + Name or path of a script + + + + + + + Select Script + + + ... + + + + + + + + + buttonBox + accepted() + Gui::RunScriptDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + Gui::RunScriptDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + + diff --git a/src/gui/runscriptwidget.cpp b/src/gui/runscriptwidget.cpp deleted file mode 100644 index 89c2e0a2..00000000 --- a/src/gui/runscriptwidget.cpp +++ /dev/null @@ -1,88 +0,0 @@ -/* - This file is part of Knut. - - SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company - - SPDX-License-Identifier: GPL-3.0-only - - Contact KDAB at for commercial licensing options. -*/ - -#include "runscriptwidget.h" -#include "core/scriptmanager.h" -#include "ui_runscriptwidget.h" - -#include -#include -#include -#include -#include - -namespace Gui { - -RunScriptWidget::RunScriptWidget(QWidget *parent) - : QWidget(parent) - , ui(new Ui::RunScriptWidget) -{ - ui->setupUi(this); - setWindowTitle(QApplication::applicationName() + ' ' + QApplication::applicationVersion() + " - " + windowTitle()); - - setProperty("panelWidget", true); - setFocusProxy(ui->lineEdit); - - connect(ui->openButton, &QToolButton::clicked, this, &RunScriptWidget::chooseScript); - ui->runButton->setEnabled(false); - connect(ui->lineEdit, &QLineEdit::textChanged, this, [this](const QString &str) { - ui->runButton->setEnabled(!str.trimmed().isEmpty()); - }); - connect(ui->runButton, &QToolButton::clicked, this, &RunScriptWidget::run); - connect(ui->lineEdit, &QLineEdit::returnPressed, this, &RunScriptWidget::run); - connect(ui->closeButton, &QToolButton::clicked, this, &RunScriptWidget::close); - - const auto &list = Core::ScriptManager::instance()->scriptList(); - QStringList scriptNames; - scriptNames.reserve(static_cast(list.size())); - for (const auto &item : list) - scriptNames.push_back(item.name); - auto completer = new QCompleter(scriptNames, this); - ui->lineEdit->setCompleter(completer); -} - -RunScriptWidget::~RunScriptWidget() = default; - -void RunScriptWidget::run() -{ - if (ui->lineEdit->text().isEmpty()) - return; - - QString scriptName = ui->lineEdit->text(); - QFileInfo fi(scriptName); - if (!fi.exists()) { - const auto &list = Core::ScriptManager::instance()->scriptList(); - auto result = kdalgorithms::find_if(list, [&scriptName](const auto &item) { - return item.name == scriptName; - }); - if (!result) - return; - scriptName = result->fileName; - } - - Core::ScriptManager::instance()->runScript(scriptName); -} - -void RunScriptWidget::open() -{ - show(); - ui->lineEdit->setFocus(); -} - -void RunScriptWidget::chooseScript() -{ - const QString fileName = - QFileDialog::getOpenFileName(this, QString(), ui->lineEdit->text(), tr("Scripts (*.js *.qml)")); - if (fileName.isEmpty()) - return; - ui->lineEdit->setText(fileName); -} - -} // namespace Gui diff --git a/src/gui/runscriptwidget.ui b/src/gui/runscriptwidget.ui deleted file mode 100644 index f89defe2..00000000 --- a/src/gui/runscriptwidget.ui +++ /dev/null @@ -1,97 +0,0 @@ - - - Gui::RunScriptWidget - - - - 0 - 0 - 1266 - 44 - - - - Run Script Dialog - - - - 6 - - - - - Select Script - - - Open - - - Qt::ToolButtonTextOnly - - - true - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - Name or path of a script - - - true - - - - - - - &Close - - - Qt::ToolButtonTextOnly - - - true - - - - - - - Script: - - - - - - - &Run - - - Qt::ToolButtonTextOnly - - - true - - - - - - - - -