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 *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 foreach (QtBrowserItem *item, itList) {
72 if (item->property() == fnProp) {
73 m_item = item;
74 break;
75 }
76 }
77 if (m_item == nullptr)
78 throw std::runtime_error("Browser item not found");
79
80 if (!m_cf) {
81 m_browser->m_browser->setExpanded(m_item, false);
82 }
83 } else {
84 m_item->property()->setPropertyName(functionName());
85 }
86
87 QtProperty *fnProp = m_item->property();
88
89 // create Type property
90 if (!m_type) {
91 m_type = m_browser->m_enumManager->addProperty("Type");
92
93 fnProp->addSubProperty(m_type);
94 if (m_parent) {
96
97 } else {
98 QStringList functionNames;
99 functionNames << "CompositeFunction"; // << "MultiBG";
100 m_browser->m_enumManager->setEnumNames(m_type, functionNames);
101 }
102 }
103 int itype = m_browser->m_enumManager->enumNames(m_type).indexOf(QString::fromStdString(m_fun->name()));
104 m_browser->m_enumManager->setValue(m_type, itype);
105 // create worspace and workspace index properties if parent is a MultiBG
107
108 // create attribute properties
110
111 // create parameter properties
113
114 // set handlers for the child functions
115 if (m_cf && m_cf->nFunctions() > 0) {
116 for (size_t i = 0; i < m_cf->nFunctions(); i++) {
117 Mantid::API::IFunction_sptr f = std::dynamic_pointer_cast<Mantid::API::IFunction>(m_cf->getFunction(i));
118 if (!f) {
119 throw std::runtime_error("IFunction expected but func function of another type");
120 }
121 auto h = std::make_unique<PropertyHandler>(f, m_cf, m_browser);
122 f->setHandler(std::move(h));
123 }
124 initTies(); // populate ties after all child functions handlers have been inititiated (post setHandler)
125 }
126
128}
129
135public:
137 Mantid::Kernel::IValidator_sptr validator = nullptr)
138 : m_browser(browser), m_handler(handler), m_name(std::move(name)) {
139 m_validator = validator;
140 }
141
142protected:
144 QtProperty *apply(const std::string &str) const override {
145 QtProperty *prop;
146
147 // if validator is string list validator, create string list property
148 if (dynamic_cast<Mantid::Kernel::StringListValidator *>(m_validator.get()) != nullptr) {
149 prop = m_browser->addStringListProperty(m_name, m_validator->allowedValues());
150 } else {
152 }
153
154 m_browser->setStringPropertyValue(prop, QString::fromStdString(str));
155 return prop;
156 }
158 QtProperty *apply(const double &d) const override {
159 QtProperty *prop = m_browser->addDoubleProperty(m_name);
160 m_browser->m_doubleManager->setValue(prop, d);
161 return prop;
162 }
164 QtProperty *apply(const int &i) const override {
165 QtProperty *prop = m_browser->m_intManager->addProperty(m_name);
166 m_browser->m_intManager->setValue(prop, i);
167 return prop;
168 }
170 QtProperty *apply(const bool &b) const override {
171 QtProperty *prop = m_browser->m_boolManager->addProperty(m_name);
172 m_browser->m_boolManager->setValue(prop, b);
173 return prop;
174 }
176 QtProperty *apply(const std::vector<double> &b) const override {
177 // throw std::runtime_error("Vector attribute property not implememted.");
178 QtProperty *prop = m_browser->m_vectorManager->addProperty(m_name);
179 m_browser->m_vectorSizeManager->blockSignals(true);
180 QtProperty *sizeProp = m_browser->m_vectorSizeManager->addProperty("Size");
181 m_browser->m_vectorSizeManager->setValue(sizeProp, static_cast<int>(b.size()));
182 prop->addSubProperty(sizeProp);
183 m_handler->m_vectorSizes << sizeProp;
184 // sizeProp->setEnabled(false);
185 m_browser->m_vectorSizeManager->blockSignals(false);
186 m_browser->m_vectorDoubleManager->blockSignals(true);
187 QString dpName = "value[%1]";
188 for (size_t i = 0; i < b.size(); ++i) {
189 QtProperty *dprop = m_browser->addDoubleProperty(dpName.arg(i), m_browser->m_vectorDoubleManager);
190 m_browser->m_vectorDoubleManager->setValue(dprop, b[i]);
191 prop->addSubProperty(dprop);
192 m_handler->m_vectorMembers << dprop;
193 }
194 m_browser->m_vectorDoubleManager->blockSignals(false);
195 return prop;
196 }
197
198private:
201 QString m_name;
202};
203
208 for (size_t iparam = 0; iparam < m_cf->nParams(); iparam++) {
209 Mantid::API::ParameterTie *tie = m_cf->getTie(iparam);
210 if (tie) {
211 // get function index from prefix (second element of pair below)
212 const auto nameIndex_pair = m_cf->parseName(m_cf->parameterName(iparam));
214 std::dynamic_pointer_cast<Mantid::API::IFunction>(m_cf->getFunction(nameIndex_pair.second));
215 auto *h = findHandler(f);
216 h->addTie(QString::fromStdString(tie->asString()));
217 }
218 }
219}
220
225 std::vector<std::string> attNames = function()->getAttributeNames();
226 for (auto &attribute : m_attributes) {
227 m_item->property()->removeSubProperty(attribute);
228 }
229 m_attributes.clear();
230 m_vectorMembers.clear();
231 for (const auto &attName : attNames) {
232 if (variableIsPrefixed(attName))
233 continue;
234 QString aName = QString::fromStdString(attName);
235 Mantid::API::IFunction::Attribute att = function()->getAttribute(attName);
237 QtProperty *prop = att.apply(tmp);
238 m_item->property()->addSubProperty(prop);
239 m_attributes << prop;
240 }
241}
242
244 for (auto &parameter : m_parameters) {
245 m_item->property()->removeSubProperty(parameter);
246 }
247 m_parameters.clear();
248 for (size_t i = 0; i < function()->nParams(); i++) {
249 QString parName = QString::fromStdString(function()->parameterName(i));
250 if (parName.contains('.'))
251 continue;
252 QtProperty *prop = m_browser->addDoubleProperty(parName, m_browser->m_parameterManager);
253
254 m_browser->m_parameterManager->setDescription(prop, function()->parameterDescription(i));
255 m_browser->m_parameterManager->setValue(prop, function()->getParameter(i));
256
257 m_item->property()->addSubProperty(prop);
258 m_parameters << prop;
259 if (m_fun->isFixed(i)) {
260 QtProperty *tieProp = m_browser->m_stringManager->addProperty("Tie");
261 m_browser->m_stringManager->setValue(tieProp, QString::number(m_fun->getParameter(i)));
262 prop->addSubProperty(tieProp);
263 m_ties[parName] = tieProp;
264 }
265 // add constraint properties
266 Mantid::API::IConstraint *c = m_fun->getConstraint(i);
267 if (c) {
268 QStringList qc = QString::fromStdString(c->asString()).split("<");
269 bool lo = false;
270 bool up = false;
271 double loBound = 0, upBound = 0;
272 if (qc.size() == 2) {
273 if (qc[0].contains(parName)) {
274 up = true;
275 upBound = qc[1].toDouble();
276 } else {
277 lo = true;
278 loBound = qc[0].toDouble();
279 }
280 } else if (qc.size() == 3) {
281 lo = up = true;
282 loBound = qc[0].toDouble();
283 upBound = qc[2].toDouble();
284 } else {
285 continue;
286 }
287 QtProperty *loProp = nullptr;
288 QtProperty *upProp = nullptr;
289 if (lo) {
290 loProp = m_browser->addDoubleProperty("LowerBound");
291 m_browser->m_doubleManager->setValue(loProp, loBound);
292 prop->addSubProperty(loProp);
293 }
294 if (up) {
295 upProp = m_browser->addDoubleProperty("UpperBound");
296 m_browser->m_doubleManager->setValue(upProp, upBound);
297 prop->addSubProperty(upProp);
298 }
299 m_constraints.insert(parName, std::pair<QtProperty *, QtProperty *>(loProp, upProp));
300 }
301 }
302}
303
305 if (m_parent && m_parent->name() == "MultiBG") {
306 // m_workspace = m_browser->m_enumManager->addProperty("Workspace");
307 // QtProperty* fnProp = m_item->property();
308 // fnProp->addSubProperty(m_workspace);
309 // m_workspaceIndex = m_browser->m_intManager->addProperty("Workspace
310 // Index");
311 // if (! m_browser->m_workspaceNames.isEmpty() )
312 //{
313 // QStringList names("All");
314 // foreach(QString name,m_browser->m_workspaceNames)
315 // {
316 // names.append(name);
317 // }
318 // m_browser->m_enumManager->setEnumNames(m_workspace, names);
319 // int iWorkspace = 0;
320 // int iWorkspaceIndex = 0;
321 // if (ifun()->getWorkspace())
322 // {
323 // Mantid::API::IFunctionMW* ifmw =
324 // dynamic_cast<Mantid::API::IFunctionMW*>(ifun());
325 // if (ifmw)
326 // {
327 // std::string wsName = ifmw->getMatrixWorkspace()->getName();
328 // iWorkspace = names.indexOf(QString::fromStdString(wsName));
329 // if (iWorkspace >= 0)
330 // {
331 // iWorkspaceIndex = static_cast<int>(ifmw->getWorkspaceIndex());
332 // fnProp->addSubProperty(m_workspaceIndex);
333 // }
334 // else
335 // {
336 // iWorkspace = 0;
337 // }
338 // }
339 // }
340 // m_browser->m_enumManager->setValue(m_workspace,iWorkspace);
341 // m_browser->m_intManager->setValue(m_workspaceIndex,iWorkspaceIndex);
342 //}
343 } else {
344 m_workspace = m_workspaceIndex = nullptr;
345 }
346}
347
354 if (!m_cf)
355 return nullptr;
358 // Create new function
359 if (fnName.find("=") == std::string::npos) { // either from name
360 f = Mantid::API::FunctionFactory::Instance().createFunction(fnName);
361 } else { // of from full initialization expression
362 f = Mantid::API::FunctionFactory::Instance().createInitialized(fnName);
363 }
364
365 // turn of the change slots (doubleChanged() etc) to avoid infinite loop
367
368 // Check if it's a peak and set its width
369 std::shared_ptr<Mantid::API::IPeakFunction> pf = std::dynamic_pointer_cast<Mantid::API::IPeakFunction>(f);
370 if (pf) {
371 if (!m_browser->workspaceName().empty() && m_browser->workspaceIndex() >= 0 && pf->centre() == 0.) {
372 pf->setCentre((m_browser->startX() + m_browser->endX()) / 2);
373 }
374 }
375
377
378 try {
379 ws = std::dynamic_pointer_cast<Mantid::API::MatrixWorkspace>(
381 } catch (...) {
382 }
383
384 size_t wi = m_browser->workspaceIndex();
385
386 // if it's a LinearBackground estimate its A0 and A1 parameters
387 // from data values at the ends of the fitting interval
388 if (f->name() == "LinearBackground" && !m_browser->workspaceName().empty()) {
389 if (ws && wi < ws->getNumberHistograms()) {
390 const auto &X = ws->x(wi);
391 size_t istart = 0, iend = 0;
392 for (size_t i = 0; i < X.size() - 1; ++i) {
393 double x = X[i];
394 if (x < m_browser->startX())
395 istart = i;
396 if (x > m_browser->endX()) {
397 iend = i;
398 if (iend > 0)
399 iend--;
400 break;
401 }
402 }
403 if (iend > istart) {
404 const auto &Y = ws->y(wi);
405 double p0 = Y[istart];
406 double p1 = Y[iend];
407 double A1 = (p1 - p0) / (X[iend] - X[istart]);
408 double A0 = p0 - A1 * X[istart];
409 f->setParameter("A0", A0);
410 f->setParameter("A1", A1);
411 }
412 }
413 }
414 if (ws) {
416 }
417
418 size_t nFunctions = m_cf->nFunctions() + 1;
419 m_cf->addFunction(f);
420 m_browser->compositeFunction()->checkFunction();
421
422 if (m_cf->nFunctions() != nFunctions) { // this may happen
423 m_browser->reset();
424 return nullptr;
425 }
426
427 f->setHandler(std::make_unique<PropertyHandler>(f, m_cf, m_browser));
428 auto h = static_cast<PropertyHandler *>(f->getHandler());
429 h->setAttribute("StartX", m_browser->startX());
430 h->setAttribute("EndX", m_browser->endX());
431
432 // enable the change slots
435 if (pf) {
436 m_browser->setDefaultPeakType(f->name());
437 } else {
439 }
440 m_browser->setFocus();
441 auto return_ptr = static_cast<PropertyHandler *>(f->getHandler());
442 m_browser->setCurrentFunction(return_ptr);
443
444 return return_ptr;
445}
446
447// Removes handled function from its parent function and
448// properties from the browser
451 if (ph) {
452 if (this == m_browser->m_autoBackground) {
453 m_browser->m_autoBackground = nullptr;
454 }
455 ph->item()->property()->removeSubProperty(m_item->property());
457 for (int i = 0; i < static_cast<int>(cf->nFunctions()); i++) {
458 if (cf->getFunction(i) == function()) {
459 emit m_browser->removePlotSignal(this);
460 cf->removeFunction(i);
461 break;
462 }
463 }
464 ph->renameChildren();
465 m_browser->setFitEnabled(cf->nFunctions() > 0);
466 }
467}
468
471 // update tie properties, as the parameter names may change
472 QMap<QString, QtProperty *>::const_iterator it = m_ties.begin();
473 for (; it != m_ties.end(); ++it) {
474 QtProperty *prop = it.value();
475 Mantid::API::ParameterTie *tie = m_fun->getTie(m_fun->parameterIndex(it.key().toStdString()));
476 if (!tie)
477 continue;
478 QStringList qtie = QString::fromStdString(tie->asString()).split("=");
479 if (qtie.size() < 2)
480 continue;
481 m_browser->m_stringManager->setValue(prop, qtie[1]);
482 }
483 if (!m_cf)
484 return;
485 // rename children
486 for (size_t i = 0; i < m_cf->nFunctions(); i++) {
488 if (!h)
489 continue;
490 QtProperty *nameProp = h->item()->property();
491 nameProp->setPropertyName(h->functionName());
492 h->renameChildren();
493 }
495}
496
500 QString name = functionPrefix();
501 if (!name.isEmpty()) {
502 name += "-";
503 }
504 name += QString::fromStdString(function()->name());
505 return name;
506}
507
510 if (ph) {
511 int iFun = -1;
513 for (int i = 0; i < static_cast<int>(cf->nFunctions()); i++) {
514 if (cf->getFunction(i) == function()) {
515 iFun = i;
516 break;
517 }
518 }
519 QString pref = ph->functionPrefix();
520 if (!pref.isEmpty())
521 pref += ".";
522 return pref + "f" + QString::number(iFun);
523 }
524 return "";
525}
526
527// Return the parent handler
529 if (!m_parent)
530 return nullptr;
531 PropertyHandler *ph = static_cast<PropertyHandler *>(m_parent->getHandler());
532 return ph;
533}
534// Return the child's handler
536 if (!m_cf || i >= m_cf->nFunctions())
537 return nullptr;
538 PropertyHandler *ph = static_cast<PropertyHandler *>(m_cf->getFunction(i)->getHandler());
539 return ph;
540}
546 if (!m_cf)
548 if (item == m_item)
549 return m_cf;
550 for (size_t i = 0; i < m_cf->nFunctions(); i++) {
552 if (res != nullptr)
553 return res;
554 }
556}
562 if (item == m_item)
563 return function();
564 if (!m_cf)
566 for (size_t i = 0; i < m_cf->nFunctions(); i++) {
568 if (res != nullptr)
569 return res;
570 }
572}
573
575 if (prop == nullptr)
576 return nullptr;
577 if (prop == m_item->property())
578 return this;
579 if (prop == m_type)
580 return this;
581 if (prop == m_workspace)
582 return this;
583 if (prop == m_workspaceIndex)
584 return this;
585 if (m_attributes.contains(prop))
586 return this;
587 if (m_parameters.contains(prop))
588 return this;
589 if (m_vectorMembers.contains(prop))
590 return this;
591 if (m_vectorSizes.contains(prop))
592 return this;
593 if (!m_ties.key(prop, "").isEmpty())
594 return this;
595 QMap<QString, std::pair<QtProperty *, QtProperty *>>::iterator it = m_constraints.begin();
596 for (; it != m_constraints.end(); ++it) {
597 if (it.value().first == prop || it.value().second == prop) {
598 return this;
599 }
600 }
601 if (!m_cf)
602 return nullptr;
603 for (size_t i = 0; i < m_cf->nFunctions(); i++) {
605 if (h != nullptr)
606 return h;
607 }
608 return nullptr;
609}
610
612 if (fun == function())
613 return this;
614 if (m_cf) {
615 for (size_t i = 0; i < m_cf->nFunctions(); i++) {
617 if (h)
618 return h;
619 }
620 }
621 return nullptr;
622}
623
625 if (fun == function().get())
626 return this;
627 if (m_cf) {
628 for (size_t i = 0; i < m_cf->nFunctions(); i++) {
630 if (h)
631 return h;
632 }
633 }
634 return nullptr;
635}
636
642bool PropertyHandler::setParameter(QtProperty *prop) {
643 if (m_parameters.contains(prop)) {
644 std::string parName = prop->propertyName().toStdString();
645 double parValue = m_browser->m_parameterManager->value(prop);
646 m_fun->setParameter(parName, parValue);
649 return true;
650 }
651 if (m_cf) {
652 for (size_t i = 0; i < m_cf->nFunctions(); i++) {
653 bool res = getHandler(i)->setParameter(prop);
654 if (res)
655 return true;
656 }
657 }
658 return false;
659}
660
666public:
667 SetAttribute(FitPropertyBrowser *browser, QtProperty *prop,
669 : m_browser(browser), m_prop(prop) {
670 m_validator = validator;
671 }
672
673protected:
675 void apply(std::string &str) const override {
676 std::string propValue = m_browser->getStringPropertyValue(m_prop).toStdString();
677
678 evaluateValidator(propValue);
679 str = propValue;
680 }
682 void apply(double &d) const override {
683 double propValue = m_browser->m_doubleManager->value(m_prop);
684
685 evaluateValidator(propValue);
686 d = propValue;
687 }
689 void apply(int &i) const override {
690 int propValue = m_browser->m_intManager->value(m_prop);
691
692 evaluateValidator(propValue);
693 i = propValue;
694 }
696 void apply(bool &b) const override {
697 bool propValue = m_browser->m_boolManager->value(m_prop);
698
699 evaluateValidator(propValue);
700 b = propValue;
701 }
703 void apply(std::vector<double> &v) const override {
704 QList<QtProperty *> members = m_prop->subProperties();
705 if (members.size() < 1) {
706 v.clear();
707 return;
708 }
709
710 int newSize = m_browser->m_vectorSizeManager->value(members[0]);
711 int vectorSize = members.size() - 1;
712 if (vectorSize > newSize) {
713 vectorSize = newSize;
714 }
715
716 // populate new vector
717 std::vector<double> newVec(newSize);
718 for (int i = 1; i < newSize + 1; ++i) {
719 double newVal = (m_validator != Mantid::Kernel::IValidator_sptr())
720 ? m_browser->m_vectorDoubleManager->value(members[vectorSize])
721 : 0.0;
722 if (i < vectorSize + 1) {
723 newVec[i - 1] = m_browser->m_vectorDoubleManager->value(members[i]);
724 } else {
725 newVec[i - 1] = newVal;
726 }
727 }
728
730 evaluateValidator(newVec);
731 }
732
733 v.resize(newSize);
734 std::copy(cbegin(newVec), cend(newVec), begin(v));
735 }
736
737private:
739 QtProperty *m_prop;
740};
741
747public:
748 SetAttributeProperty(FitPropertyBrowser *browser, QtProperty *prop) : m_browser(browser), m_prop(prop) {}
749
750protected:
752 void apply(const std::string &str) const override {
754 m_browser->setStringPropertyValue(m_prop, QString::fromStdString(str));
756 }
758 void apply(const double &d) const override {
760 m_browser->m_doubleManager->setValue(m_prop, d);
762 }
764 void apply(const int &i) const override {
766 m_browser->m_intManager->setValue(m_prop, i);
768 }
770 void apply(const bool &b) const override {
772 m_browser->m_boolManager->setValue(m_prop, b);
774 }
776 void apply(const std::vector<double> & /*unused*/) const override {
777 // this method is supposed to be called when corresponding
778 // property value changes but it doesn't have a value because
779 // it's a group property
780 throw std::runtime_error("Vector attribute not implemented.");
781 }
782
783private:
785 QtProperty *m_prop;
786};
787
795bool PropertyHandler::setAttribute(QtProperty *prop, bool resetProperties) {
796 if (m_attributes.contains(prop)) {
797 QString attName = prop->propertyName();
798 try {
799 Mantid::API::IFunction::Attribute att = m_fun->getAttribute(attName.toStdString());
801 att.apply(tmp);
802 m_fun->setAttribute(attName.toStdString(), att);
803 m_browser->compositeFunction()->checkFunction();
804 if (resetProperties) {
807 }
808 if (this == m_browser->m_autoBackground) {
809 fit();
810 }
811 } catch (Mantid::API::IFunction::ValidationException &ve) { // catch attribute validation error
814
816 ve.what()); // rethrow validation exception so it can be recaught by Fit Property Browser.
817 } catch (std::exception &e) {
820 QMessageBox::critical(m_browser, "Mantid - Error", e.what());
821 return false;
822 }
823 return true;
824 }
825 if (m_cf) {
826 for (size_t i = 0; i < m_cf->nFunctions(); i++) {
827 bool res = getHandler(i)->setAttribute(prop, resetProperties);
828 if (res)
829 return true;
830 }
831 }
832 return false;
833}
834
840void PropertyHandler::setAttribute(QString const &attName, Mantid::API::IFunction::Attribute const &attValue) {
841 auto const attributeType = attValue.type();
842 if (attributeType == "int")
843 setAttribute(attName, attValue.asInt());
844 else if (attributeType == "double")
845 setAttribute(attName, attValue.asDouble());
846 else if (attributeType == "std::string")
847 setAttribute(attName, QString::fromStdString(attValue.asString()));
848}
849
855template <typename AttributeType>
856void PropertyHandler::setAttribute(QString const &attName, AttributeType const &attValue) {
857 if (m_fun->hasAttribute(attName.toStdString())) {
858 try {
859 m_fun->setAttribute(attName.toStdString(), Mantid::API::IFunction::Attribute(attValue));
860 m_browser->compositeFunction()->checkFunction();
861 foreach (QtProperty *prop, m_attributes) {
862 if (prop->propertyName() == attName) {
863 // re-insert the attribute and parameter properties as they may
864 // depend on the value of the attribute being set
867 }
868 }
869 } catch (...) {
870 }
871 }
872 if (cfun()) {
873 for (auto i = 0u; i < cfun()->nFunctions(); ++i) {
875 h->setAttribute(attName, attValue);
876 }
877 }
878}
879
885void PropertyHandler::setAttribute(const QString &attName, const QString &attValue) {
886 const std::string name = attName.toStdString();
887 if (m_fun->hasAttribute(name)) {
888 Mantid::API::IFunction::Attribute att = m_fun->getAttribute(name);
889 att.fromString(attValue.toStdString());
890 m_fun->setAttribute(name, att);
891 m_browser->compositeFunction()->checkFunction();
892 foreach (QtProperty *prop, m_attributes) {
893 if (prop->propertyName() == attName) {
895 att.apply(tmp);
896 }
897 }
898 // re-insert the attribute and parameter properties as they may
899 // depend on the value of the attribute being set
902 }
903}
904
910 foreach (QtProperty *att, m_attributes) {
911 QList<QtProperty *> subProps = att->subProperties();
912 if (subProps.contains(prop)) {
913 bool resetProperties = m_vectorSizes.contains(prop);
914 setAttribute(att, resetProperties);
915 return;
916 }
917 }
918}
919
926 for (auto attribute : m_attributes) {
927 (this->*(func))(attribute);
928 }
929
930 if (m_cf)
931 for (std::size_t i = 0u; i < m_cf->nFunctions(); ++i)
933}
934
940
944void PropertyHandler::updateAttribute(QtProperty *attribute) {
945 if (m_attributes.contains(attribute)) {
946 auto const attributeValue = function()->getAttribute(attribute->propertyName().toStdString());
947 setAttribute(attribute->propertyName(), attributeValue);
948 }
949}
950
957 for (auto prop : m_parameters) {
958 (this->*(func))(prop);
959 }
960
961 if (m_cf) {
962 for (size_t i = 0; i < m_cf->nFunctions(); i++) {
964 }
965 }
966}
967
969
971
973
977void PropertyHandler::updateParameter(QtProperty *prop) {
978 double const parValue = function()->getParameter(prop->propertyName().toStdString());
979 m_browser->m_parameterManager->setValue(prop, parValue);
980}
981
985void PropertyHandler::updateError(QtProperty *prop) {
986 size_t index = function()->parameterIndex(prop->propertyName().toStdString());
987 double error = function()->getError(index);
988 m_browser->m_parameterManager->setError(prop, error);
989}
990
994void PropertyHandler::clearError(QtProperty *prop) { m_browser->m_parameterManager->clearError(prop); }
995
1001 if (prop == m_type) {
1002 // Create new function
1003 int i = m_browser->m_enumManager->value(prop);
1004 QStringList functionNames = m_browser->m_enumManager->enumNames(prop);
1005 const QString &fnName = functionNames[i];
1007 try {
1008 f = Mantid::API::FunctionFactory::Instance().createFunction(fnName.toStdString());
1009
1010 } catch (std::exception &e) {
1011 QMessageBox::critical(nullptr, "Mantid - Error", "Cannot create function " + fnName + "\n" + e.what());
1013 }
1014
1015 // turn of the change slots (doubleChanged() etc) to avoid infinite loop
1017
1018 // Check if it's a peak and set its width
1019 Mantid::API::IPeakFunction *pf = dynamic_cast<Mantid::API::IPeakFunction *>(f.get());
1020 if (pf) {
1021 if (!m_pf) {
1022 if (!m_browser->workspaceName().empty() && m_browser->workspaceIndex() >= 0) {
1023 pf->setCentre((m_browser->startX() + m_browser->endX()) / 2);
1024 }
1025
1026 } else {
1027 pf->setCentre(m_pf->centre());
1028 pf->setHeight(m_pf->height());
1029 pf->setFwhm(m_pf->fwhm());
1030 }
1031 }
1032
1033 if (pf) {
1034 m_browser->setDefaultPeakType(fnName.toStdString());
1035
1036 } else {
1037 m_browser->setDefaultBackgroundType(fnName.toStdString());
1038 }
1039
1040 QList<QtProperty *> subs = m_item->property()->subProperties();
1041 foreach (QtProperty *sub, subs) { m_item->property()->removeSubProperty(sub); }
1042
1044
1045 emit m_browser->removePlotSignal(this);
1046
1048 std::unique_ptr<PropertyHandler> h = std::make_unique<PropertyHandler>(f, m_parent, m_browser, m_item);
1049 if (this == m_browser->m_autoBackground) {
1050 if (dynamic_cast<Mantid::API::IBackgroundFunction *>(f.get())) {
1051 m_browser->m_autoBackground = h.get();
1052 h->fit();
1053
1054 } else {
1055 m_browser->m_autoBackground = nullptr;
1056 }
1057 }
1058 if (m_parent) {
1059 m_parent->replaceFunctionPtr(f_old, f);
1060 }
1061 // calculate the baseline
1062 if (h->pfun()) {
1063 h->setCentre(h->centre()); // this sets m_ci
1064 h->calcBase();
1065 }
1066 f->setHandler(std::move(h));
1067 // at this point this handler does not exist any more. only return is
1068 // possible
1069 return f;
1070
1071 } else if (m_cf) {
1072 for (size_t i = 0; i < m_cf->nFunctions(); i++) {
1074 if (f)
1075 return f;
1076 }
1077 }
1079}
1080
1081bool PropertyHandler::isParameter(QtProperty *prop) { return m_parameters.contains(prop); }
1082
1083QtProperty *PropertyHandler::getParameterProperty(const QString &parName) const {
1084 foreach (QtProperty *parProp, m_parameters) {
1085 if (parProp->propertyName() == parName) {
1086 return parProp;
1087 }
1088 }
1089 return nullptr;
1090}
1091
1092QtProperty *PropertyHandler::getParameterProperty(QtProperty *prop) const {
1093 foreach (QtProperty *parProp, m_parameters) {
1094 QList<QtProperty *> subs = parProp->subProperties();
1095 if (subs.contains(prop)) {
1096 return parProp;
1097 }
1098 }
1099 return nullptr;
1100}
1101
1102void PropertyHandler::addTie(const QString &tieStr) {
1103 QStringList parts = tieStr.split("=");
1104 if (parts.size() != 2)
1105 return;
1106 std::string name = parts[0].trimmed().toStdString();
1107 std::string expr = parts[1].trimmed().toStdString();
1108 try {
1109 auto &cfun = *m_browser->compositeFunction();
1110 cfun.tie(name, expr);
1111 const bool recursive = true;
1112 QString parName = QString::fromStdString(cfun.parameterLocalName(cfun.parameterIndex(name), recursive));
1113 foreach (QtProperty *parProp, m_parameters) {
1114 if (parProp->propertyName() == parName) {
1116 QtProperty *tieProp = m_ties[parName];
1117 if (!tieProp) {
1118 tieProp = m_browser->m_stringManager->addProperty("Tie");
1119 m_ties[parName] = tieProp;
1120 }
1121 m_browser->m_stringManager->setValue(tieProp, QString::fromStdString(expr));
1123 parProp->addSubProperty(tieProp);
1124 return;
1125 }
1126 }
1127 } catch (...) {
1128 }
1129 QMessageBox::critical(m_browser, "Mantid - Error", "Failed to set tie: " + tieStr);
1130}
1131
1132void PropertyHandler::fix(const QString &parName) {
1133 QtProperty *parProp = getParameterProperty(parName);
1134 if (!parProp)
1135 return;
1136 QString parValue = QString::number(m_browser->m_parameterManager->value(parProp));
1137 try {
1138 m_fun->tie(parName.toStdString(), parValue.toStdString());
1140 QtProperty *tieProp = m_ties[parName];
1141 if (!tieProp) {
1142 tieProp = m_browser->m_stringManager->addProperty("Tie");
1143 m_ties[parName] = tieProp;
1144 }
1145 m_browser->m_stringManager->setValue(tieProp, parValue);
1147 parProp->addSubProperty(tieProp);
1148 tieProp->setEnabled(false);
1149 } catch (...) {
1150 }
1151}
1152
1159void PropertyHandler::removeTie(QtProperty *prop, const std::string &globalName) {
1160 QString parName = m_ties.key(prop, "");
1161 if (parName.isEmpty())
1162 return;
1163
1164 QtProperty *parProp = getParameterProperty(parName);
1165 if (parProp) {
1167 auto &compositeFunction = *m_browser->compositeFunction();
1168 auto index = compositeFunction.parameterIndex(globalName);
1169 compositeFunction.removeTie(index);
1170 parProp->removeSubProperty(prop);
1171 m_ties.remove(QString::fromStdString(globalName));
1172 m_ties.remove(parName);
1174 parProp->setEnabled(true);
1175 }
1176}
1181void PropertyHandler::removeTie(QtProperty *prop) {
1182 QString parName = m_ties.key(prop, "");
1183 if (parName.isEmpty())
1184 return;
1185
1186 QtProperty *parProp = getParameterProperty(parName);
1187 if (parProp != nullptr) {
1189 m_fun->removeTie(parName.toStdString());
1190 parProp->removeSubProperty(prop);
1191 m_ties.remove(parName);
1193 parProp->setEnabled(true);
1194 }
1195}
1196
1201void PropertyHandler::removeTie(const QString &parName) {
1202 QtProperty *prop = m_ties[parName];
1203 if (prop)
1204 removeTie(prop);
1205}
1206
1212 double fwhm = 0.;
1213 auto ws = std::dynamic_pointer_cast<const Mantid::API::MatrixWorkspace>(m_browser->getWorkspace());
1214 if (ws) {
1215 size_t wi = m_browser->workspaceIndex();
1216 const auto &X = ws->x(wi);
1217 const auto &Y = ws->y(wi);
1218 size_t n = Y.size() - 1;
1219 if (m_ci < 0 || m_ci > static_cast<int>(n)) {
1220 fwhm = 0.;
1221 } else {
1222 double halfHeight = ((Y[m_ci] - m_base) / 2.) + m_base;
1223 // walk to the right
1224 size_t rightHwhmIndex = m_ci;
1225 while (rightHwhmIndex < n) {
1226 if (Y[rightHwhmIndex++] <= halfHeight) {
1227 break;
1228 }
1229 }
1230
1231 // walk to the left
1232 size_t leftHwhmIndex = m_ci;
1233 while (leftHwhmIndex > 0) {
1234 if (Y[leftHwhmIndex--] <= halfHeight) {
1235 break;
1236 }
1237 }
1238
1239 fwhm = fabs(X[rightHwhmIndex] - X[leftHwhmIndex]);
1240
1241 // apply a maximum limitation if larger than the fitting region
1242 double fitRange = m_browser->endX() - m_browser->startX();
1243 if (fwhm > fitRange) {
1244 // set to 10% of fitting region
1245 fwhm = fitRange * 0.1;
1246 }
1247 }
1248 }
1249 return fwhm;
1250}
1257 return;
1258
1259 auto ws = std::dynamic_pointer_cast<const Mantid::API::MatrixWorkspace>(m_browser->getWorkspace());
1260 if (ws) {
1261 size_t wi = m_browser->workspaceIndex();
1262 const auto &X = ws->x(wi);
1263 const auto &Y = ws->y(wi);
1264 int n = static_cast<int>(Y.size()) - 1;
1265 if (m_ci < 0 || m_ci > n || !m_browser->m_autoBackground) {
1266 m_base = 0.;
1267 } else {
1270 m_browser->m_autoBackground->function()->function(x, y);
1271 m_base = y[0];
1272 }
1273 } else {
1274 m_base = 0.;
1275 }
1276}
1277
1285 return;
1286 if (!m_cf)
1287 return;
1288 for (size_t i = 0; i < m_cf->nFunctions(); ++i) {
1290 if (h->pfun()) {
1291 h->calcBase();
1292 } else if (h->cfun()) {
1293 h->calcBaseAll();
1294 }
1295 }
1296}
1297
1301void PropertyHandler::setHeight(const double &h) {
1302 if (m_pf) {
1303 m_pf->setHeight(h - m_base);
1304 }
1305}
1306
1311void PropertyHandler::setCentre(const double &c) {
1312 if (m_pf) {
1313 m_pf->setCentre(c);
1314
1315 // find m_ci: x-index of the peakcentre
1316 auto ws = std::dynamic_pointer_cast<const Mantid::API::MatrixWorkspace>(m_browser->getWorkspace());
1317 if (ws) {
1318 size_t wi = m_browser->workspaceIndex();
1319 const auto &X = ws->x(wi);
1320 int n = static_cast<int>(X.size()) - 2;
1321 if (m_ci < 0)
1322 m_ci = 0;
1323 if (m_ci > n)
1324 m_ci = n;
1325 double x = X[m_ci];
1326 if (x < c) {
1327 for (; m_ci <= n; ++m_ci) {
1328 x = X[m_ci];
1329 if (x > c)
1330 break;
1331 }
1332 } else {
1333 for (; m_ci >= 0; --m_ci) {
1334 x = X[m_ci];
1335 if (x < c)
1336 break;
1337 }
1338 }
1339 }
1340 }
1341}
1342
1343void PropertyHandler::setFwhm(const double &w) {
1344 if (m_pf) {
1345 m_pf->setFwhm(w);
1346 }
1347}
1348
1350 if (m_pf) {
1351 return m_pf->height();
1352 }
1353 return 0;
1354}
1355
1357 if (m_pf) {
1358 return m_pf->centre();
1359 }
1360 return (m_browser->endX() + m_browser->startX()) / 2;
1361}
1362
1364 if (m_pf) {
1365 return m_pf->fwhm();
1366 }
1367 return 0;
1368}
1369
1371 if (m_pf) {
1372 return m_pf->getWidthParameterName();
1373 }
1374 return "";
1375}
1376
1378 if (m_pf) {
1379 return m_pf->getCentreParameterName();
1380 }
1381 return "";
1382}
1383
1384bool PropertyHandler::isParameterExplicitlySet(const std::string &param) const {
1385 if (m_pf) {
1386 return m_pf->isExplicitlySet(m_pf->parameterIndex(param));
1387 }
1388 return false;
1389}
1390
1394void PropertyHandler::addConstraint(QtProperty *parProp, bool lo, bool up, double loBound, double upBound) {
1395 QMap<QString, std::pair<QtProperty *, QtProperty *>>::iterator old = m_constraints.find(parProp->propertyName());
1396
1397 bool hasLo = false;
1398 bool hasUp = false;
1399
1400 if (old != m_constraints.end()) {
1401 hasLo = old.value().first != NULL;
1402 hasUp = old.value().second != NULL;
1403 if (hasLo && !lo) {
1404 lo = true;
1405 loBound = m_browser->m_doubleManager->value(old.value().first);
1406 }
1407 if (hasUp && !up) {
1408 up = true;
1409 upBound = m_browser->m_doubleManager->value(old.value().second);
1410 }
1411 }
1412
1414 std::pair<QtProperty *, QtProperty *> cnew; //(nullptr,nullptr); - Can't do this in constructor in C++11
1415 // Don't know if these 2 lines are necessary, but this code is hard to
1416 // understand - it could really use some comments!
1417 cnew.first = NULL;
1418 cnew.second = NULL;
1419 std::ostringstream ostr;
1420 if (lo) {
1421 ostr << loBound << "<";
1422 if (!hasLo) {
1423 cnew.first = m_browser->addDoubleProperty("LowerBound");
1424 parProp->addSubProperty(cnew.first);
1425 } else {
1426 cnew.first = old.value().first;
1427 }
1428 m_browser->m_doubleManager->setValue(cnew.first, loBound);
1429 }
1430 ostr << parProp->propertyName().toStdString();
1431 if (up) {
1432 ostr << "<" << upBound;
1433 if (!hasUp) {
1434 cnew.second = m_browser->addDoubleProperty("UpperBound");
1435 parProp->addSubProperty(cnew.second);
1436 } else {
1437 cnew.second = old.value().second;
1438 }
1439 m_browser->m_doubleManager->setValue(cnew.second, upBound);
1440 }
1441
1442 if (old != m_constraints.end()) {
1443 m_constraints.erase(old);
1444 }
1445
1446 m_constraints.insert(parProp->propertyName(), cnew);
1447
1448 auto c = std::unique_ptr<Mantid::API::IConstraint>(
1449 Mantid::API::ConstraintFactory::Instance().createInitialized(m_fun.get(), ostr.str()));
1450 m_fun->addConstraint(std::move(c));
1452}
1453
1454void PropertyHandler::removeConstraint(QtProperty *parProp) {
1455 QMap<QString, std::pair<QtProperty *, QtProperty *>>::iterator it = m_constraints.find(parProp->propertyName());
1456
1457 if (it != m_constraints.end()) {
1458 if (it.value().first) {
1459 parProp->removeSubProperty(it.value().first);
1460 }
1461 if (it.value().second) {
1462 parProp->removeSubProperty(it.value().second);
1463 }
1464 m_fun->removeConstraint(parProp->propertyName().toStdString());
1465 m_constraints.erase(it);
1466 }
1467}
1468
1474 if (m_pf) {
1475 res << this;
1476 }
1477 if (m_cf) {
1478 for (size_t i = 0; i < m_cf->nFunctions(); ++i) {
1480 if (!h)
1481 continue;
1482 if (h->pfun()) {
1483 res << h;
1484 } else if (h->cfun()) {
1485 res << h->getPeakList();
1486 }
1487 }
1488 }
1489 return res;
1490}
1491
1496
1509 QString newTooltip;
1510
1511 if (m_cf && (m_cf->name() == "CompositeFunction" || m_cf->name() == "ProductFunction")) {
1512 QStringList childrenTooltips;
1513
1514 // Update tooltips for all the children first, and use them to build this
1515 // tooltip
1516 for (size_t i = 0; i < m_cf->nFunctions(); ++i) {
1517 if (auto childHandler = getHandler(i)) {
1518 childrenTooltips << childHandler->updateStructureTooltip();
1519 } else {
1520 throw std::runtime_error("Error while building structure tooltip: no handler for child");
1521 }
1522 }
1523
1524 if (childrenTooltips.empty()) {
1525 newTooltip = QString::fromStdString("Empty " + m_cf->name());
1526 } else {
1527 QChar op('+');
1528
1529 if (m_cf->name() == "ProductFunction") {
1530 op = '*';
1531 }
1532
1533 newTooltip = QString("(%1)").arg(childrenTooltips.join(' ' + op + ' '));
1534 }
1535 } else {
1536 newTooltip = QString::fromStdString(function()->name());
1537 }
1538
1539 m_item->property()->setToolTip(newTooltip);
1540 return newTooltip;
1541}
1542
1547 emit m_browser->removePlotSignal(this);
1548 if (m_cf) {
1549 for (size_t i = 0; i < m_cf->nFunctions(); ++i) {
1551 }
1552 }
1553}
1554
1556 try {
1557 if (m_browser->workspaceName().empty())
1558 return;
1559
1561 alg->initialize();
1562 alg->setProperty("Function", m_fun);
1563 alg->setPropertyValue("InputWorkspace", m_browser->workspaceName());
1564 alg->setProperty("WorkspaceIndex", m_browser->workspaceIndex());
1565 alg->setProperty("StartX", m_browser->startX());
1566 alg->setProperty("EndX", m_browser->endX());
1567 alg->execute();
1568 Mantid::API::IFunction_sptr f = alg->getProperty("Function");
1569 if (f != m_fun) { // this should never happen, just in case...
1570 for (size_t i = 0; i < f->nParams(); ++i) {
1571 m_fun->setParameter(i, f->getParameter(i));
1572 }
1573 }
1576 } catch (...) {
1577 }
1578}
1579
1580void PropertyHandler::updateWorkspaces(QStringList oldWorkspaces) {
1581 if (m_workspace) {
1582 int index = m_browser->m_enumManager->value(m_workspace) - 1;
1583 QString wsName;
1584 if (index >= 0 && index < oldWorkspaces.size()) {
1585 wsName = oldWorkspaces[index];
1586 }
1587 QStringList names("All");
1588 foreach (QString name, m_browser->m_workspaceNames) { names.append(name); }
1589 m_browser->m_enumManager->setEnumNames(m_workspace, names);
1590 if (m_browser->m_workspaceNames.contains(wsName)) {
1591 m_browser->m_enumManager->setValue(m_workspace, m_browser->m_workspaceNames.indexOf(wsName) + 1);
1592 }
1593 }
1594 if (cfun()) {
1595 for (size_t i = 0; i < cfun()->nFunctions(); ++i) {
1596 getHandler(i)->updateWorkspaces(oldWorkspaces);
1597 }
1598 }
1599}
1600
1602 if (m_workspace) {
1603 int index = m_browser->m_enumManager->value(m_workspace) - 1;
1604 if (index >= 0 && index < m_browser->m_workspaceNames.size()) {
1605 std::string wsName = m_browser->m_workspaceNames[index].toStdString();
1607 int wsIndex = m_browser->m_intManager->value(m_workspaceIndex);
1608 auto mws = std::dynamic_pointer_cast<Mantid::API::MatrixWorkspace>(ws);
1609 if (mws) {
1610 ifun()->setMatrixWorkspace(mws, size_t(wsIndex), m_browser->startX(), m_browser->endX());
1611 } else {
1612 ifun()->setWorkspace(ws);
1613 }
1614 m_item->property()->insertSubProperty(m_workspaceIndex, m_workspace);
1615 } else {
1616 ifun()->setWorkspace(Mantid::API::Workspace_sptr());
1617 m_item->property()->removeSubProperty(m_workspaceIndex);
1618 }
1619 } else {
1620 ifun()->setWorkspace(Mantid::API::Workspace_sptr());
1621 }
1622}
1623
1624} // namespace MantidQt::MantidWidgets
gsl_vector * tmp
double error
Definition: IndexPeaks.cpp:133
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
#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)
int workspaceIndex() const
Get workspace index.
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
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 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 updateWorkspaces(QStringList oldWorkspaces)
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.
Implements FunctionDomain1D with its own storage in form of a std::vector.
Classes inherited from FunctionHandler will handle the function.
Definition: IFunction.h:743
IFunction_sptr function() const
Return the handled function.
Definition: IFunction.h:755
IFunction_sptr m_fun
pointer to the handled function
Definition: IFunction.h:758
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:235
void evaluateValidator(T1 &inputData) const
Evaluates the validator associated with attribute this visitor is to visit.
Definition: IFunction.h:228
Attribute is a non-fitting parameter.
Definition: IFunction.h:282
int asInt() const
Returns int value if attribute is a int, throws exception otherwise.
Definition: IFunction.cpp:726
Kernel::IValidator_sptr getValidator()
Return a clone of the attribute validator;.
Definition: IFunction.h:317
std::string asString() const
Returns string value if attribute is a string, throws exception otherwise.
Definition: IFunction.cpp:660
T apply(AttributeVisitor< T > &v)
Apply an attribute visitor.
Definition: IFunction.h:300
void fromString(const std::string &str)
Set value from a string.
Definition: IFunction.cpp:971
double asDouble() const
Returns double value if attribute is a double, throws exception otherwise.
Definition: IFunction.cpp:739
std::string type() const
Returns type of the attribute.
Definition: IFunction.cpp:608
Const version of AttributeVisitor.
Definition: IFunction.h:241
Mantid::Kernel::IValidator_sptr m_validator
Validator against which to evaluate attribute value to set.
Definition: IFunction.h:276
This is an interface to a fitting function - a semi-abstarct class.
Definition: IFunction.h:163
An interface to a peak function, which extend the interface of IFunctionWithLocation by adding method...
Definition: IPeakFunction.h:51
virtual void setFwhm(const double w)=0
Sets the parameters such that FWHM = w.
Ties fitting parameters.
Definition: ParameterTie.h:35
virtual std::string asString(const IFunction *fun=nullptr) const
Return the string that can be used to recreate this tie.
ListValidator is a validator that requires the value of a property to be one of a defined list of pos...
Definition: ListValidator.h:29
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
std::shared_ptr< IAlgorithm > IAlgorithm_sptr
shared pointer to Mantid::API::IAlgorithm
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
Definition: Workspace_fwd.h:20
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:732
std::shared_ptr< const IFunction > IFunction_const_sptr
shared pointer to the function base class (const version)
Definition: IFunction.h:734
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:168