Salome HOME
Multi-selection widget to be used in the extrusion feature.
authornds <natalia.donis@opencascade.com>
Tue, 17 Mar 2015 07:04:09 +0000 (10:04 +0300)
committernds <natalia.donis@opencascade.com>
Tue, 17 Mar 2015 07:04:09 +0000 (10:04 +0300)
Validator and filters redesign to unite them and simplify the isValid() method parameters

src/Model/Model_AttributeRefAttr.cpp
src/ModelAPI/ModelAPI_RefAttrValidator.h
src/ModuleBase/ModuleBase_WidgetShapeSelector.cpp
src/PartSet/PartSet_Validators.cpp
src/PartSet/PartSet_Validators.h
src/PartSet/PartSet_WidgetShapeSelector.cpp
src/PartSet/PartSet_WidgetShapeSelector.h
src/SketchPlugin/SketchPlugin_Validators.cpp
src/SketchPlugin/SketchPlugin_Validators.h

index b6ac116635859ca831369e5011715e601d55fde9..b6f9e3e67ee5fa6dffa1427c44f52232e1b5ddf3 100644 (file)
@@ -47,6 +47,10 @@ void Model_AttributeRefAttr::setObject(ObjectPtr theObject)
     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);
   }
 }
 
index ab8ffc6cd5709f22a6819110506f65be5a870b4e..087cff483c516d4c5a122e44c0182131b3d9ee0d 100644 (file)
 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
index 3d376aacea74c20b135de20aae5fbdfdc5943044..9f9cd941dece9e56d513763868ecb75f3addc4fa 100644 (file)
@@ -437,6 +437,52 @@ bool ModuleBase_WidgetShapeSelector::isValid(ObjectPtr theObj, std::shared_ptr<G
   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();
@@ -444,9 +490,9 @@ bool ModuleBase_WidgetShapeSelector::isValid(ObjectPtr theObj, std::shared_ptr<G
     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 = 
index 92100792ede4fba1b1c3a64555721c790578786b..9bda4c9dc7d73c819ccda6ebf63127aea5f3296b 100644 (file)
@@ -110,80 +110,53 @@ bool PartSet_RigidValidator::isValid(const ModuleBase_ISelection* theSelection)
   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())
@@ -213,6 +186,28 @@ bool PartSet_DifferentObjectsValidator::isValid(const AttributePtr& theAttribute
   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);
index be1bb0ef5c149fa4ea04866b636f62e49aaa74f0..0dc5f46195d640760a9d7c4b6b2f4a91a0ee8161 100644 (file)
@@ -73,25 +73,22 @@ class PartSet_RigidValidator : public ModuleBase_SelectionValidator
 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;
 };
 
 /**
@@ -106,5 +103,4 @@ class PartSet_SketchValidator : public ModelAPI_ResultValidator
   virtual bool isValid(const ObjectPtr theObject) const;
 };
 
-
-#endif
+#endif
\ No newline at end of file
index d51be0d51f5318fe90fbe977bf44722fb4f5590e..422be37e10068822241b816152f5da45be9b2eb9 100644 (file)
@@ -62,53 +62,3 @@ bool PartSet_WidgetShapeSelector::storeValueCustom() const
   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;
-}
-
index b69c66648c351c04fdc8a109da761d8feefabefa..503625684600dfea75e76b5eec45ea16af4d8190 100644 (file)
@@ -47,9 +47,6 @@ protected:
   /// \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;
index 1fcac300d526f2fdaedf7b1e180de44e4baf9145..e69dce246f6b1ff333720f7bca8b8716dfcb474e 100644 (file)
 #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,
index 61553fea865450b327c40cd3b312ce6bd17d065b..9b732adee3c456ac7dfcc70bc4e20b0f8d5c15df 100644 (file)
@@ -24,13 +24,6 @@ class SketchPlugin_DistanceAttrValidator : public ModelAPI_RefAttrValidator
   //! \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