]> SALOME platform Git repositories - modules/shaper.git/commitdiff
Salome HOME
Issue #308: Avoid selection of the same object for placement
authorvsv <vitaly.smetannikov@opencascade.com>
Fri, 5 Dec 2014 15:03:10 +0000 (18:03 +0300)
committervsv <vitaly.smetannikov@opencascade.com>
Fri, 5 Dec 2014 15:03:10 +0000 (18:03 +0300)
src/FeaturesPlugin/placement_widget.xml
src/ModuleBase/ModuleBase_WidgetShapeSelector.cpp
src/ModuleBase/ModuleBase_WidgetShapeSelector.h
src/PartSet/PartSet_Module.cpp
src/PartSet/PartSet_Validators.cpp
src/PartSet/PartSet_Validators.h

index e5f67055ca4cf186eaada0a2ac0c33f760fcb76b..0da033573ff5408b12a105cd25f669fbdde9e258 100644 (file)
@@ -7,11 +7,12 @@
     use_subshapes="true"
   />
   <shape_selector id="placement_attractable_face" 
-    label="Select a face" 
-    icon=":icons/cut_shape.png" 
-    tooltip="Select a face of another object"
-    shape_types="face"
-    use_subshapes="true"
-       concealment="true"
-  />
+               label="Select a face" 
+               icon=":icons/cut_shape.png" 
+               tooltip="Select a face of another object" 
+               shape_types="face" 
+               use_subshapes="true"    
+               concealment="true" >
+       <validator id="PartSet_DifferentObjects"/>
+  </shape_selector>
 </source>
index 74294d9a40d4dbeaf5067895f0ce0941eccc63a8..0f3c87e4869229594993b7f869ccb77cf95dd2e3 100644 (file)
@@ -29,6 +29,9 @@
 #include <ModelAPI_Tools.h>
 #include <ModelAPI_ResultBody.h>
 #include <ModelAPI_AttributeRefAttr.h>
+#include <ModelAPI_Validator.h>
+#include <ModelAPI_ResultValidator.h>
+#include <ModelAPI_RefAttrValidator.h>
 
 #include <Config_WidgetAPI.h>
 #include <Events_Error.h>
@@ -175,6 +178,26 @@ bool ModuleBase_WidgetShapeSelector::storeValue() const
   return false;
 }
 
+//********************************************************************
+void ModuleBase_WidgetShapeSelector::clearAttribute()
+{
+  DataPtr aData = myFeature->data();
+  AttributeSelectionPtr aSelect = aData->selection(attributeID());
+  if (aSelect) {
+    aSelect->setValue(ResultPtr(), std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape()));
+    return;
+  }
+  AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
+  if (aRefAttr) {
+    aRefAttr->setObject(ObjectPtr());
+    return;
+  }
+  AttributeReferencePtr aRef = aData->reference(attributeID());
+  if (aRef) {
+    aRef->setObject(ObjectPtr());
+  }
+}
+
 //********************************************************************
 bool ModuleBase_WidgetShapeSelector::restoreValue()
 {
@@ -218,6 +241,9 @@ QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
 //********************************************************************
 void ModuleBase_WidgetShapeSelector::onSelectionChanged()
 {
+  // In order to make reselection possible
+  // TODO: check with MPV clearAttribute();
+
   QObjectPtrList aObjects = myWorkshop->selection()->selectedPresentations();
   if (aObjects.size() > 0) {
     ObjectPtr aObject = aObjects.first();
@@ -272,8 +298,10 @@ void ModuleBase_WidgetShapeSelector::onSelectionChanged()
       if (!acceptObjectShape(aObject))
         return;
     }
-    setObject(aObject, aShape);
-    emit focusOutWidget(this);
+    if (isValid(aObject, aShape)) {
+      setObject(aObject, aShape);
+      emit focusOutWidget(this);
+    }
   }
 }
 
@@ -442,3 +470,44 @@ void ModuleBase_WidgetShapeSelector::deactivate()
 {
   activateSelection(false);
 }
+
+//********************************************************************
+bool ModuleBase_WidgetShapeSelector::isValid(ObjectPtr theObj, std::shared_ptr<GeomAPI_Shape> theShape)
+{
+  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 type of selected object
+  std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
+  bool isValid = true;
+  for (; aValidator != aValidators.end(); aValidator++) {
+    const ModelAPI_ResultValidator* aResValidator =
+        dynamic_cast<const ModelAPI_ResultValidator*>(*aValidator);
+    if (aResValidator) {
+      isValid = false;
+      if (aResValidator->isValid(theObj)) {
+        isValid = true;
+        break;
+      }
+    }
+  }
+  if (!isValid)
+    return false;
+
+  // Check the acceptability of the object as attribute
+  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 (!aAttrValidator->isValid(myFeature, *aArgs, theObj)) {
+        return false;
+      }
+    }
+  }
+  return true;
+}
\ No newline at end of file
index 4cf0a4d2ed6ea6c70fe65c49a879befdf77e78cf..0e479e1789d5628efcd8e14e8c1183642a28b2d9 100644 (file)
@@ -96,6 +96,12 @@ Q_OBJECT
   // Set the given object as a value of the widget
   void setObject(ObjectPtr theObj, std::shared_ptr<GeomAPI_Shape> theShape = std::shared_ptr<GeomAPI_Shape>());
 
+  /// Check the selected with validators if installed
+  virtual bool isValid(ObjectPtr theObj, std::shared_ptr<GeomAPI_Shape> theShape);
+
+  /// Clear attribute
+  void clearAttribute();
+
   //----------- Class members -------------
  protected:
   QWidget* myContainer;
index 6306448264cfa573ded0d5855851c4f77738a89f..f4c7826546742b23c4d945a7d307b1e932cbf9b7 100644 (file)
@@ -134,6 +134,7 @@ void PartSet_Module::registerValidators()
   aFactory->registerValidator("PartSet_PerpendicularValidator", new PartSet_PerpendicularValidator);
   aFactory->registerValidator("PartSet_ParallelValidator", new PartSet_ParallelValidator);
   aFactory->registerValidator("PartSet_RadiusValidator", new PartSet_RadiusValidator);
+  aFactory->registerValidator("PartSet_DifferentObjects", new PartSet_DifferentObjectsValidator);
 }
 
 
index 7a976bda33749c371cf392ef0478f5217e0edfc6..f35b3ae05158f7ca8f4b125776f5eb274fd0a0d2 100644 (file)
@@ -11,6 +11,9 @@
 #include <GeomAbs_CurveType.hxx>
 #include <ModuleBase_ISelection.h>
 
+#include <ModelAPI_AttributeRefAttr.h>
+#include <ModelAPI_AttributeSelection.h>
+
 #include <list>
 
 int shapesNbPoints(const ModuleBase_ISelection* theSelection)
@@ -92,3 +95,55 @@ bool PartSet_RadiusValidator::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
+{
+  // 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;
+      }
+    }
+  }
+  // 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;
+      }
+    }
+  }
+  return true;
+}
+
+bool PartSet_DifferentObjectsValidator::isValid(const FeaturePtr& theFeature, 
+                                                const std::list<std::string>& theArguments,
+                                                const AttributePtr& theAttribute) const
+{
+  // not implemented
+  return true;
+}
+
+bool PartSet_DifferentObjectsValidator::isValid(const AttributePtr& theAttribute, 
+                                                const std::list<std::string>& theArguments) const
+{
+  // not implemented
+  return true;
+}
\ No newline at end of file
index 02f7d21e6c53b1f8c7e8cd88385dc044a62c63dd..33deefd50474c82473c1186303d2eb89780322ce 100644 (file)
@@ -9,6 +9,7 @@
 
 #include <ModuleBase_SelectionValidator.h>
 #include <ModuleBase_ISelection.h>
+#include <ModelAPI_RefAttrValidator.h>
 
 /*
  * Selector validators
@@ -49,4 +50,18 @@ class PartSet_RadiusValidator : public ModuleBase_SelectionValidator
   PARTSET_EXPORT virtual bool isValid(const ModuleBase_ISelection* theSelection) const;
 };
 
+class PartSet_DifferentObjectsValidator : public ModelAPI_RefAttrValidator
+{
+ public:
+  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;
+
+  virtual bool isValid(const AttributePtr& theAttribute,
+                       const std::list<std::string>& theArguments) const;
+};
+
+
 #endif