From 966db4f4e10d82707737f93965705530c469f4ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benedek=20L=C3=A1szl=C3=B3?= Date: Sat, 18 May 2024 22:18:17 +0200 Subject: [PATCH] mutex, move series creation to chart, remove unused functions --- inc/graph/server/gui/chart/chart.h | 7 +- inc/graph/server/gui/specdialog/specdialog.h | 2 - inc/graph/server/spec.h | 2 + src/graph/server/gui/chart/chart.cpp | 30 ++++- src/graph/server/gui/mainwindow.cpp | 9 -- .../server/gui/specdialog/specdialog.cpp | 3 +- test/test.py | 122 ++++++++++-------- 7 files changed, 97 insertions(+), 78 deletions(-) diff --git a/inc/graph/server/gui/chart/chart.h b/inc/graph/server/gui/chart/chart.h index 4da78e1..3204163 100644 --- a/inc/graph/server/gui/chart/chart.h +++ b/inc/graph/server/gui/chart/chart.h @@ -21,12 +21,7 @@ class ChartWidget : public QListWidgetItem { void addData(const char* seriesName, const point_t* points, int count); 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; } - void clear(); private: @@ -36,7 +31,7 @@ class ChartWidget : public QListWidgetItem { chart_spec_t spec; int decimateState = 0; - mutable QMutex mutex; + QMutex mutex; }; class ContextMenu : public QMenu { diff --git a/inc/graph/server/gui/specdialog/specdialog.h b/inc/graph/server/gui/specdialog/specdialog.h index c2bbea3..5295dc8 100644 --- a/inc/graph/server/gui/specdialog/specdialog.h +++ b/inc/graph/server/gui/specdialog/specdialog.h @@ -21,8 +21,6 @@ class SpecDialog : public QDialog { static SpecDialog* getInstance(); static void close(); - - QStringList getList() const { return series->stringList(); } chart_spec_t getSpec() const; private: diff --git a/inc/graph/server/spec.h b/inc/graph/server/spec.h index 04d61dd..a893e6e 100644 --- a/inc/graph/server/spec.h +++ b/inc/graph/server/spec.h @@ -1,5 +1,6 @@ #pragma once +#include #include struct axis_spec_t { @@ -19,4 +20,5 @@ struct chart_spec_t { struct { axis_spec_t x, y; } axis; + QList series; }; \ No newline at end of file diff --git a/src/graph/server/gui/chart/chart.cpp b/src/graph/server/gui/chart/chart.cpp index 7149c03..dba14cf 100644 --- a/src/graph/server/gui/chart/chart.cpp +++ b/src/graph/server/gui/chart/chart.cpp @@ -38,6 +38,14 @@ ChartWidget::ChartWidget(QWidget* parent, const chart_spec_t& spec) : QListWidge chart->addAxis(x, Qt::AlignBottom); chart->addAxis(y, Qt::AlignLeft); + for (const QString& title : spec.series) { + QLineSeries* series = new QLineSeries(view); + series->setName(title); + chart->addSeries(series); + series->attachAxis(x); + series->attachAxis(y); + } + view->setRenderHint(QPainter::Antialiasing); view->setChart(chart); view->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); @@ -61,6 +69,8 @@ ChartWidget::~ChartWidget() { void ChartWidget::addData(const char* seriesName, const point_t* points, int count) { QMutexLocker locker(&mutex); + std::cout << "locked: " << locker.isLocked() << std::endl; + QString name(seriesName); double minX = spec.keep_all ? x->min() : std::numeric_limits::max(); @@ -74,10 +84,12 @@ void ChartWidget::addData(const char* seriesName, const point_t* points, int cou if (count >= spec.keep_limit) { ((QLineSeries*)series)->clear(); } else if (((QLineSeries*)series)->count() + count > spec.keep_limit) { + std::cout << "count: " << count << ", size: " << ((QLineSeries*)series)->count() << ", remove: " << ((QLineSeries*)series)->count() - (spec.keep_limit - count) << std::endl; ((QLineSeries*)series)->removePoints(0, ((QLineSeries*)series)->count() - (spec.keep_limit - count)); } - for (QPointF point : ((QLineSeries*)series)->points()) { + std::cout << "starting min/max" << std::endl; + for (const QPointF& point : ((QLineSeries*)series)->points()) { if (spec.axis.x.auto_limit) { minX = MIN(minX, point.x()); maxX = MAX(maxX, point.x()); @@ -87,8 +99,12 @@ void ChartWidget::addData(const char* seriesName, const point_t* points, int cou maxY = MAX(maxY, point.y()); } } + std::cout << "got min/max" << std::endl; } + std::cout << "start adding" << std::endl; + QList newPoints; + for (int i = spec.keep_all ? 0 : MAX(0, count - spec.keep_limit); i < count; i++) { if (decimateState >= spec.decimate) decimateState = 0; @@ -104,19 +120,20 @@ void ChartWidget::addData(const char* seriesName, const point_t* points, int cou maxY = MAX(maxY, points[i].y); } - *((QLineSeries*)series) << QPointF(points[i].x, points[i].y); + newPoints.append(QPointF(points[i].x, points[i].y)); } + std::cout << "adding" << std::endl; + ((QLineSeries*)series)->append(newPoints); + if (spec.axis.x.auto_limit) - x->setRange(minX, maxX); + x->setRange(MIN(x->min(), minX), MAX(x->max(), maxX)); if (spec.axis.y.auto_limit) - y->setRange(minY, maxY); + y->setRange(MIN(y->min(), minY), MAX(y->max(), maxY)); break; } } - - view->update(); } void ChartWidget::clear() { @@ -128,7 +145,6 @@ void ChartWidget::clear() { x->setRange(spec.axis.x.min, spec.axis.x.max); y->setRange(spec.axis.y.min, spec.axis.y.max); decimateState = 0; - view->update(); } ContextMenu::ContextMenu(QWidget* parent, ChartWidget* chartWidget) : QMenu(parent) { diff --git a/src/graph/server/gui/mainwindow.cpp b/src/graph/server/gui/mainwindow.cpp index e068427..9d1966a 100644 --- a/src/graph/server/gui/mainwindow.cpp +++ b/src/graph/server/gui/mainwindow.cpp @@ -85,15 +85,6 @@ void MainWindow::on_action_Add_triggered() { ui->graphsListWidget->addItem(chart); ui->graphsListWidget->setItemWidget(chart, chart->getView()); - - for (QString title : dialog->getList()) { - QLineSeries* series = new QLineSeries(); - series->setName(title); - chart->getChart()->addSeries(series); - for (QAbstractAxis* axis : chart->getChart()->axes()) { - series->attachAxis(axis); - } - } } void MainWindow::on_action_Load_triggered() { diff --git a/src/graph/server/gui/specdialog/specdialog.cpp b/src/graph/server/gui/specdialog/specdialog.cpp index ef1da2f..4db45e1 100644 --- a/src/graph/server/gui/specdialog/specdialog.cpp +++ b/src/graph/server/gui/specdialog/specdialog.cpp @@ -48,6 +48,7 @@ chart_spec_t SpecDialog::getSpec() const { .keep_limit = ui->keep->value(), .decimate = ui->decimate->value(), .axis = {.x = {.label = ui->xTitle->text(), .auto_limit = ui->xAutoRange->isChecked(), .min = ui->xMin->value(), .max = ui->xMax->value()}, - .y = {.label = ui->yTitle->text(), .auto_limit = ui->yAutoRange->isChecked(), .min = ui->yMin->value(), .max = ui->yMax->value()}}}; + .y = {.label = ui->yTitle->text(), .auto_limit = ui->yAutoRange->isChecked(), .min = ui->yMin->value(), .max = ui->yMax->value()}}, + .series = series->stringList()}; } } // namespace Graph::GUI \ No newline at end of file diff --git a/test/test.py b/test/test.py index 439f25f..4897f35 100755 --- a/test/test.py +++ b/test/test.py @@ -4,33 +4,40 @@ import requests from math import sin from time import sleep import numpy as np -import pyaudio +from argparse import ArgumentParser +# import pyaudio -def sine(): +def sine(args): x = 0.0 while True: - requests.get(f"http://localhost:8080/add?chart=0&series=R1&x={x}&y={sin(x)}") - x += 0.01 - sleep(0.01) + try: + requests.get(f"http://{args.address}:{args.port}/add?chart={args.chart}&series={args.series}&x={x}&y={sin(x)}") + x += 0.01 + sleep(0.01) + except KeyboardInterrupt: + break -def sine_array(): +def sine_array(args): start = 0 while True: - x = np.linspace(start, start+0.5, 100) - y = np.sin(x) + try: + x = np.linspace(start, start+0.5, 100) + y = np.sin(x) - start += 0.5 + start += 0.5 - data = { - "chart": 0, - "series": "R1", - "points": [ {"x": i[0], "y":i[1]} for i in zip(x,y) ] - } + data = { + "chart": args.chart, + "series": args.series, + "points": [ {"x": i[0], "y":i[1]} for i in zip(x,y) ] + } - r = requests.post("http://0.0.0.0:8080/add", json=data, headers={"Connection":"close"}) - sleep(0.25) + r = requests.post(f"http://{args.address}:{args.port}/add", json=data) + sleep(0.25) + except KeyboardInterrupt: + break -def star(): +def star(args): points = [ (2,2), (3,0), @@ -40,43 +47,52 @@ def star(): (2,2) ] for point in points: - requests.get(f"http://localhost:8080/add?chart=0&series=R1&x={point[0]}&y={point[1]}") + requests.get(f"http://{args.address}:{args.port}/add?chart={args.chart}&series={args.series}&x={point[0]}&y={point[1]}") +# def audio(): +# p = pyaudio.PyAudio() +# stream = p.open( +# input=True, +# input_device_index=0, +# format=pyaudio.paInt16, +# channels=1, +# rate=44100, +# frames_per_buffer=1024 +# ) +# stream.start_stream() +# while True: +# try: +# data = stream.read(1024, False) +# requests.post( +# "http://0.0.0.0:8080/add", +# json={ +# "chart": 0, +# "series": "audio", +# "points": [ {"x": i, "y": v} for (i, v) in enumerate(data) ] +# }, +# headers={"Connection":"close"}) +# except KeyboardInterrupt: +# break -def audio(): - p = pyaudio.PyAudio() - stream = p.open( - input=True, - input_device_index=0, - format=pyaudio.paInt16, - channels=1, - rate=44100, - frames_per_buffer=1024 - ) - stream.start_stream() - while True: - try: - data = stream.read(1024, False) - requests.post( - "http://0.0.0.0:8080/add", - json={ - "chart": 0, - "series": "audio", - "points": [ {"x": i, "y": v} for (i, v) in enumerate(data) ] - }, - headers={"Connection":"close"}) - except KeyboardInterrupt: - break - - stream.stop_stream() - stream.close() - p.terminate() - -def main(): - #sine() - # sine_array() - #star() - audio() +# stream.stop_stream() +# stream.close() +# p.terminate() if __name__ == "__main__": - main() + parser = ArgumentParser() + parser.add_argument("-t", action="store", dest="test", help="test type", type=str, choices=["sine", "sine_array", "star"], required=False, default="sin") + parser.add_argument("-n", action="store", dest="num", help="sin_array size/iteration", type=int, required=False, default=100) + parser.add_argument("-d", action="store", dest="delay", help="delay between iteration", type=float, required=False, default=0.25) + parser.add_argument("-c", action="store", dest="chart", help="chart id", type=int, required=True) + parser.add_argument("-s", action="store", dest="series", help="series name", type=str, required=True) + parser.add_argument("-p", action="store", dest="port", help="server port", type=int, required=False, default=8080) + parser.add_argument("-a", action="store", dest="address", help="server address", type=str, required=False, default="localhost") + + args = parser.parse_args() + if args.test == "sine": + sine(args) + elif args.test == "sine_array": + sine_array(args) + elif args.test == "star": + star(args) +