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