From ff85c72be9c7b9c16fa365fd94739ea83b3388e1 Mon Sep 17 00:00:00 2001 From: yuntang <123@qq.com> Date: Thu, 10 Apr 2025 15:20:57 +0800 Subject: [PATCH] 2025-04-10T15:20:55 --- src/AnalysTool.pro | 4 +- src/ui/centralwidget.cpp | 43 +++++++++-- src/ui/centralwidget.h | 4 +- .../{draglinehandler.cpp => eventhandler.cpp} | 73 +++++++++++++++---- src/ui/{draglinehandler.h => eventhandler.h} | 21 ++++-- src/ui/specificheatcomparisonmethodform.cpp | 59 +++++++++++++++ src/ui/specificheatcomparisonmethodform.h | 9 ++- src/ui/specificheatcomparisonmethodform.ui | 35 +-------- 8 files changed, 183 insertions(+), 65 deletions(-) rename src/ui/{draglinehandler.cpp => eventhandler.cpp} (77%) rename src/ui/{draglinehandler.h => eventhandler.h} (68%) diff --git a/src/AnalysTool.pro b/src/AnalysTool.pro index 6303914..53b0a8c 100644 --- a/src/AnalysTool.pro +++ b/src/AnalysTool.pro @@ -31,7 +31,7 @@ SOURCES += \ thirdparty/qcustomplot/qcustomplot.cpp \ ui/degreeofcrystallinityform.cpp \ ui/degreeofcureform.cpp \ - ui/draglinehandler.cpp \ + ui/eventhandler.cpp \ ui/experimentsettingform.cpp \ ui/instrumentcoefficientform.cpp \ ui/leftwidget.cpp \ @@ -55,7 +55,7 @@ HEADERS += \ thirdparty/qcustomplot/qcustomplot.h \ ui/degreeofcrystallinityform.h \ ui/degreeofcureform.h \ - ui/draglinehandler.h \ + ui/eventhandler.h \ ui/experimentsettingform.h \ ui/instrumentcoefficientform.h \ ui/leftwidget.h \ diff --git a/src/ui/centralwidget.cpp b/src/ui/centralwidget.cpp index 69533ad..62a8dc6 100644 --- a/src/ui/centralwidget.cpp +++ b/src/ui/centralwidget.cpp @@ -39,20 +39,20 @@ CentralWidget::CentralWidget(QWidget *parent) // 安装事件过滤器 // _graph = _customPlot->addGraph(0); - // _eventHandler = new DragLineHandler(_customPlot, _line1, _line2, _graph, nullptr); + // _eventHandler = new EventHandler(_customPlot, _line1, _line2, _graph, nullptr); - _eventHandler = new DragLineHandler(_customPlot, _line1, _line2, nullptr); + _eventHandler = new EventHandler(_customPlot, _line1, _line2, nullptr); _eventHandler->setEnable(true); _customPlot->installEventFilter(_eventHandler); _customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables); - connect(_eventHandler,&DragLineHandler::sigSendLineXCoord, + connect(_eventHandler,&EventHandler::sigSendLineXCoord, this,&CentralWidget::sigSendLineXCoord); - // 连接选中信号 - connect(_customPlot, &QCustomPlot::selectionChangedByUser, - this, &CentralWidget::slotSelectionChanged); + +// connect(_customPlot, &QCustomPlot::selectionChangedByUser, +// this, &CentralWidget::slotSelectionChanged); setEventHandlerEnable(false); @@ -185,6 +185,7 @@ void CentralWidget::slotRecvAnalysisFileName(const QString &fileName) // _currentCurve->setPen(QPen(Qt::red)); // 设置线条颜色为红色 // _currentCurve->setBrush(QBrush(QColor(255, 0, 0, 20))); // 设置填充颜色并带有透明度 _currentCurve->setSelectable(QCP::stWhole); // 设置曲线可选 +// _currentCurve->setSelectable(QCP::stPlottable); // 设置曲线可选 // 清除第一个图表上的数据 #if 0 @@ -206,6 +207,34 @@ void CentralWidget::slotRecvAnalysisFileName(const QString &fileName) void CentralWidget::slotSelectionChanged() { + logde<<"selectedPlottables:"<< _customPlot->selectedPlottables().size(); + logde<<"selectedGraphs:"<< _customPlot->selectedGraphs().size(); + logde<<"selectedItems:"<< _customPlot->selectedItems().size(); + + logde<<"plottableCount:"<< _customPlot->plottableCount(); + + return; + + for(int i = 0;i < _customPlot->plottableCount(); i++) { + QCPCurve *curve = qobject_cast(_customPlot->plottable(i)); + if(curve){ + if (curve->selected()) { + logde<<"selected..."; + curve->setPen(QPen(Qt::green)); + _customPlot->replot(); + } else { + curve->setPen(QPen(Qt::blue)); + _customPlot->replot(); + } + } + + logde << "Current pen color:" << curve->pen().color().name().toStdString(); + } + +// _customPlot->replot(); +// _customPlot->update(); + +#if 0 for (QCPAbstractPlottable *plottable : _customPlot->plottable()) { QCPCurve *curve = qobject_cast(plottable); if (curve) { @@ -225,6 +254,8 @@ void CentralWidget::slotSelectionChanged() } } } +#endif + #if 0 // 检查是否有曲线被选中 if (_customPlot->selectedPlottables().size() > 0) { diff --git a/src/ui/centralwidget.h b/src/ui/centralwidget.h index 7757c3a..b54fa35 100644 --- a/src/ui/centralwidget.h +++ b/src/ui/centralwidget.h @@ -7,7 +7,7 @@ #include "qcustomplot.h" #include "protocol.h" #include "global.h" -#include "draglinehandler.h" +#include "eventhandler.h" #include "filemanager.h" class CentralWidget:public QWidget @@ -64,7 +64,7 @@ private: // QVector _curveVtr; // QCPGraph* _currentGraph; // QVector _graphVtr; - DragLineHandler* _eventHandler; + EventHandler* _eventHandler; QCPItemStraightLine *_line1,*_line2; QVector _dataVtr; QVector _lineVtr; diff --git a/src/ui/draglinehandler.cpp b/src/ui/eventhandler.cpp similarity index 77% rename from src/ui/draglinehandler.cpp rename to src/ui/eventhandler.cpp index 6a00a5b..02291cb 100644 --- a/src/ui/draglinehandler.cpp +++ b/src/ui/eventhandler.cpp @@ -1,7 +1,7 @@ -#include "draglinehandler.h" +#include "eventhandler.h" #include "logger.h" -DragLineHandler::DragLineHandler(QCustomPlot *plot, +EventHandler::EventHandler(QCustomPlot *plot, QCPItemStraightLine *line1, QCPItemStraightLine *line2, QObject *parent) @@ -14,7 +14,7 @@ DragLineHandler::DragLineHandler(QCustomPlot *plot, } #if 0 -DragLineHandler::DragLineHandler(QCustomPlot *plot, QCPItemStraightLine *line1, +EventHandler::EventHandler(QCustomPlot *plot, QCPItemStraightLine *line1, QCPItemStraightLine *line2, QCPGraph*graph, QObject *parent) : QObject(parent), _plot(plot), _line1(line1), _line2(line2),_graph(graph) @@ -24,11 +24,11 @@ DragLineHandler::DragLineHandler(QCustomPlot *plot, QCPItemStraightLine *line1, } #endif -DragLineHandler::~DragLineHandler() +EventHandler::~EventHandler() { } -bool DragLineHandler::eventFilter(QObject *obj, QEvent *event) +bool EventHandler::eventFilter(QObject *obj, QEvent *event) { if(!_enableFlag){ // qDebug()<<"_enableFlag false."; @@ -132,15 +132,18 @@ bool DragLineHandler::eventFilter(QObject *obj, QEvent *event) } } #endif - // for (QCPItem *item : _customPlot->selectedItems()) { - // if (item->selected()) { - // } - // } + if(_plot->selectedPlottables().size() > 0){ + logde<<"selected..."; + QCPCurve *curve = qobject_cast(_plot->selectedPlottables().front()); + if(!curve){ + return QObject::eventFilter(obj, event); + } - if(_plot->selectedItems().size() > 0){ QContextMenuEvent *contextMenuEvent = static_cast(event); _menu->move(contextMenuEvent->globalPos()); _menu->show(); + + }else{ logde<<"not selected..."; } @@ -149,7 +152,7 @@ bool DragLineHandler::eventFilter(QObject *obj, QEvent *event) return QObject::eventFilter(obj, event); } -void DragLineHandler::initMenu() +void EventHandler::initMenu() { _specificHeatItemAction = new QAction("比热项",_plot); _baseLineAction = new QAction("基线",_plot); @@ -164,16 +167,58 @@ void DragLineHandler::initMenu() specificHeatItemMenu->addAction(_sampleAction); _specificHeatItemAction->setMenu(specificHeatItemMenu); + + connect(_baseLineAction,&QAction::triggered, + this,&EventHandler::slotBaseLine); + connect(_standardSampleAction,&QAction::triggered, + this,&EventHandler::slotStandardSample); + connect(_sampleAction,&QAction::triggered, + this,&EventHandler::slotSample); } -bool DragLineHandler::isNearLine(QCPItemStraightLine *line, const QPoint &pos) +bool EventHandler::isNearLine(QCPItemStraightLine *line, const QPoint &pos) { double lineX = line->point1->coords().x(); int linePixelX = _plot->xAxis->coordToPixel(lineX); return qAbs(pos.x() - linePixelX) < 5; } -void DragLineHandler::updateSelectedRegion() +void EventHandler::slotBaseLine() +{ + logde<<"slotBaseLine..."; + + QCPCurve *curve = qobject_cast(_plot->selectedPlottables().front()); + if(!curve){ + return; + } + curve->setPen(QPen(Qt::red)); + _plot->replot(); +} + +void EventHandler::slotStandardSample() +{ + + QCPCurve *curve = qobject_cast(_plot->selectedPlottables().front()); + if(!curve){ + return; + } + curve->setPen(QPen(Qt::darkGreen)); + _plot->replot(); +} + +void EventHandler::slotSample() +{ +#if 1 + QCPCurve *curve = qobject_cast(_plot->selectedPlottables().front()); + if(!curve){ + return; + } + curve->setPen(QPen(Qt::black)); + _plot->replot(); +#endif +} + +void EventHandler::updateSelectedRegion() { double x1 = _line1->point1->coords().x(); double x2 = _line1->point1->coords().x(); @@ -197,7 +242,7 @@ void DragLineHandler::updateSelectedRegion() // qDebug() << "Selected region: [" << xData[index1] << ", " << xData[index2] << "]"; } -int DragLineHandler::findClosestIndex(const QVector &data, double target) +int EventHandler::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); diff --git a/src/ui/draglinehandler.h b/src/ui/eventhandler.h similarity index 68% rename from src/ui/draglinehandler.h rename to src/ui/eventhandler.h index 93975d1..bbe4cbf 100644 --- a/src/ui/draglinehandler.h +++ b/src/ui/eventhandler.h @@ -1,5 +1,5 @@ -#ifndef DRAGlINEHANDLER_H -#define DRAGlINEHANDLER_H +#ifndef EventHandler_H +#define EventHandler_H #include #include @@ -11,17 +11,17 @@ #include "qcustomplot.h" -class DragLineHandler : public QObject +class EventHandler : public QObject { Q_OBJECT public: - DragLineHandler(QCustomPlot *plot, QCPItemStraightLine *line1, QCPItemStraightLine *line2, + EventHandler(QCustomPlot *plot, QCPItemStraightLine *line1, QCPItemStraightLine *line2, QObject *parent); #if 0 - DragLineHandler(QCustomPlot *plot, QCPItemStraightLine *line1, QCPItemStraightLine *line2, + EventHandler(QCustomPlot *plot, QCPItemStraightLine *line1, QCPItemStraightLine *line2, QCPGraph*graph,QObject *parent); #endif - ~DragLineHandler(); + ~EventHandler(); void setGraph(QCPGraph* g){ _graph = g;} void setEnable(const bool flag){_enableFlag = flag;} @@ -35,11 +35,17 @@ signals: void sigSendLineXCoord(const int,const double); protected: bool eventFilter(QObject *obj, QEvent *event) override; +private slots: + void slotBaseLine(); + void slotStandardSample(); + void slotSample(); + private: void initMenu(); int findClosestIndex(const QVector &data, double target); void updateSelectedRegion(); bool isNearLine(QCPItemStraightLine *line, const QPoint &pos); + private: Mode _mode; bool _enableFlag; @@ -48,7 +54,8 @@ private: QCPItemStraightLine *_draggingLine = nullptr; QCPGraph *_graph; QMenu* _menu; - QAction* _specificHeatItemAction,*_baseLineAction,*_standardSampleAction,*_sampleAction; + QAction* _specificHeatItemAction,*_baseLineAction, + *_standardSampleAction,*_sampleAction; }; #endif diff --git a/src/ui/specificheatcomparisonmethodform.cpp b/src/ui/specificheatcomparisonmethodform.cpp index 1b30c53..bf909ad 100644 --- a/src/ui/specificheatcomparisonmethodform.cpp +++ b/src/ui/specificheatcomparisonmethodform.cpp @@ -1,11 +1,44 @@ +#include +#include + #include "specificheatcomparisonmethodform.h" #include "ui_specificheatcomparisonmethodform.h" + SpecificHeatComparisonMethodForm::SpecificHeatComparisonMethodForm(QWidget *parent) : QWidget(parent), ui(new Ui::SpecificHeatComparisonMethodForm) + { ui->setupUi(this); + + _baseLineLineEdit = new QLineEdit(ui->groupBoxDataFile); + _baseLineLabel = new QLabel(ui->groupBoxDataFile); + _baseLineLabel->setStyleSheet("background-color: red;"); + + _standardSampleLineEdit = new QLineEdit(ui->groupBoxDataFile); + _standardSampleLabel = new QLabel(ui->groupBoxDataFile); + _standardSampleLabel->setStyleSheet("background-color: green;"); + + _sampleLineEdit = new QLineEdit(ui->groupBoxDataFile); + _sampleLabel = new QLabel(ui->groupBoxDataFile); + _sampleLabel->setStyleSheet("background-color: black;"); + + QHBoxLayout *baseLineLayout = new QHBoxLayout; + baseLineLayout->addWidget(_baseLineLineEdit); + baseLineLayout->addWidget(_baseLineLabel); + + QHBoxLayout *standardSampleLayout = new QHBoxLayout; + standardSampleLayout->addWidget(_standardSampleLineEdit); + standardSampleLayout->addWidget(_standardSampleLabel); + + QHBoxLayout *sampleLayout = new QHBoxLayout; + sampleLayout->addWidget(_sampleLineEdit); + sampleLayout->addWidget(_sampleLabel); + + ui->formLayoutDataFile->addRow("基线",baseLineLayout); + ui->formLayoutDataFile->addRow("标样",standardSampleLayout); + ui->formLayoutDataFile->addRow("样品",sampleLayout); } SpecificHeatComparisonMethodForm::~SpecificHeatComparisonMethodForm() @@ -22,3 +55,29 @@ void SpecificHeatComparisonMethodForm::on_pushButtonQuit_clicked() { hide(); } + +double calculateHeatCapacity(double theta) { + // 系数A_i + const double A0 = 1.12705; + const double A1 = 0.23260; + const double A2 = -0.21704; + const double A3 = 0.26410; + const double A4 = -0.23778; + const double A5 = -0.10023; + const double A6 = 0.15393; + const double A7 = 0.54579; + const double A8 = -0.47824; + const double A9 = -0.37623; + const double A10 = 0.34407; + + // 温度转换 + double T = theta + 273.15; // 摄氏温度转开尔文 + double x = (T - 650.0) / 550.0; // 归一化温度变量 + + // 计算比热容 + double Cp = A0 + A1 * x + A2 * std::pow(x, 2) + A3 * std::pow(x, 3) + A4 * std::pow(x, 4) + + A5 * std::pow(x, 5) + A6 * std::pow(x, 6) + A7 * std::pow(x, 7) + A8 * std::pow(x, 8) + + A9 * std::pow(x, 9) + A10 * std::pow(x, 10); + + return Cp; +} diff --git a/src/ui/specificheatcomparisonmethodform.h b/src/ui/specificheatcomparisonmethodform.h index d406fdf..dc3ff1e 100644 --- a/src/ui/specificheatcomparisonmethodform.h +++ b/src/ui/specificheatcomparisonmethodform.h @@ -2,6 +2,8 @@ #define SPECIFICHEATCOMPARISONMETHODFORM_H #include +#include +#include namespace Ui { class SpecificHeatComparisonMethodForm; @@ -19,9 +21,14 @@ private slots: void on_pushButtonCalculate_clicked(); void on_pushButtonQuit_clicked(); - +private: + double calculateHeatCapacity(double theta); private: Ui::SpecificHeatComparisonMethodForm *ui; + + QLineEdit *_baseLineLineEdit,*_standardSampleLineEdit, + *_sampleLineEdit; + QLabel *_baseLineLabel,*_standardSampleLabel,*_sampleLabel; }; #endif // SPECIFICHEATCOMPARISONMETHODFORM_H diff --git a/src/ui/specificheatcomparisonmethodform.ui b/src/ui/specificheatcomparisonmethodform.ui index 39a2ae6..a634be3 100644 --- a/src/ui/specificheatcomparisonmethodform.ui +++ b/src/ui/specificheatcomparisonmethodform.ui @@ -63,7 +63,7 @@ - + 数据文件 @@ -76,38 +76,7 @@ 81 - - - - - 基线 - - - - - - - - - - 标样 - - - - - - - - - - 样品 - - - - - - - +