Mantid
Loading...
Searching...
No Matches
PropertyHandler.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 +
9
23
25
26#include "MantidQtWidgets/Common/QtPropertyBrowser/ParameterPropertyManager.h"
27#include "MantidQtWidgets/Common/QtPropertyBrowser/qtpropertymanager.h"
28#include "MantidQtWidgets/Common/QtPropertyBrowser/qttreepropertybrowser.h"
29
30#include <QMessageBox>
31#include <algorithm>
32#include <regex>
33#include <utility>
34
35using std::size_t;
36
37namespace {
38const std::regex PREFIX_REGEX("(^[f][0-9](.*))");
39inline bool variableIsPrefixed(const std::string &name) { return std::regex_match(name, PREFIX_REGEX); }
40} // namespace
41
43
44// Constructor
46 FitPropertyBrowser *browser, QtBrowserItem *item)
47 : FunctionHandler(fun), m_browser(browser), m_cf(std::dynamic_pointer_cast<Mantid::API::CompositeFunction>(fun)),
48 m_pf(std::dynamic_pointer_cast<Mantid::API::IPeakFunction>(fun)), m_parent(std::move(parent)), m_type(nullptr),
49 m_item(item), m_isMultispectral(false), m_workspace(nullptr), m_workspaceIndex(nullptr), m_base(0), m_ci(0),
50 m_hasPlot(false) {}
51
54
58 if (m_parent == nullptr) { // the root composite function
60 } else if (m_item == nullptr) {
61 if (!m_parent->getHandler()) {
62 throw std::runtime_error("Parent function handler does not exist");
63 }
64 // PropertyHandler* ph = parentHandler();
65 QtBrowserItem const *pi = parentHandler()->item();
66 // Create group property with function name on it
67 QtProperty *fnProp = m_browser->m_groupManager->addProperty(functionName());
68 pi->property()->addSubProperty(fnProp);
69 // assign m_item
70 QList<QtBrowserItem *> itList = pi->children();
71 const auto it = std::find_if(itList.cbegin(), itList.cend(),
72 [&fnProp](auto browserItem) { return browserItem->property() == fnProp; });
73 if (it != itList.cend()) {
74 m_item = *it;
75 } else {
76 throw std::runtime_error("Browser item not found");
77 }
78
79 if (!m_cf) {
80 m_browser->m_browser->setExpanded(m_item, false);
81 }
82 } else {
83 m_item->property()->setPropertyName(functionName());
84 }
85
86 QtProperty *fnProp = m_item->property();
87
88 // create Type property
89 if (!m_type) {
90 m_type = m_browser->m_enumManager->addProperty("Type");
91
92 fnProp->addSubProperty(m_type);
93 if (m_parent) {
95
96 } else {
97 QStringList functionNames;
98 functionNames << "CompositeFunction"; // << "MultiBG";
99 m_browser->m_enumManager->setEnumNames(m_type, functionNames);
100 }
101 }
102 int itype = m_browser->m_enumManager->enumNames(m_type).indexOf(QString::fromStdString(m_fun->name()));
103 m_browser->m_enumManager->setValue(m_type, itype);
104 // create worspace and workspace index properties if parent is a MultiBG
106
107 // create attribute properties
109
110 // create parameter properties
112
113 // set handlers for the child functions
114 if (m_cf && m_cf->nFunctions() > 0) {
115 for (size_t i = 0; i < m_cf->nFunctions(); i++) {
116 Mantid::API::IFunction_sptr f = std::dynamic_pointer_cast<Mantid::API::IFunction>(m_cf->getFunction(i));
117 if (!f) {
118 throw std::runtime_error("IFunction expected but func function of another type");
119 }
120 auto h = std::make_unique<PropertyHandler>(f, m_cf, m_browser);
121 f->setHandler(std::move(h));
122 }
123 initTies(); // populate ties after all child functions handlers have been inititiated (post setHandler)
124 }
125
127}
128
134public:
136 Mantid::Kernel::IValidator_sptr validator = nullptr)
137 : m_browser(browser), m_handler(handler), m_name(std::move(name)) {
138 m_validator = validator;
139 }
140
141protected:
143 QtProperty *apply(const std::string &str) const override {
144 QtProperty *prop;
145
146 // if validator is string list validator, create string list property
147 if (dynamic_cast<Mantid::Kernel::StringListValidator *>(m_validator.get()) != nullptr) {
148 prop = m_browser->addStringListProperty(m_name, m_validator->allowedValues());
149 } else {
151 }
152
153 m_browser->setStringPropertyValue(prop, QString::fromStdString(str));
154 return prop;
155 }
157 QtProperty *apply(const double &d) const override {
158 QtProperty *prop = m_browser->addDoubleProperty(m_name);
159 m_browser->m_doubleManager->setValue(prop, d);
160 return prop;
161 }
163 QtProperty *apply(const int &i) const override {
164 QtProperty *prop = m_browser->m_intManager->addProperty(m_name);
165 m_browser->m_intManager->setValue(prop, i);
166 return prop;
167 }
169 QtProperty *apply(const bool &b) const override {
170 QtProperty *prop = m_browser->m_boolManager->addProperty(m_name);
171 m_browser->m_boolManager->setValue(prop, b);
172 return prop;
173 }
175 QtProperty *apply(const std::vector<double> &b) const override {
176 // throw std::runtime_error("Vector attribute property not implememted.");
177 QtProperty *prop = m_browser->m_vectorManager->addProperty(m_name);
178 m_browser->m_vectorSizeManager->blockSignals(true);
179 QtProperty *sizeProp = m_browser->m_vectorSizeManager->addProperty("Size");
180 m_browser->m_vectorSizeManager->setValue(sizeProp, static_cast<int>(b.size()));
181 prop->addSubProperty(sizeProp);
182 m_handler->m_vectorSizes << sizeProp;
183 // sizeProp->setEnabled(false);
184 m_browser->m_vectorSizeManager->blockSignals(false);
185 m_browser->m_vectorDoubleManager->blockSignals(true);
186 QString dpName = "value[%1]";
187 for (size_t i = 0; i < b.size(); ++i) {
188 QtProperty *dprop = m_browser->addDoubleProperty(dpName.arg(i), m_browser->m_vectorDoubleManager);
189 m_browser->m_vectorDoubleManager->setValue(dprop, b[i]);
190 prop->addSubProperty(dprop);
191 m_handler->m_vectorMembers << dprop;
192 }
193 m_browser->m_vectorDoubleManager->blockSignals(false);
194 return prop;
195 }
196
197private:
200 QString m_name;
201};
202
207 for (size_t iparam = 0; iparam < m_cf->nParams(); iparam++) {
208 const auto *tie = m_cf->getTie(iparam);
209 if (tie) {
210 // get function index from prefix (second element of pair below)
211 const auto nameIndex_pair = m_cf->parseName(m_cf->parameterName(iparam));
213 std::dynamic_pointer_cast<Mantid::API::IFunction>(m_cf->getFunction(nameIndex_pair.second));
214 auto *h = findHandler(f);
215 h->addTie(QString::fromStdString(tie->asString()));
216 }
217 }
218}
219
224 std::vector<std::string> attNames = function()->getAttributeNames();
225 for (auto &attribute : m_attributes) {
226 m_item->property()->removeSubProperty(attribute);
227 }
228 m_attributes.clear();
229 m_vectorMembers.clear();
230 for (const auto &attName : attNames) {
231 if (variableIsPrefixed(attName))
232 continue;
233 QString aName = QString::fromStdString(attName);
234 Mantid::API::IFunction::Attribute att = function()->getAttribute(attName);
236 QtProperty *prop = att.apply(tmp);
237 m_item->property()->addSubProperty(prop);
238 m_attributes << prop;
239 }
240}
241
243 for (auto &parameter : m_parameters) {
244 m_item->property()->removeSubProperty(parameter);
245 }
246 m_parameters.clear();
247 for (size_t i = 0; i < function()->nParams(); i++) {
248 QString parName = QString::fromStdString(function()->parameterName(i));
249 if (parName.contains('.'))
250 continue;
251 QtProperty *prop = m_browser->addDoubleProperty(parName, m_browser->m_parameterManager);
252
253 m_browser->m_parameterManager->setDescription(prop, function()->parameterDescription(i));
254 m_browser->m_parameterManager->setValue(prop, function()->getParameter(i));
255
256 m_item->property()->addSubProperty(prop);
257 m_parameters << prop;
258 if (m_fun->isFixed(i)) {
259 fix(parName);
261 }
262 // add constraint properties
263 const Mantid::API::IConstraint *c = m_fun->getConstraint(i);
264 if (c) {
265 QStringList qc = QString::fromStdString(c->asString()).split("<");
266 bool lo = false;
267 bool up = false;
268 double loBound = 0, upBound = 0;
269 if (qc.size() == 2) {
270 if (qc[0].contains(parName)) {
271 up = true;
272 upBound = qc[1].toDouble();
273 } else {
274 lo = true;
275 loBound = qc[0].toDouble();
276 }
277 } else if (qc.size() == 3) {
278 lo = up = true;
279 loBound = qc[0].toDouble();
280 upBound = qc[2].toDouble();
281 } else {
282 continue;
283 }
284 QtProperty *loProp = nullptr;
285 QtProperty *upProp = nullptr;
286 if (lo) {
287 loProp = m_browser->addDoubleProperty("LowerBound");
288 m_browser->m_doubleManager->setValue(loProp, loBound);
289 prop->addSubProperty(loProp);
290 }
291 if (up) {
292 upProp = m_browser->addDoubleProperty("UpperBound");
293 m_browser->m_doubleManager->setValue(upProp, upBound);
294 prop->addSubProperty(upProp);
295 }
296 m_constraints.insert(parName, std::pair<QtProperty *, QtProperty *>(loProp, upProp));
297 }
298 }
299}
300
302 if (m_parent && m_parent->name() == "MultiBG") {
303 // m_workspace = m_browser->m_enumManager->addProperty("Workspace");
304 // QtProperty* fnProp = m_item->property();
305 // fnProp->addSubProperty(m_workspace);
306 // m_workspaceIndex = m_browser->m_intManager->addProperty("Workspace
307 // Index");
308 // if (! m_browser->m_workspaceNames.isEmpty() )
309 //{
310 // QStringList names("All");
311 // foreach(QString name,m_browser->m_workspaceNames)
312 // {
313 // names.append(name);
314 // }
315 // m_browser->m_enumManager->setEnumNames(m_workspace, names);
316 // int iWorkspace = 0;
317 // int iWorkspaceIndex = 0;
318 // if (ifun()->getWorkspace())
319 // {
320 // Mantid::API::IFunctionMW* ifmw =
321 // dynamic_cast<Mantid::API::IFunctionMW*>(ifun());
322 // if (ifmw)
323 // {
324 // std::string wsName = ifmw->getMatrixWorkspace()->getName();
325 // iWorkspace = names.indexOf(QString::fromStdString(wsName));
326 // if (iWorkspace >= 0)
327 // {
328 // iWorkspaceIndex = static_cast<int>(ifmw->getWorkspaceIndex());
329 // fnProp->addSubProperty(m_workspaceIndex);
330 // }
331 // else
332 // {
333 // iWorkspace = 0;
334 // }
335 // }
336 // }
337 // m_browser->m_enumManager->setValue(m_workspace,iWorkspace);
338 // m_browser->m_intManager->setValue(m_workspaceIndex,iWorkspaceIndex);
339 //}
340 } else {
341 m_workspace = m_workspaceIndex = nullptr;
342 }
343}
344
351 if (!m_cf)
352 return nullptr;
355 // Create new function
356 if (fnName.find("=") == std::string::npos) { // either from name
357 f = Mantid::API::FunctionFactory::Instance().createFunction(fnName);
358 } else { // of from full initialization expression
359 f = Mantid::API::FunctionFactory::Instance().createInitialized(fnName);
360 }
361
362 // turn of the change slots (doubleChanged() etc) to avoid infinite loop
364
365 // Check if it's a peak and set its width
366 std::shared_ptr<Mantid::API::IPeakFunction> pf = std::dynamic_pointer_cast<Mantid::API::IPeakFunction>(f);
367 if (pf) {
368 if (!m_browser->workspaceName().empty() && m_browser->workspaceIndex() >= 0 && pf->centre() == 0.) {
369 pf->setCentre((m_browser->startX() + m_browser->endX()) / 2);
370 }
371 }
372
374
375 try {
376 ws = std::dynamic_pointer_cast<Mantid::API::MatrixWorkspace>(
377 Mantid::API::AnalysisDataService::Instance().retrieve(m_browser->workspaceName()));
378 } catch (...) {
379 }
380
381 size_t wi = m_browser->workspaceIndex();
382
383 // if it's a LinearBackground estimate its A0 and A1 parameters
384 // from data values at the ends of the fitting interval
385 if (f->name() == "LinearBackground" && !m_browser->workspaceName().empty()) {
386 if (ws && wi < ws->getNumberHistograms()) {
387 const auto &X = ws->x(wi);
388 size_t istart = 0, iend = 0;
389 for (size_t i = 0; i < X.size() - 1; ++i) {
390 double x = X[i];
391 if (x < m_browser->startX())
392 istart = i;
393 if (x > m_browser->endX()) {
394 iend = i;
395 if (iend > 0)
396 iend--;
397 break;
398 }
399 }
400 if (iend > istart) {
401 const auto &Y = ws->y(wi);
402 double p0 = Y[istart];
403 double p1 = Y[iend];
404 double A1 = (p1 - p0) / (X[iend] - X[istart]);
405 double A0 = p0 - A1 * X[istart];
406 f->setParameter("A0", A0);
407 f->setParameter("A1", A1);
408 }
409 }
410 }
411 if (ws) {
413 }
414
415 size_t nFunctions = m_cf->nFunctions() + 1;
416 m_cf->addFunction(f);
417 m_browser->compositeFunction()->checkFunction();
418
419 if (m_cf->nFunctions() != nFunctions) { // this may happen
420 m_browser->reset();
421 return nullptr;
422 }
423
424 f->setHandler(std::make_unique<PropertyHandler>(f, m_cf, m_browser));
425 auto h = static_cast<PropertyHandler *>(f->getHandler());
426 h->setAttribute("StartX", m_browser->startX());
427 h->setAttribute("EndX", m_browser->endX());
428
429 // enable the change slots
432 if (pf) {
433 m_browser->setDefaultPeakType(f->name());
434 } else {
436 }
437 m_browser->setFocus();
438 auto return_ptr = static_cast<PropertyHandler *>(f->getHandler());
439 m_browser->setCurrentFunction(return_ptr);
440
441 return return_ptr;
442}
443
444// Removes handled function from its parent function and
445// properties from the browser
448 if (ph) {
449 if (this == m_browser->m_autoBackground) {
450 m_browser->m_autoBackground = nullptr;
451 }
452 ph->item()->property()->removeSubProperty(m_item->property());
454 for (int i = 0; i < static_cast<int>(cf->nFunctions()); i++) {
455 if (cf->getFunction(i) == function()) {
456 emit m_browser->removePlotSignal(this);
457 cf->removeFunction(i);
458 break;
459 }
460 }
461 ph->renameChildren(*cf);
462 m_browser->setFitEnabled(cf->nFunctions() > 0);
463 }
464}
465
468 for (auto it = m_ties.begin(); it != m_ties.end();) {
469 QString parName = it.key();
470 QString fullName = functionPrefix() + "." + parName;
471 QtProperty *prop = it.value();
472 const auto paramIndex = m_browser->compositeFunction()->parameterIndex(fullName.toStdString());
473 const auto status = cf.getParameterStatus(paramIndex);
474 const auto *tie = cf.getTie(paramIndex);
475 if (!tie) {
478 // In this case the tie has been removed from the composite function since it contained a reference to
479 // the function which was removed
480 QtProperty *parProp = getParameterProperty(parName);
481 if (parProp != nullptr) {
482 parProp->removeSubProperty(prop);
483 // Don't increment the iterator if we delete the current tie.
484 it = m_ties.erase(it);
485 parProp->setEnabled(true);
486 }
487 } else {
488 ++it;
489 }
490 continue;
491 } else {
492 ++it;
493 }
494 // Refresh gui value in case it has been updated by the composite function re-indexing it's functions
495 // after one is removed
496 QStringList qtie = QString::fromStdString(tie->asString()).split("=");
497 if (qtie.size() < 2)
498 continue;
499 m_browser->m_stringManager->setValue(prop, qtie[1]);
500 }
501 if (!m_cf)
502 return;
503 // rename children
504 for (size_t i = 0; i < m_cf->nFunctions(); i++) {
506 if (!h)
507 continue;
508 QtProperty *nameProp = h->item()->property();
509 nameProp->setPropertyName(h->functionName());
510 h->renameChildren(cf);
511 }
513}
514
518 QString name = functionPrefix();
519 if (!name.isEmpty()) {
520 name += "-";
521 }
522 name += QString::fromStdString(function()->name());
523 return name;
524}
525
527 const PropertyHandler *ph = parentHandler();
528 if (ph) {
529 int iFun = -1;
531 for (int i = 0; i < static_cast<int>(cf->nFunctions()); i++) {
532 if (cf->getFunction(i) == function()) {
533 iFun = i;
534 break;
535 }
536 }
537 QString pref = ph->functionPrefix();
538 if (!pref.isEmpty())
539 pref += ".";
540 return pref + "f" + QString::number(iFun);
541 }
542 return "";
543}
544
545// Return the parent handler
547 if (!m_parent)
548 return nullptr;
549 PropertyHandler *ph = static_cast<PropertyHandler *>(m_parent->getHandler());
550 return ph;
551}
552// Return the child's handler
554 if (!m_cf || i >= m_cf->nFunctions())
555 return nullptr;
556 PropertyHandler *ph = static_cast<PropertyHandler *>(m_cf->getFunction(i)->getHandler());
557 return ph;
558}
564 if (!m_cf)
566 if (item == m_item)
567 return m_cf;
568 for (size_t i = 0; i < m_cf->nFunctions(); i++) {
570 if (res != nullptr)
571 return res;
572 }
574}
580 if (item == m_item)
581 return function();
582 if (!m_cf)
584 for (size_t i = 0; i < m_cf->nFunctions(); i++) {
586 if (res != nullptr)
587 return res;
588 }
590}
591
593 if (prop == nullptr)
594 return nullptr;
595 if (prop == m_item->property())
596 return this;
597 if (prop == m_type)
598 return this;
599 if (prop == m_workspace)
600 return this;
601 if (prop == m_workspaceIndex)
602 return this;
603 if (m_attributes.contains(prop))
604 return this;
605 if (m_parameters.contains(prop))
606 return this;
607 if (m_vectorMembers.contains(prop))
608 return this;
609 if (m_vectorSizes.contains(prop))
610 return this;
611 if (!m_ties.key(prop, "").isEmpty())
612 return this;
613 QMap<QString, std::pair<QtProperty *, QtProperty *>>::iterator it = m_constraints.begin();
614 for (; it != m_constraints.end(); ++it) {
615 if (it.value().first == prop || it.value().second == prop) {
616 return this;
617 }
618 }
619 if (!m_cf)
620 return nullptr;
621 for (size_t i = 0; i < m_cf->nFunctions(); i++) {
623 if (h != nullptr)
624 return h;
625 }
626 return nullptr;
627}
628
630 if (fun == function())
631 return this;
632 if (m_cf) {
633 for (size_t i = 0; i < m_cf->nFunctions(); i++) {
635 if (h)
636 return h;
637 }
638 }
639 return nullptr;
640}
641
643 if (fun == function().get())
644 return this;
645 if (m_cf) {
646 for (size_t i = 0; i < m_cf->nFunctions(); i++) {
648 if (h)
649 return h;
650 }
651 }
652 return nullptr;
653}
654
660bool PropertyHandler::setParameter(QtProperty *prop) {
661 if (m_parameters.contains(prop)) {
662 std::string parName = prop->propertyName().toStdString();
663 double parValue = m_browser->m_parameterManager->value(prop);
664 m_fun->setParameter(parName, parValue);
665
666 // If the parameter is fixed, re-fix to update the subproperty.
667 if (m_fun->isFixed(m_fun->parameterIndex(parName))) {
668 const auto subProps = prop->subProperties();
669 if (std::any_of(subProps.cbegin(), subProps.cend(),
670 [](const auto &subProp) { return subProp->propertyName() == "Fix"; })) {
671 fix(prop->propertyName());
672 }
673 }
674
677 return true;
678 }
679 if (m_cf) {
680 for (size_t i = 0; i < m_cf->nFunctions(); i++) {
681 bool res = getHandler(i)->setParameter(prop);
682 if (res) {
683 m_cf->applyTies();
685 return true;
686 }
687 }
688 }
689 return false;
690}
691
697public:
698 SetAttribute(FitPropertyBrowser *browser, QtProperty *prop,
700 : m_browser(browser), m_prop(prop) {
701 m_validator = validator;
702 }
703
704protected:
706 void apply(std::string &str) const override {
707 std::string propValue = m_browser->getStringPropertyValue(m_prop).toStdString();
708
709 evaluateValidator(propValue);
710 str = propValue;
711 }
713 void apply(double &d) const override {
714 double propValue = m_browser->m_doubleManager->value(m_prop);
715
716 evaluateValidator(propValue);
717 d = propValue;
718 }
720 void apply(int &i) const override {
721 int propValue = m_browser->m_intManager->value(m_prop);
722
723 evaluateValidator(propValue);
724 i = propValue;
725 }
727 void apply(bool &b) const override {
728 bool propValue = m_browser->m_boolManager->value(m_prop);
729
730 evaluateValidator(propValue);
731 b = propValue;
732 }
734 void apply(std::vector<double> &v) const override {
735 QList<QtProperty *> members = m_prop->subProperties();
736 if (members.size() < 1) {
737 v.clear();
738 return;
739 }
740
741 int newSize = m_browser->m_vectorSizeManager->value(members[0]);
742 int vectorSize = members.size() - 1;
743 if (vectorSize > newSize) {
744 vectorSize = newSize;
745 }
746
747 // populate new vector
748 std::vector<double> newVec(newSize);
749 for (int i = 1; i < newSize + 1; ++i) {
750 double newVal = (m_validator != Mantid::Kernel::IValidator_sptr())
751 ? m_browser->m_vectorDoubleManager->value(members[vectorSize])
752 : 0.0;
753 if (i < vectorSize + 1) {
754 newVec[i - 1] = m_browser->m_vectorDoubleManager->value(members[i]);
755 } else {
756 newVec[i - 1] = newVal;
757 }
758 }
759
761 evaluateValidator(newVec);
762 }
763
764 v.resize(newSize);
765 std::copy(cbegin(newVec), cend(newVec), begin(v));
766 }
767
768private:
770 QtProperty *m_prop;
771};
772
778public:
779 SetAttributeProperty(FitPropertyBrowser *browser, QtProperty *prop) : m_browser(browser), m_prop(prop) {}
780
781protected:
783 void apply(const std::string &str) const override {
785 m_browser->setStringPropertyValue(m_prop, QString::fromStdString(str));
787 }
789 void apply(const double &d) const override {
791 m_browser->m_doubleManager->setValue(m_prop, d);
793 }
795 void apply(const int &i) const override {
797 m_browser->m_intManager->setValue(m_prop, i);
799 }
801 void apply(const bool &b) const override {
803 m_browser->m_boolManager->setValue(m_prop, b);
805 }
807 void apply(const std::vector<double> & /*unused*/) const override {
808 // this method is supposed to be called when corresponding
809 // property value changes but it doesn't have a value because
810 // it's a group property
811 throw std::runtime_error("Vector attribute not implemented.");
812 }
813
814private:
816 QtProperty *m_prop;
817};
818
826bool PropertyHandler::setAttribute(QtProperty *prop, bool resetProperties) {
827 if (m_attributes.contains(prop)) {
828 QString attName = prop->propertyName();
829 try {
830 Mantid::API::IFunction::Attribute att = m_fun->getAttribute(attName.toStdString());
832 att.apply(tmp);
833 m_fun->setAttribute(attName.toStdString(), att);
834 m_browser->compositeFunction()->checkFunction();
835 if (resetProperties) {
838 }
839 if (this == m_browser->m_autoBackground) {
840 fit();
841 }
842 } catch (Mantid::API::IFunction::ValidationException &ve) { // catch attribute validation error
845
847 ve.what()); // rethrow validation exception so it can be recaught by Fit Property Browser.
848 } catch (std::exception &e) {
851 QMessageBox::critical(m_browser, "Mantid - Error", e.what());
852 return false;
853 }
854 return true;
855 }
856 if (m_cf) {
857 for (size_t i = 0; i < m_cf->nFunctions(); i++) {
858 bool res = getHandler(i)->setAttribute(prop, resetProperties);
859 if (res)
860 return true;
861 }
862 }
863 return false;
864}
865
871void PropertyHandler::setAttribute(QString const &attName, Mantid::API::IFunction::Attribute const &attValue) {
872 auto const attributeType = attValue.type();
873 if (attributeType == "int")
874 setAttribute(attName, attValue.asInt());
875 else if (attributeType == "double")
876 setAttribute(attName, attValue.asDouble());
877 else if (attributeType == "std::string")
878 setAttribute(attName, QString::fromStdString(attValue.asString()));
879}
880
886template <typename AttributeType>
887void PropertyHandler::setAttribute(QString const &attName, AttributeType const &attValue) {
888 if (m_fun->hasAttribute(attName.toStdString())) {
889 try {
890 m_fun->setAttribute(attName.toStdString(), Mantid::API::IFunction::Attribute(attValue));
891 m_browser->compositeFunction()->checkFunction();
892 foreach (const QtProperty *prop, m_attributes) {
893 if (prop->propertyName() == attName) {
894 // re-insert the attribute and parameter properties as they may
895 // depend on the value of the attribute being set
898 }
899 }
900 } catch (...) {
901 }
902 }
903 if (cfun()) {
904 for (auto i = 0u; i < cfun()->nFunctions(); ++i) {
906 h->setAttribute(attName, attValue);
907 }
908 }
909}
910
916void PropertyHandler::setAttribute(const QString &attName, const QString &attValue) {
917 const std::string name = attName.toStdString();
918 if (m_fun->hasAttribute(name)) {
919 Mantid::API::IFunction::Attribute att = m_fun->getAttribute(name);
920 att.fromString(attValue.toStdString());
921 m_fun->setAttribute(name, att);
922 m_browser->compositeFunction()->checkFunction();
923 foreach (QtProperty *prop, m_attributes) {
924 if (prop->propertyName() == attName) {
926 att.apply(tmp);
927 }
928 }
929 // re-insert the attribute and parameter properties as they may
930 // depend on the value of the attribute being set
933 }
934}
935
941 foreach (QtProperty *att, m_attributes) {
942 QList<QtProperty *> subProps = att->subProperties();
943 if (subProps.contains(prop)) {
944 bool resetProperties = m_vectorSizes.contains(prop);
945 setAttribute(att, resetProperties);
946 return;
947 }
948 }
949}
950
957 for (auto attribute : m_attributes) {
958 (this->*(func))(attribute);
959 }
960
961 if (m_cf)
962 for (std::size_t i = 0u; i < m_cf->nFunctions(); ++i)
964}
965
971
975void PropertyHandler::updateAttribute(QtProperty *attribute) {
976 if (m_attributes.contains(attribute)) {
977 auto const attributeValue = function()->getAttribute(attribute->propertyName().toStdString());
978 setAttribute(attribute->propertyName(), attributeValue);
979 }
980}
981
988 for (auto prop : m_parameters) {
989 (this->*(func))(prop);
990 }
991
992 if (m_cf) {
993 for (size_t i = 0; i < m_cf->nFunctions(); i++) {
995 }
996 }
997}
998
1000
1002
1004
1008void PropertyHandler::updateParameter(QtProperty *prop) {
1009 double const parValue = function()->getParameter(prop->propertyName().toStdString());
1010 m_browser->m_parameterManager->setValue(prop, parValue);
1011}
1012
1016void PropertyHandler::updateError(QtProperty *prop) {
1017 size_t index = function()->parameterIndex(prop->propertyName().toStdString());
1018 double error = function()->getError(index);
1019 m_browser->m_parameterManager->setError(prop, error);
1020}
1021
1025void PropertyHandler::clearError(QtProperty *prop) { m_browser->m_parameterManager->clearError(prop); }
1026
1032 if (prop == m_type) {
1033 // Create new function
1034 int i = m_browser->m_enumManager->value(prop);
1035 QStringList functionNames = m_browser->m_enumManager->enumNames(prop);
1036 const QString &fnName = functionNames[i];
1038 try {
1039 f = Mantid::API::FunctionFactory::Instance().createFunction(fnName.toStdString());
1040
1041 } catch (std::exception &e) {
1042 QMessageBox::critical(nullptr, "Mantid - Error", "Cannot create function " + fnName + "\n" + e.what());
1044 }
1045
1046 // turn of the change slots (doubleChanged() etc) to avoid infinite loop
1048
1049 // Check if it's a peak and set its width
1050 Mantid::API::IPeakFunction *pf = dynamic_cast<Mantid::API::IPeakFunction *>(f.get());
1051 if (pf) {
1052 if (!m_pf) {
1053 if (!m_browser->workspaceName().empty() && m_browser->workspaceIndex() >= 0) {
1054 pf->setCentre((m_browser->startX() + m_browser->endX()) / 2);
1055 }
1056
1057 } else {
1058 pf->setCentre(m_pf->centre());
1059 pf->setHeight(m_pf->height());
1060 pf->setFwhm(m_pf->fwhm());
1061 }
1062 }
1063
1064 if (pf) {
1065 m_browser->setDefaultPeakType(fnName.toStdString());
1066
1067 } else {
1068 m_browser->setDefaultBackgroundType(fnName.toStdString());
1069 }
1070
1071 QList<QtProperty *> subs = m_item->property()->subProperties();
1072 foreach (QtProperty *sub, subs) {
1073 m_item->property()->removeSubProperty(sub);
1074 }
1075
1077
1078 emit m_browser->removePlotSignal(this);
1079
1081 std::unique_ptr<PropertyHandler> h = std::make_unique<PropertyHandler>(f, m_parent, m_browser, m_item);
1082 if (this == m_browser->m_autoBackground) {
1083 if (dynamic_cast<Mantid::API::IBackgroundFunction *>(f.get())) {
1084 m_browser->m_autoBackground = h.get();
1085 h->fit();
1086
1087 } else {
1088 m_browser->m_autoBackground = nullptr;
1089 }
1090 }
1091 if (m_parent) {
1092 m_parent->replaceFunctionPtr(f_old, f);
1093 }
1094 // calculate the baseline
1095 if (h->pfun()) {
1096 h->setCentre(h->centre()); // this sets m_ci
1097 h->calcBase();
1098 }
1099 f->setHandler(std::move(h));
1100 // at this point this handler does not exist any more. only return is
1101 // possible
1102 return f;
1103
1104 } else if (m_cf) {
1105 for (size_t i = 0; i < m_cf->nFunctions(); i++) {
1107 if (f)
1108 return f;
1109 }
1110 }
1112}
1113
1114bool PropertyHandler::isParameter(QtProperty *prop) { return m_parameters.contains(prop); }
1115
1116QtProperty *PropertyHandler::getParameterProperty(const QString &parName) const {
1117 const auto it = std::find_if(m_parameters.cbegin(), m_parameters.cend(),
1118 [&parName](const auto &parProp) { return parProp->propertyName() == parName; });
1119 if (it != m_parameters.cend()) {
1120 return *it;
1121 }
1122 return nullptr;
1123}
1124
1125QtProperty *PropertyHandler::getParameterProperty(QtProperty *prop) const {
1126 const auto it = std::find_if(m_parameters.cbegin(), m_parameters.cend(),
1127 [&prop](const auto &parProp) { return parProp->subProperties().contains(prop); });
1128 if (it != m_parameters.cend()) {
1129 return *it;
1130 }
1131 return nullptr;
1132}
1133
1134void PropertyHandler::addTie(const QString &tieStr) {
1135 QStringList parts = tieStr.split("=");
1136 if (parts.size() != 2)
1137 return;
1138 std::string name = parts[0].trimmed().toStdString();
1139 std::string expr = parts[1].trimmed().toStdString();
1140 try {
1141 auto &cfunction = *m_browser->compositeFunction();
1142 cfunction.tie(name, expr);
1143 cfunction.applyTies();
1144 const auto paramIndex = cfunction.parameterIndex(name);
1145 const auto paramStatus = cfunction.getParameterStatus(paramIndex);
1146 const bool fixed = paramStatus == Mantid::API::IFunction::ParameterStatus::Fixed;
1147 const bool recursive = true;
1148 QString parName = QString::fromStdString(cfunction.parameterLocalName(paramIndex, recursive));
1149 QtProperty *parProp = getParameterProperty(parName);
1150 if (!parProp)
1151 return;
1153 QtProperty *tieProp = m_ties[parName];
1154 if (!tieProp) {
1155 const auto tiePropName = fixed ? "Fix" : "Tie";
1156 tieProp = m_browser->m_stringManager->addProperty(tiePropName);
1157 m_ties[parName] = tieProp;
1158 }
1159 m_browser->m_stringManager->setValue(tieProp, QString::fromStdString(expr));
1160 parProp->addSubProperty(tieProp);
1161 if (fixed) {
1162 tieProp->setEnabled(false);
1163 }
1165 if (!fixed) {
1167 }
1168 } catch (const std::exception &exc) {
1169 std::cerr << exc.what();
1170 QMessageBox::critical(m_browser, "Mantid - Error", "Failed to set tie: " + tieStr);
1171 }
1172}
1173
1174void PropertyHandler::fix(const QString &parName) {
1175 const QtProperty *parProp = getParameterProperty(parName);
1176 if (!parProp)
1177 return;
1178 QString parValue = QString::number(m_browser->m_parameterManager->value(parProp));
1179 addTie(functionPrefix() + "." + parName + "=" + parValue);
1180}
1181
1188void PropertyHandler::removeTie(QtProperty *prop, const std::string &globalName) {
1189 QString parName = m_ties.key(prop, "");
1190 if (parName.isEmpty())
1191 return;
1192
1193 QtProperty *parProp = getParameterProperty(parName);
1194 if (parProp) {
1196 auto &compositeFunction = *m_browser->compositeFunction();
1197 auto index = compositeFunction.parameterIndex(globalName);
1198 compositeFunction.removeTie(index);
1199 parProp->removeSubProperty(prop);
1200 m_ties.remove(QString::fromStdString(globalName));
1201 m_ties.remove(parName);
1203 parProp->setEnabled(true);
1204 }
1205}
1210void PropertyHandler::removeTie(QtProperty *prop) {
1211 QString parName = m_ties.key(prop, "");
1212 if (parName.isEmpty())
1213 return;
1214
1215 QtProperty *parProp = getParameterProperty(parName);
1216 if (parProp != nullptr) {
1218 m_fun->removeTie(parName.toStdString());
1219 parProp->removeSubProperty(prop);
1220 m_ties.remove(parName);
1222 parProp->setEnabled(true);
1223 }
1224}
1225
1230void PropertyHandler::removeTie(const QString &parName) {
1231 QtProperty *prop = m_ties[parName];
1232 if (prop)
1233 removeTie(prop);
1234}
1235
1241 double fwhmEstimate = 0.;
1242 auto ws = std::dynamic_pointer_cast<const Mantid::API::MatrixWorkspace>(m_browser->getWorkspace());
1243 if (ws) {
1244 size_t wi = m_browser->workspaceIndex();
1245 const auto &X = ws->x(wi);
1246 const auto &Y = ws->y(wi);
1247 size_t n = Y.size() - 1;
1248 if (m_ci < 0 || m_ci > static_cast<int>(n)) {
1249 fwhmEstimate = 0.;
1250 } else {
1251 double halfHeight = ((Y[m_ci] - m_base) / 2.) + m_base;
1252 // walk to the right
1253 size_t rightHwhmIndex = m_ci;
1254 while (rightHwhmIndex < n) {
1255 if (Y[rightHwhmIndex++] <= halfHeight) {
1256 break;
1257 }
1258 }
1259
1260 // walk to the left
1261 size_t leftHwhmIndex = m_ci;
1262 while (leftHwhmIndex > 0) {
1263 if (Y[leftHwhmIndex--] <= halfHeight) {
1264 break;
1265 }
1266 }
1267
1268 fwhmEstimate = fabs(X[rightHwhmIndex] - X[leftHwhmIndex]);
1269
1270 // apply a maximum limitation if larger than the fitting region
1271 double fitRange = m_browser->endX() - m_browser->startX();
1272 if (fwhmEstimate > fitRange) {
1273 // set to 10% of fitting region
1274 fwhmEstimate = fitRange * 0.1;
1275 }
1276 }
1277 }
1278 return fwhmEstimate;
1279}
1286 return;
1287
1288 auto ws = std::dynamic_pointer_cast<const Mantid::API::MatrixWorkspace>(m_browser->getWorkspace());
1289 if (ws) {
1290 size_t wi = m_browser->workspaceIndex();
1291 const auto &X = ws->x(wi);
1292 const auto &Y = ws->y(wi);
1293 int n = static_cast<int>(Y.size()) - 1;
1294 if (m_ci < 0 || m_ci > n || !m_browser->m_autoBackground) {
1295 m_base = 0.;
1296 } else {
1299 m_browser->m_autoBackground->function()->function(x, y);
1300 m_base = y[0];
1301 }
1302 } else {
1303 m_base = 0.;
1304 }
1305}
1306
1314 return;
1315 if (!m_cf)
1316 return;
1317 for (size_t i = 0; i < m_cf->nFunctions(); ++i) {
1319 if (h->pfun()) {
1320 h->calcBase();
1321 } else if (h->cfun()) {
1322 h->calcBaseAll();
1323 }
1324 }
1325}
1326
1330void PropertyHandler::setHeight(const double &h) {
1331 if (m_pf) {
1332 m_pf->setHeight(h - m_base);
1333 }
1334}
1335
1340void PropertyHandler::setCentre(const double &c) {
1341 if (m_pf) {
1342 m_pf->setCentre(c);
1343
1344 // find m_ci: x-index of the peakcentre
1345 auto ws = std::dynamic_pointer_cast<const Mantid::API::MatrixWorkspace>(m_browser->getWorkspace());
1346 if (ws) {
1347 size_t wi = m_browser->workspaceIndex();
1348 const auto &X = ws->x(wi);
1349 int n = static_cast<int>(X.size()) - 2;
1350 if (m_ci < 0)
1351 m_ci = 0;
1352 if (m_ci > n)
1353 m_ci = n;
1354 double x = X[m_ci];
1355 if (x < c) {
1356 for (; m_ci <= n; ++m_ci) {
1357 x = X[m_ci];
1358 if (x > c)
1359 break;
1360 }
1361 } else {
1362 for (; m_ci >= 0; --m_ci) {
1363 x = X[m_ci];
1364 if (x < c)
1365 break;
1366 }
1367 }
1368 }
1369 }
1370}
1371
1372void PropertyHandler::setFwhm(const double &w) {
1373 if (m_pf) {
1374 m_pf->setFwhm(w);
1375 }
1376}
1377
1379 if (m_pf) {
1380 return m_pf->height();
1381 }
1382 return 0;
1383}
1384
1386 if (m_pf) {
1387 return m_pf->centre();
1388 }
1389 return (m_browser->endX() + m_browser->startX()) / 2;
1390}
1391
1393 if (m_pf) {
1394 return m_pf->fwhm();
1395 }
1396 return 0;
1397}
1398
1400 if (m_pf) {
1401 return m_pf->getWidthParameterName();
1402 }
1403 return "";
1404}
1405
1407 if (m_pf) {
1408 return m_pf->getCentreParameterName();
1409 }
1410 return "";
1411}
1412
1413bool PropertyHandler::isParameterExplicitlySet(const std::string &param) const {
1414 if (m_pf) {
1415 return m_pf->isExplicitlySet(m_pf->parameterIndex(param));
1416 }
1417 return false;
1418}
1419
1423void PropertyHandler::addConstraint(QtProperty *parProp, bool lo, bool up, double loBound, double upBound) {
1424 QMap<QString, std::pair<QtProperty *, QtProperty *>>::iterator old = m_constraints.find(parProp->propertyName());
1425
1426 bool hasLo = false;
1427 bool hasUp = false;
1428
1429 if (old != m_constraints.end()) {
1430 hasLo = old.value().first != NULL;
1431 hasUp = old.value().second != NULL;
1432 if (hasLo && !lo) {
1433 lo = true;
1434 loBound = m_browser->m_doubleManager->value(old.value().first);
1435 }
1436 if (hasUp && !up) {
1437 up = true;
1438 upBound = m_browser->m_doubleManager->value(old.value().second);
1439 }
1440 }
1441
1443 std::pair<QtProperty *, QtProperty *> cnew; //(nullptr,nullptr); - Can't do this in constructor in C++11
1444 // Don't know if these 2 lines are necessary, but this code is hard to
1445 // understand - it could really use some comments!
1446 cnew.first = NULL;
1447 cnew.second = NULL;
1448 std::ostringstream ostr;
1449 if (lo) {
1450 ostr << loBound << "<";
1451 if (!hasLo) {
1452 cnew.first = m_browser->addDoubleProperty("LowerBound");
1453 parProp->addSubProperty(cnew.first);
1454 } else {
1455 cnew.first = old.value().first;
1456 }
1457 m_browser->m_doubleManager->setValue(cnew.first, loBound);
1458 }
1459 ostr << parProp->propertyName().toStdString();
1460 if (up) {
1461 ostr << "<" << upBound;
1462 if (!hasUp) {
1463 cnew.second = m_browser->addDoubleProperty("UpperBound");
1464 parProp->addSubProperty(cnew.second);
1465 } else {
1466 cnew.second = old.value().second;
1467 }
1468 m_browser->m_doubleManager->setValue(cnew.second, upBound);
1469 }
1470
1471 if (old != m_constraints.end()) {
1472 m_constraints.erase(old);
1473 }
1474
1475 m_constraints.insert(parProp->propertyName(), cnew);
1476
1477 auto c = std::unique_ptr<Mantid::API::IConstraint>(
1478 Mantid::API::ConstraintFactory::Instance().createInitialized(m_fun.get(), ostr.str()));
1479 m_fun->addConstraint(std::move(c));
1481}
1482
1483void PropertyHandler::removeConstraint(QtProperty *parProp) {
1484 QMap<QString, std::pair<QtProperty *, QtProperty *>>::iterator it = m_constraints.find(parProp->propertyName());
1485
1486 if (it != m_constraints.end()) {
1487 if (it.value().first) {
1488 parProp->removeSubProperty(it.value().first);
1489 }
1490 if (it.value().second) {
1491 parProp->removeSubProperty(it.value().second);
1492 }
1493 m_fun->removeConstraint(parProp->propertyName().toStdString());
1494 m_constraints.erase(it);
1495 }
1496}
1497
1503 if (m_pf) {
1504 res << this;
1505 }
1506 if (m_cf) {
1507 for (size_t i = 0; i < m_cf->nFunctions(); ++i) {
1509 if (!h)
1510 continue;
1511 if (h->pfun()) {
1512 res << h;
1513 } else if (h->cfun()) {
1514 res << h->getPeakList();
1515 }
1516 }
1517 }
1518 return res;
1519}
1520
1525
1538 QString newTooltip;
1539
1540 if (m_cf && (m_cf->name() == "CompositeFunction" || m_cf->name() == "ProductFunction")) {
1541 QStringList childrenTooltips;
1542
1543 // Update tooltips for all the children first, and use them to build this
1544 // tooltip
1545 for (size_t i = 0; i < m_cf->nFunctions(); ++i) {
1546 if (auto childHandler = getHandler(i)) {
1547 childrenTooltips << childHandler->updateStructureTooltip();
1548 } else {
1549 throw std::runtime_error("Error while building structure tooltip: no handler for child");
1550 }
1551 }
1552
1553 if (childrenTooltips.empty()) {
1554 newTooltip = QString::fromStdString("Empty " + m_cf->name());
1555 } else {
1556 QChar op('+');
1557
1558 if (m_cf->name() == "ProductFunction") {
1559 op = '*';
1560 }
1561
1562 newTooltip = QString("(%1)").arg(childrenTooltips.join(' ' + op + ' '));
1563 }
1564 } else {
1565 newTooltip = QString::fromStdString(function()->name());
1566 }
1567
1568 m_item->property()->setToolTip(newTooltip);
1569 return newTooltip;
1570}
1571
1576 emit m_browser->removePlotSignal(this);
1577 if (m_cf) {
1578 for (size_t i = 0; i < m_cf->nFunctions(); ++i) {
1580 }
1581 }
1582}
1583
1585 try {
1586 if (m_browser->workspaceName().empty())
1587 return;
1588
1589 Mantid::API::IAlgorithm_sptr alg = Mantid::API::AlgorithmManager::Instance().create("Fit");
1590 alg->initialize();
1591 alg->setProperty("Function", m_fun);
1592 alg->setPropertyValue("InputWorkspace", m_browser->workspaceName());
1593 alg->setProperty("WorkspaceIndex", m_browser->workspaceIndex());
1594 alg->setProperty("StartX", m_browser->startX());
1595 alg->setProperty("EndX", m_browser->endX());
1596 alg->execute();
1597 Mantid::API::IFunction_sptr f = alg->getProperty("Function");
1598 if (f != m_fun) { // this should never happen, just in case...
1599 for (size_t i = 0; i < f->nParams(); ++i) {
1600 m_fun->setParameter(i, f->getParameter(i));
1601 }
1602 }
1605 } catch (...) {
1606 }
1607}
1608
1609void PropertyHandler::updateWorkspaces(const QStringList &oldWorkspaces) {
1610 if (m_workspace) {
1611 int index = m_browser->m_enumManager->value(m_workspace) - 1;
1612 QString wsName;
1613 if (index >= 0 && index < oldWorkspaces.size()) {
1614 wsName = oldWorkspaces[index];
1615 }
1616 QStringList names("All");
1617 foreach (const QString &name, m_browser->m_workspaceNames) {
1618 names.append(name);
1619 }
1620 m_browser->m_enumManager->setEnumNames(m_workspace, names);
1621 if (m_browser->m_workspaceNames.contains(wsName)) {
1622 m_browser->m_enumManager->setValue(m_workspace, m_browser->m_workspaceNames.indexOf(wsName) + 1);
1623 }
1624 }
1625 if (cfun()) {
1626 for (size_t i = 0; i < cfun()->nFunctions(); ++i) {
1627 getHandler(i)->updateWorkspaces(oldWorkspaces);
1628 }
1629 }
1630}
1631
1633 if (m_workspace) {
1634 int index = m_browser->m_enumManager->value(m_workspace) - 1;
1635 if (index >= 0 && index < m_browser->m_workspaceNames.size()) {
1636 std::string wsName = m_browser->m_workspaceNames[index].toStdString();
1637 Mantid::API::Workspace_sptr ws = Mantid::API::AnalysisDataService::Instance().retrieve(wsName);
1638 int wsIndex = m_browser->m_intManager->value(m_workspaceIndex);
1639 auto mws = std::dynamic_pointer_cast<Mantid::API::MatrixWorkspace>(ws);
1640 if (mws) {
1641 ifun()->setMatrixWorkspace(mws, size_t(wsIndex), m_browser->startX(), m_browser->endX());
1642 } else {
1643 ifun()->setWorkspace(ws);
1644 }
1645 m_item->property()->insertSubProperty(m_workspaceIndex, m_workspace);
1646 } else {
1647 ifun()->setWorkspace(Mantid::API::Workspace_sptr());
1648 m_item->property()->removeSubProperty(m_workspaceIndex);
1649 }
1650 } else {
1651 ifun()->setWorkspace(Mantid::API::Workspace_sptr());
1652 }
1653}
1654
1655} // namespace MantidQt::MantidWidgets
std::string name
Definition Run.cpp:60
gsl_vector * tmp
double error
std::map< DeltaEMode::Type, std::string > index
#define fabs(x)
Definition Matrix.cpp:22
Attribute visitor to create a QtProperty.
QtProperty * apply(const int &i) const override
Create int property.
QtProperty * apply(const bool &b) const override
Create bool property.
QtProperty * apply(const std::vector< double > &b) const override
Create vector property.
QtProperty * apply(const std::string &str) const override
Create string property.
CreateAttributeProperty(FitPropertyBrowser *browser, PropertyHandler *handler, QString name, Mantid::Kernel::IValidator_sptr validator=nullptr)
QtProperty * apply(const double &d) const override
Create double property.
Class FitPropertyBrowser implements QtPropertyBrowser to display and control fitting function paramet...
void setDefaultPeakType(const std::string &fnType)
Set the default peak type.
QtProperty * addStringListProperty(const QString &name, const std::vector< std::string > &allowed_values) const
Create a string list property.
std::shared_ptr< Mantid::API::CompositeFunction > compositeFunction() const
Get Composite Function.
void setWorkspace(const Mantid::API::IFunction_sptr &function) const
Sets the workspace to a function.
std::string workspaceName() const
Get the input workspace name.
void sendParameterChanged(const Mantid::API::IFunction *f)
QtGroupPropertyManager * m_groupManager
Property managers:
void setStringPropertyValue(QtProperty *prop, const QString &value) const
Set a value to a string property.
virtual void setFitEnabled(bool enable)
Enable/disable the Fit buttons;.
QStringList m_workspaceNames
A list of available workspaces.
void disableUndo()
disable undo when the function changes
void removePlotSignal(MantidQt::MantidWidgets::PropertyHandler *)
bool m_changeSlotsEnabled
If false the change-slots (such as enumChanged(), doubleChanged()) are disabled.
QtBrowserItem * m_functionsGroup
Group for functions.
PropertyHandler * getHandler() const
Get handler to the root composite function.
QString getStringPropertyValue(QtProperty *prop) const
std::shared_ptr< Mantid::API::Workspace > getWorkspace() const
Get the workspace.
void setDefaultBackgroundType(const std::string &fnType)
Set the default background type.
void reset()
reset the function part, renew function, all handlers are new
QtProperty * addStringProperty(const QString &name) const
Create a string property and set some settings.
QStringList m_registeredFunctions
A list of registered functions.
QtProperty * addDoubleProperty(const QString &name, QtDoublePropertyManager *manager=nullptr) const
Create a double property and set some settings.
void setCurrentFunction(PropertyHandler *h) const
Set new current function.
PropertyHandler * m_autoBackground
The autobackground handler.
Helps display and edit functions in FitPropertyBrowser.
void initAttributes()
Create and attach QtProperties for function attributes.
std::shared_ptr< Mantid::API::IPeakFunction > pfun() const
void renameChildren(const Mantid::API::CompositeFunction &cf)
QList< PropertyHandler * > getPeakList()
Make a list of all peaks in this function.
bool setParameter(QtProperty *prop)
Set function parameter value read from a QtProperty.
PropertyHandler * getHandler(std::size_t i) const
void updateParameter(QtProperty *prop)
Sync function parameter value with the manager.
double EstimateFwhm() const
Estimate the FwHM for a peak.
QtProperty * m_workspaceIndex
workspace index for multispectral fitting
void removeTie(QtProperty *prop, const std::string &globalName)
Remove the tie.
bool setAttribute(QtProperty *prop, bool resetProperties=true)
Set function attribute value read from a QtProperty.
std::shared_ptr< Mantid::API::CompositeFunction > m_cf
void clearError(QtProperty *prop)
Clear function parameter error in the manager.
void addConstraint(QtProperty *parProp, bool lo, bool up, double loBound, double upBound)
Add constraint to parameter property parProp.
PropertyHandler * addFunction(const std::string &fnName)
Add a function to the function handled by this handler.
PropertyHandler(const Mantid::API::IFunction_sptr &fun, std::shared_ptr< Mantid::API::CompositeFunction > parent, FitPropertyBrowser *browser, QtBrowserItem *item=nullptr)
void updateErrors()
Set all parameter error values in the manager.
PropertyHandler * findHandler(const Mantid::API::IFunction *fun)
bool isParameterExplicitlySet(const std::string &param) const
std::shared_ptr< Mantid::API::IPeakFunction > m_pf
void setCentre(const double &c)
Set the centre of the handled peak function.
void setVectorAttribute(QtProperty *prop)
Set function vector attribute value.
void applyToAllParameters(void(PropertyHandler::*func)(QtProperty *))
Applies given function to all the parameter properties recursively.
void calcBaseAll()
If the handled function is composite calculate the peak baselines for all members.
void setHeight(const double &h)
Set the height of the handled peak function.
void removeAllPlots()
Remove all plots including children's.
std::shared_ptr< Mantid::API::IFunction > ifun() const
QString functionName() const
Creates name for this function to be displayed in the browser.
void updateError(QtProperty *prop)
Set function parameter error in the manager.
std::shared_ptr< const Mantid::API::CompositeFunction > findCompositeFunction(QtBrowserItem *item) const
Returns 'this' if item == m_item and this is a composite function or calls findCompositeFunction recu...
std::shared_ptr< const Mantid::API::IFunction > findFunction(QtBrowserItem *item) const
Returns 'this' if item == m_item or calls findFunction recursively with all its children or zero.
std::shared_ptr< Mantid::API::IFunction > changeType(QtProperty *prop)
Change the type of the function (replace the function)
void initTies()
Populate ties on parameter properties of child functions.
void applyToAllAttributes(void(PropertyHandler::*func)(QtProperty *))
Applies given function to all the attribute properties recursively.
void updateAttribute(QtProperty *prop)
Sync function attribute value with the manager.
std::shared_ptr< Mantid::API::CompositeFunction > cfun() const
void plotRemoved()
Remove the reference to the function curve as it has been deleted.
QString updateStructureTooltip()
Update high-level structure tooltip and return it.
void init() override
overrides virtual init() which is called from IFunction::setHandler(...)
void calcBase()
Calculate m_base: the baseline level under the peak (if this function is a peak and auto background i...
void updateWorkspaces(const QStringList &oldWorkspaces)
void clearErrors()
Clear all parameter error values in the manager.
void updateParameters()
Sync all parameter values with the manager.
QMap< QString, std::pair< QtProperty *, QtProperty * > > m_constraints
QMap< QString, QtProperty * > m_ties
QtProperty * getParameterProperty(const QString &parName) const
void updateAttributes()
Sync all parameter values with the manager.
std::shared_ptr< Mantid::API::CompositeFunction > m_parent
Visitor setting new attribute value.
void apply(const std::string &str) const override
Set string property.
SetAttributeProperty(FitPropertyBrowser *browser, QtProperty *prop)
void apply(const bool &b) const override
Set bool property.
void apply(const double &d) const override
Set double property.
void apply(const std::vector< double > &) const override
Set vector property.
void apply(const int &i) const override
Set int property.
Visitor setting new attribute value.
void apply(double &d) const override
Create double property.
SetAttribute(FitPropertyBrowser *browser, QtProperty *prop, Mantid::Kernel::IValidator_sptr validator=Mantid::Kernel::IValidator_sptr())
void apply(std::string &str) const override
Create string property.
void apply(std::vector< double > &v) const override
Create vector property.
void apply(bool &b) const override
Create bool property.
void apply(int &i) const override
Create int property.
A composite function is a function containing other functions.
ParameterTie * getTie(size_t i) const override
Get the tie of i-th parameter.
ParameterStatus getParameterStatus(size_t i) const override
Get status of parameter.
Implements FunctionDomain1D with its own storage in form of a std::vector.
Classes inherited from FunctionHandler will handle the function.
Definition IFunction.h:754
IFunction_sptr function() const
Return the handled function.
Definition IFunction.h:766
IFunction_sptr m_fun
pointer to the handled function
Definition IFunction.h:769
A class to store values calculated by a function.
An interface to a background function.
An interface to a constraint.
Definition IConstraint.h:26
virtual std::string asString() const =0
Return the string that can be used in this->initialize() to recreate this constraint.
virtual void setHeight(const double h)=0
Sets the parameters such that height == h.
virtual void setCentre(const double c)=0
Sets the parameters such that centre == c.
Mantid::Kernel::IValidator_sptr m_validator
Validator against which to evaluate attribute value to set.
Definition IFunction.h:238
void evaluateValidator(T1 &inputData) const
Evaluates the validator associated with attribute this visitor is to visit.
Definition IFunction.h:231
Attribute is a non-fitting parameter.
Definition IFunction.h:285
int asInt() const
Returns int value if attribute is a int, throws exception otherwise.
Kernel::IValidator_sptr getValidator()
Return a clone of the attribute validator;.
Definition IFunction.h:320
std::string asString() const
Returns string value if attribute is a string, throws exception otherwise.
T apply(AttributeVisitor< T > &v)
Apply an attribute visitor.
Definition IFunction.h:303
void fromString(const std::string &str)
Set value from a string.
double asDouble() const
Returns double value if attribute is a double, throws exception otherwise.
std::string type() const
Returns type of the attribute.
Const version of AttributeVisitor.
Definition IFunction.h:244
Mantid::Kernel::IValidator_sptr m_validator
Validator against which to evaluate attribute value to set.
Definition IFunction.h:279
This is an interface to a fitting function - a semi-abstarct class.
Definition IFunction.h:166
An interface to a peak function, which extend the interface of IFunctionWithLocation by adding method...
virtual void setFwhm(const double w)=0
Sets the parameters such that FWHM = w.
ListValidator is a validator that requires the value of a property to be one of a defined list of pos...
std::shared_ptr< IAlgorithm > IAlgorithm_sptr
shared pointer to Mantid::API::IAlgorithm
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
std::shared_ptr< const CompositeFunction > CompositeFunction_const_sptr
shared pointer to the composite function base class (const version)
std::shared_ptr< IFunction > IFunction_sptr
shared pointer to the function base class
Definition IFunction.h:743
std::shared_ptr< const IFunction > IFunction_const_sptr
shared pointer to the function base class (const version)
Definition IFunction.h:745
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::shared_ptr< CompositeFunction > CompositeFunction_sptr
shared pointer to the composite function base class
std::shared_ptr< IValidator > IValidator_sptr
A shared_ptr to an IValidator.
Definition IValidator.h:26
Helper class which provides the Collimation Length for SANS instruments.
STL namespace.
Simple Exception Struct to differentiate validation error from other exceptions.
Definition IFunction.h:171