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/plugin.h>
#include <qfiledialog.h>
#include <QMainWindow>
#include <QMenuBar>
#include <QStatusBar>
#include <QToolBar>
#include <cstddef>
#include <vector>
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<ChartWidget*> charts;
QFileDialog* fileDialog;
};
} // namespace Graph::GUI

View File

@ -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;

View File

@ -4,6 +4,7 @@
#include <graph/server/plugin.h>
#include <graph/server/spec.h>
#include <graph/server/ui_mainwindow.h>
#include <qfiledialog.h>
#include <QFileDialog>
#include <QMessageBox>
#include <iostream>
@ -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,18 +121,17 @@ 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;
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.", QMessageBox::StandardButton::Ignore);
QMessageBox::critical(this, "Error", "Failed to load plugin:\n" + file, QMessageBox::StandardButton::Ignore);
}
int row = pluginModel->rowCount();
@ -135,5 +140,6 @@ void MainWindow::on_action_Load_triggered() {
ui->pluginsListView->doItemsLayout();
ui->pluginsListView->setCurrentIndex(index);
}
}
} // namespace Graph::GUI

View File

@ -3,6 +3,20 @@
#include <graph/server/ui_specdialog.h>
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);