Newer
Older
Janik Zikovsky
committed
#include "MantidKernel/EnabledWhenProperty.h"
#include <boost/lexical_cast.hpp>
#include <exception>
#include <memory>
Janik Zikovsky
committed
using namespace Mantid::Kernel;
using EnabledPropPtr = std::unique_ptr<EnabledWhenProperty>;
EnabledWhenProperty::EnabledWhenProperty(const std::string &otherPropName,
const ePropertyCriterion when,
const std::string &value)
: IPropertySettings() {
m_propertyDetails =
std::make_unique<PropertyDetails>(otherPropName, when, value);
}
EnabledWhenProperty::EnabledWhenProperty(EnabledPropPtr &&conditionOne,
EnabledPropPtr &&conditionTwo,
eLogicOperator logicalOperator)
: IPropertySettings() {
m_comparisonDetails = std::make_unique<ComparisonDetails>(
std::move(conditionOne), std::move(conditionTwo), logicalOperator);
bool EnabledWhenProperty::fulfillsCriterion(
const IPropertyManager *algo) const {
if (algo == nullptr)
Property *prop = nullptr;
prop = algo->getPointerToProperty(m_propertyDetails->otherPropName);
} catch (Exception::NotFoundError &) {
return true; // Property not found. Ignore
}
if (!prop)
return true;
// Value of the other property
const std::string propValue = prop->value();
// OK, we have the property. Check the condition
switch (m_propertyDetails->criterion) {
case IS_DEFAULT:
return prop->isDefault();
case IS_NOT_DEFAULT:
return !prop->isDefault();
case IS_EQUAL_TO:
return (propValue == m_propertyDetails->value);
return (propValue != m_propertyDetails->value);
int check = boost::lexical_cast<int>(m_propertyDetails->value);
int iPropV = boost::lexical_cast<int>(propValue);
return (iPropV >= check);
}
default:
// Unknown criterion
std::string errString = "The EnabledWhenProperty criterion set"
" for the following property ";
errString += m_propertyDetails->otherPropName;
errString += " is unknown";
throw std::invalid_argument(errString);
}
}
bool EnabledWhenProperty::isEnabled(const IPropertyManager *algo) const {
return fulfillsCriterion(algo);
}
bool EnabledWhenProperty::isVisible(const IPropertyManager *) const {
return true;
}
/// does nothing in this case and put here to satisfy the interface.
void EnabledWhenProperty::modify_allowed_values(Property *const) {}
//--------------------------------------------------------------------------------------------
/// Make a copy of the present type of validator
IPropertySettings *EnabledWhenProperty::clone() {
const PropertyDetails &propDetails = *m_propertyDetails;
EnabledWhenProperty *out = new EnabledWhenProperty(
propDetails.otherPropName, propDetails.criterion, propDetails.value);
return out;
}
} // namespace Mantid
Janik Zikovsky
committed
} // namespace Kernel