From b9d1470a6d1e2dc406707d3707e74710a8c2516f Mon Sep 17 00:00:00 2001 From: yuntang <123@qq.com> Date: Fri, 21 Mar 2025 17:28:36 +0800 Subject: [PATCH] 2025-03-21T17:28:35 --- src/AnalysTool.pro | 4 ++ src/data/filemanager.cpp | 2 +- src/data/filemanager.h | 2 +- src/mainwindow.cpp | 50 +++++++++++++--- src/mainwindow.h | 14 +++++ src/mainwindow.ui | 18 ++++++ src/rightwidget.cpp | 7 +++ src/rightwidget.h | 20 +++++++ src/ui/analysissettingform.cpp | 93 ++++++++++++++++++++++++++++++ src/ui/analysissettingform.h | 35 +++++++++++ src/ui/centralwidget.cpp | 102 ++++++++++++++++++++++++--------- src/ui/centralwidget.h | 11 +++- src/ui/draglinehandler.cpp | 11 ++++ src/ui/draglinehandler.h | 2 + src/ui/leftwidget.cpp | 2 +- src/ui/leftwidget.h | 2 +- 16 files changed, 335 insertions(+), 40 deletions(-) create mode 100644 src/rightwidget.cpp create mode 100644 src/rightwidget.h create mode 100644 src/ui/analysissettingform.cpp create mode 100644 src/ui/analysissettingform.h diff --git a/src/AnalysTool.pro b/src/AnalysTool.pro index 5f8ac66..2fe5441 100644 --- a/src/AnalysTool.pro +++ b/src/AnalysTool.pro @@ -18,6 +18,8 @@ DEFINES += QT_DEPRECATED_WARNINGS SOURCES += \ data/filemanager.cpp \ global.cpp \ + rightwidget.cpp \ + ui/analysissettingform.cpp \ ui/centralwidget.cpp \ main.cpp \ mainwindow.cpp \ @@ -33,6 +35,8 @@ HEADERS += \ data/filemanager.h \ defines.h \ global.h \ + rightwidget.h \ + ui/analysissettingform.h \ ui/centralwidget.h \ mainwindow.h \ serialport/dataparser.h \ diff --git a/src/data/filemanager.cpp b/src/data/filemanager.cpp index f30aa45..14556df 100644 --- a/src/data/filemanager.cpp +++ b/src/data/filemanager.cpp @@ -60,7 +60,7 @@ void test() writeExperimentFile(cd); } -void fileClose() +void close() { if(_expeFile.isOpen()){ _expeFile.close(); diff --git a/src/data/filemanager.h b/src/data/filemanager.h index 452928a..61b4cf7 100644 --- a/src/data/filemanager.h +++ b/src/data/filemanager.h @@ -29,7 +29,7 @@ extern QFile _expeFile; void createExperimentFile(); void writeExperimentFile(const CommonData&); -void fileClose(); +void close(); void readExperimentFile(const QString fileName,QVector&); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index d33c99f..d2e33ad 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -8,16 +8,25 @@ #include "filemanager.h" MainWindow::MainWindow(QWidget *parent) - : QMainWindow(parent), ui(new Ui::MainWindow), _centralWidget(new CentralWidget), _leftWidget(new LeftWidget), _expertmentSettingForm(new ExperimentSettingForm), _realTimeDataForm(new RealTimeDataForm) + : QMainWindow(parent), + ui(new Ui::MainWindow), + _centralWidget(new CentralWidget(this)), + _leftWidget(new LeftWidget(this)), + _expertmentSettingForm(new ExperimentSettingForm(this)), + _realTimeDataForm(new RealTimeDataForm(this)), + _rightWidget(new QDockWidget(this)) + ,_analysisSettingWidget(new AnalysisSettingForm(this)) + ,_contextMenu(new QMenu(this)) { ui->setupUi(this); - - setActionEnable(false); - this->setToolTip("....."); setCentralWidget(_centralWidget); addDockWidget(Qt::LeftDockWidgetArea, _leftWidget); + addDockWidget(Qt::RightDockWidgetArea, _rightWidget); + + _rightWidget->setWidget(_analysisSettingWidget); + _rightWidget->hide(); // ui->statusbar->showMessage("showMessage show temp message!"); // permenent show @@ -27,12 +36,31 @@ MainWindow::MainWindow(QWidget *parent) // connections(); // - _realTimeDataForm->show(); + _expertmentSettingForm->setWindowFlags(_expertmentSettingForm->windowFlags()| Qt::Dialog); + _realTimeDataForm->setWindowFlags(_realTimeDataForm->windowFlags()| Qt::Dialog); + + // _realTimeDataForm->show(); + setActionEnable(true); + } MainWindow::~MainWindow() { + // 删除非QObject派生类的对象 + // 假设 Ui::MainWindow 不是 QObject 派生类 delete ui; + + // 清理 status bar,如果它不是通过 new 创建的,则不需要删除 + if (_statusBar) { + delete _statusBar; + } + + FileManager::close(); +} + +void MainWindow::slotContextMenuShow(const QPoint point) +{ + _contextMenu->exec(point); } void MainWindow::connections() @@ -61,6 +89,13 @@ void MainWindow::connections() //analysis connect(_leftWidget,&LeftWidget::sigSendAnalysisFileName, _centralWidget,&CentralWidget::slotRecvAnalysisFileName); + connect(_centralWidget,&CentralWidget::sigSendLineXCoord, + _analysisSettingWidget,&AnalysisSettingForm::slotRecvLineXCoord); + + connect(_analysisSettingWidget,&AnalysisSettingForm::sigApply, + _centralWidget,&CentralWidget::slotAnalysisSettingApply); + connect(_analysisSettingWidget,&AnalysisSettingForm::sigConfirm, + _centralWidget,&CentralWidget::slotAnalysisSettingConfirm); } void MainWindow::setActionEnable(const bool flag) @@ -87,7 +122,7 @@ void MainWindow::on_actionStop_triggered() SerialPort::instance()->slotSendData(ba); // SerialPort::instance()->slotCloseSp(); - FileManager::fileClose(); + FileManager::close(); } void MainWindow::on_actionNew_triggered() @@ -146,5 +181,6 @@ void MainWindow::on_actionStopPoint_triggered() void MainWindow::on_actionNumericalLabel_triggered() { - + _rightWidget->show(); + _centralWidget->setAnalysisMode(CentralWidget::AnalysisMode::NumericalLabel); } diff --git a/src/mainwindow.h b/src/mainwindow.h index 1d5b85e..f1e2ddf 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -3,12 +3,15 @@ #include #include +#include #include "centralwidget.h" #include "leftwidget.h" #include "experimentsettingform.h" #include "realtimedataform.h" #include "dataparser.h" +#include "rightwidget.h" +#include "analysissettingform.h" QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } @@ -21,13 +24,21 @@ class MainWindow : public QMainWindow public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); + +public slots: + void slotContextMenuShow(const QPoint); private slots: + //experiment void on_actionConnectToDev_triggered(); void on_actionNew_triggered(); void on_actionStart_triggered(); void on_actionStop_triggered(); void on_actionReadOnly_triggered(); void on_actionRealTimeWidget_triggered(); + //analysis + void on_actionNumericalLabel_triggered(); + void on_actionStartPoint_triggered(); + void on_actionStopPoint_triggered(); private: void connections(); void setActionEnable(const bool); @@ -35,8 +46,11 @@ private: Ui::MainWindow *ui; CentralWidget *_centralWidget; LeftWidget*_leftWidget; + QDockWidget* _rightWidget; ExperimentSettingForm *_expertmentSettingForm; RealTimeDataForm* _realTimeDataForm; QStatusBar *_statusBar; + QMenu* _contextMenu; + AnalysisSettingForm* _analysisSettingWidget; }; #endif // MAINWINDOW_H diff --git a/src/mainwindow.ui b/src/mainwindow.ui index ab0fb22..3f48724 100644 --- a/src/mainwindow.ui +++ b/src/mainwindow.ui @@ -44,6 +44,9 @@ 分析 + + + @@ -148,6 +151,21 @@ 连接设备 + + + 起始点 + + + + + 终止点 + + + + + 数值标记 + + diff --git a/src/rightwidget.cpp b/src/rightwidget.cpp new file mode 100644 index 0000000..4a625e7 --- /dev/null +++ b/src/rightwidget.cpp @@ -0,0 +1,7 @@ +#include "rightwidget.h" + +RightWidget::RightWidget(QWidget *parent) : QDockWidget(parent) + ,_analysisSettingForm(new AnalysisSettingForm(this)) +{ + setWidget(_analysisSettingForm); +} diff --git a/src/rightwidget.h b/src/rightwidget.h new file mode 100644 index 0000000..6ef9206 --- /dev/null +++ b/src/rightwidget.h @@ -0,0 +1,20 @@ +#ifndef RIGHTWIDGET_H +#define RIGHTWIDGET_H + +#include + +#include "analysissettingform.h" + +class RightWidget : public QDockWidget +{ + Q_OBJECT +public: + explicit RightWidget(QWidget *parent = nullptr); + +signals: + +private: + AnalysisSettingForm *_analysisSettingForm; +}; + +#endif // RIGHTWIDGET_H diff --git a/src/ui/analysissettingform.cpp b/src/ui/analysissettingform.cpp new file mode 100644 index 0000000..e50eedf --- /dev/null +++ b/src/ui/analysissettingform.cpp @@ -0,0 +1,93 @@ +#include +#include +#include + +#include "analysissettingform.h" + +AnalysisSettingForm::AnalysisSettingForm(QWidget *parent) : + QWidget(parent) + ,_leftBorderSpinBox(new QDoubleSpinBox(this)) + ,_rightBorderSpinBox(new QDoubleSpinBox(this)) + ,_thresholdLineEdit(new QLineEdit(this)) + ,_applyButton(new QPushButton("应用",this)) + ,_undoButton(new QPushButton("撤销",this)) + ,_confirmButton(new QPushButton("确定",this)) + ,_cancelButton(new QPushButton("取消",this)) +{ + QVBoxLayout *layout = new QVBoxLayout(this); + // 设置间距和边距 + // layout->setSpacing(0); // 设置子布局之间的间距 + // layout->setContentsMargins(0, 0, 0, 0); // 设置布局的边距 + + QFormLayout *editLayout = new QFormLayout(); + editLayout->addRow("左边界:",_leftBorderSpinBox); + editLayout->addRow("有边界:",_rightBorderSpinBox); + editLayout->addRow("阈值:",_thresholdLineEdit); + + QGridLayout *buttonLayout = new QGridLayout(); + + buttonLayout->addWidget(_applyButton, 0, 0); // 第一行第一列 + buttonLayout->addWidget(_undoButton, 0, 1); // 第一行第二列 + buttonLayout->addWidget(_confirmButton, 1, 0); // 第二行第一列 + buttonLayout->addWidget(_cancelButton, 1, 1); // 第二行第二列 + // buttonLayout->setSpacing(0); + + // 创建一个空白行 + QSpacerItem *spacer = + new QSpacerItem(20, 20, QSizePolicy::Minimum, QSizePolicy::Expanding); + + // 将 editLayout 和 buttonLayout 添加到主布局,并在它们之间添加空白行 + layout->addLayout(editLayout); + layout->addSpacerItem(spacer); + layout->addLayout(buttonLayout); + layout->addLayout(editLayout); + // layout->setSpacing(1); + layout->addLayout(buttonLayout); + layout->addStretch(1); + // + connect(_applyButton,&QPushButton::click, + this,&AnalysisSettingForm::slotApply); + connect(_confirmButton,&QPushButton::click, + this,&AnalysisSettingForm::slotConfirm); + connect(_undoButton,&QPushButton::click, + this,&AnalysisSettingForm::slotUndo); + connect(_cancelButton,&QPushButton::click, + this,&AnalysisSettingForm::slotCancel); + // + _leftBorderSpinBox->setRange(0.0, 10000.0); // 设置范围 + _rightBorderSpinBox->setRange(0.0, 10000.0); // 设置范围 +} + +AnalysisSettingForm::~AnalysisSettingForm() +{ + +} + +void AnalysisSettingForm::slotRecvLineXCoord(const int index, const double point) +{ + if(1 == index){ + _leftBorderSpinBox->setValue(point); + }else{ + _rightBorderSpinBox->setValue(point); + } +} + +void AnalysisSettingForm::slotApply() +{ + +} + +void AnalysisSettingForm::slotConfirm() +{ + +} + +void AnalysisSettingForm::slotUndo() +{ + +} + +void AnalysisSettingForm::slotCancel() +{ + +} diff --git a/src/ui/analysissettingform.h b/src/ui/analysissettingform.h new file mode 100644 index 0000000..22ff8c8 --- /dev/null +++ b/src/ui/analysissettingform.h @@ -0,0 +1,35 @@ +#ifndef ANALYSISSETTINGFORM_H +#define ANALYSISSETTINGFORM_H + +#include +#include +#include +#include + +class AnalysisSettingForm : public QWidget +{ + Q_OBJECT + +public: + explicit AnalysisSettingForm(QWidget *parent = nullptr); + ~AnalysisSettingForm(); +signals: + void sigApply(); + void sigConfirm(); +public slots: + void slotRecvLineXCoord(const int,const double); +private slots: + void slotApply(); + void slotConfirm(); + void slotUndo(); + void slotCancel(); +private: + QDoubleSpinBox *_leftBorderSpinBox,*_rightBorderSpinBox; + QLineEdit *_thresholdLineEdit; + QPushButton *_applyButton, + *_confirmButton, + *_undoButton, + *_cancelButton; +}; + +#endif // ANALYSISSETTINGFORM_H diff --git a/src/ui/centralwidget.cpp b/src/ui/centralwidget.cpp index 5e0a3b4..b887984 100644 --- a/src/ui/centralwidget.cpp +++ b/src/ui/centralwidget.cpp @@ -7,11 +7,15 @@ CentralWidget::CentralWidget(QWidget *parent) : QWidget(parent), _customPlot(new QCustomPlot) + ,_nanlysisMode(AnalysisMode::None) { + setMouseTracking(true); + setStyleSheet("background-color: lightgray;"); resize(888, 666); QHBoxLayout *layout = new QHBoxLayout(); + layout->setMargin(0); layout->addWidget(_customPlot); this->setLayout(layout); @@ -29,9 +33,15 @@ CentralWidget::CentralWidget(QWidget *parent) _line2->setSelectable(true); // 安装事件过滤器 - _eventHandler = new DragLineHandler(_customPlot, _line1, _line2, _graph,nullptr); + _graph = _customPlot->addGraph(0); + _eventHandler = new DragLineHandler(_customPlot, _line1, _line2, _graph, nullptr); _customPlot->installEventFilter(_eventHandler); + connect(_eventHandler,&DragLineHandler::sigSendLineXCoord, + this,&CentralWidget::sigSendLineXCoord); + + setEventHandlerEnable(false); + // #if 0 // init data QVector x(101), y(101); @@ -54,12 +64,28 @@ CentralWidget::CentralWidget(QWidget *parent) CentralWidget::~CentralWidget() { - FileManager::fileClose(); + FileManager::close(); } -void CentralWidget::setAnalysisMode(const CentralWidget::AnalysisMode) +void CentralWidget::setAnalysisMode(const CentralWidget::AnalysisMode mode) { + if(_nanlysisMode == mode){ + return; + }else{ + _nanlysisMode = mode; + } + switch (mode) + { + case AnalysisMode::NumericalLabel: + setEventHandlerEnable(true); + break; + case AnalysisMode::None: + setEventHandlerEnable(false); + break; + default: + break; + } } void CentralWidget::slotModeModify(const Global::Mode mode) @@ -78,14 +104,15 @@ void CentralWidget::slotModeModify(const Global::Mode mode) } else if (Global::Mode::Analysis == mode) { - qDebug()<<"file close..."; - FileManager::fileClose(); + qDebug() << "file close..."; + FileManager::close(); } } void CentralWidget::slotRecvCommonData(const CommonData &cd) { - qDebug()<<"slotRevCommonData"; + qDebug() << "slotRevCommonData"; + _customPlot->graph(0)->addData(cd.sample_temp, cd.dsc); // 添加数据到曲线 _customPlot->replot(); // @@ -94,19 +121,23 @@ void CentralWidget::slotRecvCommonData(const CommonData &cd) void CentralWidget::slotRecvAnalysisFileName(const QString &fileName) { - qDebug()<<"slotRecvAnalysisFileName"< dataVtr; - FileManager::readExperimentFile(fileName,dataVtr); + FileManager::readExperimentFile(fileName, dataVtr); - if(dataVtr.size() < 0){ + if (dataVtr.size() < 0) + { return; } + +#if 0 //判断界面上是不是有曲线,有的话先删除。 _customPlot->clearGraphs(); // 创建画布,设置画布上的点数据 _customPlot->addGraph(); +#endif // 设置坐标轴标签 _customPlot->yAxis->setLabel("DSC/mW"); @@ -115,16 +146,34 @@ void CentralWidget::slotRecvAnalysisFileName(const QString &fileName) _customPlot->xAxis->setRange(0, 400); _customPlot->yAxis->setRange(-20, 20); - QVector xVtr,yVtr; - for(FileManager::ExperimentData &ed:dataVtr){ + QVector xVtr, yVtr; + for (FileManager::ExperimentData &ed : dataVtr) + { xVtr.push_back(ed.sampleTemp); yVtr.push_back(ed.dsc); } - _customPlot->graph(0)->addData(xVtr,yVtr); + // 清除第一个图表上的数据 + if (_customPlot->graphCount() > 0 && _graph) + { + // 清除第一个图表上的数据 + _graph->setData(QVector(), QVector()); + } + + _graph->addData(xVtr, yVtr); _customPlot->replot(); } +void CentralWidget::slotAnalysisSettingApply() +{ + +} + +void CentralWidget::slotAnalysisSettingConfirm() +{ + +} + void CentralWidget::timerEvent(QTimerEvent *event) { // key的单位是秒 @@ -151,22 +200,19 @@ void CentralWidget::timerEvent(QTimerEvent *event) _customPlot->replot(); } -void CentralWidget::analysisMode(const bool flag) +void CentralWidget::contextMenuEvent(QContextMenuEvent *event) { - _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 + qDebug()<<"left menu..."; + QPoint point = event->globalPos(); + emit sigContextMenuShow(point); } +void CentralWidget::setEventHandlerEnable(const bool flag) +{ + _eventHandler->setEnable(flag); + _line1->setVisible(flag); + if(AnalysisMode::NumericalLabel != _nanlysisMode){ + _line2->setVisible(flag); + } + _customPlot->replot(); +} diff --git a/src/ui/centralwidget.h b/src/ui/centralwidget.h index e2a9c1c..7a3f0d2 100644 --- a/src/ui/centralwidget.h +++ b/src/ui/centralwidget.h @@ -2,6 +2,7 @@ #define CENTRALWIDGET_H #include +#include #include "qcustomplot.h" #include "protocol.h" @@ -22,16 +23,24 @@ public: ~CentralWidget(); void setAnalysisMode(const AnalysisMode); +signals: + void sigContextMenuShow(const QPoint); + void sigSendLineXCoord(const int,const double); public slots: void slotModeModify(const Global::Mode); void slotRecvCommonData(const CommonData&); void slotRecvAnalysisFileName(const QString&); + //analysis setting + void slotAnalysisSettingApply(); + void slotAnalysisSettingConfirm(); protected: void timerEvent(QTimerEvent* event); + void contextMenuEvent(QContextMenuEvent *event); private: - void analysisMode(const bool); + void setEventHandlerEnable(const bool); private: + AnalysisMode _nanlysisMode; QCustomPlot *_customPlot; QCPGraph* _graph; DragLineHandler* _eventHandler; diff --git a/src/ui/draglinehandler.cpp b/src/ui/draglinehandler.cpp index d7dff3a..0c16af5 100644 --- a/src/ui/draglinehandler.cpp +++ b/src/ui/draglinehandler.cpp @@ -12,12 +12,16 @@ DragLineHandler::DragLineHandler(QCustomPlot *plot, QCPItemStraightLine *line1, bool DragLineHandler::eventFilter(QObject *obj, QEvent *event) { if(!_enableFlag){ + qDebug()<<"_enableFlag false."; return false; } + if (obj == mPlot) { if (event->type() == QEvent::MouseMove) { + qDebug()<<"mouse move..."; + QMouseEvent *mouseEvent = static_cast(event); QPoint mousePos = mouseEvent->pos(); qDebug()<<"x:"<xAxis->pixelToCoord(mousePos.x()); + if(mDraggingLine == mLine1){ + emit sigSendLineXCoord(1,x); + }else{ + emit sigSendLineXCoord(2,x); + } mDraggingLine->point1->setCoords(x, mPlot->yAxis->range().lower); mDraggingLine->point2->setCoords(x, mPlot->yAxis->range().upper); mPlot->replot(); @@ -49,6 +58,8 @@ bool DragLineHandler::eventFilter(QObject *obj, QEvent *event) } else if (event->type() == QEvent::MouseButtonPress) { + qDebug()<<"mouse press..."; + QMouseEvent *mouseEvent = static_cast(event); if (mouseEvent->button() == Qt::LeftButton) { diff --git a/src/ui/draglinehandler.h b/src/ui/draglinehandler.h index 275a507..b3afb33 100644 --- a/src/ui/draglinehandler.h +++ b/src/ui/draglinehandler.h @@ -18,6 +18,8 @@ public: QCPGraph*graph,QObject *parent); void setEnable(const bool flag){_enableFlag = flag;} +signals: + void sigSendLineXCoord(const int,const double); protected: bool eventFilter(QObject *obj, QEvent *event) override; private: diff --git a/src/ui/leftwidget.cpp b/src/ui/leftwidget.cpp index 1354d22..2977b91 100644 --- a/src/ui/leftwidget.cpp +++ b/src/ui/leftwidget.cpp @@ -6,7 +6,7 @@ #include "filemanager.h" #include "global.h" -LeftWidget::LeftWidget() +LeftWidget::LeftWidget(QWidget *parent ): QDockWidget(parent) { // setStyleSheet("background-color: yellow;"); diff --git a/src/ui/leftwidget.h b/src/ui/leftwidget.h index 3bf3b61..e226628 100644 --- a/src/ui/leftwidget.h +++ b/src/ui/leftwidget.h @@ -10,7 +10,7 @@ class LeftWidget:public QDockWidget { Q_OBJECT public: - LeftWidget(); + LeftWidget(QWidget *parent = nullptr); private: void initData(); void initFileName(QTreeWidgetItem*,const QString &folderPath);