Mantid
Loading...
Searching...
No Matches
SetDefaultWhenProperty.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("SetDefaultWhenProperty");
24
25} // namespace
26
27namespace Mantid::Kernel {
28
37 const std::string &changedPropName) const {
38 UNUSED_ARG(algo);
39 return changedPropName == m_watchedPropName;
40}
41
42bool SetDefaultWhenProperty::applyChanges(const IPropertyManager *algo, const std::string &currentPropName) {
43 try {
44 auto currentProperty = algo->getPointerToProperty(currentPropName);
45 auto watchedProperty = algo->getPointerToProperty(m_watchedPropName);
46
47 // For this `IPropertySettings`: only apply changes if the property still has its default value,
48 // or its value has been set programmatically.
49 if (!(currentProperty->isDefault() || currentProperty->isDynamicDefault()))
50 return false;
51
52 if (m_changeCriterion(algo, currentProperty, watchedProperty)) {
53 currentProperty->setIsDynamicDefault(true);
54 return true;
55 }
56 } catch (const Exception::NotFoundError &) {
57 g_log.warning() << "`SetDefaultWhenProperty::applyChanges`: one of the properies '" << currentPropName << "' or '"
58 << m_watchedPropName << "\n"
59 << " is not present on the algorithm.\n";
60 } catch (const std::runtime_error &e) {
61 // Note: the exception might be a `PythonException`, but `MantidPythonInterface/core`
62 // is not accessible at this level.
63 g_log.warning() << "`SetDefaultWhenProperty::applyChanges`: exception thrown within provided callback.\n"
64 << "If the callback was a Python `Callable`, the stack trace follows:\n"
65 << "-------------------------------------------------------------------------\n"
66 << e.what() << "\n-------------------------------------------------------------------------\n";
67 }
68 return false;
69}
70
72std::vector<std::string> SetDefaultWhenProperty::dependsOn(const std::string &thisProp) const {
73 if (m_watchedPropName == thisProp)
74 throw std::runtime_error("SetDefaultWhenProperty: circular dependency detected");
75 return std::vector<std::string>{m_watchedPropName};
76}
77
79
80} // namespace Mantid::Kernel
#define UNUSED_ARG(x)
Function arguments are sometimes unused in certain implmentations but are required for documentation ...
Definition System.h:48
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
void setIsDynamicDefault(const bool &flag)
Set or clear the flag indicating whether or not the property's value has been set programmatically.
Definition Property.cpp:385
std::vector< std::string > dependsOn(const std::string &thisProp) const override
Other properties that this property depends on.
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::function< bool(const Mantid::Kernel::IPropertyManager *, Property *, Property *)> m_changeCriterion
Criterion to determine if a new dynamic-default value should be set: in which case this function shou...
bool isConditionChanged(const IPropertyManager *algo, const std::string &changedPropName) const override
Return true when change criterion should be tested.
IPropertySettings * clone() const override
Make a copy of the present type of IPropertySettings.
std::string m_watchedPropName
Name of the watched property.