diff --git a/src/AnalysTool.pro b/src/AnalysTool.pro index 9cffb55..5f8ac66 100644 --- a/src/AnalysTool.pro +++ b/src/AnalysTool.pro @@ -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 diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index c753a8b..d33c99f 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -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() +{ + +} diff --git a/src/ui/centralwidget.cpp b/src/ui/centralwidget.cpp index 67208d2..5e0a3b4 100644 --- a/src/ui/centralwidget.cpp +++ b/src/ui/centralwidget.cpp @@ -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 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 +} + diff --git a/src/ui/centralwidget.h b/src/ui/centralwidget.h index 6fb3606..e2a9c1c 100644 --- a/src/ui/centralwidget.h +++ b/src/ui/centralwidget.h @@ -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 diff --git a/src/ui/draglinehandler.cpp b/src/ui/draglinehandler.cpp new file mode 100644 index 0000000..d7dff3a --- /dev/null +++ b/src/ui/draglinehandler.cpp @@ -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(event); + QPoint mousePos = mouseEvent->pos(); + qDebug()<<"x:"<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(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 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 &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); +} diff --git a/src/ui/draglinehandler.h b/src/ui/draglinehandler.h new file mode 100644 index 0000000..275a507 --- /dev/null +++ b/src/ui/draglinehandler.h @@ -0,0 +1,35 @@ +#ifndef DRAGlINEHANDLER_H +#define DRAGlINEHANDLER_H + +#include +#include +#include +#include +#include +#include + +#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 &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 diff --git a/src/ui/leftwidget.cpp b/src/ui/leftwidget.cpp index b0a4f57..1354d22 100644 --- a/src/ui/leftwidget.cpp +++ b/src/ui/leftwidget.cpp @@ -38,7 +38,7 @@ LeftWidget::LeftWidget() expandAll(_analysisStateItem); //connections - connect(_treeWidget,&QTreeWidget::itemClicked, + connect(_treeWidget,&QTreeWidget::itemDoubleClicked, this,&LeftWidget::slotTreeWidgetItemClicked); }