1st working demo

This commit is contained in:
Benedek László 2024-05-15 12:05:13 +02:00
parent 5e8455c8a2
commit 60cef3cb54
9 changed files with 90 additions and 20 deletions

View File

@ -13,7 +13,7 @@ typedef struct {
typedef bool (*start_t)();
typedef void (*destroy_t)();
typedef void (*update_callback_t)(unsigned int chartIndex, const char* seriesName, const point_t* points);
typedef void (*update_callback_t)(unsigned int chartIndex, const char* seriesName, const point_t* points, int count);
typedef struct {
uint32_t version;

View File

@ -16,10 +16,14 @@ class ChartWidget : public QListWidgetItem {
QChartView* getView() const { return view; }
QChart* getChart() const { return chart; }
QList<QAbstractSeries*> getSeries() const { return chart->series(); }
QValueAxis* getX() const { return x; }
QValueAxis* getY() const { return y; }
const chart_spec_t& getSpec() const { return spec; }
private:
QChartView* view;
QChart* chart;
QValueAxis *x, *y;
chart_spec_t spec;
};
} // namespace Graph::GUI

View File

@ -1,14 +1,12 @@
#pragma once
#include <graph/server/gui/chart/chart.h>
#include <graph/server/plugin.h>
#include <qlineseries.h>
#include <qlistwidget.h>
#include <QMainWindow>
#include <QMenuBar>
#include <QStatusBar>
#include <QToolBar>
#include <vector>
#include "graph/server/gui/chart/chart.h"
namespace Ui {
class MainWindow;
@ -17,10 +15,19 @@ class MainWindow;
namespace Graph::GUI {
class MainWindow : public QMainWindow {
Q_OBJECT
public:
private:
explicit MainWindow(QWidget* parent = nullptr);
public:
MainWindow(MainWindow& other) = delete;
void operator=(const MainWindow&) = delete;
~MainWindow();
static MainWindow* getInstance();
static void close();
void addData(unsigned int chartIndex, const char* seriesName, const point_t* points, int count);
private slots:
void on_action_Add_triggered();
void on_action_Load_triggered();

View File

@ -32,6 +32,7 @@ class PluginModel : public QAbstractListModel {
void add(Plugin* plugin);
void remove(int index);
QList<Plugin*>& getPlugins() { return plugins; }
private:
QList<Plugin*> plugins;

View File

@ -14,12 +14,11 @@ bool init() {
std::cout << "init" << std::endl;
point_t points[100];
for (int i = 0; i < 100; i++) {
double deg = i*360/100.0;
points[i].x = deg;
points[i].y = sin(to_rad(deg));
points[i].x = i;
points[i].y = i;
}
plugin.update_callback(0, "1st", points);
plugin.update_callback(0, "1st", points, 100);
return true;
}

View File

@ -3,7 +3,7 @@
#include <iostream>
namespace Graph::GUI {
ChartWidget::ChartWidget(QWidget* parent, const chart_spec_t& spec) : QListWidgetItem() {
ChartWidget::ChartWidget(QWidget* parent, const chart_spec_t& spec) : QListWidgetItem(), spec(spec) {
view = new QChartView(parent);
chart = new QChart();
@ -30,9 +30,10 @@ ChartWidget::ChartWidget(QWidget* parent, const chart_spec_t& spec) : QListWidge
chart->addAxis(x, Qt::AlignBottom);
chart->addAxis(y, Qt::AlignLeft);
view->setRenderHint(QPainter::Antialiasing);
view->setChart(chart);
view->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
setSizeHint(view->sizeHint()*2);
setSizeHint(view->sizeHint() * 4);
}
ChartWidget::~ChartWidget() {

View File

@ -9,7 +9,62 @@
#include <iostream>
#include <stdexcept>
#define MIN(a, b) (a < b ? a : b)
#define MAX(a, b) (a > b ? a : b)
namespace Graph::GUI {
MainWindow* instance = nullptr;
MainWindow* MainWindow::getInstance() {
if (!instance)
instance = new MainWindow();
return instance;
}
void MainWindow::close() {
if (instance)
delete instance;
}
void addData(unsigned int chartIndex, const char* seriesName, const point_t* points, int count) {
MainWindow::getInstance()->addData(chartIndex, seriesName, points, count);
}
void MainWindow::addData(unsigned int chartIndex, const char* seriesName, const point_t* points, int count) {
if (chartIndex < 0 || chartIndex >= charts.size())
return;
QString name(seriesName);
ChartWidget* chart = charts[chartIndex];
double minX = std::numeric_limits<double>::max(), maxX = std::numeric_limits<double>::min();
double minY = std::numeric_limits<double>::max(), maxY = std::numeric_limits<double>::min();
for (QAbstractSeries* series : chart->getSeries()) {
if (series->name() == name) {
for (int i = 0; i < count; i++) {
*((QLineSeries*)series) << QPointF(points[i].x, points[i].y);
}
}
for (QPointF point : ((QLineSeries*)series)->points()) {
minX = MIN(minX, point.x());
maxX = MAX(maxX, point.x());
minY = MIN(minY, point.y());
maxY = MAX(maxY, point.y());
}
}
if (chart->getSpec().axis.x.auto_limit)
chart->getX()->setRange(minX, maxX);
if (chart->getSpec().axis.y.auto_limit)
chart->getY()->setRange(minY, maxY);
chart->getView()->update();
}
MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
@ -44,6 +99,8 @@ void MainWindow::on_action_Add_triggered() {
ChartWidget* chart = new ChartWidget(ui->graphsListWidget, dialog.getSpec());
charts.push_back(chart);
ui->graphsListWidget->addItem(chart);
ui->graphsListWidget->setItemWidget(chart, chart->getView());
@ -51,8 +108,9 @@ void MainWindow::on_action_Add_triggered() {
QLineSeries* series = new QLineSeries();
series->setName(title);
chart->getChart()->addSeries(series);
*series << QPointF(0, 1) << QPointF(1, 2) << QPoint(2, 3);
for (QAbstractAxis* axis : chart->getChart()->axes()) {
series->attachAxis(axis);
}
}
}
@ -64,7 +122,7 @@ void MainWindow::on_action_Load_triggered() {
try {
Plugin* plugin = new Plugin(file.toStdString());
// plugin->init(update_callback);
plugin->init(&Graph::GUI::addData);
pluginModel->add(plugin);
} catch (std::runtime_error e) {
std::cout << e.what() << std::endl;

View File

@ -3,8 +3,8 @@
int main(int argc, char** argv) {
QApplication app(argc, argv);
Graph::GUI::MainWindow window;
window.show();
return app.exec();
Graph::GUI::MainWindow::getInstance()->show();
int result = app.exec();
Graph::GUI::MainWindow::close();
return result;
}

View File

@ -17,10 +17,10 @@ Plugin::Plugin(const std::string& path) : path(path) {
}
Plugin::~Plugin() {
if (this->plugin->update_callback)
if (this->plugin->destroy_method)
this->plugin->destroy_method();
if (this->handle)
if (handle)
dlclose((void*)this->handle);
}