BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
MinimizerSettingsWidget.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/Fit/MinimizerSettingsWidget.cpp
6 //! @brief Implements class MinimizerSettingsWidget
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
17 #include "GUI/Model/Job/JobItem.h"
22 #include <QComboBox>
23 #include <QFormLayout>
24 #include <QPushButton>
25 #include <QVBoxLayout>
26 
28  : QWidget(parent)
29  , m_currentItem(nullptr)
30 {
31  setWindowTitle(QLatin1String("Minimizer Settings"));
32  setAttribute(Qt::WA_StyledBackground, true);
33  setProperty("stylable", true); // for stylesheet addressing
34 
35  m_mainLayout = new QFormLayout(this);
36  m_mainLayout->setContentsMargins(8, 8, 8, 8);
37  m_mainLayout->setSpacing(5);
38 }
39 
41 {
42  ASSERT(jobItem);
44 }
45 
47 {
48  ASSERT(minimizerItem);
49 
51  m_updaters.clear();
52  m_currentItem = minimizerItem;
53 
54  if (!m_currentItem)
55  return;
56 
57  auto minimizerDescriptor = m_currentItem->minimizers();
58  auto* minimizerCombo = GUI::Util::createSelectionCombo(this, minimizerDescriptor,
59  [=](int) { createMimimizerEdits(); });
60 
61  m_updaters << [=]() {
62  QSignalBlocker b(minimizerCombo);
63  minimizerCombo->setCurrentIndex(minimizerDescriptor.currentIndex());
64  };
65  m_mainLayout->addRow("Minimizer:", minimizerCombo);
66 
67  auto* w = new QWidget(this);
68  m_minimizerLayout = new QFormLayout(w);
69  m_minimizerLayout->setContentsMargins(10, 8, 0, 8);
70  m_mainLayout->addRow("", w);
71 
72  m_mainLayout->addRow("Objective metric:", createComboBox(m_currentItem->objectiveMetric()));
73  m_mainLayout->addRow("Norm function:", createComboBox(m_currentItem->normFunction()));
74 
77 }
78 
80 {
81  auto* combo = new QComboBox(this);
82  combo->addItems(d.options);
83  combo->setMaxCount(d.options.size());
84  combo->setToolTip(d.tooltip);
85 
86  connect(combo, qOverload<int>(&QComboBox::currentIndexChanged),
87  [=](int newIndex) { d.setCurrentIndex(newIndex); });
88 
89  m_updaters << [=]() {
90  QSignalBlocker b(combo);
91  combo->setCurrentIndex(d.currentIndex());
92  };
93 
94  return combo;
95 }
96 
98 {
99  auto* spinBox = new DoubleSpinBox(this, d);
100  spinBox->setToolTip(d.tooltip);
101  QObject::connect(spinBox, &DoubleSpinBox::baseValueChanged,
102  [=](double newValue) { d.set(newValue); });
103 
104  m_updaters << [=]() { spinBox->updateValue(); };
105 
106  return spinBox;
107 }
108 
110 {
111  auto* spinBox = new QSpinBox(this);
112  spinBox->setToolTip(d.tooltip);
113  spinBox->setMaximum(std::numeric_limits<int>::max());
114  spinBox->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
115 
116  if (d.limits.hasLowerLimit())
117  spinBox->setMinimum(static_cast<int>(d.limits.lowerLimit()));
118  if (d.limits.hasUpperLimit())
119  spinBox->setMaximum(static_cast<int>(d.limits.upperLimit()));
120 
121  spinBox->setValue(d.get());
122 
123  QObject::connect(spinBox, QOverload<int>::of(&QSpinBox::valueChanged),
124  [=](int newValue) { d.set(newValue); });
125 
126  m_updaters << [=]() {
127  QSignalBlocker b(spinBox);
128  spinBox->setValue(d.get());
129  };
130 
131  return spinBox;
132 }
133 
135 {
137 
138  for (auto v : m_currentItem->minimizers().currentItem()->valueDescriptorsForUI()) {
139  if (std::holds_alternative<DoubleDescriptor>(v)) {
140  auto d = std::get<DoubleDescriptor>(v);
141  m_minimizerLayout->addRow(d.label + ":", createDoubleSpinbox(d));
142  } else if (std::holds_alternative<SelectionDescriptor<QString>>(v)) {
143  auto d = std::get<SelectionDescriptor<QString>>(v);
144  m_minimizerLayout->addRow(d.label + ":", createComboBox(d));
145  } else if (std::holds_alternative<UIntDescriptor>(v)) {
146  auto d = std::get<UIntDescriptor>(v);
147  m_minimizerLayout->addRow(d.label + ":", createSpinbox(d));
148  }
149  }
150 }
151 
153 {
154  for (const auto& updater : m_updaters)
155  updater();
156 }
Defines class DoubleSpinBox.
Defines class FitSuiteItem.
Defines class JobItem.
Defines namespace GUI::Util::Layout.
Defines MinimizerItem class.
Defines class MinimizerSettingsWidget.
Defines GUI::Util namespace.
Describes properties of a double value which are necessary to allow GUI representation,...
function< void(double)> set
function to set the value
QString tooltip
Tooltip text.
SpinBox for DoubleDescriptors, supporting units.
Definition: DoubleSpinBox.h:22
void baseValueChanged(double newBaseValue)
Emitted whenever the value changes.
MinimizerContainerItem * minimizerContainerItem()
FitSuiteItem * fitSuiteItem()
Definition: JobItem.cpp:257
The MinimizerContainerItem class holds collection of minimizers.
Definition: MinimizerItem.h:44
SelectionDescriptor< QString > objectiveMetric() const
SelectionDescriptor< MinimizerItem * > minimizers() const
SelectionDescriptor< QString > normFunction() const
MinimizerSettingsWidget(QWidget *parent=nullptr)
QWidget * createSpinbox(UIntDescriptor d)
QWidget * createDoubleSpinbox(DoubleDescriptor d)
QList< function< void()> > m_updaters
QWidget * createComboBox(SelectionDescriptor< QString > d)
MinimizerContainerItem * m_currentItem
void setItem(JobItem *jobItem)
Describes a selection (various possibilities and the current one).
int currentIndex() const override
Get currently selected option.
QStringList options
List of options, usually presented as combo entries.
void setCurrentIndex(int newIndex) const override
Set currently selected option.
QString tooltip
Tooltip text.
Describes properties of a uint value which are necessary to allow GUI representation,...
function< void(uint)> set
function to set the value
QString tooltip
Tooltip text.
RealLimits limits
Limits of the value.
function< uint()> get
function to get the current value
void clearLayout(QLayout *layout, bool deleteWidgets=true)
Removes content from box layout.
Definition: LayoutUtils.cpp:68
QComboBox * createSelectionCombo(QWidget *parent, const SelectionDescriptor< T > d, std::function< void(int)> slot=nullptr)
Create a combo box with the information found in a selection descriptor.
Definition: WidgetUtils.h:45