diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index a8ad4fb..7e5992a 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -173,12 +173,14 @@ void MainWindow::on_actionConnectToDev_triggered() void MainWindow::on_actionStartPoint_triggered() { - + _rightWidget->show(); + _centralWidget->setAnalysisMode(CentralWidget::AnalysisMode::StartPoint); } void MainWindow::on_actionStopPoint_triggered() { - + _rightWidget->show(); + _centralWidget->setAnalysisMode(CentralWidget::AnalysisMode::StopPoint); } void MainWindow::on_actionNumericalLabel_triggered() @@ -193,3 +195,8 @@ void MainWindow::on_actionPeakSynthesisAnalysis_triggered() _rightWidget->show(); _centralWidget->setAnalysisMode(CentralWidget::AnalysisMode::PeakSynthesisAnalysis); } + +void MainWindow::on_actionClearAllData_triggered() +{ + _centralWidget->clearAllData(); +} diff --git a/src/mainwindow.h b/src/mainwindow.h index cd076e3..79bb693 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -41,6 +41,7 @@ private slots: void on_actionStopPoint_triggered(); void on_actionPeakSynthesisAnalysis_triggered(); + void on_actionClearAllData_triggered(); private: void connections(); void setActionEnable(const bool); diff --git a/src/mainwindow.ui b/src/mainwindow.ui index e806d92..f0260db 100644 --- a/src/mainwindow.ui +++ b/src/mainwindow.ui @@ -53,6 +53,7 @@ 工具 + @@ -98,6 +99,7 @@ + @@ -172,6 +174,11 @@ 峰综合分析 + + + 清除所有数据 + + diff --git a/src/ui/centralwidget.cpp b/src/ui/centralwidget.cpp index 8257162..32d5856 100644 --- a/src/ui/centralwidget.cpp +++ b/src/ui/centralwidget.cpp @@ -1,5 +1,8 @@ #include #include +#include +#include +#include #include "centralwidget.h" #include "filemanager.h" @@ -172,9 +175,25 @@ void CentralWidget::slotRecvAnalysisFileName(const QString &fileName) void CentralWidget::slotAnalysisSettingApply() { - double x = _line1->point1->coords().x(); + switch (_nanlysisMode) { + case AnalysisMode::NumericalLabel: + { + double x = _line1->point1->coords().x(); + drawText(x,"11111"); + break; + } + case AnalysisMode::PeakSynthesisAnalysis: + { + double x1 = _line1->point1->coords().x(); + double x2 = _line2->point1->coords().x(); - drawText(x,"11111"); + fillGraph(x1,x2); + // + break; + } + default: + break; + } } void CentralWidget::slotAnalysisSettingConfirm() @@ -232,8 +251,11 @@ void CentralWidget::contextMenuEvent(QContextMenuEvent *event) void CentralWidget::setEventHandlerEnable(const bool flag) { + // 当竖线隐藏时,需要设置不可选择模式。 + // _line1->setSelectable(false); _eventHandler->setEnable(flag); _line1->setVisible(flag); + if(AnalysisMode::NumericalLabel != _nanlysisMode){ _line2->setVisible(flag); } @@ -260,29 +282,197 @@ void CentralWidget::drawText(const double x, const QString text) arrow->setHead(QCPLineEnding::esSpikeArrow); // 添加箭头 arrow->setPen(QPen(Qt::red, 2)); #endif + +#if 0 if (!_graph || !_customPlot) { - return; - } - // 创建标注文字(QCPItemText) - QCPItemText *textLabel = new QCPItemText(_customPlot); - textLabel->setPositionAlignment(Qt::AlignBottom|Qt::AlignHCenter); // 对齐方式 - textLabel->position->setType(QCPItemPosition::ptPlotCoords); // 使用数据坐标 + 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); // 文字背景 + // 查找最接近 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)); - } + // 创建指向点的线段(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)); + } +#endif + + double y = findClosestY(x); + // 创建标注文字(QCPItemText) + QCPItemText *textLabel = new QCPItemText(_customPlot); + textLabel->setPositionAlignment(Qt::AlignBottom | Qt::AlignHCenter); // 对齐方式 + textLabel->position->setType(QCPItemPosition::ptPlotCoords); // 使用数据坐标 + textLabel->position->setCoords(x, y + 10); // 设置文本位置在指定点上方 + + 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, y); // 线段终点设置为指定的点 + arrow->setHead(QCPLineEnding::esSpikeArrow); // 添加箭头 + arrow->setPen(QPen(Qt::red, 2)); + + // 重绘图表以显示文本标签和箭头 + _customPlot->replot(); +} + +double CentralWidget::findClosestY(double targetX) { + // 获取曲线数据容器 + QSharedPointer> dataContainer = _graph->data(); + // 初始化最小差值和对应的 y 值 + double minDiff = std::numeric_limits::max(); + double closestY = 0.0; + + // 遍历数据容器 + for (int i = 0; i < dataContainer->size(); ++i) { + double currentX = dataContainer->at(i)->key; + double currentY = dataContainer->at(i)->value; + + // 计算当前 x 与目标 x 的差值的绝对值 + double diff = std::abs(currentX - targetX); + + // 更新最小差值和对应的 y 值 + if (diff < minDiff) { + minDiff = diff; + closestY = currentY; + } + } + + return closestY; +} + +void CentralWidget::fillGraph(const double x1, const double x2) +{ + //未寻找x1\x2之间最大值。 + double y1 = findClosestY(x1); + double y2 = findClosestY(x2); + + QVector xVtr,yVtr; + xVtr.push_back(x1); + xVtr.push_back(x2); + + yVtr.push_back(y1); + yVtr.push_back(y2); + + QCPGraph *mainGraph = _customPlot->addGraph(); + mainGraph->setData(xVtr, yVtr); + + // 样式配置 + mainGraph->setPen(QPen(Qt::red, 1)); + + _graph->setBrush(QBrush(Qt::lightGray)); + _graph->setChannelFillGraph(mainGraph); + + // customPlot->graph(0)->setChannelFillGraph(customPlot->graph(1)); + // _customPlot->graph(1); + _customPlot->replot(); +} + +void CentralWidget::clearAllData() +{ + +#if 0 + // 删除不需要的 Item + for (auto *item : itemsToRemove) { + _customPlot->removeItem(item); + delete item; + } + _customPlot->replot(); + // + if (_customPlot->graphCount() > 0 && _graph) + { + // 清除第一个图表上的数据 + _graph->setData(QVector(), QVector()); + } + + _customPlot->clearItems(); // 清除所有 QCPItem 对象 + _customPlot->replot(); // 刷新显示 +#endif + +#if 0 + // 从后向前遍历更安全 + for (int i = _customPlot->itemCount()-1; i >= 0; --i) { + QCPItem* item = _customPlot->item(i); + if (item != _line1 && item != _line2) { + _customPlot->removeItem(item); + delete item; + } + } + _customPlot->replot(); +#endif +#if 1 + _line1->setVisible(false); + _line2->setVisible(false); + + + if (_customPlot->graphCount() > 0 && _graph) + { + // 清除第一个图表上的数据 + _graph->setData(QVector(), QVector()); + } + + // 保留的元素列表 + QList itemsToKeep; + itemsToKeep << _line1 << _line2; + + // 遍历所有项并移除不在保留列表中的项 + // QCPAbstractItem + // QList allItems = _customPlot->item(); + for (int i = _customPlot->itemCount() - 1; i >= 0; --i) { + QCPAbstractItem *item = _customPlot->item(i); + if (!itemsToKeep.contains(item)) { + // 从图表中移除项 + _customPlot->removeItem(item); + // 删除项,释放内存 + // delete item; + } + } + + // 重绘图表以显示更改 + _customPlot->replot(); +#endif + + +#if 0 + // 遍历并删除所有图形元素,除了指定的元素 + for (int i = _customPlot->itemCount() - 1; i >= 0; --i) { + QCPAbstractItem *item = _customPlot->item(i); + if (item != _line1 && item != _line2) { + _customPlot->removeItem(item); + } + } + + // 遍历并删除所有绘图元素,除了指定的元素 + for (int i = _customPlot->graphCount() - 1; i >= 0; --i) { + QCPGraph *graph = _customPlot->graph(i); + if (graph != _graph) { + _customPlot->removeGraph(graph); + } + } + + // 遍历并删除所有文本元素 + for (int i = _customPlot->textLayoutCount() - 1; i >= 0; --i) { + QCPTextElement *textElement = _customPlot->textLayout(i); + _customPlot->removeTextLayout(textElement); + } + +#endif } diff --git a/src/ui/centralwidget.h b/src/ui/centralwidget.h index f26bd25..a6decc9 100644 --- a/src/ui/centralwidget.h +++ b/src/ui/centralwidget.h @@ -24,6 +24,7 @@ public: ~CentralWidget(); void setAnalysisMode(const AnalysisMode); + void clearAllData(); signals: void sigContextMenuShow(const QPoint); void sigSendLineXCoord(const int,const double); @@ -43,6 +44,9 @@ protected: private: void setEventHandlerEnable(const bool); void drawText(const double,const QString); + double findClosestY(double targetX); + void fillGraph(const double x1,const double x2); + void findPeakPoint(); private: AnalysisMode _nanlysisMode; QCustomPlot *_customPlot;