chart context menu

This commit is contained in:
Benedek László 2024-05-16 02:31:36 +02:00
parent 9d27db7777
commit 2af8578cf2
5 changed files with 74 additions and 14 deletions

View File

@ -1,10 +1,13 @@
#pragma once #pragma once
#include <graph/server/spec.h> #include <graph/server/spec.h>
#include <qevent.h>
#include <QAction>
#include <QChart> #include <QChart>
#include <QChartView> #include <QChartView>
#include <QLineSeries> #include <QLineSeries>
#include <QListWidgetItem> #include <QListWidgetItem>
#include <QMenu>
#include <QValueAxis> #include <QValueAxis>
namespace Graph::GUI { namespace Graph::GUI {
@ -20,10 +23,25 @@ class ChartWidget : public QListWidgetItem {
QValueAxis* getY() const { return y; } QValueAxis* getY() const { return y; }
const chart_spec_t& getSpec() const { return spec; } const chart_spec_t& getSpec() const { return spec; }
void clear();
private: private:
QChartView* view; QChartView* view;
QChart* chart; QChart* chart;
QValueAxis *x, *y; QValueAxis *x, *y;
chart_spec_t spec; 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 } // namespace Graph::GUI

View File

@ -30,6 +30,8 @@ class MainWindow : public QMainWindow {
void addData(unsigned int chartIndex, const char* seriesName, const point_t* points, int count); void addData(unsigned int chartIndex, const char* seriesName, const point_t* points, int count);
void removeChart(ChartWidget* chartWidget);
private slots: private slots:
void on_action_Add_triggered(); void on_action_Add_triggered();
void on_action_Load_triggered(); void on_action_Load_triggered();

View File

@ -1,5 +1,8 @@
#include <graph/server/gui/chart/chart.h> #include <graph/server/gui/chart/chart.h>
#include <qsizepolicy.h> #include <graph/server/gui/mainwindow.h>
#include <qabstractseries.h>
#include <qlineseries.h>
#include <QIcon>
#include <iostream> #include <iostream>
namespace Graph::GUI { namespace Graph::GUI {
@ -34,6 +37,13 @@ ChartWidget::ChartWidget(QWidget* parent, const chart_spec_t& spec) : QListWidge
view->setChart(chart); view->setChart(chart);
view->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); view->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
setSizeHint(view->sizeHint() * 4); 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() { ChartWidget::~ChartWidget() {
@ -42,4 +52,30 @@ ChartWidget::~ChartWidget() {
// delete x; // delete x;
// delete y; // 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 } // namespace Graph::GUI

View File

@ -4,7 +4,6 @@
#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 <qlineseries.h>
#include <QFileDialog> #include <QFileDialog>
#include <QMessageBox> #include <QMessageBox>
#include <iostream> #include <iostream>
@ -70,7 +69,7 @@ void MainWindow::addData(unsigned int chartIndex, const char* seriesName, const
minY = MIN(minY, point.y()); minY = MIN(minY, point.y());
maxY = MAX(maxY, point.y()); maxY = MAX(maxY, point.y());
} }
if (spec.axis.x.auto_limit) if (spec.axis.x.auto_limit)
chart->getX()->setRange(minX, maxX); chart->getX()->setRange(minX, maxX);
if (spec.axis.y.auto_limit) if (spec.axis.y.auto_limit)
@ -153,16 +152,20 @@ void MainWindow::on_action_Load_triggered() {
try { try {
Plugin* plugin = new Plugin(file.toStdString(), &Graph::GUI::addData, argc, argv); Plugin* plugin = new Plugin(file.toStdString(), &Graph::GUI::addData, argc, argv);
pluginModel->add(plugin); pluginModel->add(plugin);
int row = pluginModel->rowCount();
QThread* thread = new QThread(); QThread* thread = new QThread();
plugin->moveToThread(thread); plugin->moveToThread(thread);
connect(plugin, &Plugin::exited, this, [](int code) { std::cout << "plugin exited: " << code << std::endl; }); connect(plugin, &Plugin::exited, this, [](int code) { std::cout << "plugin exited: " << code << std::endl; });
connect(thread, &QThread::started, plugin, &Plugin::run); connect(thread, &QThread::started, plugin, &Plugin::run);
connect(plugin, &Plugin::exited, thread, &QThread::quit); connect(plugin, &Plugin::exited, thread, &QThread::quit);
connect(plugin, &Plugin::exited, this, [this, file](int code) { connect(plugin, &Plugin::exited, this, [this, file, row](int code) {
if (code != 0) if (code != 0) {
QMessageBox::critical(this, "Error", "Plugin exited with an error!\n" + file, QMessageBox::StandardButton::Ignore); 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); connect(thread, &QThread::finished, thread, &QThread::deleteLater);
thread->start(); 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 } // namespace Graph::GUI

View File

@ -61,7 +61,6 @@
<string>&amp;Graph</string> <string>&amp;Graph</string>
</property> </property>
<addaction name="action_Add"/> <addaction name="action_Add"/>
<addaction name="action_Remove"/>
</widget> </widget>
<widget class="QMenu" name="menu_Plugin"> <widget class="QMenu" name="menu_Plugin">
<property name="title"> <property name="title">
@ -88,14 +87,6 @@
<string>Ctrl+A</string> <string>Ctrl+A</string>
</property> </property>
</action> </action>
<action name="action_Remove">
<property name="text">
<string>&amp;Remove</string>
</property>
<property name="shortcut">
<string>Ctrl+R</string>
</property>
</action>
</widget> </widget>
<resources/> <resources/>
<connections/> <connections/>