diff --git a/inc/graph/plugins/plugin.h b/inc/graph/plugins/plugin.h index d36851f..ea40369 100644 --- a/inc/graph/plugins/plugin.h +++ b/inc/graph/plugins/plugin.h @@ -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; diff --git a/inc/graph/server/gui/chart/chart.h b/inc/graph/server/gui/chart/chart.h index 1c873ca..cf2daae 100644 --- a/inc/graph/server/gui/chart/chart.h +++ b/inc/graph/server/gui/chart/chart.h @@ -16,10 +16,14 @@ class ChartWidget : public QListWidgetItem { QChartView* getView() const { return view; } QChart* getChart() const { return chart; } QList 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 diff --git a/inc/graph/server/gui/mainwindow.h b/inc/graph/server/gui/mainwindow.h index 8dae1c3..b85b88c 100644 --- a/inc/graph/server/gui/mainwindow.h +++ b/inc/graph/server/gui/mainwindow.h @@ -1,14 +1,12 @@ #pragma once +#include #include -#include -#include #include #include #include #include #include -#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(); diff --git a/inc/graph/server/plugin.h b/inc/graph/server/plugin.h index e44e981..4caefef 100644 --- a/inc/graph/server/plugin.h +++ b/inc/graph/server/plugin.h @@ -32,6 +32,7 @@ class PluginModel : public QAbstractListModel { void add(Plugin* plugin); void remove(int index); + QList& getPlugins() { return plugins; } private: QList plugins; diff --git a/src/graph/plugins/http/http.cpp b/src/graph/plugins/http/http.cpp index 9d96ab2..9cc92a0 100644 --- a/src/graph/plugins/http/http.cpp +++ b/src/graph/plugins/http/http.cpp @@ -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; } diff --git a/src/graph/server/gui/chart/chart.cpp b/src/graph/server/gui/chart/chart.cpp index 099e3d7..45053a0 100644 --- a/src/graph/server/gui/chart/chart.cpp +++ b/src/graph/server/gui/chart/chart.cpp @@ -3,7 +3,7 @@ #include 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() { diff --git a/src/graph/server/gui/mainwindow.cpp b/src/graph/server/gui/mainwindow.cpp index 4243a72..6d6700f 100644 --- a/src/graph/server/gui/mainwindow.cpp +++ b/src/graph/server/gui/mainwindow.cpp @@ -9,7 +9,62 @@ #include #include +#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::max(), maxX = std::numeric_limits::min(); + double minY = std::numeric_limits::max(), maxY = std::numeric_limits::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; diff --git a/src/graph/server/main.cpp b/src/graph/server/main.cpp index 8ed8875..643c368 100644 --- a/src/graph/server/main.cpp +++ b/src/graph/server/main.cpp @@ -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; } \ No newline at end of file diff --git a/src/graph/server/plugin.cpp b/src/graph/server/plugin.cpp index 087d75f..e8f4a01 100644 --- a/src/graph/server/plugin.cpp +++ b/src/graph/server/plugin.cpp @@ -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); }