#include "ParticleCountPlotView.h" #include "CustomQwtPlot.h" #include "DataProcessWorkPool.h" #include "MeasureAnalysisProjectModel.h" #include "csv.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "BatchEnergyScaleDialog.h" #include "FindPeaksResultDialog.h" ParticleCountPlotView::ParticleCountPlotView(QWidget* parent) : MeasureAnalysisView { parent } { this->setViewType(PlotFrame); QHBoxLayout* layout = new QHBoxLayout(this); this->_plot = new CustomQwtPlot(this); layout->addWidget(this->_plot); this->_menu = new QMenu(this); setupPlot(); setupMenu(); setupPeakResultDlg(); setupEnergyScaleDlg(); } ParticleCountPlotView::~ParticleCountPlotView() { LOG_DEBUG(QStringLiteral(u"%1析构.").arg(this->GetViewName())); } void ParticleCountPlotView::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(this->GetViewName()); if (QDir(workspace).exists()) { this->_workspace = workspace; } else if (project_dir.mkpath(workspace)) { this->_workspace = workspace; } QString win_title = _find_peaks_result_dlg->windowTitle() + QStringLiteral(u"[%1]").arg(project_name); _find_peaks_result_dlg->setWindowTitle(win_title); const QString& peaks_result_filename = QDir(workspace).filePath(QStringLiteral(u"自动寻峰结果.csv")); _find_peaks_result_dlg->SetPeakResultDataFilename(peaks_result_filename); _find_peaks_result_dlg->UpdatePeakResult(); win_title = _batch_energy_scale_dlg->windowTitle() + QStringLiteral(u"[%1]").arg(project_name); _batch_energy_scale_dlg->setWindowTitle(win_title); _batch_energy_scale_dlg->SetProjectName(project_name); _batch_energy_scale_dlg->SetViewWorkspace(workspace); _batch_energy_scale_dlg->SetPeakResultDataModel(_find_peaks_result_dlg->GetPeakResultDataModel()); } void ParticleCountPlotView::SetAnalyzeDataFilename(const QMap& data_files_set) { QStringList ch_count_data_name = data_files_set.keys(); std::sort(ch_count_data_name.begin(), ch_count_data_name.end(), [](const QString& a, const QString& b) { int num_a = ExtractNumberFromString(a); int num_b = ExtractNumberFromString(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); } } _data_files_set_ptr = data_files_set; QStringList list_ch_names; for (QwtPlotCurve* curve : this->_plot->GetCurveList()) { list_ch_names.append(curve->title().text()); } std::sort(list_ch_names.begin(), list_ch_names.end(), [](const QString& a, const QString& b) { int num_a = ExtractNumberFromString(a); int num_b = ExtractNumberFromString(b); return num_a < num_b; }); if (_find_peaks_result_dlg ) { _find_peaks_result_dlg->SetChannelNameList(list_ch_names); } if (_batch_energy_scale_dlg ) { _batch_energy_scale_dlg->SetChannelNameList(list_ch_names); } if (_batch_energy_scale_dlg) { _batch_energy_scale_dlg->LoadEnergyScaleData(); } } void ParticleCountPlotView::setupMenu() { this->setContextMenuPolicy(Qt::CustomContextMenu); connect(this, &ParticleCountPlotView::customContextMenuRequested, [this](const QPoint& pos) { this->_menu->exec(this->mapToGlobal(pos)); }); QAction* action_plot_reset = this->_menu->addAction(QStringLiteral(u"还原")); action_plot_reset->setObjectName("plot_reset"); connect(action_plot_reset, &QAction::triggered, [this]() { this->_plot->ResetPlot(); }); this->_menu->addSeparator(); 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, &ParticleCountPlotView::onActionCurveShowSetting); 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, &ParticleCountPlotView::onActionFindPeaksResult); 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, &ParticleCountPlotView::onActionAutoFindPeaks); 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, &ParticleCountPlotView::onActionManualFindPeaks); 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, &ParticleCountPlotView::onActionEnergyScale); QAction* action_plot_config = this->_menu->addAction(QStringLiteral(u"图表配置")); action_plot_config->setObjectName("plot_config"); connect(action_plot_config, &QAction::triggered, this, &ParticleCountPlotView::onActionPlotConfigure); } void ParticleCountPlotView::setupPlot() { _plot->setCanvasBackground(Qt::white); QwtPlotCanvas* canvas = qobject_cast(_plot->canvas()); canvas->setFrameStyle(QFrame::NoFrame); QFont font = this->font(); font.setBold(false); QwtText addr_label = QStringLiteral(u"道址"); addr_label.setFont(font); QwtText count_label = QStringLiteral(u"计数"); count_label.setFont(font); _plot->setAxisTitle(QwtPlot::xBottom, addr_label); _plot->setAxisTitle(QwtPlot::yLeft, count_label); // 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::ReadOnly); _plot->insertLegend(legend, QwtPlot::RightLegend); _plot->SetAxisDragScale(QwtPlot::xBottom, true); // new CustomQwtPlotXaxisPanner(_plot->canvas()); // new CustomQwtPlotXaxisMagnifier(_plot->canvas()); _data_selector = new CustomQwtPlotXaxisSelector(_plot->canvas()); _data_selector->setEnabled(false); } void ParticleCountPlotView::setupPeakResultDlg() { if (!_find_peaks_result_dlg) { _find_peaks_result_dlg = new FindPeaksResultDialog(this); connect(_find_peaks_result_dlg, &FindPeaksResultDialog::peakInfoChanged, [this](QVariantMap peak_infos){ this->updatePlotPeakInfo(peak_infos); }); } } void ParticleCountPlotView::setupEnergyScaleDlg() { if (!_batch_energy_scale_dlg) { _batch_energy_scale_dlg = new BatchEnergyScaleDialog(this); connect(_data_selector, &CustomQwtPlotXaxisSelector::selectionFinished, _batch_energy_scale_dlg, &BatchEnergyScaleDialog::onSelectedScaleRange); connect(_batch_energy_scale_dlg, &BatchEnergyScaleDialog::close, [this](){ this->_data_selector->setEnabled(false); }); } } void ParticleCountPlotView::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; unsigned long long particle_count; QVector x, y; 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); } void ParticleCountPlotView::updatePlotPeakInfo(const QVariantMap &peak_infos) { const QString& channel = peak_infos["channel"].toString(); int peak_pos = peak_infos["peak_pos"].toInt(); int left_bound = peak_infos["left_bound"].toInt(); int right_bound = peak_infos["right_bound"].toInt(); int peak_width = peak_infos["peak_width"].toInt(); int peak_height = peak_infos["peak_height"].toInt(); int peak_fwhm = peak_infos["peak_fwhm"].toInt(); double peak_area = peak_infos["peak_area"].toDouble(); bool is_checked = peak_infos["checked"].toBool(); bool is_show_peak_area = peak_infos["show_peak_area"].toBool(); const QString& postion = QString::number(peak_pos); QwtPlotMarker* peak_marker = this->_plot->GetMarker(channel, postion); if (is_checked) { if (!peak_marker) { peak_marker = new QwtPlotMarker(); peak_marker->setLineStyle(QwtPlotMarker::VLine); peak_marker->setValue(peak_pos, 0.0); peak_marker->setLabelAlignment(Qt::AlignTop | Qt::AlignRight); const QString& peak_info_text = QStringLiteral(u"峰位:%1\n峰宽:%2\n左界:%3\n右界:%4\n峰高:%5\nFWHM:%6\n峰面积:%7\n") .arg(peak_pos).arg(peak_width).arg(left_bound).arg(right_bound) .arg(peak_height).arg(peak_fwhm).arg(peak_area); peak_marker->setLabel(peak_info_text); this->_plot->AddMarker(peak_marker, channel, postion); } QwtText label_text = peak_marker->label(); if (is_show_peak_area) { label_text.setColor(Qt::black); peak_marker->setZ(peak_marker->z() + 1); } else { label_text.setColor(Qt::transparent); peak_marker->setZ(peak_marker->z() - 1); } peak_marker->setLabel(label_text); } else { if (peak_marker) { this->_plot->RemoveMarker(channel, postion); } } QwtPlotZoneItem* peak_area_zone_item = this->_plot->GetZoneItem(channel); if (!peak_area_zone_item && is_show_peak_area) { peak_area_zone_item = new QwtPlotZoneItem; peak_area_zone_item->setOrientation(Qt::Vertical); peak_area_zone_item->setInterval(left_bound, right_bound); this->_plot->AddZoneItem(peak_area_zone_item, channel); } else if ((!is_checked) || (!is_show_peak_area)) { this->_plot->RemoveZoneItem(channel); } this->_plot->replot(); } void ParticleCountPlotView::onAutoFindPeaksFinished(bool ok, const QString &project_name, const QVariant& data) { Q_UNUSED(project_name); Q_UNUSED(data); if ( !ok ) return; this->_plot->CleanMarkers(); this->_plot->replot(); if (this->_find_peaks_result_dlg) { this->_find_peaks_result_dlg->UpdatePeakResult(); _find_peaks_result_dlg->show(); } } void ParticleCountPlotView::onActionCurveShowSetting() { if (!_curve_show_setting_dlg) { _curve_show_setting_dlg = new QDialog(this, Qt::Dialog | Qt::WindowCloseButtonHint); _curve_show_setting_dlg->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); _curve_show_setting_dlg->setSizeGripEnabled(false); _curve_show_setting_dlg->setWindowTitle(QString(QStringLiteral(u"选择%1曲线显示").arg(this->_plot->title().text()))); _curve_show_setting_dlg->setWindowModality(Qt::WindowModal); _curve_show_setting_dlg->setModal(false); QVBoxLayout* layout = new QVBoxLayout(_curve_show_setting_dlg); // 自动计算多列排布 QMap 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(); QStringList list_ch_names; for (QwtPlotCurve* curve : this->_plot->GetCurveList()) { list_ch_names.append(curve->title().text()); } std::sort(list_ch_names.begin(), list_ch_names.end(), [](const QString& a, const QString& b) { int num_a = ExtractNumberFromString(a); int num_b = ExtractNumberFromString(b); return num_a < num_b; }); for (const QString& ch_name : list_ch_names) { QwtPlotCurve* curve = this->_plot->GetCurve(ch_name); QCheckBox* check_box = new QCheckBox(curve->title().text()); check_box->setChecked(curve->isVisible()); checkbox_column_layout->addWidget(check_box); connect(check_box, &QCheckBox::stateChanged, [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, 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, 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 ParticleCountPlotView::onActionFindPeaksResult() { if (_find_peaks_result_dlg) { _find_peaks_result_dlg->show(); } } void ParticleCountPlotView::onActionAutoFindPeaks() { QDialog set_find_peak_step_win_width_dlg(this, Qt::Dialog | Qt::WindowCloseButtonHint); set_find_peak_step_win_width_dlg.setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); set_find_peak_step_win_width_dlg.setFixedSize(300, 100); set_find_peak_step_win_width_dlg.setSizeGripEnabled(false); 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::Fixed); spinbox_set_step_width->setRange(7, 65535); QHBoxLayout* layout_input = new QHBoxLayout(); 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_cancel, &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); QVBoxLayout* layout = new QVBoxLayout(&set_find_peak_step_win_width_dlg); layout->addLayout(layout_input); layout->addStretch(); 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 ParticleCountPlotView::onActionManualFindPeaks() { // _data_selector->setEnabled(true); } void ParticleCountPlotView::onActionEnergyScale() { if (_batch_energy_scale_dlg && _data_selector) { _batch_energy_scale_dlg->show(); _data_selector->setEnabled(true); } } void ParticleCountPlotView::onActionPlotConfigure() { }