2025-03-25T16:45:16

This commit is contained in:
yuntang 2025-03-25 16:45:16 +08:00
parent 0fb8219dd9
commit 6c19e0fc79
5 changed files with 235 additions and 26 deletions

View File

@ -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();
}

View File

@ -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);

View File

@ -53,6 +53,7 @@
<property name="title">
<string>工具</string>
</property>
<addaction name="actionClearAllData"/>
</widget>
<widget class="QMenu" name="menu_6">
<property name="title">
@ -98,6 +99,7 @@
<addaction name="actionStop"/>
<addaction name="actionRealTimeWidget"/>
<addaction name="actionReadOnly"/>
<addaction name="actionClearAllData"/>
</widget>
<action name="actionNew">
<property name="icon">
@ -172,6 +174,11 @@
<string>峰综合分析</string>
</property>
</action>
<action name="actionClearAllData">
<property name="text">
<string>清除所有数据</string>
</property>
</action>
</widget>
<resources>
<include location="images.qrc"/>

View File

@ -1,5 +1,8 @@
#include <QHBoxLayout>
#include <QRandomGenerator>
#include <cmath>
#include <limits>
#include <qcustomplot.h>
#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<QCPDataContainer<QCPGraphData>> dataContainer = _graph->data();
// 初始化最小差值和对应的 y 值
double minDiff = std::numeric_limits<double>::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<double> 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<double>(), QVector<double>());
}
_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<double>(), QVector<double>());
}
// 保留的元素列表
QList<QCPAbstractItem *> itemsToKeep;
itemsToKeep << _line1 << _line2;
// 遍历所有项并移除不在保留列表中的项
// QCPAbstractItem
// QList<QCPAbstractItem *> 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
}

View File

@ -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;