Skip to content

Commit

Permalink
Added function in fit parameters context menu to be able to load outp…
Browse files Browse the repository at this point in the history
…ut/fit_params/*.cav params
  • Loading branch information
Arthur Glowacki committed Aug 16, 2024
1 parent d7c963f commit aad4b84
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
69 changes: 69 additions & 0 deletions src/mvc/FitSpectraWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <QSpacerItem>
#include <QInputDialog>
#include <QDesktopServices>
#include <QFileDialog>
#include <math.h>

#include "data_struct/element_info.h"
Expand Down Expand Up @@ -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)));

}

Expand Down Expand Up @@ -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<double> fit_params = _fit_params_table_model->getFitParams();
data_struct::Fit_Parameters<double> 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<double> fit_params;
Expand Down
2 changes: 2 additions & 0 deletions src/mvc/FitSpectraWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions src/mvc/FittingDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit aad4b84

Please sign in to comment.