2025-03-24T17:30:41

This commit is contained in:
yuntang 2025-03-24 17:30:42 +08:00
parent b9d1470a6d
commit 0fb8219dd9
11 changed files with 6271 additions and 53 deletions

File diff suppressed because it is too large Load Diff

View File

@ -50,11 +50,6 @@ MainWindow::~MainWindow()
// 假设 Ui::MainWindow 不是 QObject 派生类
delete ui;
// 清理 status bar如果它不是通过 new 创建的,则不需要删除
if (_statusBar) {
delete _statusBar;
}
FileManager::close();
}
@ -96,6 +91,10 @@ void MainWindow::connections()
_centralWidget,&CentralWidget::slotAnalysisSettingApply);
connect(_analysisSettingWidget,&AnalysisSettingForm::sigConfirm,
_centralWidget,&CentralWidget::slotAnalysisSettingConfirm);
connect(_analysisSettingWidget,&AnalysisSettingForm::sigUndo,
_centralWidget,&CentralWidget::slotAnalysisSettingUndo);
connect(_analysisSettingWidget,&AnalysisSettingForm::sigCancel,
_centralWidget,&CentralWidget::slotAnalysisSettingCancel);
}
void MainWindow::setActionEnable(const bool flag)
@ -120,9 +119,10 @@ void MainWindow::on_actionStop_triggered()
{
QByteArray ba = DataParser::setDeviceStartStop(DeviceStartMode::Stop);
SerialPort::instance()->slotSendData(ba);
// SerialPort::instance()->slotCloseSp();
FileManager::close();
Global::instance()->setMode(Global::Mode::Analysis);
}
void MainWindow::on_actionNew_triggered()
@ -140,6 +140,8 @@ void MainWindow::on_actionStart_triggered()
SerialPort::instance()->slotSendData(ba);
//
FileManager::createExperimentFile();
Global::instance()->setMode(Global::Mode::ExperimentStart);
}
void MainWindow::on_actionReadOnly_triggered()
@ -184,3 +186,10 @@ void MainWindow::on_actionNumericalLabel_triggered()
_rightWidget->show();
_centralWidget->setAnalysisMode(CentralWidget::AnalysisMode::NumericalLabel);
}
void MainWindow::on_actionPeakSynthesisAnalysis_triggered()
{
//峰综合分析
_rightWidget->show();
_centralWidget->setAnalysisMode(CentralWidget::AnalysisMode::PeakSynthesisAnalysis);
}

View File

@ -39,6 +39,8 @@ private slots:
void on_actionNumericalLabel_triggered();
void on_actionStartPoint_triggered();
void on_actionStopPoint_triggered();
void on_actionPeakSynthesisAnalysis_triggered();
private:
void connections();
void setActionEnable(const bool);
@ -49,7 +51,6 @@ private:
QDockWidget* _rightWidget;
ExperimentSettingForm *_expertmentSettingForm;
RealTimeDataForm* _realTimeDataForm;
QStatusBar *_statusBar;
QMenu* _contextMenu;
AnalysisSettingForm* _analysisSettingWidget;
};

View File

@ -47,6 +47,7 @@
<addaction name="actionNumericalLabel"/>
<addaction name="actionStartPoint"/>
<addaction name="actionStopPoint"/>
<addaction name="actionPeakSynthesisAnalysis"/>
</widget>
<widget class="QMenu" name="menu_5">
<property name="title">
@ -166,6 +167,11 @@
<string>数值标记</string>
</property>
</action>
<action name="actionPeakSynthesisAnalysis">
<property name="text">
<string>峰综合分析</string>
</property>
</action>
</widget>
<resources>
<include location="images.qrc"/>

View File

@ -78,7 +78,7 @@ SerialPort *SerialPort::instance()
SerialPort::~SerialPort()
{
if (_sp->isOpen())
if (_sp && _sp->isOpen())
{
_sp->clear();
_sp->close();
@ -136,7 +136,7 @@ void SerialPort::slotReadData()
{
emit sigSendCommonData(cd);
}
emit sigSendCommonDataToRealDataForm(cd);
// emit sigSendCommonDataToRealDataForm(cd);
}
}
#endif

View File

@ -45,13 +45,13 @@ AnalysisSettingForm::AnalysisSettingForm(QWidget *parent) :
layout->addLayout(buttonLayout);
layout->addStretch(1);
//
connect(_applyButton,&QPushButton::click,
connect(_applyButton,&QPushButton::clicked,
this,&AnalysisSettingForm::slotApply);
connect(_confirmButton,&QPushButton::click,
connect(_confirmButton,&QPushButton::clicked,
this,&AnalysisSettingForm::slotConfirm);
connect(_undoButton,&QPushButton::click,
connect(_undoButton,&QPushButton::clicked,
this,&AnalysisSettingForm::slotUndo);
connect(_cancelButton,&QPushButton::click,
connect(_cancelButton,&QPushButton::clicked,
this,&AnalysisSettingForm::slotCancel);
//
_leftBorderSpinBox->setRange(0.0, 10000.0); // 设置范围
@ -74,7 +74,7 @@ void AnalysisSettingForm::slotRecvLineXCoord(const int index, const double point
void AnalysisSettingForm::slotApply()
{
emit sigApply();
}
void AnalysisSettingForm::slotConfirm()

View File

@ -16,6 +16,8 @@ public:
signals:
void sigApply();
void sigConfirm();
void sigUndo();
void sigCancel();
public slots:
void slotRecvLineXCoord(const int,const double);
private slots:

View File

@ -6,7 +6,7 @@
CentralWidget::CentralWidget(QWidget *parent)
: QWidget(parent),
_customPlot(new QCustomPlot)
_customPlot(new QCustomPlot(this))
,_nanlysisMode(AnalysisMode::None)
{
setMouseTracking(true);
@ -78,11 +78,11 @@ void CentralWidget::setAnalysisMode(const CentralWidget::AnalysisMode mode)
switch (mode)
{
case AnalysisMode::NumericalLabel:
case AnalysisMode::StartPoint:
case AnalysisMode::StopPoint:
case AnalysisMode::PeakSynthesisAnalysis:
setEventHandlerEnable(true);
break;
case AnalysisMode::None:
setEventHandlerEnable(false);
break;
default:
break;
}
@ -92,8 +92,14 @@ void CentralWidget::slotModeModify(const Global::Mode mode)
{
if (Global::Mode::ExperimentStart == mode)
{
if (_customPlot->graphCount() > 0 && _graph)
{
// 清除第一个图表上的数据
_graph->setData(QVector<double>(), QVector<double>());
}
// 创建画布,设置画布上的点数据
_graph = _customPlot->addGraph();
// _graph = _customPlot->addGraph();
// 设置坐标轴标签
_customPlot->xAxis->setLabel("Temp/℃");
@ -166,7 +172,9 @@ void CentralWidget::slotRecvAnalysisFileName(const QString &fileName)
void CentralWidget::slotAnalysisSettingApply()
{
double x = _line1->point1->coords().x();
drawText(x,"11111");
}
void CentralWidget::slotAnalysisSettingConfirm()
@ -174,6 +182,21 @@ void CentralWidget::slotAnalysisSettingConfirm()
}
void CentralWidget::slotAnalysisSettingUndo()
{
}
void CentralWidget::slotAnalysisSettingCancel()
{
}
void CentralWidget::slotAnalysisSettingLineXPoint(const int index, const double)
{
}
void CentralWidget::timerEvent(QTimerEvent *event)
{
// key的单位是秒
@ -216,3 +239,50 @@ void CentralWidget::setEventHandlerEnable(const bool flag)
}
_customPlot->replot();
}
void CentralWidget::drawText(const double x, const QString text)
{
#if 0
// 创建标注文字QCPItemText
QCPItemText *textLabel = new QCPItemText(_customPlot);
textLabel->setPositionAlignment(Qt::AlignBottom|Qt::AlignHCenter); // 对齐方式
textLabel->position->setType(QCPItemPosition::ptPlotCoords); // 使用数据坐标
textLabel->position->setCoords(x,
_graph->data()->at(x)->value + 0.5); // 假设标注在 x=5 的点上方
textLabel->setText("标注文字");
textLabel->setFont(QFont("Arial", 10));
textLabel->setPen(QPen(Qt::black)); // 文字边框
textLabel->setBrush(Qt::white); // 文字背景
// 创建指向点的线段QCPItemLine
QCPItemLine *arrow = new QCPItemLine(_customPlot);
arrow->start->setParentAnchor(textLabel->bottom); // 线段起点绑定到标注文字底部
arrow->end->setCoords(x, _graph->data()->at(x)->value); // 线段终点绑定到数据点
arrow->setHead(QCPLineEnding::esSpikeArrow); // 添加箭头
arrow->setPen(QPen(Qt::red, 2));
#endif
if (!_graph || !_customPlot) {
return;
}
// 创建标注文字QCPItemText
QCPItemText *textLabel = new QCPItemText(_customPlot);
textLabel->setPositionAlignment(Qt::AlignBottom|Qt::AlignHCenter); // 对齐方式
textLabel->position->setType(QCPItemPosition::ptPlotCoords); // 使用数据坐标
// 查找最接近 x 的数据点
auto it = _graph->data()->findBegin(x);
if (it != _graph->data()->end()) {
double y = it->value + 0.5;
textLabel->position->setCoords(x, y);
textLabel->setText(text);
textLabel->setFont(QFont("Arial", 10));
textLabel->setPen(QPen(Qt::black)); // 文字边框
textLabel->setBrush(Qt::white); // 文字背景
// 创建指向点的线段QCPItemLine
QCPItemLine *arrow = new QCPItemLine(_customPlot);
arrow->start->setParentAnchor(textLabel->bottom); // 线段起点绑定到标注文字底部
arrow->end->setCoords(x, it->value); // 线段终点绑定到数据点
arrow->setHead(QCPLineEnding::esSpikeArrow); // 添加箭头
arrow->setPen(QPen(Qt::red, 2));
}
}

View File

@ -18,6 +18,7 @@ public:
NumericalLabel,
StartPoint,
StopPoint,
PeakSynthesisAnalysis
};
CentralWidget(QWidget *parent = nullptr);
~CentralWidget();
@ -31,14 +32,17 @@ public slots:
void slotRecvCommonData(const CommonData&);
void slotRecvAnalysisFileName(const QString&);
//analysis setting
void slotAnalysisSettingLineXPoint(const int index,const double);
void slotAnalysisSettingApply();
void slotAnalysisSettingConfirm();
void slotAnalysisSettingUndo();
void slotAnalysisSettingCancel();
protected:
void timerEvent(QTimerEvent* event);
void contextMenuEvent(QContextMenuEvent *event);
private:
void setEventHandlerEnable(const bool);
void drawText(const double,const QString);
private:
AnalysisMode _nanlysisMode;
QCustomPlot *_customPlot;

View File

@ -3,20 +3,29 @@
DragLineHandler::DragLineHandler(QCustomPlot *plot, QCPItemStraightLine *line1,
QCPItemStraightLine *line2, QCPGraph*graph,
QObject *parent)
: QObject(parent), mPlot(plot), mLine1(line1), mLine2(line2),mGraph(graph)
: QObject(parent), _plot(plot), _line1(line1), _line2(line2),_graph(graph)
,_enableFlag(false)
{
mPlot->setInteractions(QCP::iSelectItems);
_plot->setInteractions(QCP::iSelectItems);
}
DragLineHandler::~DragLineHandler()
{
}
bool DragLineHandler::eventFilter(QObject *obj, QEvent *event)
{
if(!_enableFlag){
qDebug()<<"_enableFlag false.";
return false;
#if 0
if(mPlot){
mPlot->setCursor(Qt::ArrowCursor);
}
#endif
return QObject::eventFilter(obj, event);
}
if (obj == mPlot)
if (obj == _plot)
{
if (event->type() == QEvent::MouseMove)
{
@ -26,33 +35,33 @@ bool DragLineHandler::eventFilter(QObject *obj, QEvent *event)
QPoint mousePos = mouseEvent->pos();
qDebug()<<"x:"<<mousePos.x();
bool nearLine1 = isNearLine(mLine1, mousePos);
bool nearLine2 = isNearLine(mLine2, mousePos);
bool nearLine1 = isNearLine(_line1, mousePos);
bool nearLine2 = isNearLine(_line2, mousePos);
if (nearLine1 || nearLine2)
{
mPlot->setCursor(Qt::SplitHCursor);
_plot->setCursor(Qt::SplitHCursor);
qDebug()<<"mIconLabel visiable.";
// qDebug()<<"mIconLabel visiable.";
}
else
{
qDebug()<<"mIconLabel not visiable.";
// qDebug()<<"mIconLabel not visiable.";
mPlot->setCursor(Qt::ArrowCursor);
_plot->setCursor(Qt::ArrowCursor);
}
if (mDraggingLine)
if (_draggingLine)
{
double x = mPlot->xAxis->pixelToCoord(mousePos.x());
if(mDraggingLine == mLine1){
double x = _plot->xAxis->pixelToCoord(mousePos.x());
if(_draggingLine == _line1){
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();
_draggingLine->point1->setCoords(x, _plot->yAxis->range().lower);
_draggingLine->point2->setCoords(x, _plot->yAxis->range().upper);
_plot->replot();
updateSelectedRegion();
}
}
@ -64,24 +73,24 @@ bool DragLineHandler::eventFilter(QObject *obj, QEvent *event)
if (mouseEvent->button() == Qt::LeftButton)
{
QPoint mousePos = mouseEvent->pos();
if (isNearLine(mLine1, mousePos))
if (isNearLine(_line1, mousePos))
{
mDraggingLine = mLine1;
mLine1->setSelected(true);
_draggingLine = _line1;
_line1->setSelected(true);
}
else if (isNearLine(mLine2, mousePos))
else if (isNearLine(_line2, mousePos))
{
mDraggingLine = mLine2;
mLine2->setSelected(true);
_draggingLine = _line2;
_line2->setSelected(true);
}
}
}
else if (event->type() == QEvent::MouseButtonRelease)
{
if (mDraggingLine)
if (_draggingLine)
{
mDraggingLine->setSelected(false);
mDraggingLine = nullptr;
_draggingLine->setSelected(false);
_draggingLine = nullptr;
}
}
}
@ -91,21 +100,21 @@ bool DragLineHandler::eventFilter(QObject *obj, QEvent *event)
bool DragLineHandler::isNearLine(QCPItemStraightLine *line, const QPoint &pos)
{
double lineX = line->point1->coords().x();
int linePixelX = mPlot->xAxis->coordToPixel(lineX);
int linePixelX = _plot->xAxis->coordToPixel(lineX);
return qAbs(pos.x() - linePixelX) < 5;
}
void DragLineHandler::updateSelectedRegion()
{
double x1 = mLine1->point1->coords().x();
double x2 = mLine2->point1->coords().x();
double x1 = _line1->point1->coords().x();
double x2 = _line1->point1->coords().x();
if (x1 > x2)
std::swap(x1, x2);
// 获取曲线数据
QVector<double> xData, yData;
// mGraph->data()->data(xData, yData);
auto dataContainer = mGraph->data();
auto dataContainer = _graph->data();
for (auto it = dataContainer->begin(); it != dataContainer->end(); ++it)
{
xData.append(it->key);

View File

@ -17,6 +17,8 @@ public:
DragLineHandler(QCustomPlot *plot, QCPItemStraightLine *line1, QCPItemStraightLine *line2,
QCPGraph*graph,QObject *parent);
~DragLineHandler();
void setEnable(const bool flag){_enableFlag = flag;}
signals:
void sigSendLineXCoord(const int,const double);
@ -28,10 +30,10 @@ private:
bool isNearLine(QCPItemStraightLine *line, const QPoint &pos);
private:
bool _enableFlag;
QCustomPlot *mPlot;
QCPItemStraightLine *mLine1, *mLine2;
QCPItemStraightLine *mDraggingLine = nullptr;
QCPGraph *mGraph;
QCustomPlot *_plot;
QCPItemStraightLine *_line1,*_line2;
QCPItemStraightLine *_draggingLine = nullptr;
QCPGraph *_graph;
};
#endif