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
#include <graph/server/spec.h>
#include <qevent.h>
#include <QAction>
#include <QChart>
#include <QChartView>
#include <QLineSeries>
#include <QListWidgetItem>
#include <QMenu>
#include <QValueAxis>
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

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 removeChart(ChartWidget* chartWidget);
private slots:
void on_action_Add_triggered();
void on_action_Load_triggered();

View File

@ -1,5 +1,8 @@
#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>
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

View File

@ -4,7 +4,6 @@
#include <graph/server/plugin.h>
#include <graph/server/spec.h>
#include <graph/server/ui_mainwindow.h>
#include <qlineseries.h>
#include <QFileDialog>
#include <QMessageBox>
#include <iostream>
@ -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

View File

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