2025-03-20T17:28:21

This commit is contained in:
yuntang 2025-03-20 17:28:22 +08:00
parent d985a8b39e
commit 0cc7c09f15
7 changed files with 226 additions and 2 deletions

View File

@ -24,6 +24,7 @@ SOURCES += \
serialport/dataparser.cpp \
serialport/serialport.cpp \
thirdparty/qcustomplot/qcustomplot.cpp \
ui/draglinehandler.cpp \
ui/experimentsettingform.cpp \
ui/leftwidget.cpp \
ui/realtimedataform.cpp
@ -38,6 +39,7 @@ HEADERS += \
serialport/protocol.h \
serialport/serialport.h \
thirdparty/qcustomplot/qcustomplot.h \
ui/draglinehandler.h \
ui/experimentsettingform.h \
ui/leftwidget.h \
ui/realtimedataform.h

View File

@ -133,3 +133,18 @@ void MainWindow::on_actionConnectToDev_triggered()
QMessageBox::warning(this, "warnning", "Serial Port open failed.");
}
}
void MainWindow::on_actionStartPoint_triggered()
{
}
void MainWindow::on_actionStopPoint_triggered()
{
}
void MainWindow::on_actionNumericalLabel_triggered()
{
}

View File

@ -15,6 +15,23 @@ CentralWidget::CentralWidget(QWidget *parent)
layout->addWidget(_customPlot);
this->setLayout(layout);
// 创建两条竖线
_line1 = new QCPItemStraightLine(_customPlot);
_line1->point1->setCoords(20, _customPlot->yAxis->range().lower);
_line1->point2->setCoords(20, _customPlot->yAxis->range().upper);
_line1->setPen(QPen(Qt::red));
_line1->setSelectable(true);
_line2 = new QCPItemStraightLine(_customPlot);
_line2->point1->setCoords(40, _customPlot->yAxis->range().lower);
_line2->point2->setCoords(40, _customPlot->yAxis->range().upper);
_line2->setPen(QPen(Qt::red));
_line2->setSelectable(true);
// 安装事件过滤器
_eventHandler = new DragLineHandler(_customPlot, _line1, _line2, _graph,nullptr);
_customPlot->installEventFilter(_eventHandler);
#if 0
// init data
QVector<double> x(101), y(101);
@ -40,12 +57,17 @@ CentralWidget::~CentralWidget()
FileManager::fileClose();
}
void CentralWidget::setAnalysisMode(const CentralWidget::AnalysisMode)
{
}
void CentralWidget::slotModeModify(const Global::Mode mode)
{
if (Global::Mode::ExperimentStart == mode)
{
// 创建画布,设置画布上的点数据
_customPlot->addGraph();
_graph = _customPlot->addGraph();
// 设置坐标轴标签
_customPlot->xAxis->setLabel("Temp/℃");
@ -129,3 +151,22 @@ void CentralWidget::timerEvent(QTimerEvent *event)
_customPlot->replot();
}
void CentralWidget::analysisMode(const bool flag)
{
_line1->setVisible(flag);
_line2->setVisible(flag);
_eventHandler->setEnable(flag);
#if 0
if(flag){
_line1->setVisible(true);
_line2->setVisible(true);
_eventHandler->setEnable(true);
}else{
_line1->setVisible(true);
_line2->setVisible(true);
_eventHandler->setEnable(true);
}
#endif
}

View File

@ -6,13 +6,22 @@
#include "qcustomplot.h"
#include "protocol.h"
#include "global.h"
#include "draglinehandler.h"
class CentralWidget:public QWidget
{
Q_OBJECT
public:
enum AnalysisMode{
None,
NumericalLabel,
StartPoint,
StopPoint,
};
CentralWidget(QWidget *parent = nullptr);
~CentralWidget();
void setAnalysisMode(const AnalysisMode);
public slots:
void slotModeModify(const Global::Mode);
void slotRecvCommonData(const CommonData&);
@ -20,8 +29,13 @@ public slots:
protected:
void timerEvent(QTimerEvent* event);
private:
void analysisMode(const bool);
private:
QCustomPlot *_customPlot;
QCPGraph* _graph;
DragLineHandler* _eventHandler;
QCPItemStraightLine *_line1,*_line2;
};
#endif // CENTRALWIDGET_H

117
src/ui/draglinehandler.cpp Normal file
View File

@ -0,0 +1,117 @@
#include "draglinehandler.h"
DragLineHandler::DragLineHandler(QCustomPlot *plot, QCPItemStraightLine *line1,
QCPItemStraightLine *line2, QCPGraph*graph,
QObject *parent)
: QObject(parent), mPlot(plot), mLine1(line1), mLine2(line2),mGraph(graph)
,_enableFlag(false)
{
mPlot->setInteractions(QCP::iSelectItems);
}
bool DragLineHandler::eventFilter(QObject *obj, QEvent *event)
{
if(!_enableFlag){
return false;
}
if (obj == mPlot)
{
if (event->type() == QEvent::MouseMove)
{
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
QPoint mousePos = mouseEvent->pos();
qDebug()<<"x:"<<mousePos.x();
bool nearLine1 = isNearLine(mLine1, mousePos);
bool nearLine2 = isNearLine(mLine2, mousePos);
if (nearLine1 || nearLine2)
{
mPlot->setCursor(Qt::SplitHCursor);
qDebug()<<"mIconLabel visiable.";
}
else
{
qDebug()<<"mIconLabel not visiable.";
mPlot->setCursor(Qt::ArrowCursor);
}
if (mDraggingLine)
{
double x = mPlot->xAxis->pixelToCoord(mousePos.x());
mDraggingLine->point1->setCoords(x, mPlot->yAxis->range().lower);
mDraggingLine->point2->setCoords(x, mPlot->yAxis->range().upper);
mPlot->replot();
updateSelectedRegion();
}
}
else if (event->type() == QEvent::MouseButtonPress)
{
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
if (mouseEvent->button() == Qt::LeftButton)
{
QPoint mousePos = mouseEvent->pos();
if (isNearLine(mLine1, mousePos))
{
mDraggingLine = mLine1;
mLine1->setSelected(true);
}
else if (isNearLine(mLine2, mousePos))
{
mDraggingLine = mLine2;
mLine2->setSelected(true);
}
}
}
else if (event->type() == QEvent::MouseButtonRelease)
{
if (mDraggingLine)
{
mDraggingLine->setSelected(false);
mDraggingLine = nullptr;
}
}
}
return QObject::eventFilter(obj, event);
}
bool DragLineHandler::isNearLine(QCPItemStraightLine *line, const QPoint &pos)
{
double lineX = line->point1->coords().x();
int linePixelX = mPlot->xAxis->coordToPixel(lineX);
return qAbs(pos.x() - linePixelX) < 5;
}
void DragLineHandler::updateSelectedRegion()
{
double x1 = mLine1->point1->coords().x();
double x2 = mLine2->point1->coords().x();
if (x1 > x2)
std::swap(x1, x2);
// 获取曲线数据
QVector<double> xData, yData;
// mGraph->data()->data(xData, yData);
auto dataContainer = mGraph->data();
for (auto it = dataContainer->begin(); it != dataContainer->end(); ++it)
{
xData.append(it->key);
yData.append(it->value);
}
// 查找对应x位置
int index1 = findClosestIndex(xData, x1);
int index2 = findClosestIndex(xData, x2);
qDebug() << "Selected region: [" << xData[index1] << ", " << xData[index2] << "]";
}
int DragLineHandler::findClosestIndex(const QVector<double> &data, double target)
{
auto it = std::min_element(data.begin(), data.end(), [target](double a, double b) {
return qAbs(a - target) < qAbs(b - target);
});
return std::distance(data.begin(), it);
}

35
src/ui/draglinehandler.h Normal file
View File

@ -0,0 +1,35 @@
#ifndef DRAGlINEHANDLER_H
#define DRAGlINEHANDLER_H
#include <QApplication>
#include <QMouseEvent>
#include <QDebug>
#include <QPixmap>
#include <QLabel>
#include <QCursor>
#include "qcustomplot.h"
class DragLineHandler : public QObject
{
Q_OBJECT
public:
DragLineHandler(QCustomPlot *plot, QCPItemStraightLine *line1, QCPItemStraightLine *line2,
QCPGraph*graph,QObject *parent);
void setEnable(const bool flag){_enableFlag = flag;}
protected:
bool eventFilter(QObject *obj, QEvent *event) override;
private:
int findClosestIndex(const QVector<double> &data, double target);
void updateSelectedRegion();
bool isNearLine(QCPItemStraightLine *line, const QPoint &pos);
private:
bool _enableFlag;
QCustomPlot *mPlot;
QCPItemStraightLine *mLine1, *mLine2;
QCPItemStraightLine *mDraggingLine = nullptr;
QCPGraph *mGraph;
};
#endif

View File

@ -38,7 +38,7 @@ LeftWidget::LeftWidget()
expandAll(_analysisStateItem);
//connections
connect(_treeWidget,&QTreeWidget::itemClicked,
connect(_treeWidget,&QTreeWidget::itemDoubleClicked,
this,&LeftWidget::slotTreeWidgetItemClicked);
}