Salome HOME
Issue #353 constraint on 2 segments from not acive sketches
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetShapeSelector.cpp
index 070cec76c88d88845c2fcb4b2bb20fe29d5ac6f2..30787899f28139e6146833f6afbc4ebad2bc6cbb 100644 (file)
@@ -35,6 +35,7 @@
 #include <ModelAPI_Validator.h>
 #include <ModelAPI_ResultValidator.h>
 #include <ModelAPI_RefAttrValidator.h>
+#include <ModelAPI_ShapeValidator.h>
 
 #include <Config_WidgetAPI.h>
 #include <Events_Error.h>
@@ -128,7 +129,7 @@ ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
 }
 
 //********************************************************************
-bool ModuleBase_WidgetShapeSelector::storeValue() const
+bool ModuleBase_WidgetShapeSelector::storeValueCustom() const
 {
   FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(mySelectedObject);
   if (aSelectedFeature == myFeature)  // In order to avoid selection of the same object
@@ -282,6 +283,18 @@ bool ModuleBase_WidgetShapeSelector::setSelection(ModuleBase_ViewerPrs theValue)
 //  if (!acceptObjectShape(aObject))
 //      return false;
 
+  // Check whether the value is valid for the viewer selection filters
+  Handle(SelectMgr_EntityOwner) anOwner = theValue.owner();
+  if (!anOwner.IsNull()) {
+    SelectMgr_ListOfFilter aFilters;
+    selectionFilters(aFilters);
+    SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
+    for (; aIt.More(); aIt.Next()) {
+      const Handle(SelectMgr_Filter)& aFilter = aIt.Value();
+      if (!aFilter->IsOk(anOwner))
+        return false;
+    }
+  }
   if (isValid(aObject, aShape)) {
     setObject(aObject, aShape);
     return true;
@@ -403,19 +416,39 @@ void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate)
       aList.append(shapeType(aType));
     }
     myWorkshop->activateSubShapesSelection(aList);
-    if (!myObjectTypes.isEmpty()) {
-      myObjTypeFilter = new ModuleBase_ObjectTypesFilter(myWorkshop, myObjectTypes);
-      aViewer->clearSelectionFilters();
-      aViewer->addSelectionFilter(myObjTypeFilter);
-    }
   } else {
     disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
-    if (!myObjTypeFilter.IsNull()) {
-      aViewer->removeSelectionFilter(myObjTypeFilter);
-      myObjTypeFilter.Nullify();
-    }
     myWorkshop->deactivateSubShapesSelection();
   }
+
+  // the initial code is moved here in order to clear filters, it is possible, that it is excessive and 
+  // is performed outside
+  if (myIsActive && !myObjectTypes.isEmpty()) {
+    aViewer->clearSelectionFilters();
+  }
+  SelectMgr_ListOfFilter aFilters;
+  selectionFilters(aFilters);
+  SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
+  for (; aIt.More(); aIt.Next()) {
+    const Handle(SelectMgr_Filter)& aSelFilter = aIt.Value();
+    if (myIsActive)
+      aViewer->addSelectionFilter(aSelFilter);
+    else
+      aViewer->removeSelectionFilter(aSelFilter);
+  }
+  // the internal filter should be removed by the widget deactivation, it is done historically
+  if (!myIsActive && !myObjTypeFilter.IsNull()) {
+    myObjTypeFilter.Nullify();
+  }
+}
+
+//********************************************************************
+void ModuleBase_WidgetShapeSelector::selectionFilters(SelectMgr_ListOfFilter& theFilters)
+{
+  if (!myObjectTypes.isEmpty()) {
+    myObjTypeFilter = new ModuleBase_ObjectTypesFilter(myWorkshop, myObjectTypes);
+    theFilters.Append(myObjTypeFilter);
+  }
   // apply filters loaded from the XML definition of the widget
   ModuleBase_FilterFactory* aFactory = myWorkshop->selectionFilters();
   SelectMgr_ListOfFilter aFilters;
@@ -426,13 +459,7 @@ void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate)
     if (aSelFilter.IsNull())
       continue;
 
-    //Handle(ModuleBase_Filter) aFilter = Handle(ModuleBase_Filter)::DownCast(aIt.Value());
-    //if (aFilter.IsNull())
-    //  continue;
-    if (myIsActive)
-      aViewer->addSelectionFilter(aSelFilter);
-    else
-      aViewer->removeSelectionFilter(aSelFilter);
+    theFilters.Append(aSelFilter);
   }
 }
 
@@ -502,6 +529,17 @@ bool ModuleBase_WidgetShapeSelector::isValid(ObjectPtr theObj, std::shared_ptr<G
         return false;
       }
     }
+    else {
+      const ModelAPI_ShapeValidator* aShapeValidator = 
+                               dynamic_cast<const ModelAPI_ShapeValidator*>(*aValidator);
+      if (aShapeValidator) {
+        DataPtr aData = myFeature->data();
+        AttributeSelectionPtr aSelectAttr = aData->selection(attributeID());
+        if (!aShapeValidator->isValid(myFeature, *aArgs, theObj, aSelectAttr, theShape)) {
+          return false;
+        }
+      }
+    }
   }
   return true;
 }
\ No newline at end of file