Mantid
Loading...
Searching...
No Matches
FitScriptOptionsBrowser.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2021 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
8
11
12#include "MantidQtWidgets/Common/QtPropertyBrowser/qteditorfactory.h"
13#include "MantidQtWidgets/Common/QtPropertyBrowser/qtpropertymanager.h"
14#include "MantidQtWidgets/Common/QtPropertyBrowser/qttreepropertybrowser.h"
15
16#include <QVBoxLayout>
17
18namespace {
19
20using namespace Mantid::API;
21
22int constexpr DEFAULT_MAX_ITERATIONS = 500;
23QString const DEFAULT_MINIMIZER = "Levenberg-Marquardt";
24QString const DEFAULT_OUTPUT_BASE_NAME = "Output_Fit";
25bool const DEFAULT_PLOT_OUTPUT = true;
26QStringList const EVALUATION_TYPES = {"CentrePoint", "Histogram"};
27QStringList const FITTING_MODES = {"Sequential", "Simultaneous"};
28
29QStringList convertToQStringList(std::vector<std::string> const &vec) {
30 QStringList list;
31 list.reserve(static_cast<int>(vec.size()));
32 std::transform(vec.cbegin(), vec.cend(), std::back_inserter(list),
33 [](std::string const &str) { return QString::fromStdString(str); });
34 return list;
35}
36
37QStringList minimizers() { return convertToQStringList(FuncMinimizerFactory::Instance().getKeys()); }
38
39QStringList costFunctions() { return convertToQStringList(CostFunctionFactory::Instance().getKeys()); }
40
41int defaultMinimizerIndex() {
42 auto const index = minimizers().indexOf(DEFAULT_MINIMIZER);
43 return index >= 0 ? index : 0;
44}
45
46} // namespace
47
49
51 : QWidget(parent), m_fittingMode(nullptr), m_maxIterations(nullptr), m_minimizer(nullptr), m_costFunction(nullptr),
52 m_evaluationType(nullptr) {
55}
56
58 m_browser->unsetFactoryForManager(m_stringManager);
59 m_browser->unsetFactoryForManager(m_intManager);
60 m_browser->unsetFactoryForManager(m_enumManager);
61 m_browser->unsetFactoryForManager(m_boolManager);
62}
63
65 m_stringManager = new QtStringPropertyManager(this);
66 m_intManager = new QtIntPropertyManager(this);
67 m_enumManager = new QtEnumPropertyManager(this);
68 m_boolManager = new QtBoolPropertyManager(this);
69
70 auto *lineEditFactory = new QtLineEditFactory(this);
71 auto *spinBoxFactory = new QtSpinBoxFactory(this);
72 auto *comboBoxFactory = new QtEnumEditorFactory(this);
73 auto *checkBoxFactory = new QtCheckBoxFactory(this);
74
75 m_browser = new QtTreePropertyBrowser(nullptr, QStringList(), false);
76 m_browser->setFactoryForManager(m_stringManager, lineEditFactory);
77 m_browser->setFactoryForManager(m_intManager, spinBoxFactory);
78 m_browser->setFactoryForManager(m_enumManager, comboBoxFactory);
79 m_browser->setFactoryForManager(m_boolManager, checkBoxFactory);
80 m_browser->setMinimumHeight(110);
81
82 connect(m_stringManager, SIGNAL(propertyChanged(QtProperty *)), this, SLOT(stringChanged(QtProperty *)));
83 connect(m_enumManager, SIGNAL(propertyChanged(QtProperty *)), this, SLOT(enumChanged(QtProperty *)));
84
85 auto *layout = new QVBoxLayout(this);
86 layout->addWidget(m_browser);
87 layout->setContentsMargins(0, 0, 0, 0);
88}
89
98}
99
101 m_fittingMode = m_enumManager->addProperty("Fitting Mode");
102 m_enumManager->setEnumNames(m_fittingMode, FITTING_MODES);
103 m_browser->addProperty(m_fittingMode);
104
106}
107
109 m_maxIterations = m_intManager->addProperty("Max Iterations");
110
111 m_intManager->setValue(m_maxIterations, DEFAULT_MAX_ITERATIONS);
112 m_intManager->setMinimum(m_maxIterations, 0);
113 m_browser->addProperty(m_maxIterations);
114
117}
118
120 m_minimizer = m_enumManager->addProperty("Minimizer");
121
122 m_enumManager->setEnumNames(m_minimizer, minimizers());
123 m_enumManager->setValue(m_minimizer, defaultMinimizerIndex());
124 m_browser->addProperty(m_minimizer);
125
128}
129
131 m_costFunction = m_enumManager->addProperty("Cost Function");
132
133 m_enumManager->setEnumNames(m_costFunction, costFunctions());
134 m_browser->addProperty(m_costFunction);
135
138}
139
141 m_evaluationType = m_enumManager->addProperty("Evaluation Type");
142
143 m_enumManager->setEnumNames(m_evaluationType, EVALUATION_TYPES);
144 m_browser->addProperty(m_evaluationType);
145
148}
149
151 m_outputBaseName = m_stringManager->addProperty("Output Base Name");
152
153 m_stringManager->setValue(m_outputBaseName, DEFAULT_OUTPUT_BASE_NAME);
154 m_browser->addProperty(m_outputBaseName);
155
158}
159
161 m_plotOutput = m_boolManager->addProperty("Plot Output");
162
163 m_boolManager->setValue(m_plotOutput, DEFAULT_PLOT_OUTPUT);
164 m_browser->addProperty(m_plotOutput);
165
166 m_propertyNameMap["Plot Output"] = m_plotOutput;
167}
168
169void FitScriptOptionsBrowser::addProperty(std::string const &name, QtProperty *prop, PropertyGetter getter,
170 PropertySetter setter) {
171 m_propertyNameMap[name] = prop;
172 m_getters[prop] = getter;
173 m_setters[prop] = setter;
174}
175
176void FitScriptOptionsBrowser::setProperty(std::string const &name, std::string const &value) {
177 auto const prop = getQtPropertyFor(name);
178 auto const f = m_setters[prop];
179 (this->*f)(prop, value);
180}
181
182std::string FitScriptOptionsBrowser::getProperty(std::string const &name) const {
183 auto const prop = getQtPropertyFor(name);
184 auto const f = m_getters[prop];
185 return (this->*f)(prop);
186}
187
188bool FitScriptOptionsBrowser::getBoolProperty(std::string const &name) const {
189 auto const prop = getQtPropertyFor(name);
190 return m_boolManager->value(prop);
191}
192
193QtProperty *FitScriptOptionsBrowser::getQtPropertyFor(std::string const &name) const {
194 if (!m_propertyNameMap.contains(name)) {
195 throw std::runtime_error("Property " + name + " isn't supported by the browser.");
196 }
197 return m_propertyNameMap[name];
198}
199
201 if (prop == m_outputBaseName) {
202 emit outputBaseNameChanged(prop->valueText().toStdString());
203 }
204}
205
207 if (prop == m_fittingMode) {
209 }
210}
211
212void FitScriptOptionsBrowser::setStringProperty(QtProperty *prop, std::string const &value) {
213 m_stringManager->setValue(prop, QString::fromStdString(value));
214}
215
216std::string FitScriptOptionsBrowser::getStringProperty(QtProperty *prop) const {
217 return m_stringManager->value(prop).toStdString();
218}
219
220void FitScriptOptionsBrowser::setIntProperty(QtProperty *prop, std::string const &value) {
221 m_intManager->setValue(prop, QString::fromStdString(value).toInt());
222}
223
224std::string FitScriptOptionsBrowser::getIntProperty(QtProperty *prop) const {
225 return QString::number(m_intManager->value(prop)).toStdString();
226}
227
228void FitScriptOptionsBrowser::setStringEnumProperty(QtProperty *prop, std::string const &value) {
229 auto const i = m_enumManager->enumNames(prop).indexOf(QString::fromStdString(value));
230 if (i < 0)
231 throw std::invalid_argument("An invalid property value was provided.");
232
233 m_enumManager->setValue(prop, i);
234}
235
236std::string FitScriptOptionsBrowser::getStringEnumProperty(QtProperty *prop) const {
237 auto const i = m_enumManager->value(prop);
238 if (i < 0)
239 throw std::invalid_argument("An invalid property was provided.");
240
241 return m_enumManager->enumNames(prop)[i].toStdString();
242}
243
245 switch (fittingMode) {
247 m_enumManager->setValue(m_fittingMode, 0);
248 return;
250 m_enumManager->setValue(m_fittingMode, 1);
251 return;
252 default:
253 throw std::invalid_argument("Fitting mode must be SEQUENTIAL or SIMULTANEOUS.");
254 }
255}
256
258 return static_cast<FittingMode>(m_enumManager->value(m_fittingMode));
259}
260
261} // namespace MantidQt::MantidWidgets
CostFunctions::CostFuncFitting & m_costFunction
The cost function.
double value
The value of the point.
Definition: FitMW.cpp:51
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
std::string getStringEnumProperty(QtProperty *prop) const
std::string getProperty(std::string const &name) const
void addProperty(std::string const &name, QtProperty *prop, PropertyGetter getter, PropertySetter setter)
QtTreePropertyBrowser * m_browser
Qt property browser which displays properties.
QMap< std::string, QtProperty * > m_propertyNameMap
Maps algorithm property name to the QtProperty.
QMap< QtProperty *, PropertyGetter > m_getters
Store for the property getter methods.
void setProperty(std::string const &name, std::string const &value)
QMap< QtProperty *, PropertySetter > m_setters
Store for the property setter methods.
QtProperty * getQtPropertyFor(std::string const &name) const
void setIntProperty(QtProperty *prop, std::string const &value)
std::string(FitScriptOptionsBrowser::*)(QtProperty *) const PropertyGetter
void fittingModeChanged(FittingMode fittingMode)
void(FitScriptOptionsBrowser::*)(QtProperty *, std::string const &) PropertySetter
void setStringProperty(QtProperty *prop, std::string const &value)
void outputBaseNameChanged(std::string const &outputBaseName)
QtStringPropertyManager * m_stringManager
Property managers.
void setStringEnumProperty(QtProperty *prop, std::string const &value)
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...