Mantid
Loading...
Searching...
No Matches
FunctionMultiDomainPresenter.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 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 +
10
12
15
16#include <QApplication>
17#include <QClipboard>
18#include <QMessageBox>
19#include <QWidget>
20#include <utility>
21
22namespace {
23Mantid::Kernel::Logger g_log("FunctionMultiDomain");
24}
25
27
28using namespace Mantid::API;
29using namespace Mantid::Kernel;
30
32 : m_view(view), m_model(std::make_unique<FunctionModel>()), m_editLocalParameterDialog(nullptr) {
33 connect(m_view, SIGNAL(parameterChanged(const QString &)), this, SLOT(viewChangedParameter(const QString &)));
34 connect(m_view, SIGNAL(functionReplaced(const QString &)), this, SLOT(viewPastedFunction(const QString &)));
35 connect(m_view, SIGNAL(functionAdded(const QString &)), this, SLOT(viewAddedFunction(const QString &)));
36 connect(m_view, SIGNAL(functionRemoved(const QString &)), this, SLOT(viewRemovedFunction(const QString &)));
37 connect(m_view, SIGNAL(parameterTieChanged(const QString &, const QString &)), this,
38 SLOT(viewChangedTie(const QString &, const QString &)));
39 connect(m_view, SIGNAL(parameterConstraintAdded(const QString &, const QString &)), this,
40 SLOT(viewAddedConstraint(const QString &, const QString &)));
41 connect(m_view, SIGNAL(parameterConstraintRemoved(const QString &)), this,
42 SLOT(viewRemovedConstraint(const QString &)));
43 connect(m_view, SIGNAL(localParameterButtonClicked(const QString &)), this,
44 SLOT(editLocalParameter(const QString &)));
45 connect(m_view, SIGNAL(copyToClipboardRequest()), this, SLOT(viewRequestedCopyToClipboard()));
46 connect(m_view, SIGNAL(globalsChanged(const QStringList &)), this, SLOT(viewChangedGlobals(const QStringList &)));
47 connect(m_view, SIGNAL(functionHelpRequest()), this, SLOT(viewRequestedFunctionHelp()));
48 connect(m_view, SIGNAL(attributePropertyChanged(const QString &)), this, SLOT(viewChangedAttribute(const QString &)));
49}
50
52 m_model->setFunction(std::move(fun));
53 m_view->setFunction(m_model->getCurrentFunction());
55}
56
58 m_model->setFunctionString(funStr);
59 m_view->setFunction(m_model->getCurrentFunction());
61}
62
64
65QString FunctionMultiDomainPresenter::getFitFunctionString() const { return m_model->getFitFunctionString(); }
66
67bool FunctionMultiDomainPresenter::hasFunction() const { return m_model->hasFunction(); }
68
70 return getFunctionWithPrefix(index, m_model->getCurrentFunction());
71}
72
73void FunctionMultiDomainPresenter::setParameter(const QString &paramName, double value) {
74 m_model->setParameter(paramName, value);
75 m_view->setParameter(paramName, value);
76}
77
78void FunctionMultiDomainPresenter::setParameterError(const QString &paramName, double value) {
79 m_model->setParameterError(paramName, value);
80 m_view->setParameterError(paramName, value);
81}
82
83double FunctionMultiDomainPresenter::getParameter(const QString &paramName) { return m_model->getParameter(paramName); }
84
85bool FunctionMultiDomainPresenter::isParameterFixed(const QString &parName) const {
86 return m_model->isParameterFixed(parName);
87}
88
89QString FunctionMultiDomainPresenter::getParameterTie(const QString &parName) const {
90 return m_model->getParameterTie(parName);
91}
92
94 m_model->updateParameters(fun);
96}
97
99 m_model->updateMultiDatasetParameters(fun);
101}
102
104 m_model->updateMultiDatasetAttributes(fun);
106}
107
109
112}
113
115
116void FunctionMultiDomainPresenter::setDatasets(const QStringList &datasetNames) { m_model->setDatasets(datasetNames); }
117
119 m_model->setDatasets(datasets);
120}
121
122void FunctionMultiDomainPresenter::addDatasets(const QStringList &datasetNames) { m_model->addDatasets(datasetNames); }
123
124QStringList FunctionMultiDomainPresenter::getDatasetNames() const { return m_model->getDatasetNames(); }
125
126QStringList FunctionMultiDomainPresenter::getDatasetDomainNames() const { return m_model->getDatasetDomainNames(); }
127
128int FunctionMultiDomainPresenter::getNumberOfDatasets() const { return m_model->getNumberDomains(); }
129
130int FunctionMultiDomainPresenter::getCurrentDataset() const { return m_model->currentDomainIndex(); }
131
133 m_model->setCurrentDomainIndex(index);
135}
136
138 m_model->removeDatasets(indices);
139 setCurrentDataset(m_model->currentDomainIndex());
140}
141
142double FunctionMultiDomainPresenter::getLocalParameterValue(const QString &parName, int i) const {
143 return m_model->getLocalParameterValue(parName, i);
144}
145
146bool FunctionMultiDomainPresenter::isLocalParameterFixed(const QString &parName, int i) const {
147 return m_model->isLocalParameterFixed(parName, i);
148}
149
150QString FunctionMultiDomainPresenter::getLocalParameterTie(const QString &parName, int i) const {
151 return m_model->getLocalParameterTie(parName, i);
152}
153
154QString FunctionMultiDomainPresenter::getLocalParameterConstraint(const QString &parName, int i) const {
155 return m_model->getLocalParameterConstraint(parName, i);
156}
157
158void FunctionMultiDomainPresenter::setLocalParameterValue(const QString &parName, int i, double value) {
159 m_model->setLocalParameterValue(parName, i, value);
160 if (m_model->currentDomainIndex() == i) {
161 m_view->setParameter(parName, value);
162 }
163}
164
165void FunctionMultiDomainPresenter::setLocalParameterValue(const QString &parName, int i, double value, double error) {
166 m_model->setLocalParameterValue(parName, i, value, error);
167 if (m_model->currentDomainIndex() == i) {
168 m_view->setParameter(parName, value);
169 m_view->setParameterError(parName, error);
170 }
171}
172
173void FunctionMultiDomainPresenter::setLocalParameterFixed(const QString &parName, int i, bool fixed) {
174 m_model->setLocalParameterFixed(parName, i, fixed);
175 if (m_model->currentDomainIndex() == i) {
176 if (fixed) {
177 m_view->setParameterTie(parName, QString::number(m_model->getParameter(parName)));
178 } else {
179 m_view->setParameterTie(parName, "");
180 }
181 }
182}
183
184void FunctionMultiDomainPresenter::setLocalParameterTie(const QString &parName, int i, const QString &tie) {
185 m_model->setLocalParameterTie(parName, i, tie);
186 if (m_model->currentDomainIndex() == i) {
187 m_view->setParameterTie(parName, tie);
188 }
189}
190
192 const QString &constraint) {
193 m_model->setLocalParameterConstraint(parName, i, constraint);
194 if (m_model->currentDomainIndex() == i) {
195 m_view->setParameterConstraint(parName, constraint);
196 }
197}
198
199QStringList FunctionMultiDomainPresenter::getGlobalParameters() const { return m_model->getGlobalParameters(); }
200
202 m_model->setGlobalParameters(globals);
203 m_view->setGlobalParameters(m_model->getGlobalParameters());
204}
205
206QStringList FunctionMultiDomainPresenter::getLocalParameters() const { return m_model->getLocalParameters(); }
207
209 auto const paramName = m_model->setBackgroundA0(value);
210 if (!paramName.isEmpty())
211 m_view->setParameter(paramName, value);
212}
213
215 m_model->setFunctionString(funStr);
217}
218
220 auto const prefix = m_view->currentFunctionIndex();
221 auto const prefixValue = prefix ? *prefix : "";
222 m_model->addFunction(prefixValue, funStr);
223 m_view->setGlobalParameters(m_model->getGlobalParameters());
225}
226
227void FunctionMultiDomainPresenter::viewRemovedFunction(const QString &functionIndex) {
228 m_model->removeFunction(functionIndex);
229 m_view->setGlobalParameters(m_model->getGlobalParameters());
231}
232
233void FunctionMultiDomainPresenter::viewChangedTie(const QString &paramName, const QString &tie) {
234 m_model->changeTie(paramName, tie);
236}
237
238void FunctionMultiDomainPresenter::viewAddedConstraint(const QString &functionIndex, const QString &constraint) {
239 m_model->addConstraint(functionIndex, constraint);
241}
242
244 m_model->removeConstraint(parName);
246}
247
249 auto fun = getFunction();
250 if (fun) {
251 QApplication::clipboard()->setText(QString::fromStdString(fun->asString()));
252 }
253}
254
255void FunctionMultiDomainPresenter::viewChangedGlobals(const QStringList &globalParameters) {
256 m_model->setGlobalParameters(globalParameters);
258}
259
261 auto func = m_view->getSelectedFunction();
262 if (func)
263 m_view->showFunctionHelp(QString::fromStdString(func->name()));
264}
265
266QString FunctionMultiDomainPresenter::getFunctionString() const { return m_model->getFunctionString(); }
267
268IFunction_sptr FunctionMultiDomainPresenter::getFunction() const { return m_model->getCurrentFunction(); }
269
271 m_model->clear();
272 m_view->clear();
274}
275
277 auto treeView = dynamic_cast<FunctionTreeView *>(m_view);
278 if (treeView)
279 treeView->setColumnSizes(s0, s1, s2);
280}
281
283 if (auto treeView = dynamic_cast<FunctionTreeView *>(m_view))
284 treeView->setStretchLastColumn(stretch);
285}
286
288
290 auto const value = m_view->getParameter(paramName);
291 m_model->setParameter(paramName, value);
292 auto const parts = splitParameterName(paramName);
293 emit parameterChanged(parts.first, parts.second);
294}
295
297 try {
298 auto value = m_view->getAttribute(attrName);
299 m_model->setAttribute(attrName, value);
300 emit attributeChanged(attrName);
301 } catch (const std::invalid_argument &e) {
303 g_log.error(e.what());
304 }
305}
306
312 auto const datasetNames = getDatasetNames();
313 auto const domainNames = getDatasetDomainNames();
314 QList<double> values;
315 QList<bool> fixes;
316 QStringList ties;
317 QStringList constraints;
318 const int n = domainNames.size();
319 for (int i = 0; i < n; ++i) {
320 const double value = getLocalParameterValue(parName, i);
321 values.push_back(value);
322 const bool fixed = isLocalParameterFixed(parName, i);
323 fixes.push_back(fixed);
324 const auto tie = getLocalParameterTie(parName, i);
325 ties.push_back(tie);
326 const auto constraint = getLocalParameterConstraint(parName, i);
327 constraints.push_back(constraint);
328 }
329
331 new EditLocalParameterDialog(m_view, parName, datasetNames, domainNames, values, fixes, ties, constraints);
332 connect(m_editLocalParameterDialog, SIGNAL(finished(int)), this, SLOT(editLocalParameterFinish(int)));
334}
335
337 if (result == QDialog::Accepted) {
339 auto values = m_editLocalParameterDialog->getValues();
340 auto fixes = m_editLocalParameterDialog->getFixes();
341 auto ties = m_editLocalParameterDialog->getTies();
342 auto constraints = m_editLocalParameterDialog->getConstraints();
343 assert(values.size() == getNumberOfDatasets());
344 for (int i = 0; i < values.size(); ++i) {
345 setLocalParameterValue(parName, i, values[i]);
346 if (!ties[i].isEmpty()) {
347 setLocalParameterTie(parName, i, ties[i]);
348 } else if (fixes[i]) {
349 setLocalParameterFixed(parName, i, fixes[i]);
350 } else {
351 setLocalParameterTie(parName, i, "");
352 }
353 setLocalParameterConstraint(parName, i, constraints[i]);
354 }
355 }
358}
359
361 const auto index = m_model->currentDomainIndex();
362 for (auto const &name : m_model->getParameterNames()) {
363 auto const value = m_model->getParameter(name);
364 m_view->setParameter(name, value);
365 m_view->setParameterError(name, m_model->getParameterError(name));
366 if (m_model->isLocalParameterFixed(name, index)) {
367 m_view->setParameterTie(name, QString::number(value));
368 } else {
369 m_view->setParameterTie(name, m_model->getLocalParameterTie(name, index));
370 }
371 m_view->setParameterConstraint(name, m_model->getLocalParameterConstraint(name, index));
372 }
374}
375
377 for (const auto &name : m_model->getAttributeNames()) {
378 // Create a single lambda expression capable of visiting each attribute
379 auto visitAttribute = [&](auto val) { m_view->setAttributeValue(name, val); };
380 auto value = m_model->getAttribute(name);
381 auto visitor = AttributeLambdaVisitor{visitAttribute};
382 value.apply(visitor);
383 }
384}
385
387 auto treeView = dynamic_cast<FunctionTreeView *>(m_view);
388 if (treeView) {
389 treeView->hideGlobals();
390 }
391}
392
394 auto treeView = dynamic_cast<FunctionTreeView *>(m_view);
395 if (treeView) {
396 treeView->showGlobals();
397 }
398}
399
400} // namespace MantidQt::MantidWidgets
double value
The value of the point.
Definition: FitMW.cpp:51
double error
Definition: IndexPeaks.cpp:133
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
A dialog for displaying and editing values of local parameters.
QList< double > getValues() const
Get the list of new parameter values.
QStringList getConstraints() const
Get a list of the constraints.
QStringList getTies() const
Get a list of the ties.
QList< bool > getFixes() const
Get a list with the "fixed" attribute.
void setParameter(const QString &paramName, double value)
bool isLocalParameterFixed(const QString &parName, int i) const
void attributeChanged(const QString &attributeName)
void setLocalParameterValue(const QString &parName, int i, double value)
void editLocalParameter(const QString &parName)
Launches the Edit Local Parameter dialog and deals with the input from it.
QString getLocalParameterTie(const QString &parName, int i) const
void viewChangedTie(const QString &parName, const QString &tie)
void setLocalParameterConstraint(const QString &parName, int i, const QString &constraint)
void viewAddedConstraint(const QString &functionIndex, const QString &constraint)
void parameterChanged(const QString &funcIndex, const QString &paramName)
void setLocalParameterFixed(const QString &parName, int i, bool fixed)
double getLocalParameterValue(const QString &parName, int i) const
QString getLocalParameterConstraint(const QString &parName, int i) const
void setParameterError(const QString &paramName, double value)
void setLocalParameterTie(const QString &parName, int i, const QString &tie)
Class FitPropertyBrowser implements QtPropertyBrowser to display and control fitting function paramet...
void setColumnSizes(int s0, int s1, int s2=-1)
Resize the browser's columns.
The interface to a function view.
Definition: IFunctionView.h:26
virtual double getParameter(const QString &paramName) const =0
virtual void setFunction(IFunction_sptr fun)=0
virtual void setParameterError(const QString &paramName, double error)=0
virtual void setParameterTie(const QString &paramName, const QString &tie)=0
virtual boost::optional< QString > currentFunctionIndex() const =0
virtual void setParameter(const QString &paramName, double value)=0
void setAttributeValue(const QString &attributeName, T &value)
Definition: IFunctionView.h:47
virtual IFunction_sptr getSelectedFunction()=0
virtual void setErrorsEnabled(bool enabled)=0
virtual void showFunctionHelp(const QString &) const =0
virtual void setGlobalParameters(const QStringList &)=0
virtual void setParameterConstraint(const QString &paramName, const QString &constraint)=0
virtual IFunction::Attribute getAttribute(const QString &attrName) const =0
This is an interface to a fitting function - a semi-abstarct class.
Definition: IFunction.h:163
The Logger class is in charge of the publishing messages from the framework through various channels.
Definition: Logger.h:52
void error(const std::string &msg)
Logs at error level.
Definition: Logger.cpp:77
EXPORT_OPT_MANTIDQT_COMMON IFunction_sptr getFunctionWithPrefix(const QString &prefix, const IFunction_sptr &fun)
Get a child function of a parent function whose parameters start with a given prefix.
EXPORT_OPT_MANTIDQT_COMMON std::pair< QString, QString > splitParameterName(const QString &paramName)
Split a qualified parameter name into function index and local parameter name.
Kernel::Logger g_log("ExperimentInfo")
static logger object
std::shared_ptr< IFunction > IFunction_sptr
shared pointer to the function base class
Definition: IFunction.h:732
STL namespace.
Attribute visitor structure supporting lambda expressions Example usage: AttributeLambdaVisitor{[](co...
Definition: IFunction.h:55