App: Implement Save() & Restore() for int/float constraint properties
Implement the methods Save() and Restore() for PropertyIntegerConstraint and PropertyFloatConstraint. Handle also the case of a user-defined ranges. This fixes https://github.com/FreeCAD/FreeCAD/issues/24571. Hint: For PropertyQuantityConstraint this is not doable because it doesn't support user-defined ranges
This commit is contained in:
@@ -770,6 +770,54 @@ void PropertyIntegerConstraint::setPyObject(PyObject* value)
|
||||
}
|
||||
}
|
||||
|
||||
void PropertyIntegerConstraint::Save(Base::Writer& writer) const
|
||||
{
|
||||
writer.Stream() << writer.ind()
|
||||
<< "<Integer value=\"" << _lValue << "\"";
|
||||
if (_ConstStruct && _ConstStruct->isDeletable()) {
|
||||
long minimum = _ConstStruct->LowerBound;
|
||||
long maximum = _ConstStruct->UpperBound;
|
||||
long stepsize = _ConstStruct->StepSize;
|
||||
writer.Stream() << " min=\"" << minimum << "\""
|
||||
<< " max=\"" << maximum << "\""
|
||||
<< " step=\"" << stepsize << "\"";
|
||||
}
|
||||
writer.Stream() << "/>\n";
|
||||
}
|
||||
|
||||
void PropertyIntegerConstraint::Restore(Base::XMLReader& reader)
|
||||
{
|
||||
// read my Element
|
||||
reader.readElement("Integer");
|
||||
// get the value of my Attribute
|
||||
setValue(reader.getAttributeAsInteger("value"));
|
||||
|
||||
bool createConstraint = false;
|
||||
long minimum = std::numeric_limits<int>::lowest();
|
||||
long maximum = std::numeric_limits<int>::max();
|
||||
long stepsize = 1.0;
|
||||
if (reader.hasAttribute("min")) {
|
||||
minimum = reader.getAttributeAsInteger("min");
|
||||
createConstraint = true;
|
||||
}
|
||||
if (reader.hasAttribute("max")) {
|
||||
maximum = reader.getAttributeAsInteger("max");
|
||||
createConstraint = true;
|
||||
}
|
||||
if (reader.hasAttribute("step")) {
|
||||
stepsize = reader.getAttributeAsInteger("step");
|
||||
}
|
||||
|
||||
if (createConstraint) {
|
||||
Constraints* c = new Constraints();
|
||||
c->setDeletable(true);
|
||||
c->LowerBound = minimum;
|
||||
c->UpperBound = maximum;
|
||||
c->StepSize = stepsize;
|
||||
setConstraints(c);
|
||||
}
|
||||
}
|
||||
|
||||
//**************************************************************************
|
||||
//**************************************************************************
|
||||
// PropertyPercent
|
||||
@@ -1272,6 +1320,54 @@ void PropertyFloatConstraint::setPyObject(PyObject* value)
|
||||
}
|
||||
}
|
||||
|
||||
void PropertyFloatConstraint::Save(Base::Writer& writer) const
|
||||
{
|
||||
writer.Stream() << writer.ind()
|
||||
<< "<Float value=\"" << _dValue << "\"";
|
||||
if (_ConstStruct && _ConstStruct->isDeletable()) {
|
||||
double minimum = _ConstStruct->LowerBound;
|
||||
double maximum = _ConstStruct->UpperBound;
|
||||
double stepsize = _ConstStruct->StepSize;
|
||||
writer.Stream() << " min=\"" << minimum << "\""
|
||||
<< " max=\"" << maximum << "\""
|
||||
<< " step=\"" << stepsize << "\"";
|
||||
}
|
||||
writer.Stream() << "/>\n";
|
||||
}
|
||||
|
||||
void PropertyFloatConstraint::Restore(Base::XMLReader& reader)
|
||||
{
|
||||
// read my Element
|
||||
reader.readElement("Float");
|
||||
// get the value of my Attribute
|
||||
setValue(reader.getAttributeAsFloat("value"));
|
||||
|
||||
bool createConstraint = false;
|
||||
double minimum = std::numeric_limits<double>::lowest();
|
||||
double maximum = std::numeric_limits<double>::max();
|
||||
double stepsize = 1.0;
|
||||
if (reader.hasAttribute("min")) {
|
||||
minimum = reader.getAttributeAsFloat("min");
|
||||
createConstraint = true;
|
||||
}
|
||||
if (reader.hasAttribute("max")) {
|
||||
maximum = reader.getAttributeAsFloat("max");
|
||||
createConstraint = true;
|
||||
}
|
||||
if (reader.hasAttribute("step")) {
|
||||
stepsize = reader.getAttributeAsFloat("step");
|
||||
}
|
||||
|
||||
if (createConstraint) {
|
||||
Constraints* c = new Constraints();
|
||||
c->setDeletable(true);
|
||||
c->LowerBound = minimum;
|
||||
c->UpperBound = maximum;
|
||||
c->StepSize = stepsize;
|
||||
setConstraints(c);
|
||||
}
|
||||
}
|
||||
|
||||
//**************************************************************************
|
||||
// PropertyPrecision
|
||||
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
|
||||
@@ -339,6 +339,9 @@ public:
|
||||
}
|
||||
void setPyObject(PyObject* py) override;
|
||||
|
||||
void Save(Base::Writer& writer) const override;
|
||||
void Restore(Base::XMLReader& reader) override;
|
||||
|
||||
protected:
|
||||
const Constraints* _ConstStruct {nullptr};
|
||||
};
|
||||
@@ -673,6 +676,9 @@ public:
|
||||
|
||||
void setPyObject(PyObject* py) override;
|
||||
|
||||
void Save(Base::Writer& writer) const override;
|
||||
void Restore(Base::XMLReader& reader) override;
|
||||
|
||||
protected:
|
||||
const Constraints* _ConstStruct {nullptr};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user