Mantid
Loading...
Searching...
No Matches
FitDialog.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 +
7//------------------------------------------------------------------------------
8// Includes
9//------------------------------------------------------------------------------
12// Qt
13#include <QCheckBox>
14#include <QComboBox>
15#include <QFileInfo>
16#include <QUrl>
17
18// Mantid
26
27#include <limits>
28
29using namespace MantidQt::API;
30
32
33// Declare the dialog. Name must match the class name
34DECLARE_DIALOG(FitDialog)
35
36//------------------------------------------------------
37// InputWorkspaceWidget methods
38//------------------------------------------------------
39
40
47 : QWidget(parent), m_fitDialog(parent), m_domainIndex(domainIndex), m_wsPropName("InputWorkspace"),
48 m_workspaceName(new QComboBox(this)), m_dynamicProperties(nullptr), m_layout(new QVBoxLayout(this)) {
49 if (domainIndex > 0) {
50 m_wsPropName += "_" + QString::number(domainIndex);
51 }
52 m_layout->addWidget(m_workspaceName);
53
54 QStringList allowedValues = getAllowedPropertyValues(m_wsPropName);
55 m_workspaceName->clear();
56 m_workspaceName->insertItems(0, allowedValues);
57 connect(m_workspaceName, SIGNAL(currentIndexChanged(int)), this, SLOT(setDynamicProperties()));
58
59 setDynamicProperties();
60}
61
66 QString wsName = m_workspaceName->currentText();
67 return !wsName.isEmpty();
68}
69
74 QString wsName = m_workspaceName->currentText();
75 if (wsName.isEmpty())
76 return false;
77 try {
78 auto ws = Mantid::API::AnalysisDataService::Instance().retrieve(wsName.toStdString());
79 return dynamic_cast<Mantid::API::MatrixWorkspace *>(ws.get()) != nullptr;
80 } catch (...) {
81 return false;
82 }
83}
84
89 QString wsName = m_workspaceName->currentText();
90 if (wsName.isEmpty())
91 return false;
92 try {
93 auto ws = Mantid::API::AnalysisDataService::Instance().retrieve(wsName.toStdString());
94 return dynamic_cast<Mantid::API::IMDWorkspace *>(ws.get()) != nullptr;
95 } catch (...) {
96 return false;
97 }
98}
99
104
109 if (!isWSNameSet())
110 return;
111
112 auto item = m_layout->takeAt(1);
113 if (item) {
114 QWidget *w = item->widget();
115 delete item;
116 delete w;
117 }
118
119 m_dynamicProperties = nullptr;
120
121 if (m_fitDialog->isMD()) {
122 if (isMDWorkspace()) {
124 m_layout->insertWidget(1, m_dynamicProperties);
125 } else {
126 m_layout->insertWidget(1, new QLabel("MD Workspace is expected"));
127 }
128 } else if (isMatrixWorkspace()) {
130 m_layout->insertWidget(1, m_dynamicProperties);
131 } else {
132 m_layout->insertWidget(1, new QLabel("Workspace of this type is not supported"));
133 }
134}
135
137QString InputWorkspaceWidget::getWorkspaceName() const { return m_workspaceName->currentText(); }
138
140void InputWorkspaceWidget::setWorkspaceName(const QString &wsName) {
141 int i = m_workspaceName->findText(wsName);
142 if (i >= 0) {
143 m_workspaceName->setCurrentIndex(i);
144 }
145}
146
152void InputWorkspaceWidget::setPropertyValue(const QString &propName, const QString &propValue) {
153 if (m_fitDialog->getAlgorithm()->existsProperty(propName.toStdString())) {
154 m_fitDialog->getAlgorithm()->setPropertyValue(propName.toStdString(), propValue.toStdString());
155 m_fitDialog->storePropertyValue(propName, propValue);
156 }
157}
158
170
175 m_workspaceIndex = new QSpinBox(this);
176 m_startX = new QLineEdit(this);
177 m_endX = new QLineEdit(this);
178
179 auto layout = new QGridLayout(this);
180 layout->addWidget(new QLabel("Workspace index"), 0, 0);
181 layout->addWidget(m_workspaceIndex, 0, 1);
182 layout->addWidget(new QLabel("StartX"), 1, 0);
183 layout->addWidget(m_startX, 1, 1);
184 layout->addWidget(new QLabel("EndX"), 2, 0);
185 layout->addWidget(m_endX, 2, 1);
186
187 if (parent->getDomainType() > 0) {
188 m_maxSize = new QSpinBox(this);
189 m_maxSize->setMinimum(1);
190 m_maxSize->setMaximum(std::numeric_limits<int>::max());
191 layout->addWidget(new QLabel("Maximum size"), 3, 0);
192 layout->addWidget(m_maxSize, 3, 1);
193 } else {
194 m_maxSize = nullptr;
195 }
196
197 QString wsName = parent->getWorkspaceName();
198 if (wsName.isEmpty())
199 return;
200 try {
201 const auto *ws = dynamic_cast<Mantid::API::MatrixWorkspace *>(
202 Mantid::API::AnalysisDataService::Instance().retrieve(wsName.toStdString()).get());
203 if (ws) {
204 m_workspaceIndex->setRange(0, static_cast<int>(ws->getNumberHistograms()));
205 const Mantid::MantidVec &x = ws->readX(0);
206 if (!x.empty()) {
207 m_startX->setText(QString::number(x.front()));
208 m_endX->setText(QString::number(x.back()));
209 }
210 }
211 } catch (...) {
212 }
213}
214
219
224 QString wsIndexName = "WorkspaceIndex";
225 QString startXName = "StartX";
226 QString endXName = "EndX";
227 QString maxSizeName = "MaxSize";
228
229 int domainIndex = m_wsWidget->getDomainIndex();
230 if (domainIndex > 0) {
231 QString suffix = "_" + QString::number(domainIndex);
232 wsIndexName += suffix;
233 startXName += suffix;
234 endXName += suffix;
235 maxSizeName += suffix;
236 }
237
238 QString value = m_workspaceIndex->text();
239 if (!value.isEmpty()) {
240 m_wsWidget->setPropertyValue(wsIndexName, value);
241 }
242 value = m_startX->text();
243 if (!value.isEmpty()) {
244 m_wsWidget->setPropertyValue(startXName, value);
245 }
246 value = m_endX->text();
247 if (!value.isEmpty()) {
249 }
250
251 if (m_wsWidget->getDomainType() > 0) {
252 value = m_maxSize->text();
253 m_wsWidget->setPropertyValue(maxSizeName, value);
254 }
255}
256
257//------------------------------------------------------
258// MDPropertiesWidget methods
259//------------------------------------------------------
265 if (parent->getDomainType() > 0) {
266 auto layout = new QGridLayout(this);
267 m_maxSize = new QSpinBox(this);
268 m_maxSize->setMinimum(1);
269 m_maxSize->setMaximum(std::numeric_limits<int>::max());
270 layout->addWidget(new QLabel("Maximum size"), 3, 0);
271 layout->addWidget(m_maxSize, 3, 1);
272 } else {
273 m_maxSize = nullptr;
274 }
275}
276
281 QString maxSizeName = "MaxSize";
282
283 int domainIndex = m_wsWidget->getDomainIndex();
284 if (domainIndex > 0) {
285 QString suffix = "_" + QString::number(domainIndex);
286 maxSizeName += suffix;
287 }
288
289 if (m_wsWidget->getDomainType() > 0) {
290 QString value = m_maxSize->text();
291 m_wsWidget->setPropertyValue(maxSizeName, value);
292 }
293}
294
295//------------------------------------------------------
296// FitDialog methods
297//------------------------------------------------------
298
300FitDialog::FitDialog(QWidget *parent) : API::AlgorithmDialog(parent), m_form() {}
301
304 m_form.setupUi(this);
305 m_form.dialogLayout->addLayout(this->createDefaultButtonLayout());
306
307 tieStaticWidgets(true);
308}
309
315 QString funStr = QString::fromStdString(m_form.function->getFunctionString());
316 if (!funStr.isEmpty()) {
317 storePropertyValue("Function", funStr);
318 }
320}
321
326 // int domainType = getDomainType();
328 getAlgorithm()->setPropertyValue("DomainType", getDomainTypeString().toStdString());
329 auto funStr = m_form.function->getFunctionString();
330 if (!funStr.empty()) {
331 storePropertyValue("Function", QString::fromStdString(funStr));
332 getAlgorithm()->setPropertyValue("Function", funStr);
333 } else {
334 // Cannot set any other properties until Function is set
335 return;
336 }
337 foreach (QWidget *t, m_tabs) {
338 auto iww = dynamic_cast<InputWorkspaceWidget *>(t);
339 if (iww) {
340 iww->setProperties();
341 }
342 }
343}
344
349void FitDialog::tieStaticWidgets(const bool readHistory) {
350 QString funValue = getPreviousValue("Function");
351 if (!funValue.isEmpty()) {
352 m_form.function->setFunction(funValue.toStdString());
353 }
354
355 tie(m_form.chbCreateOutput, "CreateOutput", m_form.staticLayout, readHistory);
356 tie(m_form.leOutput, "Output", m_form.staticLayout, readHistory);
357 tie(m_form.leMaxIterations, "MaxIterations", m_form.staticLayout, readHistory);
358
359 m_form.cbCostFunction->addItems(getAllowedPropertyValues("CostFunction"));
360 tie(m_form.cbCostFunction, "CostFunction", m_form.staticLayout, readHistory);
361
362 QStringList allowedDomainTypes = getAllowedPropertyValues("DomainType");
363 // Disable some domain types in the GUI until their imlpementations have been
364 // finished
365 allowedDomainTypes.removeAll("Sequential");
366 allowedDomainTypes.removeAll("Parallel");
367 m_form.cbDomainType->addItems(allowedDomainTypes);
368 // tie(m_form.cbDomainType, "DomainType", m_form.staticLayout, readHistory);
369 connect(m_form.cbDomainType, SIGNAL(currentIndexChanged(int)), this, SLOT(domainTypeChanged()));
370 QString domainTypeValue = getPreviousValue("DomainType");
371 if (!domainTypeValue.isEmpty()) {
372 m_form.cbDomainType->setItemText(-1, domainTypeValue);
373 }
374
375 // this creates input workspace widgets and adjusts minimizers list
376 // according to the domain type
378
379 // read value from history
380 tie(m_form.cbMinimizer, "Minimizer", m_form.staticLayout, readHistory);
381
382 auto value = getPreviousValue("InputWorkspace");
384}
385
390 getAlgorithm()->setPropertyValue("DomainType", getDomainTypeString().toStdString());
391 auto minimizerList = getAllowedPropertyValues("Minimizer");
392 if (getDomainType() != 0) {
393 minimizerList.removeAll("Levenberg-Marquardt");
394 }
395 QString currentMinimizer = m_form.cbMinimizer->currentText();
396 m_form.cbMinimizer->clear();
397 m_form.cbMinimizer->addItems(minimizerList);
398 int index = m_form.cbMinimizer->findText(currentMinimizer);
399 if (index >= 0) {
400 m_form.cbMinimizer->setItemText(-1, currentMinimizer);
401 }
403}
404
409 m_form.tabWidget->clear();
410 QStringList wsNames;
411 foreach (QWidget *t, m_tabs) {
412 const auto *tab = dynamic_cast<InputWorkspaceWidget *>(t);
413 if (tab) {
414 wsNames << tab->getWorkspaceName();
415 } else {
416 wsNames << "";
417 }
418 delete t;
419 }
420 m_tabs.clear();
421 auto tab = new InputWorkspaceWidget(this, 0);
422 if (!wsNames.isEmpty()) {
423 tab->setWorkspaceName(wsNames[0]);
424 }
425 m_form.tabWidget->addTab(tab, "InputWorkspace");
426 m_tabs << tab;
427
428 auto fun = m_form.function->getFunction();
429 if (!fun)
430 return;
431 auto multid = std::dynamic_pointer_cast<Mantid::API::MultiDomainFunction>(fun);
432 if (multid) {
433 // number of domains that the function expects
434 size_t nd = multid->getMaxIndex();
435 for (int i = 1; i < static_cast<int>(nd); ++i) {
436 QString propName = "InputWorkspace_" + QString::number(i);
437 auto t = new InputWorkspaceWidget(this, i);
438 if (wsNames.size() > (i)) {
439 tab->setWorkspaceName(wsNames[i]);
440 }
441 m_form.tabWidget->addTab(t, propName);
442 m_tabs << t;
443 }
444 }
445}
446
452void FitDialog::setWorkspaceName(int i, const QString &wsName) {
453 if (i < 0 || i >= m_form.tabWidget->count())
454 return;
455 auto tab = dynamic_cast<InputWorkspaceWidget *>(m_form.tabWidget->widget(i));
456 if (tab)
457 tab->setWorkspaceName(wsName);
458}
459
460void FitDialog::workspaceChanged(const QString & /*unused*/) { this->setPropertyValues(); }
461
463 // this->setPropertyValues();
464 // removeOldInputWidgets();
465 // createDynamicLayout();
466}
467
472QStringList FitDialog::getAllowedPropertyValues(const QString &propName) const {
473 QStringList out;
474 std::vector<std::string> workspaces = getAlgorithmProperty(propName)->allowedValues();
475 for (std::vector<std::string>::const_iterator itr = workspaces.begin(); itr != workspaces.end(); ++itr) {
476 out << QString::fromStdString(*itr);
477 }
478 return out;
479}
480
481namespace {
486bool isFunctionMD(const Mantid::API::IFunction_sptr &fun) {
487 auto cf = std::dynamic_pointer_cast<Mantid::API::CompositeFunction>(fun);
488 if (!cf)
489 return static_cast<bool>(std::dynamic_pointer_cast<Mantid::API::IFunctionMD>(fun));
490 for (size_t i = 0; i < cf->nFunctions(); ++i) {
491 bool yes = isFunctionMD(cf->getFunction(i));
492 if (yes)
493 return true;
494 }
495 return false;
496}
497} // namespace
498
502bool FitDialog::isMD() const {
503 auto fun = m_form.function->getFunction();
504 return isFunctionMD(fun);
505}
506
511 QString type = m_form.cbDomainType->currentText();
512 if (type == "Simple") {
513 return 0;
514 } else if (type == "Sequential") {
515 return 1;
516 } else if (type == "Parallel") {
517 return 2;
518 } else {
519 return 0;
520 }
521}
522
524QString FitDialog::getDomainTypeString() const { return m_form.cbDomainType->currentText(); }
525
526} // namespace MantidQt::CustomDialogs
#define DECLARE_DIALOG(classname)
double value
The value of the point.
Definition FitMW.cpp:51
std::map< DeltaEMode::Type, std::string > index
This class should be the basis for all customised algorithm dialogs.
bool setPropertyValues(const QStringList &skipList=QStringList())
Set properties on this algorithm by pulling values from the tied widgets.
QString getPreviousValue(const QString &propName) const
Get the property value from either the previous input store or from Python argument.
QLayout * createDefaultButtonLayout(const QString &helpText=QString("?"), const QString &loadText=QString("Run"), const QString &cancelText=QString("Close"), const QString &keepOpenText=QString("Keep Open"))
Create a row layout of buttons with specified text.
void storePropertyValue(const QString &name, const QString &value)
Adds a property (name,value) pair to the stored map.
QWidget * tie(QWidget *widget, const QString &property, QLayout *parent_layout=nullptr, bool readHistory=true)
Tie a widget to a property.
Mantid::API::IAlgorithm_sptr getAlgorithm() const
Get the algorithm pointer.
Mantid::Kernel::Property * getAlgorithmProperty(const QString &propName) const
Get a pointer to the named property.
virtual void saveInput()
Save the input history of an accepted dialog.
Base class for input workspace's dynamic properties widget.
Definition FitDialog.h:158
InputWorkspaceWidget * m_wsWidget
Parent InputWorkspaceWidget.
Definition FitDialog.h:169
virtual void setProperties()=0
Set all workspace properties.
This class gives specialised dialog for the Load algorithm.
Definition FitDialog.h:54
void initLayout() override
Initialize the layout.
QString getDomainTypeString() const
Get the domain type: Simple, Sequential, or Parallel.
void setWorkspaceName(int i, const QString &wsName)
Set i-th workspace name.
bool isMD() const
Is the function MD?
void parseInput() override
Parse input.
void workspaceChanged(const QString &)
Override the help button clicked method.
void saveInput() override
Save the input history.
FitDialog(QWidget *parent=nullptr)
Default constructor.
int getDomainType() const
Get the domain type: Simple, Sequential, or Parallel.
void domainTypeChanged()
Update user interface when domain type changes.
void createInputWorkspaceWidgets()
Create InputWorkspaceWidgets and populate the tabs of the tab widget.
QStringList getAllowedPropertyValues(const QString &propName) const
Get allowed values for a property.
void tieStaticWidgets(const bool readHistory)
Tie static widgets to their properties.
Widget for inputting workspace information.
Definition FitDialog.h:102
bool isMatrixWorkspace() const
Is the workspace MW?
Definition FitDialog.cpp:73
bool isWorkspaceSupported() const
is current workspace supported by Fit?
int getDomainType() const
Get the domain type: Simple, Sequential, or Parallel.
Definition FitDialog.h:124
int getDomainIndex() const
Return the domain index.
Definition FitDialog.h:118
void setWorkspaceName(const QString &wsName)
Set workspace name.
void setDynamicProperties()
Set the dynamic properties.
void setPropertyValue(const QString &propName, const QString &propValue)
Set a property.
QString getWorkspaceName() const
Get workspace name.
DynamicPropertiesWidget * m_dynamicProperties
Dynamic propeties widget.
Definition FitDialog.h:149
bool isWSNameSet() const
Is ws name set?
Definition FitDialog.cpp:65
QVBoxLayout * m_layout
The main layout.
Definition FitDialog.h:152
void setProperties()
Set all workspace properties.
QComboBox * m_workspaceName
Workspace name widget.
Definition FitDialog.h:147
FitDialog * m_fitDialog
Parent FitDialog.
Definition FitDialog.h:140
QString m_wsPropName
Name of the property for the input workspace.
Definition FitDialog.h:145
bool isMDWorkspace() const
Is the workspace MD?
Definition FitDialog.cpp:88
Widgets to set properties for a IMDWorkspace: MaxSize.
Definition FitDialog.h:196
MDPropertiesWidget(InputWorkspaceWidget *parent)
Constructor.
void setProperties() override
Set all workspace properties.
Widgets to set properties for a MatrixWorkspace: WorkspaceIndex, StartX, EndX.
Definition FitDialog.h:175
MWPropertiesWidget(InputWorkspaceWidget *parent)
Constructor.
void setProperties() override
Set all workspace properties.
void init() override
Initialize the child widgets with stored and allowed values.
Basic MD Workspace Abstract Class.
Base MatrixWorkspace Abstract Class.
virtual std::vector< std::string > allowedValues() const
Returns the set of valid values for this property, if such a set exists.
Definition Property.cpp:155
std::shared_ptr< IFunction > IFunction_sptr
shared pointer to the function base class
Definition IFunction.h:743
std::vector< double > MantidVec
typedef for the data storage used in Mantid matrix workspaces
Definition cow_ptr.h:172