Mantid
Loading...
Searching...
No Matches
SetValueWhenProperty.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2021 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 +
8
13
14#include <boost/lexical_cast.hpp>
15#include <cassert>
16#include <exception>
17#include <stdexcept>
18
19using namespace Mantid::Kernel;
20
21namespace {
22
23Mantid::Kernel::Logger g_log("SetValueWhenProperty");
24
25} // namespace
26
27namespace Mantid::Kernel {
28
36bool SetValueWhenProperty::isConditionChanged(const IPropertyManager *algo, const std::string &changedPropName) const {
37 const auto *watchedProp = algo->getPointerToProperty(m_watchedPropName);
38 return watchedProp->name() == changedPropName;
39}
40
41bool SetValueWhenProperty::applyChanges(const IPropertyManager *algo, const std::string &currentPropName) {
42 try {
43 auto *currentProperty = algo->getPointerToProperty(currentPropName);
44 const auto *watchedProperty = algo->getPointerToProperty(m_watchedPropName);
45 const std::string currentValue = currentProperty->value();
46 const std::string watchedValue = watchedProperty->value();
47
48 std::string newValue = m_changeCriterion(currentValue, watchedValue);
49 if (newValue != currentValue) {
50 currentProperty->setValue(newValue);
51 return true;
52 }
53 return false;
54 } catch (const Exception::NotFoundError &) {
55 g_log.warning() << "`SetValueWhenProperty::applyChanges`: one of the properies '" << currentPropName << "' or '"
56 << m_watchedPropName << "\n"
57 << " is not present on the algorithm.\n";
58 } catch (const std::runtime_error &e) {
59 // Note: the exception might be a `PythonException`, but `MantidPythonInterface/core`
60 // is not accessible at this level.
61 g_log.warning() << "`SetValueWhenProperty::applyChanges`: exception thrown within provided callback.\n"
62 << "If the callback was a Python `Callable`, the stack trace follows:\n"
63 << "-------------------------------------------------------------------------\n"
64 << e.what() << "\n-------------------------------------------------------------------------\n";
65 }
66 return false;
67}
68
70std::vector<std::string> SetValueWhenProperty::dependsOn(const std::string &thisProp) const {
71 if (m_watchedPropName == thisProp)
72 throw std::runtime_error("SetValueWhenProperty: circular dependency detected");
73 return std::vector<std::string>{m_watchedPropName};
74}
75
77
78} // namespace Mantid::Kernel
Exception for when an item is not found in a collection.
Definition Exception.h:145
Interface to PropertyManager.
virtual Property * getPointerToProperty(const std::string &name) const =0
Get a pointer to property by name.
Interface for modifiers to Property's that specify if they should be enabled or visible in a GUI.
The Logger class is in charge of the publishing messages from the framework through various channels.
Definition Logger.h:51
void warning(const std::string &msg)
Logs at warning level.
Definition Logger.cpp:117
const std::string & name() const
Get the property's name.
Definition Property.cpp:61
virtual std::string value() const =0
Returns the value of the property as a string.
bool isConditionChanged(const IPropertyManager *algo, const std::string &changedPropName) const override
Return true when the property changed is the property that the current property's value depends on.
std::function< std::string(std::string, std::string)> m_changeCriterion
Callback to check and actually apply any required changes: this function returns a new string value f...
bool applyChanges(const Mantid::Kernel::IPropertyManager *algo, const std::string &currentPropName) override
If a new value should be set, the change is applied here.
std::string m_watchedPropName
Name of the watched property, which is the property that the current property's value depends on.
IPropertySettings * clone() const override
Make a copy of the present type of IPropertySettings.
std::vector< std::string > dependsOn(const std::string &thisProp) const override
Other properties that this property depends on.