新增保存拟合数据保存,新增拟合数据框选完成以后框选淡化,新增右侧图例显示
This commit is contained in:
parent
2124725051
commit
a27195899a
|
|
@ -24,6 +24,48 @@
|
||||||
#include <QInputDialog>
|
#include <QInputDialog>
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
|
||||||
|
#include <QMessageBox>
|
||||||
|
#include <QListWidget>
|
||||||
|
#include <QDateTime>
|
||||||
|
#include <QJsonDocument>
|
||||||
|
#include <QJsonArray>
|
||||||
|
#include <QJsonObject>
|
||||||
|
#include <QDir>
|
||||||
|
#include "MeasureAnalysisProjectModel.h"
|
||||||
|
|
||||||
|
QJsonObject PeakFitHistoryItem::toJson() const
|
||||||
|
{
|
||||||
|
QJsonObject obj;
|
||||||
|
obj["timestamp"] = timestamp.toString(Qt::ISODate);
|
||||||
|
obj["center"] = center;
|
||||||
|
obj["fwhm"] = fwhm;
|
||||||
|
obj["area"] = area;
|
||||||
|
obj["amplitude"] = amplitude;
|
||||||
|
obj["sigma"] = sigma;
|
||||||
|
obj["sigmoidH"] = sigmoidH;
|
||||||
|
obj["sigmoidW"] = sigmoidW;
|
||||||
|
obj["baseline"] = baseline;
|
||||||
|
obj["xMin"] = xMin;
|
||||||
|
obj["xMax"] = xMax;
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
PeakFitHistoryItem PeakFitHistoryItem::fromJson(const QJsonObject& obj)
|
||||||
|
{
|
||||||
|
PeakFitHistoryItem item;
|
||||||
|
item.timestamp = QDateTime::fromString(obj["timestamp"].toString(), Qt::ISODate);
|
||||||
|
item.center = obj["center"].toDouble();
|
||||||
|
item.fwhm = obj["fwhm"].toDouble();
|
||||||
|
item.area = obj["area"].toDouble();
|
||||||
|
item.amplitude = obj["amplitude"].toDouble();
|
||||||
|
item.sigma = obj["sigma"].toDouble();
|
||||||
|
item.sigmoidH = obj["sigmoidH"].toDouble();
|
||||||
|
item.sigmoidW = obj["sigmoidW"].toDouble();
|
||||||
|
item.baseline = obj["baseline"].toDouble();
|
||||||
|
item.xMin = obj["xMin"].toDouble();
|
||||||
|
item.xMax = obj["xMax"].toDouble();
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
EnergyCountPeakFitView::EnergyCountPeakFitView(QWidget *parent)
|
EnergyCountPeakFitView::EnergyCountPeakFitView(QWidget *parent)
|
||||||
: MeasureAnalysisView { parent }
|
: MeasureAnalysisView { parent }
|
||||||
|
|
@ -49,6 +91,17 @@ EnergyCountPeakFitView::~EnergyCountPeakFitView()
|
||||||
void EnergyCountPeakFitView::InitViewWorkspace(const QString &project_name)
|
void EnergyCountPeakFitView::InitViewWorkspace(const QString &project_name)
|
||||||
{
|
{
|
||||||
Q_UNUSED(project_name);
|
Q_UNUSED(project_name);
|
||||||
|
if (project_name.isEmpty()) return;
|
||||||
|
auto project_model = ProjectList::Instance()->GetProjectModel(project_name);
|
||||||
|
if (!project_model) return;
|
||||||
|
|
||||||
|
QDir project_dir(project_model->GetProjectDir());
|
||||||
|
_workspace = project_dir.filePath(this->GetViewName());
|
||||||
|
if (!QDir(_workspace).exists())
|
||||||
|
project_dir.mkpath(_workspace);
|
||||||
|
|
||||||
|
_historyFilePath = QDir(_workspace).filePath("peak_fit_history.json");
|
||||||
|
loadHistoryFromFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
void EnergyCountPeakFitView::SetAnalyzeDataFilename(const QMap<QString, QVariant> &data_files_set)
|
void EnergyCountPeakFitView::SetAnalyzeDataFilename(const QMap<QString, QVariant> &data_files_set)
|
||||||
|
|
@ -85,19 +138,21 @@ void EnergyCountPeakFitView::setupPlot()
|
||||||
_plot->enableAxis(QwtPlot::yLeft);
|
_plot->enableAxis(QwtPlot::yLeft);
|
||||||
|
|
||||||
// 设置QWT图例
|
// 设置QWT图例
|
||||||
// QwtLegend* legend = new QwtLegend();
|
QwtLegend* legend = new QwtLegend();
|
||||||
// legend->setDefaultItemMode(QwtLegendData::ReadOnly);
|
legend->setDefaultItemMode(QwtLegendData::ReadOnly);
|
||||||
// _plot->insertLegend(legend, QwtPlot::RightLegend);
|
_plot->insertLegend(legend, QwtPlot::RightLegend);
|
||||||
|
|
||||||
_plot->SetAxisDragScale(QwtPlot::xBottom, true);
|
_plot->SetAxisDragScale(QwtPlot::xBottom, true);
|
||||||
|
|
||||||
|
|
||||||
_data_selector = new CustomQwtPlotXaxisSelector(_plot->canvas());
|
_data_selector = new CustomQwtPlotXaxisSelector(_plot->canvas());
|
||||||
_data_selector->setEnabled(false);
|
_data_selector->setEnabled(false);
|
||||||
|
|
||||||
// 启用鼠标追踪,并安装事件过滤器
|
// 启用鼠标追踪,并安装事件过滤器
|
||||||
_plot->canvas()->setMouseTracking(true);
|
_plot->canvas()->setMouseTracking(true);
|
||||||
_plot->canvas()->installEventFilter(this);
|
_plot->canvas()->installEventFilter(this);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void EnergyCountPeakFitView::setupMenu()
|
void EnergyCountPeakFitView::setupMenu()
|
||||||
|
|
@ -123,12 +178,20 @@ void EnergyCountPeakFitView::setupMenu()
|
||||||
QAction* action_clear_rect = this->_menu->addAction(QStringLiteral(u"清除框选标记"));
|
QAction* action_clear_rect = this->_menu->addAction(QStringLiteral(u"清除框选标记"));
|
||||||
connect(action_clear_rect, &QAction::triggered, this, &EnergyCountPeakFitView::clearAllSelectionRects);
|
connect(action_clear_rect, &QAction::triggered, this, &EnergyCountPeakFitView::clearAllSelectionRects);
|
||||||
|
|
||||||
this->_menu->addSeparator();
|
// this->_menu->addSeparator();
|
||||||
QAction* action_select_algorithm = this->_menu->addAction(QStringLiteral(u"选择拟合算法"));
|
// QAction* action_select_algorithm = this->_menu->addAction(QStringLiteral(u"选择拟合算法"));
|
||||||
|
|
||||||
QAction* action_clear_fit = this->_menu->addAction(QStringLiteral(u"清除拟合曲线"));
|
QAction* action_clear_fit = this->_menu->addAction(QStringLiteral(u"清除拟合曲线"));
|
||||||
connect(action_clear_fit, &QAction::triggered, this, &EnergyCountPeakFitView::clearFitCurves);
|
connect(action_clear_fit, &QAction::triggered, this, &EnergyCountPeakFitView::clearFitCurves);
|
||||||
|
|
||||||
|
// [NEW] 保存当前拟合结果
|
||||||
|
QAction* action_save_fit = this->_menu->addAction(QStringLiteral(u"保存当前拟合结果"));
|
||||||
|
connect(action_save_fit, &QAction::triggered, this, &EnergyCountPeakFitView::onActionSaveCurrentFit);
|
||||||
|
|
||||||
|
// [NEW] 查看历史拟合结果
|
||||||
|
QAction* action_show_history = this->_menu->addAction(QStringLiteral(u"峰拟合结果"));
|
||||||
|
connect(action_show_history, &QAction::triggered, this, &EnergyCountPeakFitView::onActionShowFitHistory);
|
||||||
|
|
||||||
QAction* action_hint = this->_menu->addAction(QStringLiteral(u"框选提示:按住 Ctrl 键并拖动左键"));
|
QAction* action_hint = this->_menu->addAction(QStringLiteral(u"框选提示:按住 Ctrl 键并拖动左键"));
|
||||||
action_hint->setEnabled(false);
|
action_hint->setEnabled(false);
|
||||||
}
|
}
|
||||||
|
|
@ -155,8 +218,9 @@ void EnergyCountPeakFitView::loadDataFromFile(const QString &data_name, const QS
|
||||||
y.push_back(energy_count);
|
y.push_back(energy_count);
|
||||||
}
|
}
|
||||||
// 绘制曲线
|
// 绘制曲线
|
||||||
QwtPlotCurve* curve = new QwtPlotCurve(data_name);
|
QwtPlotCurve* curve = new QwtPlotCurve(QStringLiteral(u"原始数据"));
|
||||||
curve->setSamples(x, y);
|
curve->setSamples(x, y);
|
||||||
|
curve->setPen(QPen(Qt::red, 2)); // 原始数据统一红色
|
||||||
_plot->AddCurve(curve);
|
_plot->AddCurve(curve);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -277,7 +341,7 @@ bool EnergyCountPeakFitView::eventFilter(QObject* watched, QEvent* event)
|
||||||
else if (event->type() == QEvent::MouseButtonRelease &&
|
else if (event->type() == QEvent::MouseButtonRelease &&
|
||||||
me->button() == Qt::LeftButton && _isSelecting) {
|
me->button() == Qt::LeftButton && _isSelecting) {
|
||||||
finishSelection();
|
finishSelection();
|
||||||
clearAllSelectionRects();
|
fadeSelectionRectBorders();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -419,16 +483,32 @@ void EnergyCountPeakFitView::addSelectionRect(const QRectF &plotRect)
|
||||||
PlotRectItem* rectItem = new PlotRectItem("Selection");
|
PlotRectItem* rectItem = new PlotRectItem("Selection");
|
||||||
rectItem->setAxes(QwtPlot::xBottom, QwtPlot::yLeft);
|
rectItem->setAxes(QwtPlot::xBottom, QwtPlot::yLeft);
|
||||||
rectItem->setRect(plotRect);
|
rectItem->setRect(plotRect);
|
||||||
|
// 设置填充为透明(无填充)
|
||||||
|
rectItem->setBrush(QBrush(Qt::transparent)); // 或 Qt::NoBrush
|
||||||
|
// 设置边框颜色为红色,线宽2
|
||||||
|
rectItem->setPen(QPen(Qt::red, 2));
|
||||||
rectItem->attach(_plot);
|
rectItem->attach(_plot);
|
||||||
_selectionRectItems.append(rectItem);
|
_selectionRectItems.append(rectItem);
|
||||||
_plot->replot();
|
_plot->replot();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void EnergyCountPeakFitView::fadeSelectionRectBorders()
|
||||||
|
{
|
||||||
|
for (PlotRectItem* item : _selectionRectItems) {
|
||||||
|
if (item) {
|
||||||
|
QPen fadedPen(Qt::red, 1, Qt::DashLine); // 可根据原样式调整
|
||||||
|
item->setPen(fadedPen);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_plot->replot();
|
||||||
|
}
|
||||||
|
|
||||||
QList<PeakFitResult> EnergyCountPeakFitView::performPeakFitting(const QVector<double> &x, const QVector<double> &y, const arma::vec* userP0)
|
QList<PeakFitResult> EnergyCountPeakFitView::performPeakFitting(const QVector<double> &x, const QVector<double> &y, const arma::vec* userP0)
|
||||||
{
|
{
|
||||||
// 清除之前的拟合曲线(每次拟合全新绘制)
|
// 清除之前的拟合曲线(每次拟合全新绘制)
|
||||||
// clearFitCurves();
|
// clearFitCurves();
|
||||||
|
|
||||||
|
|
||||||
QList<PeakFitResult> results;
|
QList<PeakFitResult> results;
|
||||||
if (x.size() != y.size() || x.size() < 3)
|
if (x.size() != y.size() || x.size() < 3)
|
||||||
{
|
{
|
||||||
|
|
@ -478,13 +558,17 @@ QList<PeakFitResult> EnergyCountPeakFitView::performPeakFitting(const QVector<do
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
const double fitRangeEnergy = 15.0;
|
// 选择最重要的峰(例如幅值最大的峰)
|
||||||
for (const auto& pinfo : peaks) {
|
auto bestPeak = *std::max_element(peaks.begin(), peaks.end(),
|
||||||
int idxCenter = pinfo.pos;
|
[](const FindPeaksBySvd::PeakInfo& a, const FindPeaksBySvd::PeakInfo& b) {
|
||||||
|
return a.height < b.height;
|
||||||
|
});
|
||||||
|
int idxCenter = bestPeak.pos;
|
||||||
if (idxCenter < 0 || idxCenter >= x.size())
|
if (idxCenter < 0 || idxCenter >= x.size())
|
||||||
continue;
|
return results;
|
||||||
double centerEnergy = armaX(idxCenter);
|
double centerEnergy = armaX(idxCenter);
|
||||||
|
|
||||||
|
const double fitRangeEnergy = 15.0;
|
||||||
// 提取局部数据
|
// 提取局部数据
|
||||||
QVector<double> localX, localY;
|
QVector<double> localX, localY;
|
||||||
for (int i = 0; i < x.size(); ++i) {
|
for (int i = 0; i < x.size(); ++i) {
|
||||||
|
|
@ -494,8 +578,8 @@ QList<PeakFitResult> EnergyCountPeakFitView::performPeakFitting(const QVector<do
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (localX.size() < 5) {
|
if (localX.size() < 5) {
|
||||||
qDebug() << QStringLiteral(u"局部数据点不足5,跳过峰 at") << centerEnergy;
|
qDebug() << QStringLiteral(u"局部数据点不足5,跳过拟合");
|
||||||
continue;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
arma::vec xLocal(localX.size()), yLocal(localY.size());
|
arma::vec xLocal(localX.size()), yLocal(localY.size());
|
||||||
|
|
@ -506,13 +590,13 @@ QList<PeakFitResult> EnergyCountPeakFitView::performPeakFitting(const QVector<do
|
||||||
|
|
||||||
arma::vec p0;
|
arma::vec p0;
|
||||||
if (userP0 && userP0->n_elem == 6) {
|
if (userP0 && userP0->n_elem == 6) {
|
||||||
p0 = *userP0; // 使用用户提供的初值
|
p0 = *userP0;
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
p0 = EstimatePhotonPeakModelInitialParams(xLocal, yLocal);
|
p0 = EstimatePhotonPeakModelInitialParams(xLocal, yLocal);
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
qDebug() << QStringLiteral(u"初始参数估算失败,跳过峰 at") << centerEnergy;
|
qDebug() << QStringLiteral(u"初始参数估算失败,跳过拟合");
|
||||||
continue;
|
return results;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -521,7 +605,7 @@ QList<PeakFitResult> EnergyCountPeakFitView::performPeakFitting(const QVector<do
|
||||||
p_fit = NolinearLeastSquaresCurveFit::Lsqcurvefit(PhotonPeakModel, xLocal, yLocal, p0);
|
p_fit = NolinearLeastSquaresCurveFit::Lsqcurvefit(PhotonPeakModel, xLocal, yLocal, p0);
|
||||||
} catch (const std::exception& e) {
|
} catch (const std::exception& e) {
|
||||||
qDebug() << QStringLiteral(u"拟合失败:") << e.what();
|
qDebug() << QStringLiteral(u"拟合失败:") << e.what();
|
||||||
continue;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
double sigma = p_fit(1);
|
double sigma = p_fit(1);
|
||||||
|
|
@ -547,22 +631,23 @@ QList<PeakFitResult> EnergyCountPeakFitView::performPeakFitting(const QVector<do
|
||||||
result.baseline = p_fit(4);
|
result.baseline = p_fit(4);
|
||||||
result.sigmoidH = p_fit(2);
|
result.sigmoidH = p_fit(2);
|
||||||
result.sigmoidW = p_fit(3);
|
result.sigmoidW = p_fit(3);
|
||||||
// result.localX = localX; // 保存局部数据,备用
|
|
||||||
// result.localY = localY;
|
|
||||||
|
|
||||||
// 绘制拟合曲线到主图
|
|
||||||
double xMinPlot = result.center - 3 * result.sigma;
|
double xMinPlot = result.center - 3 * result.sigma;
|
||||||
double xMaxPlot = result.center + 3 * result.sigma;
|
double xMaxPlot = result.center + 3 * result.sigma;
|
||||||
xMinPlot = qMax(xMinPlot, localX.first());
|
xMinPlot = qMax(xMinPlot, localX.first());
|
||||||
xMaxPlot = qMin(xMaxPlot, localX.last());
|
xMaxPlot = qMin(xMaxPlot, localX.last());
|
||||||
QString curveName = QStringLiteral(u"Fit_%1keV").arg(result.center, 0, 'f', 2);
|
QwtPlotCurve* fitCurve = createFitCurve(result, xMinPlot, xMaxPlot, QStringLiteral(u"拟合数据"));
|
||||||
QwtPlotCurve* fitCurve = createFitCurve(result, xMinPlot, xMaxPlot, curveName);
|
fitCurve->setPen(QPen(Qt::blue, 2));
|
||||||
_fitCurves.append(fitCurve);
|
_fitCurves.append(fitCurve);
|
||||||
|
|
||||||
results.append(result);
|
results.append(result);
|
||||||
}
|
_plot->replot();
|
||||||
|
|
||||||
_plot->replot(); // 刷新显示拟合曲线
|
_lastFitResult = result;
|
||||||
|
_lastFitParams = p_fit;
|
||||||
|
_lastXMin = xMinPlot;
|
||||||
|
_lastXMax = xMaxPlot;
|
||||||
|
_hasLastFit = true;
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -586,10 +671,9 @@ QwtPlotCurve *EnergyCountPeakFitView::createFitCurve(const PeakFitResult &result
|
||||||
double y = PhotonPeakModel(x, p);
|
double y = PhotonPeakModel(x, p);
|
||||||
ys.append(y);
|
ys.append(y);
|
||||||
}
|
}
|
||||||
|
QwtPlotCurve* curve = new QwtPlotCurve(QStringLiteral(u"拟合数据"));
|
||||||
QwtPlotCurve* curve = new QwtPlotCurve(name);
|
|
||||||
curve->setSamples(xs, ys);
|
|
||||||
curve->setPen(QPen(Qt::blue, 2));
|
curve->setPen(QPen(Qt::blue, 2));
|
||||||
|
curve->setSamples(xs, ys);
|
||||||
curve->attach(_plot);
|
curve->attach(_plot);
|
||||||
return curve;
|
return curve;
|
||||||
}
|
}
|
||||||
|
|
@ -605,3 +689,168 @@ void EnergyCountPeakFitView::clearFitCurves()
|
||||||
_plot->replot();
|
_plot->replot();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void EnergyCountPeakFitView::loadHistoryFromFile()
|
||||||
|
{
|
||||||
|
if (!QFileInfo::exists(_historyFilePath))
|
||||||
|
return;
|
||||||
|
|
||||||
|
QFile file(_historyFilePath);
|
||||||
|
if (!file.open(QIODevice::ReadOnly))
|
||||||
|
return;
|
||||||
|
|
||||||
|
QByteArray data = file.readAll();
|
||||||
|
QJsonDocument doc = QJsonDocument::fromJson(data);
|
||||||
|
if (!doc.isArray())
|
||||||
|
return;
|
||||||
|
|
||||||
|
_fitHistoryList.clear();
|
||||||
|
QJsonArray arr = doc.array();
|
||||||
|
for (const auto& val : arr) {
|
||||||
|
if (val.isObject())
|
||||||
|
_fitHistoryList.append(PeakFitHistoryItem::fromJson(val.toObject()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void EnergyCountPeakFitView::saveHistoryToFile()
|
||||||
|
{
|
||||||
|
QJsonArray arr;
|
||||||
|
for (const auto& item : _fitHistoryList) {
|
||||||
|
arr.append(item.toJson());
|
||||||
|
}
|
||||||
|
QJsonDocument doc(arr);
|
||||||
|
QFile file(_historyFilePath);
|
||||||
|
if (file.open(QIODevice::WriteOnly)) {
|
||||||
|
file.write(doc.toJson());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void EnergyCountPeakFitView::saveCurrentFitToHistory()
|
||||||
|
{
|
||||||
|
if (!_hasLastFit) {
|
||||||
|
QMessageBox::information(this, QStringLiteral(u"提示"),
|
||||||
|
QStringLiteral(u"没有可保存的拟合结果,请先框选区域进行拟合。"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
PeakFitHistoryItem item;
|
||||||
|
item.timestamp = QDateTime::currentDateTime();
|
||||||
|
item.center = _lastFitResult.center;
|
||||||
|
item.fwhm = _lastFitResult.fwhm;
|
||||||
|
item.area = _lastFitResult.area;
|
||||||
|
item.amplitude = _lastFitParams(0);
|
||||||
|
item.sigma = _lastFitParams(1);
|
||||||
|
item.sigmoidH = _lastFitParams(2);
|
||||||
|
item.sigmoidW = _lastFitParams(3);
|
||||||
|
item.baseline = _lastFitParams(4);
|
||||||
|
item.xMin = _lastXMin;
|
||||||
|
item.xMax = _lastXMax;
|
||||||
|
|
||||||
|
_fitHistoryList.append(item);
|
||||||
|
saveHistoryToFile();
|
||||||
|
|
||||||
|
QMessageBox::information(this, QStringLiteral(u"保存成功"),
|
||||||
|
QStringLiteral(u"已保存拟合结果(峰中心 = %1 keV)").arg(item.center));
|
||||||
|
}
|
||||||
|
|
||||||
|
void EnergyCountPeakFitView::displayFitFromHistory(const PeakFitHistoryItem& item)
|
||||||
|
{
|
||||||
|
// clearFitCurves();
|
||||||
|
|
||||||
|
arma::vec p(6);
|
||||||
|
p(0) = item.amplitude;
|
||||||
|
p(1) = item.sigma;
|
||||||
|
p(2) = item.sigmoidH;
|
||||||
|
p(3) = item.sigmoidW;
|
||||||
|
p(4) = item.baseline;
|
||||||
|
p(5) = item.center;
|
||||||
|
|
||||||
|
const int numPoints = 200;
|
||||||
|
QVector<double> xs, ys;
|
||||||
|
double step = (item.xMax - item.xMin) / (numPoints - 1);
|
||||||
|
for (int i = 0; i < numPoints; ++i) {
|
||||||
|
double x = item.xMin + i * step;
|
||||||
|
xs.append(x);
|
||||||
|
ys.append(PhotonPeakModel(x, p));
|
||||||
|
}
|
||||||
|
|
||||||
|
QwtPlotCurve* curve = new QwtPlotCurve(QStringLiteral(u"历史拟合曲线"));
|
||||||
|
curve->setPen(QPen(Qt::blue, 2, Qt::DashLine));
|
||||||
|
curve->setSamples(xs, ys);
|
||||||
|
curve->attach(_plot);
|
||||||
|
_fitCurves.append(curve);
|
||||||
|
|
||||||
|
QwtPlotMarker* infoMarker = new QwtPlotMarker();
|
||||||
|
infoMarker->setValue(item.center, 0);
|
||||||
|
infoMarker->setLabel(QStringLiteral(u"历史峰: %1 keV\nFWHM: %2")
|
||||||
|
.arg(item.center, 0, 'f', 2)
|
||||||
|
.arg(item.fwhm, 0, 'f', 2));
|
||||||
|
infoMarker->setLabelAlignment(Qt::AlignTop | Qt::AlignRight);
|
||||||
|
infoMarker->attach(_plot);
|
||||||
|
|
||||||
|
|
||||||
|
_plot->replot();
|
||||||
|
}
|
||||||
|
|
||||||
|
void EnergyCountPeakFitView::onActionSaveCurrentFit()
|
||||||
|
{
|
||||||
|
saveCurrentFitToHistory();
|
||||||
|
}
|
||||||
|
|
||||||
|
void EnergyCountPeakFitView::onActionShowFitHistory()
|
||||||
|
{
|
||||||
|
if (_fitHistoryList.isEmpty()) {
|
||||||
|
QMessageBox::information(this, QStringLiteral(u"峰拟合结果"),
|
||||||
|
QStringLiteral(u"暂无历史拟合数据,请先进行拟合并保存。"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
QDialog dlg(this);
|
||||||
|
dlg.setWindowTitle(QStringLiteral(u"峰拟合结果历史"));
|
||||||
|
dlg.setMinimumWidth(450);
|
||||||
|
QVBoxLayout* layout = new QVBoxLayout(&dlg);
|
||||||
|
|
||||||
|
QListWidget* listWidget = new QListWidget(&dlg);
|
||||||
|
for (const auto& item : _fitHistoryList) {
|
||||||
|
QString text = QStringLiteral(u"%1 峰中心: %2 keV FWHM: %3 面积: %4")
|
||||||
|
.arg(item.timestamp.toString("yyyy-MM-dd hh:mm:ss"))
|
||||||
|
.arg(item.center, 0, 'f', 2)
|
||||||
|
.arg(item.fwhm, 0, 'f', 2)
|
||||||
|
.arg(item.area, 0, 'f', 0);
|
||||||
|
listWidget->addItem(text);
|
||||||
|
}
|
||||||
|
layout->addWidget(listWidget);
|
||||||
|
|
||||||
|
QPushButton* btnShow = new QPushButton(QStringLiteral(u"显示选中曲线"), &dlg);
|
||||||
|
QPushButton* btnDelete = new QPushButton(QStringLiteral(u"删除选中"), &dlg);
|
||||||
|
QHBoxLayout* btnLayout = new QHBoxLayout();
|
||||||
|
btnLayout->addStretch();
|
||||||
|
btnLayout->addWidget(btnShow);
|
||||||
|
btnLayout->addWidget(btnDelete);
|
||||||
|
layout->addLayout(btnLayout);
|
||||||
|
|
||||||
|
connect(btnShow, &QPushButton::clicked, [&]() {
|
||||||
|
int row = listWidget->currentRow();
|
||||||
|
if (row >= 0 && row < _fitHistoryList.size()) {
|
||||||
|
displayFitFromHistory(_fitHistoryList[row]);
|
||||||
|
dlg.accept();
|
||||||
|
} else {
|
||||||
|
QMessageBox::warning(&dlg, QStringLiteral(u"提示"), QStringLiteral(u"请先选择一条记录。"));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
connect(btnDelete, &QPushButton::clicked, [&]() {
|
||||||
|
int row = listWidget->currentRow();
|
||||||
|
if (row >= 0) {
|
||||||
|
_fitHistoryList.removeAt(row);
|
||||||
|
saveHistoryToFile();
|
||||||
|
delete listWidget->takeItem(row);
|
||||||
|
QMessageBox::information(&dlg, QStringLiteral(u"删除"), QStringLiteral(u"已删除该记录。"));
|
||||||
|
if (_fitHistoryList.isEmpty())
|
||||||
|
dlg.close();
|
||||||
|
} else {
|
||||||
|
QMessageBox::warning(&dlg, QStringLiteral(u"提示"), QStringLiteral(u"请先选择要删除的记录。"));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
dlg.exec();
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,9 @@
|
||||||
#include <QwtPickerDragRectMachine>
|
#include <QwtPickerDragRectMachine>
|
||||||
#include <QRubberBand> // 新增
|
#include <QRubberBand> // 新增
|
||||||
#include <qwt_plot_shapeitem.h> // 新增
|
#include <qwt_plot_shapeitem.h> // 新增
|
||||||
|
#include <QDateTime>
|
||||||
|
#include <QJsonArray>
|
||||||
|
#include <QJsonObject>
|
||||||
#include "PeakFitParamsDialog.h"
|
#include "PeakFitParamsDialog.h"
|
||||||
|
|
||||||
#include "DataCalcProcess/FindPeaksBySvd.h"
|
#include "DataCalcProcess/FindPeaksBySvd.h"
|
||||||
|
|
@ -25,7 +28,22 @@ struct PeakFitResult
|
||||||
double sigmoidH; // Sigmoid 项高度 H
|
double sigmoidH; // Sigmoid 项高度 H
|
||||||
double sigmoidW; // Sigmoid 项宽度 W
|
double sigmoidW; // Sigmoid 项宽度 W
|
||||||
};
|
};
|
||||||
|
struct PeakFitHistoryItem {
|
||||||
|
QDateTime timestamp; // 时间戳
|
||||||
|
double center; // 峰中心能量 (keV)
|
||||||
|
double fwhm; // 半高宽
|
||||||
|
double area; // 峰面积
|
||||||
|
double amplitude; // 幅度 A
|
||||||
|
double sigma; // 标准差 delt
|
||||||
|
double sigmoidH; // H
|
||||||
|
double sigmoidW; // W
|
||||||
|
double baseline; // C
|
||||||
|
double xMin; // 拟合曲线显示范围左边界
|
||||||
|
double xMax; // 拟合曲线显示范围右边界
|
||||||
|
|
||||||
|
QJsonObject toJson() const;
|
||||||
|
static PeakFitHistoryItem fromJson(const QJsonObject& obj);
|
||||||
|
};
|
||||||
|
|
||||||
class PlotRectItem; // 前向声明
|
class PlotRectItem; // 前向声明
|
||||||
class QMenu;
|
class QMenu;
|
||||||
|
|
@ -49,12 +67,21 @@ private:
|
||||||
void setupMenu();
|
void setupMenu();
|
||||||
void loadDataFromFile(const QString &data_name, const QString& filename);
|
void loadDataFromFile(const QString &data_name, const QString& filename);
|
||||||
|
|
||||||
|
|
||||||
|
void loadHistoryFromFile();
|
||||||
|
void saveHistoryToFile();
|
||||||
|
void saveCurrentFitToHistory(); // 保存当前拟合结果
|
||||||
|
void displayFitFromHistory(const PeakFitHistoryItem& item); // 显示历史拟合曲线
|
||||||
private slots:
|
private slots:
|
||||||
void onActionCurveShowSetting();
|
void onActionCurveShowSetting();
|
||||||
void onActionPlotConfigure();
|
void onActionPlotConfigure();
|
||||||
void clearAllSelectionRects();
|
void clearAllSelectionRects();
|
||||||
void clearFitCurves(); // 清除所有拟合曲线
|
void clearFitCurves(); // 清除所有拟合曲线
|
||||||
|
|
||||||
|
|
||||||
|
// [NEW] 新增菜单槽函数
|
||||||
|
void onActionSaveCurrentFit();
|
||||||
|
void onActionShowFitHistory();
|
||||||
protected:
|
protected:
|
||||||
bool eventFilter(QObject* watched, QEvent* event) override; // 事件过滤器
|
bool eventFilter(QObject* watched, QEvent* event) override; // 事件过滤器
|
||||||
|
|
||||||
|
|
@ -63,6 +90,7 @@ private:
|
||||||
void updateSelection(const QPoint& pos);
|
void updateSelection(const QPoint& pos);
|
||||||
void finishSelection();
|
void finishSelection();
|
||||||
void addSelectionRect(const QRectF& plotRect);
|
void addSelectionRect(const QRectF& plotRect);
|
||||||
|
void fadeSelectionRectBorders();
|
||||||
|
|
||||||
QList<PeakFitResult> performPeakFitting(const QVector<double>& x, const QVector<double>& y, const arma::vec* userP0 = nullptr);
|
QList<PeakFitResult> performPeakFitting(const QVector<double>& x, const QVector<double>& y, const arma::vec* userP0 = nullptr);
|
||||||
// 根据拟合参数生成曲线
|
// 根据拟合参数生成曲线
|
||||||
|
|
@ -83,6 +111,18 @@ private:
|
||||||
// 存储当前显示的拟合曲线,用于清除
|
// 存储当前显示的拟合曲线,用于清除
|
||||||
QList<QwtPlotCurve*> _fitCurves;
|
QList<QwtPlotCurve*> _fitCurves;
|
||||||
|
|
||||||
|
|
||||||
|
// [NEW] 历史记录相关成员
|
||||||
|
QList<PeakFitHistoryItem> _fitHistoryList;
|
||||||
|
QString _historyFilePath;
|
||||||
|
|
||||||
|
// [NEW] 最近一次拟合结果(用于手动保存)
|
||||||
|
PeakFitResult _lastFitResult;
|
||||||
|
arma::vec _lastFitParams; // 6个拟合参数 (A, delt, H, W, C, P)
|
||||||
|
double _lastXMin = 0.0, _lastXMax = 0.0;
|
||||||
|
bool _hasLastFit = false;
|
||||||
|
QString _workspace;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // ENERGYCOUNTPEAKFITVIEW_H
|
#endif // ENERGYCOUNTPEAKFITVIEW_H
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user