2026-03-03 18:15:34 +08:00
|
|
|
#include "MeasureAnalysisParticleCountPlotView.h"
|
2026-03-09 16:50:21 +08:00
|
|
|
#include "CustomQwtPlot.h"
|
2026-03-03 18:15:34 +08:00
|
|
|
#include "csv.h"
|
2026-03-04 21:16:35 +08:00
|
|
|
#include <GlobalDefine.h>
|
|
|
|
|
#include <QHBoxLayout>
|
2026-03-09 16:50:21 +08:00
|
|
|
#include <QPen>
|
|
|
|
|
#include <QRegularExpression>
|
|
|
|
|
#include <QVector>
|
2026-03-05 15:46:39 +08:00
|
|
|
#include <QwtLegend>
|
|
|
|
|
#include <QwtPlotCanvas>
|
2026-03-09 16:50:21 +08:00
|
|
|
#include <QwtPlotCurve>
|
2026-03-09 21:57:26 +08:00
|
|
|
#include <QMenu>
|
|
|
|
|
#include <QAction>
|
|
|
|
|
#include <QCheckBox>
|
|
|
|
|
#include <QDialog>
|
|
|
|
|
#include <QwtText>
|
|
|
|
|
#include <QPushButton>
|
2026-03-03 18:15:34 +08:00
|
|
|
|
|
|
|
|
MeasureAnalysisParticleCountPlotView::MeasureAnalysisParticleCountPlotView(QWidget* parent)
|
|
|
|
|
: MeasureAnalysisView { parent }
|
|
|
|
|
{
|
2026-03-06 14:05:15 +08:00
|
|
|
this->setViewType(PlotFrame);
|
2026-03-09 16:50:21 +08:00
|
|
|
|
2026-03-04 21:16:35 +08:00
|
|
|
QHBoxLayout* layout = new QHBoxLayout(this);
|
2026-03-09 21:57:26 +08:00
|
|
|
this->_plot = new CustomQwtPlot(this);
|
|
|
|
|
layout->addWidget(this->_plot);
|
2026-03-04 21:16:35 +08:00
|
|
|
|
2026-03-05 15:46:39 +08:00
|
|
|
setupPlot();
|
2026-03-09 21:57:26 +08:00
|
|
|
|
|
|
|
|
this->_menu = new QMenu(this);
|
|
|
|
|
setupMenu();
|
2026-03-03 18:15:34 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-09 16:50:21 +08:00
|
|
|
void MeasureAnalysisParticleCountPlotView::SetAnalyzeDataFilename(const QMap<QString, QVariant>& data_files_set)
|
2026-03-03 18:15:34 +08:00
|
|
|
{
|
2026-03-09 16:50:21 +08:00
|
|
|
auto extractNumber = [](const QString& str) {
|
|
|
|
|
int ret_num = 0;
|
|
|
|
|
QRegularExpression regex("\\d+");
|
|
|
|
|
QRegularExpressionMatch match = regex.match(str);
|
|
|
|
|
if (match.hasMatch()) {
|
|
|
|
|
ret_num = match.captured().toInt();
|
|
|
|
|
}
|
|
|
|
|
return ret_num;
|
|
|
|
|
};
|
|
|
|
|
QStringList ch_count_data_name = data_files_set.keys();
|
|
|
|
|
std::sort(ch_count_data_name.begin(), ch_count_data_name.end(), [extractNumber](const QString& a, const QString& b) {
|
|
|
|
|
int num_a = extractNumber(a);
|
|
|
|
|
int num_b = extractNumber(b);
|
|
|
|
|
return num_a < num_b;
|
|
|
|
|
});
|
|
|
|
|
for (const QString& ch_count_data_name : ch_count_data_name) {
|
|
|
|
|
if (ch_count_data_name.contains(ch_count_data_name)) {
|
|
|
|
|
const QString ch_count_data_filename = data_files_set[ch_count_data_name].toString();
|
|
|
|
|
loadDataFromFile(ch_count_data_name, ch_count_data_filename);
|
|
|
|
|
}
|
2026-03-03 18:15:34 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-09 21:57:26 +08:00
|
|
|
void MeasureAnalysisParticleCountPlotView::setupMenu()
|
|
|
|
|
{
|
|
|
|
|
this->setContextMenuPolicy(Qt::CustomContextMenu);
|
|
|
|
|
connect(this, &MeasureAnalysisParticleCountPlotView::customContextMenuRequested, this, [this](const QPoint &pos){
|
|
|
|
|
this->_menu->exec(this->mapToGlobal(pos));
|
|
|
|
|
});
|
|
|
|
|
QAction* action_set_curve_show = this->_menu->addAction(QStringLiteral(u"选择曲线"));
|
|
|
|
|
action_set_curve_show->setObjectName("curve_show_setting");
|
|
|
|
|
connect(action_set_curve_show, &QAction::triggered, this, &MeasureAnalysisParticleCountPlotView::onCurveShowSetting);
|
|
|
|
|
QAction* action_find_peaks = this->_menu->addAction(QStringLiteral(u"自动寻峰"));
|
|
|
|
|
action_find_peaks->setObjectName("auto_find_peaks");
|
|
|
|
|
connect(action_find_peaks, &QAction::triggered, this, &MeasureAnalysisParticleCountPlotView::onAutoFindPeaks);
|
|
|
|
|
QAction* action_set_energy_scale = this->_menu->addAction(QStringLiteral(u"能量刻度"));
|
|
|
|
|
action_set_energy_scale->setObjectName("energy_scale");
|
|
|
|
|
connect(action_set_energy_scale, &QAction::triggered, this, &MeasureAnalysisParticleCountPlotView::onEneryScale);
|
|
|
|
|
QAction* action_plot_config = this->_menu->addAction(QStringLiteral(u"图表配置"));
|
|
|
|
|
action_plot_config->setObjectName("plot_config");
|
|
|
|
|
connect(action_plot_config, &QAction::triggered, this, &MeasureAnalysisParticleCountPlotView::onPlotConfigure);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-05 15:46:39 +08:00
|
|
|
void MeasureAnalysisParticleCountPlotView::setupPlot()
|
2026-03-03 18:15:34 +08:00
|
|
|
{
|
2026-03-05 15:46:39 +08:00
|
|
|
_plot->setTitle(QString(QStringLiteral(u"粒子计数谱")));
|
|
|
|
|
|
|
|
|
|
_plot->setCanvasBackground(Qt::white);
|
|
|
|
|
QwtPlotCanvas* canvas = qobject_cast<QwtPlotCanvas*>(_plot->canvas());
|
|
|
|
|
canvas->setFrameStyle(QFrame::NoFrame);
|
|
|
|
|
|
|
|
|
|
_plot->setAxisTitle(QwtPlot::xBottom, QString(QStringLiteral(u"道址")));
|
|
|
|
|
_plot->setAxisTitle(QwtPlot::yLeft, QString(QStringLiteral(u"计数")));
|
|
|
|
|
// set axis auto scale
|
|
|
|
|
_plot->setAxisAutoScale(QwtPlot::xBottom, true);
|
|
|
|
|
_plot->setAxisAutoScale(QwtPlot::yLeft, true);
|
2026-03-06 14:05:15 +08:00
|
|
|
// 启用网格线
|
|
|
|
|
_plot->enableAxis(QwtPlot::xBottom);
|
|
|
|
|
_plot->enableAxis(QwtPlot::yLeft);
|
2026-03-05 15:46:39 +08:00
|
|
|
|
|
|
|
|
// 设置QWT图例
|
2026-03-09 16:50:21 +08:00
|
|
|
QwtLegend* legend = new QwtLegend();
|
|
|
|
|
legend->setDefaultItemMode(QwtLegendData::Checkable);
|
2026-03-05 15:46:39 +08:00
|
|
|
_plot->insertLegend(legend, QwtPlot::RightLegend);
|
2026-03-03 18:15:34 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-04 21:16:35 +08:00
|
|
|
void MeasureAnalysisParticleCountPlotView::loadDataFromFile(const QString& data_name, const QString& filename)
|
2026-03-03 18:15:34 +08:00
|
|
|
{
|
|
|
|
|
std::string address_str = QString(QStringLiteral(u"道址")).toStdString();
|
|
|
|
|
std::string count_str = QString(QStringLiteral(u"计数")).toStdString();
|
2026-03-09 16:50:21 +08:00
|
|
|
|
2026-03-03 18:15:34 +08:00
|
|
|
io::CSVReader<
|
|
|
|
|
2,
|
|
|
|
|
io::trim_chars<' ', '\t'>,
|
|
|
|
|
io::double_quote_escape<',', '"'>,
|
|
|
|
|
io::throw_on_overflow,
|
2026-03-09 16:50:21 +08:00
|
|
|
io::empty_line_comment>
|
|
|
|
|
reader(QStrToSysPath(filename));
|
2026-03-03 18:15:34 +08:00
|
|
|
reader.read_header(io::ignore_extra_column, address_str, count_str);
|
|
|
|
|
|
|
|
|
|
int address;
|
|
|
|
|
int particle_count;
|
2026-03-05 15:46:39 +08:00
|
|
|
QVector<float> x, y;
|
2026-03-03 18:15:34 +08:00
|
|
|
while (reader.read_row(address, particle_count)) {
|
|
|
|
|
x.push_back(address);
|
|
|
|
|
y.push_back(particle_count);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-05 15:46:39 +08:00
|
|
|
// 绘制曲线
|
|
|
|
|
QwtPlotCurve* curve = new QwtPlotCurve(data_name);
|
|
|
|
|
curve->setSamples(x, y);
|
2026-03-09 16:50:21 +08:00
|
|
|
_plot->AddCurve(curve);
|
|
|
|
|
LOG_DEBUG(data_name);
|
2026-03-03 18:15:34 +08:00
|
|
|
}
|
2026-03-09 21:57:26 +08:00
|
|
|
|
|
|
|
|
void MeasureAnalysisParticleCountPlotView::onCurveShowSetting()
|
|
|
|
|
{
|
|
|
|
|
QDialog curve_show_setting_dlg(nullptr, Qt::WindowCloseButtonHint | Qt::WindowStaysOnTopHint);
|
|
|
|
|
curve_show_setting_dlg.setWindowTitle(QString(QStringLiteral(u"选择曲线")));
|
|
|
|
|
curve_show_setting_dlg.setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
|
|
|
|
|
curve_show_setting_dlg.setWindowModality(Qt::NonModal);
|
|
|
|
|
curve_show_setting_dlg.setModal(false);
|
|
|
|
|
QVBoxLayout layout(&curve_show_setting_dlg);
|
|
|
|
|
// 自动计算多列排布
|
|
|
|
|
QMap<QwtPlotCurve*, QCheckBox*> curve_checkbox_map;
|
|
|
|
|
int num_columns = std::sqrt(this->_plot->GetCurveList().size());
|
|
|
|
|
if (num_columns == 0) num_columns = 1;
|
|
|
|
|
QVBoxLayout* checkbox_layout = new QVBoxLayout();
|
|
|
|
|
QHBoxLayout* checkbox_column_layout = new QHBoxLayout();
|
|
|
|
|
for (QwtPlotCurve* curve : this->_plot->GetCurveList()) {
|
|
|
|
|
QCheckBox* check_box = new QCheckBox(curve->title().text());
|
|
|
|
|
check_box->setChecked(curve->isVisible());
|
|
|
|
|
checkbox_column_layout->addWidget(check_box);
|
|
|
|
|
connect(check_box, &QCheckBox::stateChanged, this, [curve](int state){
|
|
|
|
|
curve->setVisible(state == Qt::Checked);
|
|
|
|
|
curve->plot()->replot();
|
|
|
|
|
});
|
|
|
|
|
curve_checkbox_map[curve] = check_box;
|
|
|
|
|
if (checkbox_column_layout->count() >= num_columns) {
|
|
|
|
|
checkbox_layout->addLayout(checkbox_column_layout);
|
|
|
|
|
checkbox_column_layout = new QHBoxLayout();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (checkbox_column_layout->count() < num_columns) {
|
|
|
|
|
checkbox_column_layout->addStretch();
|
|
|
|
|
}
|
|
|
|
|
checkbox_layout->addLayout(checkbox_column_layout);
|
|
|
|
|
// 全选和反选
|
|
|
|
|
auto curveCheckboxUpdate = [this, curve_checkbox_map](){
|
|
|
|
|
for (QwtPlotCurve* curve : this->_plot->GetCurveList()) {
|
|
|
|
|
curve_checkbox_map[curve]->setChecked(curve->isVisible());
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
QHBoxLayout* button_layout = new QHBoxLayout();
|
|
|
|
|
QPushButton* btn_all_select = new QPushButton(QString(QStringLiteral(u"全选")));
|
|
|
|
|
connect(btn_all_select, &QPushButton::clicked, this, [this, curveCheckboxUpdate](){
|
|
|
|
|
for (QwtPlotCurve* curve : this->_plot->GetCurveList()) {
|
|
|
|
|
curve->setVisible(true);
|
|
|
|
|
}
|
|
|
|
|
curveCheckboxUpdate();
|
|
|
|
|
this->_plot->replot();
|
|
|
|
|
});
|
|
|
|
|
button_layout->addWidget(btn_all_select);
|
|
|
|
|
QPushButton* btn_reserve_select = new QPushButton(QString(QStringLiteral(u"反选")));
|
|
|
|
|
connect(btn_reserve_select, &QPushButton::clicked, this, [this, curveCheckboxUpdate](){
|
|
|
|
|
for (QwtPlotCurve* curve : this->_plot->GetCurveList()) {
|
|
|
|
|
curve->setVisible(!curve->isVisible());
|
|
|
|
|
}
|
|
|
|
|
curveCheckboxUpdate();
|
|
|
|
|
this->_plot->replot();
|
|
|
|
|
});
|
|
|
|
|
button_layout->addWidget(btn_reserve_select);
|
|
|
|
|
|
|
|
|
|
layout.addLayout(button_layout);
|
|
|
|
|
layout.addLayout(checkbox_layout);
|
|
|
|
|
curve_show_setting_dlg.exec();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MeasureAnalysisParticleCountPlotView::onAutoFindPeaks()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MeasureAnalysisParticleCountPlotView::onEneryScale()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MeasureAnalysisParticleCountPlotView::onPlotConfigure()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|