From 71d6ad87630c26accf2300293082217cb921cc15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benedek=20L=C3=A1szl=C3=B3?= Date: Wed, 15 May 2024 12:32:41 +0200 Subject: [PATCH] persistent dialogs --- inc/graph/server/gui/mainwindow.h | 5 ++ inc/graph/server/gui/specdialog/specdialog.h | 10 +++- src/graph/server/gui/mainwindow.cpp | 48 +++++++++++-------- .../server/gui/specdialog/specdialog.cpp | 14 ++++++ 4 files changed, 55 insertions(+), 22 deletions(-) diff --git a/inc/graph/server/gui/mainwindow.h b/inc/graph/server/gui/mainwindow.h index b85b88c..1272037 100644 --- a/inc/graph/server/gui/mainwindow.h +++ b/inc/graph/server/gui/mainwindow.h @@ -2,10 +2,12 @@ #include #include +#include #include #include #include #include +#include #include namespace Ui { @@ -33,9 +35,12 @@ class MainWindow : public QMainWindow { void on_action_Load_triggered(); private: + static MainWindow* instance; Ui::MainWindow* ui; PluginModel* pluginModel; std::vector charts; + + QFileDialog* fileDialog; }; } // namespace Graph::GUI diff --git a/inc/graph/server/gui/specdialog/specdialog.h b/inc/graph/server/gui/specdialog/specdialog.h index a1d2bbd..c2bbea3 100644 --- a/inc/graph/server/gui/specdialog/specdialog.h +++ b/inc/graph/server/gui/specdialog/specdialog.h @@ -11,14 +11,22 @@ class SpecDialog; namespace Graph::GUI { class SpecDialog : public QDialog { Q_OBJECT - public: + private: explicit SpecDialog(QWidget* parent = nullptr); + + public: + SpecDialog(SpecDialog& other) = delete; + void operator=(const SpecDialog&) = delete; ~SpecDialog(); + static SpecDialog* getInstance(); + static void close(); + QStringList getList() const { return series->stringList(); } chart_spec_t getSpec() const; private: + static SpecDialog* instance; Ui::SpecDialog* ui; QStringListModel* series; diff --git a/src/graph/server/gui/mainwindow.cpp b/src/graph/server/gui/mainwindow.cpp index 6d6700f..c6659f2 100644 --- a/src/graph/server/gui/mainwindow.cpp +++ b/src/graph/server/gui/mainwindow.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -13,7 +14,8 @@ #define MAX(a, b) (a > b ? a : b) namespace Graph::GUI { -MainWindow* instance = nullptr; + +MainWindow* MainWindow::instance = nullptr; MainWindow* MainWindow::getInstance() { if (!instance) @@ -23,6 +25,7 @@ MainWindow* MainWindow::getInstance() { } void MainWindow::close() { + SpecDialog::close(); if (instance) delete instance; } @@ -82,6 +85,8 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWi ui->pluginsListView->doItemsLayout(); } }); + + fileDialog = new QFileDialog(this, "Load plugin", "./plugins", "Plugin (*.so *.dll *.dlsym)"); } MainWindow::~MainWindow() { @@ -90,21 +95,22 @@ MainWindow::~MainWindow() { } delete pluginModel; + delete fileDialog; } void MainWindow::on_action_Add_triggered() { - SpecDialog dialog; - if (dialog.exec() == SpecDialog::Rejected) + SpecDialog* dialog = SpecDialog::getInstance(); + if (dialog->exec() == SpecDialog::Rejected) return; - ChartWidget* chart = new ChartWidget(ui->graphsListWidget, dialog.getSpec()); + ChartWidget* chart = new ChartWidget(ui->graphsListWidget, dialog->getSpec()); charts.push_back(chart); ui->graphsListWidget->addItem(chart); ui->graphsListWidget->setItemWidget(chart, chart->getView()); - for (QString title : dialog.getList()) { + for (QString title : dialog->getList()) { QLineSeries* series = new QLineSeries(); series->setName(title); chart->getChart()->addSeries(series); @@ -115,25 +121,25 @@ void MainWindow::on_action_Add_triggered() { } void MainWindow::on_action_Load_triggered() { - QString file = QFileDialog::getOpenFileName(this, "Load plugin", ".", "Plugins (*.so *.dll *.dylib)"); - - if (file.isEmpty()) + if (!fileDialog->exec()) return; - try { - Plugin* plugin = new Plugin(file.toStdString()); - plugin->init(&Graph::GUI::addData); - pluginModel->add(plugin); - } catch (std::runtime_error e) { - std::cout << e.what() << std::endl; - QMessageBox::critical(this, "Error", "Failed to load plugin.", QMessageBox::StandardButton::Ignore); - } + for (QString file : fileDialog->selectedFiles()) { + try { + Plugin* plugin = new Plugin(file.toStdString()); + plugin->init(&Graph::GUI::addData); + pluginModel->add(plugin); + } catch (std::runtime_error e) { + std::cout << e.what() << std::endl; + QMessageBox::critical(this, "Error", "Failed to load plugin:\n" + file, QMessageBox::StandardButton::Ignore); + } - int row = pluginModel->rowCount(); - pluginModel->insertRow(row); - QModelIndex index = pluginModel->index(row); - ui->pluginsListView->doItemsLayout(); - ui->pluginsListView->setCurrentIndex(index); + int row = pluginModel->rowCount(); + pluginModel->insertRow(row); + QModelIndex index = pluginModel->index(row); + ui->pluginsListView->doItemsLayout(); + ui->pluginsListView->setCurrentIndex(index); + } } } // namespace Graph::GUI diff --git a/src/graph/server/gui/specdialog/specdialog.cpp b/src/graph/server/gui/specdialog/specdialog.cpp index f065d3e..92f8ce1 100644 --- a/src/graph/server/gui/specdialog/specdialog.cpp +++ b/src/graph/server/gui/specdialog/specdialog.cpp @@ -3,6 +3,20 @@ #include namespace Graph::GUI { +SpecDialog* SpecDialog::instance = nullptr; + +SpecDialog* SpecDialog::getInstance() { + if (!instance) + instance = new SpecDialog(); + + return instance; +} + +void SpecDialog::close() { + if(instance) + delete instance; +} + SpecDialog::SpecDialog(QWidget* parent) : QDialog(parent), ui(new Ui::SpecDialog) { ui->setupUi(this);