From a1ca7309fb2f08868f7ce12ee23ae5398bc74a0b Mon Sep 17 00:00:00 2001 From: wmayer Date: Sun, 16 Jul 2017 11:09:48 +0200 Subject: [PATCH] allow to set constraints via Python --- src/App/PropertyStandard.cpp | 72 ++++++++++++++++++++---------------- src/App/PropertyStandard.h | 54 +++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 32 deletions(-) diff --git a/src/App/PropertyStandard.cpp b/src/App/PropertyStandard.cpp index 1c3c3af7db..f6d74d22ba 100644 --- a/src/App/PropertyStandard.cpp +++ b/src/App/PropertyStandard.cpp @@ -580,7 +580,7 @@ void PropertyEnumeration::setPathValue(const ObjectIdentifier &path, const boost // PropertyIntegerConstraint //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -TYPESYSTEM_SOURCE(App::PropertyIntegerConstraint, App::PropertyInteger); +TYPESYSTEM_SOURCE(App::PropertyIntegerConstraint, App::PropertyInteger) //************************************************************************** // Construction/Destruction @@ -595,11 +595,17 @@ PropertyIntegerConstraint::PropertyIntegerConstraint() PropertyIntegerConstraint::~PropertyIntegerConstraint() { - + if (_ConstStruct && _ConstStruct->isDeletable()) + delete _ConstStruct; } void PropertyIntegerConstraint::setConstraints(const Constraints* sConstrain) { + if (_ConstStruct != sConstrain) { + if (_ConstStruct && _ConstStruct->isDeletable()) + delete _ConstStruct; + } + _ConstStruct = sConstrain; } @@ -644,20 +650,16 @@ void PropertyIntegerConstraint::setPyObject(PyObject *value) throw Base::TypeError("Type in tuple must be int"); } - if (!_ConstStruct) { - Constraints* c = new Constraints(); - c->LowerBound = values[1]; - c->UpperBound = values[2]; - c->StepSize = std::max(1, values[3]); - if (values[0] > c->UpperBound) - values[0] = c->UpperBound; - else if (values[0] < c->LowerBound) - values[0] = c->LowerBound; - setConstraints(c); - } - else { - throw Base::RuntimeError("Cannot override limits of constraint"); - } + Constraints* c = new Constraints(); + c->setDeletable(true); + c->LowerBound = values[1]; + c->UpperBound = values[2]; + c->StepSize = std::max(1, values[3]); + if (values[0] > c->UpperBound) + values[0] = c->UpperBound; + else if (values[0] < c->LowerBound) + values[0] = c->LowerBound; + setConstraints(c); aboutToSetValue(); _lValue = values[0]; @@ -1108,7 +1110,7 @@ const boost::any PropertyFloat::getPathValue(const ObjectIdentifier &path) const // PropertyFloatConstraint //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -TYPESYSTEM_SOURCE(App::PropertyFloatConstraint, App::PropertyFloat); +TYPESYSTEM_SOURCE(App::PropertyFloatConstraint, App::PropertyFloat) //************************************************************************** // Construction/Destruction @@ -1122,11 +1124,16 @@ PropertyFloatConstraint::PropertyFloatConstraint() PropertyFloatConstraint::~PropertyFloatConstraint() { - + if (_ConstStruct && _ConstStruct->isDeletable()) + delete _ConstStruct; } void PropertyFloatConstraint::setConstraints(const Constraints* sConstrain) { + if (_ConstStruct != sConstrain) { + if (_ConstStruct && _ConstStruct->isDeletable()) + delete _ConstStruct; + } _ConstStruct = sConstrain; } @@ -1186,20 +1193,21 @@ void PropertyFloatConstraint::setPyObject(PyObject *value) throw Base::TypeError("Type in tuple must be float or int"); } - if (!_ConstStruct) { - Constraints* c = new Constraints(); - c->LowerBound = values[1]; - c->UpperBound = values[2]; - c->StepSize = std::max(0.1, values[3]); - if (values[0] > c->UpperBound) - values[0] = c->UpperBound; - else if (values[0] < c->LowerBound) - values[0] = c->LowerBound; - setConstraints(c); - } - else { - throw Base::RuntimeError("Cannot override limits of constraint"); - } + double stepSize = values[3]; + // need a value > 0 + if (stepSize < DBL_EPSILON) + throw Base::ValueError("Step size must be greater than zero"); + + Constraints* c = new Constraints(); + c->setDeletable(true); + c->LowerBound = values[1]; + c->UpperBound = values[2]; + c->StepSize = stepSize; + if (values[0] > c->UpperBound) + values[0] = c->UpperBound; + else if (values[0] < c->LowerBound) + values[0] = c->LowerBound; + setConstraints(c); aboutToSetValue(); _dValue = values[0]; diff --git a/src/App/PropertyStandard.h b/src/App/PropertyStandard.h index cbf5b5c815..eccc8fcf4e 100644 --- a/src/App/PropertyStandard.h +++ b/src/App/PropertyStandard.h @@ -227,6 +227,33 @@ public: /// the boundary struct struct Constraints { long LowerBound, UpperBound, StepSize; + Constraints() + : LowerBound(0) + , UpperBound(0) + , StepSize(0) + , candelete(false) + { + } + Constraints(long l, long u, long s) + : LowerBound(l) + , UpperBound(u) + , StepSize(s) + , candelete(false) + { + } + ~Constraints() + { + } + void setDeletable(bool on) + { + candelete = on; + } + bool isDeletable() const + { + return candelete; + } + private: + bool candelete; }; /** setting the boundaries * This sets the constraint struct. It can be dynamically @@ -495,6 +522,33 @@ public: /// the boundary struct struct Constraints { double LowerBound, UpperBound, StepSize; + Constraints() + : LowerBound(0) + , UpperBound(0) + , StepSize(0) + , candelete(false) + { + } + Constraints(double l, double u, double s) + : LowerBound(l) + , UpperBound(u) + , StepSize(s) + , candelete(false) + { + } + ~Constraints() + { + } + void setDeletable(bool on) + { + candelete = on; + } + bool isDeletable() const + { + return candelete; + } + private: + bool candelete; }; /** setting the boundaries * This sets the constraint struct. It can be dynamcly