Validator and filters redesign to unite them and simplify the isValid() method parameters
myRef->Set(aData->label().Father());
myID->Set(""); // feature is identified by the empty ID
owner()->data()->sendAttributeUpdated(this);
+ } else if (theObject.get() == NULL) {
+ myRef->Set(myRef->Label()); // reference to itself means that object is null
+ myID->Set(""); // feature is identified by the empty ID
+ owner()->data()->sendAttributeUpdated(this);
}
}
class ModelAPI_RefAttrValidator : public ModelAPI_AttributeValidator
{
public:
- //! Returns true if object is good for the feature attribute
- virtual bool isValid(const FeaturePtr& theFeature, const std::list<std::string>& theArguments,
- const ObjectPtr& theObject) const = 0;
-
- //! Returns true if the attribute is good for the feature attribute
- virtual bool isValid(const FeaturePtr& theFeature, const std::list<std::string>& theArguments,
- const AttributePtr& theAttribute) const = 0;
-
};
#endif
std::list<std::list<std::string> > anArguments;
aFactory->validators(parentID(), attributeID(), aValidators, anArguments);
+ DataPtr aData = myFeature->data();
+ //AttributePtr aAttr = aData->attribute(attributeID());
+ AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
+ if (aRefAttr) {
+ // 1. saves the previous attribute values
+ bool isObject = aRefAttr->isObject();
+ ObjectPtr aPrevObject = aRefAttr->object();
+ AttributePtr aPrevAttr = aRefAttr->attr();
+
+ // 2. store the current values, disable the model's update
+ aData->blockSendAttributeUpdated(true);
+ ObjectPtr aPrevSelectedObject = mySelectedObject;
+ GeomShapePtr aPrevShape = myShape;
+
+ mySelectedObject = theObj;
+ myShape = theShape;
+ storeValueCustom();
+
+ // 3. check the acceptability of the current values
+ std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
+ std::list<std::list<std::string> >::iterator aArgs = anArguments.begin();
+ bool aValid = true;
+ for (; aValidator != aValidators.end() && aValid; aValidator++, aArgs++) {
+ const ModelAPI_RefAttrValidator* aAttrValidator =
+ dynamic_cast<const ModelAPI_RefAttrValidator*>(*aValidator);
+ if (aAttrValidator) {
+ aValid = aAttrValidator->isValid(aRefAttr, *aArgs);
+ }
+ }
+
+ // 4. if the values are not valid, restore the previous values to the attribute
+ //if (!aValid) {
+ mySelectedObject = aPrevSelectedObject;
+ myShape = aPrevShape;
+ if (isObject)
+ aRefAttr->setObject(aPrevObject);
+ else
+ aRefAttr->setAttr(aPrevAttr);
+ //}
+ // 5. enable the model's update
+ aData->blockSendAttributeUpdated(false);
+ //updateObject(myFeature);
+
+ return aValid;
+ }
+
// Check the acceptability of the object as attribute
std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
std::list<std::list<std::string> >::iterator aArgs = anArguments.begin();
const ModelAPI_RefAttrValidator* aAttrValidator =
dynamic_cast<const ModelAPI_RefAttrValidator*>(*aValidator);
if (aAttrValidator) {
- if (!aAttrValidator->isValid(myFeature, *aArgs, theObj)) {
- return false;
- }
+ //if (!aAttrValidator->isValid(myFeature, *aArgs, theObj)) {
+ // return false;
+ //}
}
else {
const ModelAPI_ShapeValidator* aShapeValidator =
return (aCount > 0) && (aCount < 2);
}
-bool PartSet_DifferentObjectsValidator::isValid(const FeaturePtr& theFeature,
- const std::list<std::string>& theArguments,
- const ObjectPtr& theObject) const
+bool PartSet_DifferentObjectsValidator::isValid(const AttributePtr& theAttribute,
+ const std::list<std::string>& theArguments) const
{
- // Check RefAttr attributes
- std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs =
- theFeature->data()->attributes(ModelAPI_AttributeRefAttr::type());
- if (anAttrs.size() > 0) {
- std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
- for(; anAttr != anAttrs.end(); anAttr++) {
- if (*anAttr) {
- std::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
- std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttr);
- // check the object is already presented
- if (aRef->isObject() && aRef->object() == theObject)
- return false;
- }
+ FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
+
+ // 1. check whether the object of the attribute is not among the feature attributes
+ // find the attribute's object
+ ObjectPtr anObject = getObject(theAttribute);
+
+ // check whether the object is not among other feature attributes
+ if (anObject.get() != NULL) {
+ // Check RefAttr attributes
+ std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs = aFeature->data()->attributes("");
+ //if (anAttrs.size() > 0) {
+ std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anIt = anAttrs.begin();
+ for(; anIt != anAttrs.end(); anIt++) {
+ AttributePtr anAttr = *anIt;
+ // the function parameter attribute should be skipped
+ if (anAttr.get() == NULL || anAttr->id() == theAttribute->id())
+ continue;
+ ObjectPtr aCurObject = getObject(anAttr);
+ if (aCurObject && aCurObject == anObject)
+ return false;
}
}
- // Check selection attributes
- anAttrs = theFeature->data()->attributes(ModelAPI_AttributeSelection::type());
- if (anAttrs.size() > 0) {
- std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
- for(; anAttr != anAttrs.end(); anAttr++) {
- if (*anAttr) {
- std::shared_ptr<ModelAPI_AttributeSelection> aRef =
- std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*anAttr);
- // check the object is already presented
- if (aRef->isInitialized() && aRef->context() == theObject)
- return false;
+ else {
+ // 2. collect object referenced by theAttribute and ...
+ if (featureHasReferences(theAttribute)) {
+ // 3. check whether the attribute value is not among other feature attributes
+ std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs =
+ aFeature->data()->attributes(ModelAPI_AttributeRefAttr::type());
+ std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
+ for(; anAttr != anAttrs.end(); anAttr++) {
+ if (*anAttr) {
+ std::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
+ std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttr);
+ // check the object is already presented
+ if (!aRef->isObject() && aRef->attr() == theAttribute)
+ return false;
+ }
}
+ return true;
}
}
- // Check selection attributes
- anAttrs = theFeature->data()->attributes(ModelAPI_AttributeReference::type());
- if (anAttrs.size() > 0) {
- std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
- for(; anAttr != anAttrs.end(); anAttr++) {
- if (*anAttr) {
- std::shared_ptr<ModelAPI_AttributeReference> aRef =
- std::dynamic_pointer_cast<ModelAPI_AttributeReference>(*anAttr);
- // check the object is already presented
- if (aRef->isInitialized() && aRef->value() == theObject)
- return false;
- }
- }
- }
- return true;
}
-bool PartSet_DifferentObjectsValidator::isValid(const FeaturePtr& theFeature,
- const std::list<std::string>& theArguments,
- const AttributePtr& theAttribute) const
-{
- if (PartSet_DifferentObjectsValidator::isValid(theAttribute, theArguments)) {
- std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs =
- theFeature->data()->attributes(ModelAPI_AttributeRefAttr::type());
- std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
- for(; anAttr != anAttrs.end(); anAttr++) {
- if (*anAttr) {
- std::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
- std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttr);
- // check the object is already presented
- if (!aRef->isObject() && aRef->attr() == theAttribute)
- return false;
- }
- }
- return true;
- }
- return false;
-}
-
-bool PartSet_DifferentObjectsValidator::isValid(const AttributePtr& theAttribute,
- const std::list<std::string>& theArguments) const
+bool PartSet_DifferentObjectsValidator::featureHasReferences(const AttributePtr& theAttribute) const
{
std::list<std::pair<std::string, std::list<ObjectPtr> > > allRefs;
if (theAttribute->owner().get() && theAttribute->owner()->data().get())
return true;
}
+ObjectPtr PartSet_DifferentObjectsValidator::getObject(const AttributePtr& theAttribute) const
+{
+ ObjectPtr anObject;
+ std::string anAttrType = theAttribute->attributeType();
+ if (anAttrType == ModelAPI_AttributeRefAttr::type()) {
+ AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
+ if (anAttr != NULL && anAttr->isObject())
+ anObject = anAttr->object();
+ }
+ if (anAttrType == ModelAPI_AttributeSelection::type()) {
+ AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
+ if (anAttr != NULL && anAttr->isInitialized())
+ anObject = anAttr->context();
+ }
+ if (anAttrType == ModelAPI_AttributeReference::type()) {
+ AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
+ if (anAttr.get() != NULL && anAttr->isInitialized())
+ anObject = anAttr->value();
+ }
+ return anObject;
+}
+
bool PartSet_SketchValidator::isValid(const ObjectPtr theObject) const
{
FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
class PartSet_DifferentObjectsValidator : public ModelAPI_RefAttrValidator
{
public:
- /// Returns True if the feature is valid
- /// \param theFeature a feature to check
- /// \param theArguments a list of arguments (names of attributes to check)
- /// \param theObject a selected object
- virtual bool isValid(const FeaturePtr& theFeature, const std::list<std::string>& theArguments,
- const ObjectPtr& theObject) const;
-
- //! Returns true if the attribute is good for the feature attribute
- //! \param theFeature a feature to check
- //! \param theArguments a list of arguments (names of attributes to check)
- //! \param theAttribute an attribute
- virtual bool isValid(const FeaturePtr& theFeature, const std::list<std::string>& theArguments,
- const AttributePtr& theAttribute) const;
-
//! Returns true if the attribute is good for the feature attribute
//! \param theAttribute an attribute
//! \param theArguments a list of arguments (names of attributes to check)
virtual bool isValid(const AttributePtr& theAttribute,
const std::list<std::string>& theArguments) const;
+
+protected:
+ //! Casts the attribute to an attribute kind and obtains an object value if it is possible
+ //! \param theAttribute a source attribute to find object
+ //! \return an attribute object or NULL
+ ObjectPtr getObject(const AttributePtr& theAttribute) const;
+
+ //! Checks whethe other feature attributes has a reference to the given attribute
+ //! \param theAttribute a source attribute to find object
+ //! \return a boolean value
+ bool featureHasReferences(const AttributePtr& theAttribute) const;
};
/**
virtual bool isValid(const ObjectPtr theObject) const;
};
-
-#endif
+#endif
\ No newline at end of file
return ModuleBase_WidgetShapeSelector::storeValueCustom();
}
-//********************************************************************
-bool PartSet_WidgetShapeSelector::isValid(ObjectPtr theObj, std::shared_ptr<GeomAPI_Shape> theShape)
-{
- bool isValid = ModuleBase_WidgetValidated::isValid(theObj, theShape);
- if (!isValid)
- return false;
-
- // the method is redefined to analize the selected shape in validators
- SessionPtr aMgr = ModelAPI_Session::get();
- ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
- std::list<ModelAPI_Validator*> aValidators;
- std::list<std::list<std::string> > anArguments;
- aFactory->validators(parentID(), attributeID(), aValidators, anArguments);
-
- // Check the acceptability of the object and shape as validator attribute
- AttributePtr aPntAttr;
- DataPtr aData = myFeature->data();
- if (theShape.get() != NULL) {
- AttributePtr aAttr = aData->attribute(attributeID());
- AttributeRefAttrPtr aRefAttr =
- std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
- if (aRefAttr) {
- TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
- aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theObj, aShape, mySketch);
- }
- }
- // Check the acceptability of the object as attribute
- std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
- std::list<std::list<std::string> >::iterator aArgs = anArguments.begin();
- for (; aValidator != aValidators.end(); aValidator++, aArgs++) {
- const ModelAPI_RefAttrValidator* aAttrValidator =
- dynamic_cast<const ModelAPI_RefAttrValidator*>(*aValidator);
- if (aAttrValidator) {
- if (aPntAttr.get() != NULL)
- {
- if (!aAttrValidator->isValid(myFeature, *aArgs, aPntAttr)) {
- return false;
- }
- }
- else
- {
- if (!aAttrValidator->isValid(myFeature, *aArgs, theObj)) {
- return false;
- }
- }
- }
- }
- return true;
-}
-
/// \return True in success
virtual bool storeValueCustom() const;
- /// Check the selected with validators if installed
- virtual bool isValid(ObjectPtr theObj, std::shared_ptr<GeomAPI_Shape> theShape);
-
private:
/// Pointer to a sketch
CompositeFeaturePtr mySketch;
#include <ModelAPI_Session.h>
#include <GeomDataAPI_Point2D.h>
-bool SketchPlugin_DistanceAttrValidator::isValid(const FeaturePtr& theFeature,
- const std::list<std::string>& theArguments,
- const ObjectPtr& theObject) const
+bool SketchPlugin_DistanceAttrValidator::isValid(
+ const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
{
+ // there is a check whether the feature contains a point and a linear edge or two point values
std::string aParamA = theArguments.front();
SessionPtr aMgr = ModelAPI_Session::get();
ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
- const ModelAPI_ResultValidator* anArcValidator =
- dynamic_cast<const ModelAPI_ResultValidator*>(aFactory->validator("SketchPlugin_ResultArc"));
- bool anArcValid = anArcValidator->isValid(theObject);
- if (anArcValid)
+ AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
+ if (!aRefAttr)
return false;
-
- // If the object is not a line then it is accepted
- const ModelAPI_ResultValidator* aLineValidator =
- dynamic_cast<const ModelAPI_ResultValidator*>(aFactory->validator("SketchPlugin_ResultLine"));
- bool aLineValid = aLineValidator->isValid(theObject);
- if (!aLineValid)
+ bool isObject = aRefAttr->isObject();
+ if (!isObject) {
+ // an attribute is a point. A point value is valid always for the distance
return true;
+ } else {
+ // 1. check whether the references object is a linear
+ ObjectPtr anObject = aRefAttr->object();
+ const ModelAPI_ResultValidator* anArcValidator =
+ dynamic_cast<const ModelAPI_ResultValidator*>(aFactory->validator("SketchPlugin_ResultArc"));
+ bool anArcValid = anArcValidator->isValid(anObject);
+ if (anArcValid)
+ return false;
- // If it is a line then we have to check that first attribute id not a line
- std::shared_ptr<GeomDataAPI_Point2D> aPoint = getFeaturePoint(theFeature->data(), aParamA);
- if (aPoint)
- return true;
- return false;
-}
+ // If the object is not a line then it is accepted. It can be a point feature selected
+ const ModelAPI_ResultValidator* aLineValidator =
+ dynamic_cast<const ModelAPI_ResultValidator*>(aFactory->validator("SketchPlugin_ResultLine"));
+ bool aLineValid = aLineValidator->isValid(anObject);
+ if (!aLineValid)
+ return true;
+ FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
-bool SketchPlugin_DistanceAttrValidator::isValid(
- const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
-{
- // any point attribute is acceptable for the distance operation
- return true;
-}
-
-bool SketchPlugin_DistanceAttrValidator::isValid(const FeaturePtr& theFeature,
- const std::list<std::string>& theArguments,
- const AttributePtr& theAttribute) const
-{
- return isValid(theAttribute, theArguments);
+ // If it is a line then we have to check that first attribute id not a line
+ std::shared_ptr<GeomDataAPI_Point2D> aPoint = getFeaturePoint(aFeature->data(), aParamA);
+ if (aPoint)
+ return true;
+ }
+ return false;
}
//bool SketchPlugin_DifferentObjectsValidator::isValid(const FeaturePtr& theFeature,
//! \param theArguments arguments of the attribute
virtual bool isValid(
const AttributePtr& theAttribute, const std::list<std::string>& theArguments) const;
- //! Returns true if object is good for the feature attribute
- virtual bool isValid(const FeaturePtr& theFeature, const std::list<std::string>& theArguments,
- const ObjectPtr& theObject) const;
-
- //! Returns true if the attribute is good for the feature attribute
- virtual bool isValid(const FeaturePtr& theFeature, const std::list<std::string>& theArguments,
- const AttributePtr& theAttribute) const;
};
/**\class SketchPlugin_DifferentObjectsValidator