From fc5eb437d04475ce69aead599d0d12e0f109e89c Mon Sep 17 00:00:00 2001 From: guangzong chen Date: Fri, 10 Jun 2022 19:19:38 -0400 Subject: [PATCH] update chart zoom in and out --- CMakeLists.txt | 2 +- GChartView.cpp | 83 +++++++++++++++++++++++++++++++++++++++++++++++++ GChartView.h | 38 ++++++++++++++++++++++ main.ui | 12 +++---- main_window.cpp | 58 +++++++++------------------------- main_window.h | 30 +++++++++--------- 6 files changed, 157 insertions(+), 66 deletions(-) create mode 100644 GChartView.cpp create mode 100644 GChartView.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 0812caf..5cae0f8 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,7 +15,7 @@ find_package(Qt6 COMPONENTS Charts REQUIRED) -add_executable(com main.cpp main_window.cpp main_window.h) +add_executable(com main.cpp main_window.cpp main_window.h GChartView.cpp GChartView.h) #add_executable(untitled WIN32 main.cpp main_window.cpp main_window.h) target_link_libraries(com Qt::Core diff --git a/GChartView.cpp b/GChartView.cpp new file mode 100644 index 0000000..db59013 --- /dev/null +++ b/GChartView.cpp @@ -0,0 +1,83 @@ +// +// Created by zong on 6/10/22. +// + +// You may need to build the project (run Qt uic code generator) to get "ui_gchartview.h" resolved + +#include "GChartView.h" + + +GChartView::GChartView(QWidget *parent) : QChartView(parent) { + data = new QLineSeries(); + axis_x = new QValueAxis(); + axis_y = new QValueAxis(); + chart = new QChart(); + chart->setTitle("WIP"); + axis_x->setRange(0, 5); + axis_y->setRange(-2.5, 2.5); + axis_x->setTickType(QValueAxis::TickType::TicksDynamic); + axis_x->setTickInterval(1); + axis_x->setLabelFormat("%d"); + axis_y->setTickCount(21); + chart->addAxis(axis_x, Qt::AlignBottom); + chart->addAxis(axis_y, Qt::AlignLeft); + chart->addSeries(data); + this->setChart(chart); + data->attachAxis(axis_x); + data->attachAxis(axis_y); +} + +GChartView::~GChartView() { +} + +void GChartView::wheelEvent(QWheelEvent *event) { + auto pixels = event->pixelDelta(); + auto degrees = event->angleDelta(); + auto cursor_y = event->position().y(); + auto cur_range_max = axis_y->max(); + auto cur_range_min = axis_y->min(); + + // left and top corner is origin + auto pos_max = chart->mapToPosition(QPointF(0, cur_range_min)).y(); + auto pos_min = chart->mapToPosition(QPointF(0, cur_range_max)).y(); + auto new_range = 5.0; + auto center = (qreal) 0.0; + if (cursor_y < pos_max && cursor_y > pos_min) { + center = chart->mapToValue(QPointF(0, cursor_y)).y(); + auto cur_range = (cur_range_max - cur_range_min); + if (pixels.y() > 0) + new_range = cur_range * 0.8; + else + new_range = cur_range / 0.8; + } + // setup new axis + auto new_max = (center + new_range / 2.0); + auto new_min = (center - new_range / 2.0); + if (new_max > 2.5) { + new_min -= (new_max - 2.5); + new_max = 2.5; + } + if (new_min < -2.5) { + new_max += (-2.5 - new_min); + new_min = 2.5; + new_max = new_max > 2.5 ? 2.5 : new_max; + } + + axis_y->setRange(new_min, new_max); + qDebug() << new_min << " " << new_max; + + +} + +void GChartView::append(qreal x, qreal y) { + data->append(x, y); + while (data->count() > 5000) { + data->remove(0); + } + auto beg = data->at(0); + auto end = data->at(data->count()); + if (end.x() > 5) { + axis_x->setRange(beg.x(), end.x()); + } + +} diff --git a/GChartView.h b/GChartView.h new file mode 100644 index 0000000..75ec7ae --- /dev/null +++ b/GChartView.h @@ -0,0 +1,38 @@ +// +// Created by zong on 6/10/22. +// + +#ifndef COM_GCHARTVIEW_H +#define COM_GCHARTVIEW_H + +#include + +#include +#include +#include + +QT_BEGIN_NAMESPACE +namespace Ui { class gchartview; } +QT_END_NAMESPACE + +class GChartView : public QChartView { +Q_OBJECT + +public: + explicit GChartView(QWidget *parent = nullptr); + ~GChartView() override; + void wheelEvent(QWheelEvent *even) override; + +private: + QLineSeries* data; + QValueAxis* axis_x; + QValueAxis* axis_y; + QChart* chart; +public: + + void append(qreal x, qreal y); + +}; + + +#endif //COM_GCHARTVIEW_H diff --git a/main.ui b/main.ui index 70ac1c3..cb6175d 100755 --- a/main.ui +++ b/main.ui @@ -6,8 +6,8 @@ 0 0 - 1089 - 943 + 2028 + 1035 @@ -160,7 +160,7 @@ - + 600 @@ -177,7 +177,7 @@ 0 0 - 1089 + 2028 31 @@ -186,9 +186,9 @@ - QChartView + GChartView QGraphicsView -
qchartview.h
+
GChartView.h
diff --git a/main_window.cpp b/main_window.cpp index 1531c86..8ddd624 100755 --- a/main_window.cpp +++ b/main_window.cpp @@ -42,8 +42,6 @@ void main_window::config_enable(bool ena) { receivefileStream.setDevice(&receivefile); rawDataFile.setFileName(filename + "raw"); rawDataFile.open(QIODevice::Append); - - } } @@ -69,35 +67,6 @@ void main_window::setUiComponent() { QObject::connect(portOpenBtn, SIGNAL(clicked()), this, SLOT(open_port())); QObject::connect(portUpdBtn, SIGNAL(clicked()), this, SLOT(update_ui_port())); - lineChart.chart->setTitle("WIP"); - lineChart.axis_x->setRange(0, 5); - lineChart.axis_y->setRange(-2.5, 2.5); - lineChart.axis_x->setTickCount(11); - lineChart.axis_y->setTickCount(11); - lineChart.chart->addAxis(lineChart.axis_x, Qt::AlignBottom); - lineChart.chart->addAxis(lineChart.axis_y, Qt::AlignLeft); - lineChart.chart->addSeries(lineChart.data); - plotChartView->setChart(lineChart.chart); - lineChart.data->attachAxis(lineChart.axis_x); - lineChart.data->attachAxis(lineChart.axis_y); - -// linedata = new QLineSeries(); -// chart = new QChart(); -// chart->setTitle("WIP"); -// QValueAxis *axis_x = new QValueAxis(); -// axis_x->setRange(0, 5); -// axis_x->setTickCount(11); -// QValueAxis *axis_y = new QValueAxis(); -// axis_y->setRange(-2.5, 2.5); -// axis_y->setTickCount(11); -// chart->legend()->hide(); -// chart->addAxis(axis_x, Qt::AlignBottom); -// chart->addAxis(axis_y, Qt::AlignLeft); -// chart->addSeries(linedata); -// plotChartView->setChart(chart); -// linedata->attachAxis(axis_x); -// linedata->attachAxis(axis_y); - } @@ -140,7 +109,7 @@ main_window::main_window(QWidget *parent) : QMainWindow(parent) { } main_window::~main_window() { -// delete ui; + delete ui; } void main_window::update_ui_port() { @@ -154,7 +123,7 @@ void main_window::open_port() { if (port.isOpen()) { port.close(); config_enable(true); - statusbar->showMessage("close port success"); + statusbar->showMessage("close port success", 500); return; } if (portsComboBox->count() == 0) { return; } @@ -166,7 +135,7 @@ void main_window::open_port() { else if (stopbits == QString("1.5")) port.setStopBits(QSerialPort::OneAndHalfStop); else if (stopbits == QString("2")) port.setStopBits(QSerialPort::TwoStop); else { - box.setText("wrong stopbits settings"), box.exec(); + msgBox.setText("wrong stopbits settings"), msgBox.exec(); return; } // TODO: read info from config @@ -176,7 +145,7 @@ void main_window::open_port() { port.setFlowControl(QSerialPort::NoFlowControl); if (port.open(QIODevice::ReadWrite)) { config_enable(false); - statusbar->showMessage("open port success"); + statusbar->showMessage("open port success", 500); } else { config_enable(true); } @@ -190,7 +159,7 @@ bool first = true; void main_window::read_data() { - static int receive_num_cnt = 0; + static int receive_num_cnt = -2; static int receive_byte_cnt = 0; static int val = 0; auto data = port.readAll(); @@ -217,37 +186,38 @@ void main_window::read_data() { data = first_frame; first = false; } + // TODO: BUG int data overflow rawDataFile.write(data); for (int i = 0; i < data.size(); i++) { if (receive_byte_cnt % 4 == 0) { if (data[i] != 0) { - // TODO: raise error + msgBox.setText("Error occurred, please restart transmission."); + msgBox.exec(); return; } receive_num_cnt++; + if (receive_num_cnt == -1) + continue; int real_number = val; if (real_number & 0x800000) { - // negtive number + // negative number real_number = -(~real_number & 0x7fffff + 1); } receivefileStream << real_number << " "; double scaled = (double) real_number / (double) (0x7fffff) * 2.5; - lineChart.data->append((double) receive_byte_cnt / 1000.0, scaled); - while (lineChart.data->count() > 5000) { - // data more than 5000 second, remove first data - lineChart.data->remove(0); - } + plotChartView->append ((double) receive_num_cnt / 1000.0, scaled); val = 0; continue; } val <<= 8; val |= data[i]; - receive_byte_cnt++; } QString new_text = QString(data.toHex(' ').toUpper()) + " "; hexTextEdit->moveCursor(QTextCursor::End); hexTextEdit->insertPlainText(new_text); auto tmp = hexTextEdit->toPlainText(); + asciiTextEdit->moveCursor(QTextCursor::End); + asciiTextEdit->insertPlainText(QString(data)); if (tmp.length() > 1e3) { hexTextEdit->setPlainText(tmp.last((int) 1e3)); hexTextEdit->moveCursor(QTextCursor::End); diff --git a/main_window.h b/main_window.h index b4b7923..ded134e 100755 --- a/main_window.h +++ b/main_window.h @@ -14,18 +14,18 @@ #include #include -typedef struct _Glinechart{ - QLineSeries* data; - QValueAxis* axis_x; - QValueAxis* axis_y; - QChart* chart; - _Glinechart() { - data = new QLineSeries(); - axis_x = new QValueAxis(); - axis_y = new QValueAxis(); - chart = new QChart(); - } -}GLineChart; +//typedef struct _Glinechart{ +// QLineSeries* data; +// QValueAxis* axis_x; +// QValueAxis* axis_y; +// QChart* chart; +// _Glinechart() { +// data = new QLineSeries(); +// axis_x = new QValueAxis(); +// axis_y = new QValueAxis(); +// chart = new QChart(); +// } +//}GLineChart; class main_window : public QMainWindow { Q_OBJECT @@ -62,10 +62,10 @@ private: QTextEdit *asciiTextEdit; QMenuBar *menubar; QStatusBar *statusbar; - QChartView *plotChartView; + GChartView *plotChartView; - QMessageBox box; - GLineChart lineChart; + QMessageBox msgBox; +// GLineChart lineChart; private: void setUiComponent();