diff --git a/inc/graph/server/gui/chart/chart.h b/inc/graph/server/gui/chart/chart.h index cf2daae..0cbf6da 100644 --- a/inc/graph/server/gui/chart/chart.h +++ b/inc/graph/server/gui/chart/chart.h @@ -1,10 +1,13 @@ #pragma once #include +#include +#include #include #include #include #include +#include #include namespace Graph::GUI { @@ -20,10 +23,25 @@ class ChartWidget : public QListWidgetItem { QValueAxis* getY() const { return y; } const chart_spec_t& getSpec() const { return spec; } + void clear(); + private: QChartView* view; QChart* chart; QValueAxis *x, *y; chart_spec_t spec; }; + +class ContextMenu : public QMenu { + Q_OBJECT + public: + ContextMenu(QWidget* parent = nullptr, ChartWidget* chartWidget = nullptr); + ~ContextMenu(); + + private: + QAction* remove; + QAction* clear; + QAction* edit; +}; + } // namespace Graph::GUI diff --git a/inc/graph/server/gui/mainwindow.h b/inc/graph/server/gui/mainwindow.h index 6d45e04..971cc53 100644 --- a/inc/graph/server/gui/mainwindow.h +++ b/inc/graph/server/gui/mainwindow.h @@ -30,6 +30,8 @@ class MainWindow : public QMainWindow { void addData(unsigned int chartIndex, const char* seriesName, const point_t* points, int count); + void removeChart(ChartWidget* chartWidget); + private slots: void on_action_Add_triggered(); void on_action_Load_triggered(); diff --git a/src/graph/server/gui/chart/chart.cpp b/src/graph/server/gui/chart/chart.cpp index 45053a0..ab718d8 100644 --- a/src/graph/server/gui/chart/chart.cpp +++ b/src/graph/server/gui/chart/chart.cpp @@ -1,5 +1,8 @@ #include -#include +#include +#include +#include +#include #include namespace Graph::GUI { @@ -34,6 +37,13 @@ ChartWidget::ChartWidget(QWidget* parent, const chart_spec_t& spec) : QListWidge view->setChart(chart); view->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); setSizeHint(view->sizeHint() * 4); + + // context menu + view->setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu); + view->connect(view, &QChartView::customContextMenuRequested, view, [this](const QPoint& pos) { + ContextMenu menu(nullptr, this); + menu.exec(view->mapToGlobal(pos)); + }); } ChartWidget::~ChartWidget() { @@ -42,4 +52,30 @@ ChartWidget::~ChartWidget() { // delete x; // delete y; } + +void ChartWidget::clear() { + for (QAbstractSeries* series : chart->series()) { + ((QLineSeries*)series)->removePoints(0, ((QLineSeries*)series)->count()); + } +} + +ContextMenu::ContextMenu(QWidget* parent, ChartWidget* chartWidget) : QMenu(parent) { + remove = new QAction(tr("&Remove"), this); + connect(remove, &QAction::triggered, this, [chartWidget]() { + MainWindow::getInstance()->removeChart(chartWidget); + }); + addAction(remove); + + clear = new QAction(tr("&Clear"), this); + connect(clear, &QAction::triggered, this, [chartWidget]() { chartWidget->clear(); }); + addAction(clear); + + edit = new QAction(tr("&Edit"), this); + connect(edit, &QAction::triggered, this, []() { + // TODO: implement chart editing + }); + addAction(edit); +} + +ContextMenu::~ContextMenu() {} } // namespace Graph::GUI \ No newline at end of file diff --git a/src/graph/server/gui/mainwindow.cpp b/src/graph/server/gui/mainwindow.cpp index fe60348..bde399c 100644 --- a/src/graph/server/gui/mainwindow.cpp +++ b/src/graph/server/gui/mainwindow.cpp @@ -4,7 +4,6 @@ #include #include #include -#include #include #include #include @@ -70,7 +69,7 @@ void MainWindow::addData(unsigned int chartIndex, const char* seriesName, const minY = MIN(minY, point.y()); maxY = MAX(maxY, point.y()); } - + if (spec.axis.x.auto_limit) chart->getX()->setRange(minX, maxX); if (spec.axis.y.auto_limit) @@ -153,16 +152,20 @@ void MainWindow::on_action_Load_triggered() { try { Plugin* plugin = new Plugin(file.toStdString(), &Graph::GUI::addData, argc, argv); + pluginModel->add(plugin); + int row = pluginModel->rowCount(); QThread* thread = new QThread(); plugin->moveToThread(thread); connect(plugin, &Plugin::exited, this, [](int code) { std::cout << "plugin exited: " << code << std::endl; }); connect(thread, &QThread::started, plugin, &Plugin::run); connect(plugin, &Plugin::exited, thread, &QThread::quit); - connect(plugin, &Plugin::exited, this, [this, file](int code) { - if (code != 0) + connect(plugin, &Plugin::exited, this, [this, file, row](int code) { + if (code != 0) { QMessageBox::critical(this, "Error", "Plugin exited with an error!\n" + file, QMessageBox::StandardButton::Ignore); + pluginModel->remove(row - 1); + } }); connect(thread, &QThread::finished, thread, &QThread::deleteLater); thread->start(); @@ -179,4 +182,14 @@ void MainWindow::on_action_Load_triggered() { } } +void MainWindow::removeChart(ChartWidget* chartWidget) { + for (auto it = charts.begin(); it != charts.end(); it++) { + if ((*it) == chartWidget) { + charts.erase(it); + delete chartWidget; + break; + } + } +} + } // namespace Graph::GUI diff --git a/src/graph/server/gui/mainwindow.ui b/src/graph/server/gui/mainwindow.ui index 1e38c3d..a5490a0 100644 --- a/src/graph/server/gui/mainwindow.ui +++ b/src/graph/server/gui/mainwindow.ui @@ -61,7 +61,6 @@ &Graph - @@ -88,14 +87,6 @@ Ctrl+A - - - &Remove - - - Ctrl+R - -