persistent dialogs

This commit is contained in:
Benedek László 2024-05-15 12:32:41 +02:00
parent 60cef3cb54
commit 71d6ad8763
4 changed files with 55 additions and 22 deletions

View File

@ -2,10 +2,12 @@
#include <graph/server/gui/chart/chart.h> #include <graph/server/gui/chart/chart.h>
#include <graph/server/plugin.h> #include <graph/server/plugin.h>
#include <qfiledialog.h>
#include <QMainWindow> #include <QMainWindow>
#include <QMenuBar> #include <QMenuBar>
#include <QStatusBar> #include <QStatusBar>
#include <QToolBar> #include <QToolBar>
#include <cstddef>
#include <vector> #include <vector>
namespace Ui { namespace Ui {
@ -33,9 +35,12 @@ class MainWindow : public QMainWindow {
void on_action_Load_triggered(); void on_action_Load_triggered();
private: private:
static MainWindow* instance;
Ui::MainWindow* ui; Ui::MainWindow* ui;
PluginModel* pluginModel; PluginModel* pluginModel;
std::vector<ChartWidget*> charts; std::vector<ChartWidget*> charts;
QFileDialog* fileDialog;
}; };
} // namespace Graph::GUI } // namespace Graph::GUI

View File

@ -11,14 +11,22 @@ class SpecDialog;
namespace Graph::GUI { namespace Graph::GUI {
class SpecDialog : public QDialog { class SpecDialog : public QDialog {
Q_OBJECT Q_OBJECT
public: private:
explicit SpecDialog(QWidget* parent = nullptr); explicit SpecDialog(QWidget* parent = nullptr);
public:
SpecDialog(SpecDialog& other) = delete;
void operator=(const SpecDialog&) = delete;
~SpecDialog(); ~SpecDialog();
static SpecDialog* getInstance();
static void close();
QStringList getList() const { return series->stringList(); } QStringList getList() const { return series->stringList(); }
chart_spec_t getSpec() const; chart_spec_t getSpec() const;
private: private:
static SpecDialog* instance;
Ui::SpecDialog* ui; Ui::SpecDialog* ui;
QStringListModel* series; QStringListModel* series;

View File

@ -4,6 +4,7 @@
#include <graph/server/plugin.h> #include <graph/server/plugin.h>
#include <graph/server/spec.h> #include <graph/server/spec.h>
#include <graph/server/ui_mainwindow.h> #include <graph/server/ui_mainwindow.h>
#include <qfiledialog.h>
#include <QFileDialog> #include <QFileDialog>
#include <QMessageBox> #include <QMessageBox>
#include <iostream> #include <iostream>
@ -13,7 +14,8 @@
#define MAX(a, b) (a > b ? a : b) #define MAX(a, b) (a > b ? a : b)
namespace Graph::GUI { namespace Graph::GUI {
MainWindow* instance = nullptr;
MainWindow* MainWindow::instance = nullptr;
MainWindow* MainWindow::getInstance() { MainWindow* MainWindow::getInstance() {
if (!instance) if (!instance)
@ -23,6 +25,7 @@ MainWindow* MainWindow::getInstance() {
} }
void MainWindow::close() { void MainWindow::close() {
SpecDialog::close();
if (instance) if (instance)
delete instance; delete instance;
} }
@ -82,6 +85,8 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWi
ui->pluginsListView->doItemsLayout(); ui->pluginsListView->doItemsLayout();
} }
}); });
fileDialog = new QFileDialog(this, "Load plugin", "./plugins", "Plugin (*.so *.dll *.dlsym)");
} }
MainWindow::~MainWindow() { MainWindow::~MainWindow() {
@ -90,21 +95,22 @@ MainWindow::~MainWindow() {
} }
delete pluginModel; delete pluginModel;
delete fileDialog;
} }
void MainWindow::on_action_Add_triggered() { void MainWindow::on_action_Add_triggered() {
SpecDialog dialog; SpecDialog* dialog = SpecDialog::getInstance();
if (dialog.exec() == SpecDialog::Rejected) if (dialog->exec() == SpecDialog::Rejected)
return; return;
ChartWidget* chart = new ChartWidget(ui->graphsListWidget, dialog.getSpec()); ChartWidget* chart = new ChartWidget(ui->graphsListWidget, dialog->getSpec());
charts.push_back(chart); charts.push_back(chart);
ui->graphsListWidget->addItem(chart); ui->graphsListWidget->addItem(chart);
ui->graphsListWidget->setItemWidget(chart, chart->getView()); ui->graphsListWidget->setItemWidget(chart, chart->getView());
for (QString title : dialog.getList()) { for (QString title : dialog->getList()) {
QLineSeries* series = new QLineSeries(); QLineSeries* series = new QLineSeries();
series->setName(title); series->setName(title);
chart->getChart()->addSeries(series); chart->getChart()->addSeries(series);
@ -115,25 +121,25 @@ void MainWindow::on_action_Add_triggered() {
} }
void MainWindow::on_action_Load_triggered() { void MainWindow::on_action_Load_triggered() {
QString file = QFileDialog::getOpenFileName(this, "Load plugin", ".", "Plugins (*.so *.dll *.dylib)"); if (!fileDialog->exec())
if (file.isEmpty())
return; return;
try { for (QString file : fileDialog->selectedFiles()) {
Plugin* plugin = new Plugin(file.toStdString()); try {
plugin->init(&Graph::GUI::addData); Plugin* plugin = new Plugin(file.toStdString());
pluginModel->add(plugin); plugin->init(&Graph::GUI::addData);
} catch (std::runtime_error e) { pluginModel->add(plugin);
std::cout << e.what() << std::endl; } catch (std::runtime_error e) {
QMessageBox::critical(this, "Error", "Failed to load plugin.", QMessageBox::StandardButton::Ignore); std::cout << e.what() << std::endl;
} QMessageBox::critical(this, "Error", "Failed to load plugin:\n" + file, QMessageBox::StandardButton::Ignore);
}
int row = pluginModel->rowCount(); int row = pluginModel->rowCount();
pluginModel->insertRow(row); pluginModel->insertRow(row);
QModelIndex index = pluginModel->index(row); QModelIndex index = pluginModel->index(row);
ui->pluginsListView->doItemsLayout(); ui->pluginsListView->doItemsLayout();
ui->pluginsListView->setCurrentIndex(index); ui->pluginsListView->setCurrentIndex(index);
}
} }
} // namespace Graph::GUI } // namespace Graph::GUI

View File

@ -3,6 +3,20 @@
#include <graph/server/ui_specdialog.h> #include <graph/server/ui_specdialog.h>
namespace Graph::GUI { 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) { SpecDialog::SpecDialog(QWidget* parent) : QDialog(parent), ui(new Ui::SpecDialog) {
ui->setupUi(this); ui->setupUi(this);