EnergySpectrumAnalyer/src/MeasureAnalysisParticleCountPlotView.cpp

372 lines
16 KiB
C++
Raw Normal View History

#include "MeasureAnalysisParticleCountPlotView.h"
#include "CustomQwtPlot.h"
#include "csv.h"
#include <GlobalDefine.h>
#include <QHBoxLayout>
#include <QPen>
#include <QRegularExpression>
#include <QVector>
#include <QwtLegend>
#include <QwtPlotCanvas>
#include <QwtPlotCurve>
#include <QMenu>
#include <QAction>
#include <QCheckBox>
#include <QDialog>
#include <QwtText>
#include <QPushButton>
2026-03-11 13:59:46 +08:00
#include "MeasureAnalysisProjectModel.h"
#include <QDir>
#include "DataProcessWorkPool.h"
#include <QLabel>
#include <QComboBox>
#include <QTableWidget>
#include <QSpinBox>
MeasureAnalysisParticleCountPlotView::MeasureAnalysisParticleCountPlotView(QWidget* parent)
: MeasureAnalysisView { parent }
{
this->setViewType(PlotFrame);
QHBoxLayout* layout = new QHBoxLayout(this);
this->_plot = new CustomQwtPlot(this);
layout->addWidget(this->_plot);
setupPlot();
this->_menu = new QMenu(this);
setupMenu();
}
2026-03-11 13:59:46 +08:00
void MeasureAnalysisParticleCountPlotView::InitViewWorkspace(const QString &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());
QString workspace = project_dir.filePath("AddressCountSpectrumView");
if ( QDir(workspace).exists() ) {
this->_workspace = workspace;
} else if (project_dir.mkpath(workspace) ) {
this->_workspace = workspace;
}
}
void MeasureAnalysisParticleCountPlotView::SetAnalyzeDataFilename(const QMap<QString, QVariant>& data_files_set)
{
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-11 13:59:46 +08:00
_data_files_set_ptr = data_files_set;
}
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);
2026-03-11 13:59:46 +08:00
QMenu* menu_find_peaks = this->_menu->addMenu(QStringLiteral(u"寻峰"));
QAction* action_find_peaks_result = menu_find_peaks->addAction(QStringLiteral(u"寻峰结果"));
action_find_peaks_result->setObjectName("manual_find_peaks");
connect(action_find_peaks_result, &QAction::triggered, this, &MeasureAnalysisParticleCountPlotView::onFindPeaksResult);
QAction* action_auto_find_peaks = menu_find_peaks->addAction(QStringLiteral(u"自动寻峰"));
action_auto_find_peaks->setObjectName("auto_find_peaks");
connect(action_auto_find_peaks, &QAction::triggered, this, &MeasureAnalysisParticleCountPlotView::onAutoFindPeaks);
QAction* action_manual_find_peaks = menu_find_peaks->addAction(QStringLiteral(u"手动寻峰"));
action_manual_find_peaks->setObjectName("manual_find_peaks");
connect(action_auto_find_peaks, &QAction::triggered, this, &MeasureAnalysisParticleCountPlotView::onManualFindPeaks);
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);
}
void MeasureAnalysisParticleCountPlotView::setupPlot()
{
_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);
// 启用网格线
_plot->enableAxis(QwtPlot::xBottom);
_plot->enableAxis(QwtPlot::yLeft);
// 设置QWT图例
QwtLegend* legend = new QwtLegend();
legend->setDefaultItemMode(QwtLegendData::Checkable);
_plot->insertLegend(legend, QwtPlot::RightLegend);
}
void MeasureAnalysisParticleCountPlotView::loadDataFromFile(const QString& data_name, const QString& filename)
{
std::string address_str = QString(QStringLiteral(u"道址")).toStdString();
std::string count_str = QString(QStringLiteral(u"计数")).toStdString();
io::CSVReader<
2,
io::trim_chars<' ', '\t'>,
io::double_quote_escape<',', '"'>,
io::throw_on_overflow,
io::empty_line_comment>
reader(QStrToSysPath(filename));
reader.read_header(io::ignore_extra_column, address_str, count_str);
int address;
int particle_count;
QVector<float> x, y;
2026-03-11 13:59:46 +08:00
while (reader.read_row(address, particle_count)) {
x.push_back(address);
y.push_back(particle_count);
}
// 绘制曲线
QwtPlotCurve* curve = new QwtPlotCurve(data_name);
curve->setSamples(x, y);
_plot->AddCurve(curve);
2026-03-11 13:59:46 +08:00
}
void MeasureAnalysisParticleCountPlotView::onAutoFindPeaksFinished(const QString &project_name)
{
Q_UNUSED(project_name);
onFindPeaksResult();
}
void MeasureAnalysisParticleCountPlotView::onCurveShowSetting()
{
2026-03-11 13:59:46 +08:00
if (!_curve_show_setting_dlg) {
_curve_show_setting_dlg = new QDialog(this, Qt::Dialog | Qt::WindowCloseButtonHint);
_curve_show_setting_dlg->setWindowTitle(QString(QStringLiteral(u"选择%1曲线显示").arg(this->_plot->title().text())));
_curve_show_setting_dlg->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
_curve_show_setting_dlg->setWindowModality(Qt::WindowModal);
_curve_show_setting_dlg->setModal(false);
QVBoxLayout* layout = new QVBoxLayout(_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()) {
2026-03-11 13:59:46 +08:00
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();
}
}
2026-03-11 13:59:46 +08:00
if (checkbox_column_layout->count() < num_columns) {
checkbox_column_layout->addStretch();
}
2026-03-11 13:59:46 +08:00
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->show();
}
void MeasureAnalysisParticleCountPlotView::onFindPeaksResult()
{
if (!_find_peaks_result_dlg) {
const QString& peaks_result_filename = QDir(this->_workspace).filePath("AutoFindPeaksResult.csv");
_find_peaks_result_dlg = new QDialog(this, Qt::Dialog | Qt::WindowCloseButtonHint);
_find_peaks_result_dlg->setWindowTitle(QString(QStringLiteral(u"寻峰结果")));
_find_peaks_result_dlg->setWindowModality(Qt::WindowModal);
_find_peaks_result_dlg->setModal(false);
QLabel* filter_channel_label = new QLabel(QString(QStringLiteral(u"筛选通道:")));
QComboBox* filter_channel_combo_box = new QComboBox();
filter_channel_combo_box->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
filter_channel_combo_box->addItem(QString(QStringLiteral(u"所有通道")));
for (QwtPlotCurve* curve : this->_plot->GetCurveList()) {
2026-03-11 13:59:46 +08:00
filter_channel_combo_box->addItem(curve->title().text());
}
2026-03-11 13:59:46 +08:00
QHBoxLayout* filter_channel_layout = new QHBoxLayout();
filter_channel_layout->addWidget(filter_channel_label);
filter_channel_layout->addWidget(filter_channel_combo_box);
// filter_channel_layout->addStretch();
const QString& channel_col_name = QString(QStringLiteral(u"通道"));
const QString& peak_pos_col_name = QString(QStringLiteral(u"峰位"));
const QString& left_bound_col_name = QString(QStringLiteral(u"左边界"));
const QString& right_bound_col_name = QString(QStringLiteral(u"右边界"));
const QString& peak_width_col_name = QString(QStringLiteral(u"峰宽"));
QTableWidget* peaks_result_table = new QTableWidget();
peaks_result_table->setColumnCount(5);
peaks_result_table->setHorizontalHeaderLabels({
channel_col_name, peak_pos_col_name, left_bound_col_name, right_bound_col_name, peak_width_col_name
});
peaks_result_table->setSelectionBehavior(QAbstractItemView::SelectRows);
peaks_result_table->setSelectionMode(QAbstractItemView::SingleSelection);
// 使用csv.h从peaks_result_filename CSV文件读取数据
io::CSVReader<
5,
io::trim_chars<' ', '\t'>,
io::double_quote_escape<',', '"'>,
io::throw_on_overflow,
io::empty_line_comment
> reader(QStrToSysPath(peaks_result_filename));
reader.read_header(io::ignore_extra_column,
channel_col_name.toStdString(),
peak_pos_col_name.toStdString(),
left_bound_col_name.toStdString(),
right_bound_col_name.toStdString(),
peak_width_col_name.toStdString()
);
std::string ch_name; int peak_pos; int left_bound, right_bound, peak_width;
while (reader.read_row(ch_name, peak_pos, left_bound, right_bound, peak_width))
{
if (!ch_name.empty()) {
int row = peaks_result_table->rowCount();
peaks_result_table->insertRow(row);
peaks_result_table->setItem(row, 0, new QTableWidgetItem(QString::fromStdString(ch_name)));
peaks_result_table->setItem(row, 1, new QTableWidgetItem(QString::number(peak_pos)));
peaks_result_table->setItem(row, 2, new QTableWidgetItem(QString::number(left_bound)));
peaks_result_table->setItem(row, 3, new QTableWidgetItem(QString::number(right_bound)));
peaks_result_table->setItem(row, 4, new QTableWidgetItem(QString::number(peak_width)));
}
}
connect(filter_channel_combo_box, &QComboBox::currentTextChanged, this, [this, peaks_result_table](const QString& text){
auto row_count = peaks_result_table->rowCount();
if (text == QString(QStringLiteral(u"所有通道"))) {
for (int i = 0; i < row_count - 1; i++) {
peaks_result_table->setRowHidden(i, false);
}
} else {
for (int i = row_count - 1; i >= 0 ; i--) {
const QString& channel = peaks_result_table->item(i, 0)->text();
bool is_hidden = text == channel ? false : true;
peaks_result_table->setRowHidden(i, is_hidden);
}
}
});
2026-03-11 13:59:46 +08:00
QVBoxLayout* layout = new QVBoxLayout(_find_peaks_result_dlg);
layout->addLayout(filter_channel_layout);
layout->addWidget(peaks_result_table);
}
_find_peaks_result_dlg->show();
}
void MeasureAnalysisParticleCountPlotView::onAutoFindPeaks()
2026-03-11 13:59:46 +08:00
{
QDialog set_find_peak_step_win_width_dlg(this, Qt::Dialog | Qt::WindowCloseButtonHint);
set_find_peak_step_win_width_dlg.setWindowTitle(QString(QStringLiteral(u"设置自动寻峰步宽")));
QLabel* set_step_width_label = new QLabel(QString(QStringLiteral(u"自动寻峰步宽:")));
QSpinBox * spinbox_set_step_width = new QSpinBox();
spinbox_set_step_width->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
QVBoxLayout* layout_input = new QVBoxLayout();
layout_input->addWidget(set_step_width_label);
layout_input->addWidget(spinbox_set_step_width);
QPushButton* btn_ok = new QPushButton(QStringLiteral(u"确认"));
connect(btn_ok, &QPushButton::clicked, &set_find_peak_step_win_width_dlg, &QDialog::accept);
QPushButton* btn_cancel = new QPushButton(QStringLiteral(u"取消"));
connect(btn_ok, &QPushButton::clicked, &set_find_peak_step_win_width_dlg, &QDialog::reject);
QHBoxLayout* layout_btns = new QHBoxLayout();
layout_btns->addStretch();
layout_btns->addWidget(btn_ok);
layout_btns->addWidget(btn_cancel);
QHBoxLayout* layout = new QHBoxLayout(&set_find_peak_step_win_width_dlg);
layout->addLayout(layout_input);
layout->addLayout(layout_btns);
if (QDialog::Accepted == set_find_peak_step_win_width_dlg.exec() ) {
int step_width = spinbox_set_step_width->value();
const QString& project_name = GetProjectName();
auto auto_find_peaks_task = new DataProcessWorkPool::AutoFindPeaksTask;
auto_find_peaks_task->SetAnalysisType(this->GetAnalyzeType());
auto_find_peaks_task->SetDataFileList(this->_data_files_set_ptr);
auto_find_peaks_task->SetFindPeakSetpWinWidth(step_width);
auto_find_peaks_task->SetResultDir(this->_workspace);
auto_find_peaks_task->SetFinishedNotifier(this, "onAutoFindPeaksFinished", project_name);
auto_find_peaks_task->StartTask();
}
}
void MeasureAnalysisParticleCountPlotView::onManualFindPeaks()
{
}
void MeasureAnalysisParticleCountPlotView::onEneryScale()
{
}
void MeasureAnalysisParticleCountPlotView::onPlotConfigure()
{
}