From aad4b8406323b66c266a14adb9710830e5641235 Mon Sep 17 00:00:00 2001 From: Arthur Glowacki Date: Fri, 16 Aug 2024 11:05:13 -0500 Subject: [PATCH] Added function in fit parameters context menu to be able to load output/fit_params/*.cav params --- src/mvc/FitSpectraWidget.cpp | 69 ++++++++++++++++++++++++++++++++++++ src/mvc/FitSpectraWidget.h | 2 ++ src/mvc/FittingDialog.cpp | 6 ++-- 3 files changed, 75 insertions(+), 2 deletions(-) diff --git a/src/mvc/FitSpectraWidget.cpp b/src/mvc/FitSpectraWidget.cpp index d749aa5..45b5f84 100644 --- a/src/mvc/FitSpectraWidget.cpp +++ b/src/mvc/FitSpectraWidget.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include "data_struct/element_info.h" @@ -98,6 +99,8 @@ FitSpectraWidget::FitSpectraWidget(QWidget* parent) : QWidget(parent) //FIXED=1, LIMITED_LO_HI=2, LIMITED_LO=3, LIMITED_HI=4, FIT=5}; _fit_param_contextMenu = new QMenu(("Context menu"), this); _fit_param_contextMenu->addMenu(_set_fit_params_bounds_menu); + action_bounds = _fit_param_contextMenu->addAction("Load from CSV"); + connect(action_bounds, SIGNAL(triggered(bool)), this, SLOT(show_load_fit_params_dialog(bool))); } @@ -435,6 +438,72 @@ void FitSpectraWidget::onSettingsDialog() //--------------------------------------------------------------------------- +void FitSpectraWidget::show_load_fit_params_dialog(bool val) +{ + QString fileName = QFileDialog::getOpenFileName(this, + "Open Fit Parameters", _dataset_dir.absolutePath(), + "Fit Parameters (*.csv);;All Files (*.*)"); + + // Dialog returns a nullptr string if user press cancel. + if (fileName.isNull() || fileName.isEmpty()) return; + + QString filePath = QFileInfo(fileName).canonicalFilePath(); + + QFile file(filePath); + if (!file.open(QIODevice::ReadOnly)) + { + logE<< file.errorString().toStdString(); + return; + } + bool conv_log10 = Preferences::inst()->getValue(STR_PFR_LOG_10).toBool(); + data_struct::Fit_Parameters fit_params = _fit_params_table_model->getFitParams(); + data_struct::Fit_Parameters fit_elements = _fit_elements_table_model->getAsFitParams(); + QStringList wordList; + while (!file.atEnd()) + { + QByteArray line = file.readLine(); + QString sline = QString(line).trimmed(); + wordList = sline.split(','); + if( wordList.size() == 2 ) + { + std::string sfirst = wordList[0].toStdString(); + if( fit_params.contains(sfirst) ) + { + bool ok = false; + double val = wordList[1].toDouble(&ok); + if(ok) + { + if( sfirst == STR_COMPTON_AMPLITUDE || sfirst == STR_COHERENT_SCT_AMPLITUDE) + { + if(conv_log10) + { + val = log10(val); + } + } + fit_params[sfirst].value = val; + } + } + else if( fit_elements.contains(sfirst) ) + { + bool ok = false; + double val = wordList[1].toDouble(&ok); + if(ok) + { + if(conv_log10) + { + val = log10(val); + } + fit_elements[sfirst].value = val; + } + } + } + } + _fit_params_table_model->setFitParams(fit_params); + _fit_elements_table_model->updateElementValues(&fit_elements); +} + +//--------------------------------------------------------------------------- + void FitSpectraWidget::on_export_fit_paramters() { data_struct::Fit_Parameters fit_params; diff --git a/src/mvc/FitSpectraWidget.h b/src/mvc/FitSpectraWidget.h index d4fa471..d34300e 100644 --- a/src/mvc/FitSpectraWidget.h +++ b/src/mvc/FitSpectraWidget.h @@ -174,6 +174,8 @@ private slots: void set_fit_params_bounds_limited_hi(bool v) { set_fit_params_bounds(data_struct::E_Bound_Type::LIMITED_HI); } + void show_load_fit_params_dialog(bool); + void set_fit_params_bounds(data_struct::E_Bound_Type e_type); void pileup_chk_changed(int state); diff --git a/src/mvc/FittingDialog.cpp b/src/mvc/FittingDialog.cpp index ba70459..23d1599 100644 --- a/src/mvc/FittingDialog.cpp +++ b/src/mvc/FittingDialog.cpp @@ -77,13 +77,15 @@ void FittingDialog::_createLayout() _fit_params_table->setItemDelegateForColumn(5, npDelegate); _fit_params_table->horizontalHeader()->setSectionResizeMode(QHeaderView::Interactive); _fit_params_table->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents); - + _fit_params_table->horizontalHeader()->setSectionResizeMode(2, QHeaderView::ResizeToContents); + _new_fit_params_table = new QTableView(); _new_fit_params_table->setModel(_new_fit_params_table_model); _new_fit_params_table->sortByColumn(0, Qt::AscendingOrder); _new_fit_params_table->setItemDelegateForColumn(2, cbDelegate); _new_fit_params_table->horizontalHeader()->setSectionResizeMode(QHeaderView::Interactive); - + _new_fit_params_table->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents); + connect(_new_fit_params_table->verticalScrollBar(), &QAbstractSlider::valueChanged, _fit_params_table->verticalScrollBar(), &QAbstractSlider::setValue); _btn_run = new QPushButton("Run");