Salome HOME
Fix selection of non-visible features.
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetValidated.cpp
index 308e14770edb8d0017fd96ef3200c0d52554cb87..bd9cdbd76b9a019995b20b451caf0bcbdcfa4767 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2017  CEA/DEN, EDF R&D
+// Copyright (C) 2014-2019  CEA/DEN, EDF R&D
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 //
 // You should have received a copy of the GNU Lesser General Public
 // License along with this library; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-// See http://www.salome-platform.org/ or
-// email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
 
 #include <ModuleBase_WidgetValidated.h>
@@ -102,22 +101,25 @@ void ModuleBase_WidgetValidated::restoreAttributeValue(const AttributePtr& theAt
 bool ModuleBase_WidgetValidated::isValidInFilters(const ModuleBase_ViewerPrsPtr& thePrs)
 {
   bool aValid = true;
-  Handle(SelectMgr_EntityOwner) anOwner = thePrs->owner();
+  AIS_NListOfEntityOwner aOwnersList;
+  if (!thePrs->owner().IsNull())
+    aOwnersList.Append(thePrs->owner());
 
   // if an owner is null, the selection happens in the Object browser.
   // creates a selection owner on the base of object shape and the object AIS object
-  if (anOwner.IsNull() && thePrs->owner().IsNull() && thePrs->object().get()) {
+  if ((aOwnersList.Size() == 0) && thePrs->object().get()) {
     ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
     GeomShapePtr aShape = aResult.get() ? aResult->shape() : GeomShapePtr();
     // some results have no shape, e.g. the parameter one. So, they should not be validated
     if (aShape.get()) {
       const TopoDS_Shape aTDShape = aShape->impl<TopoDS_Shape>();
       Handle(AIS_InteractiveObject) anIO = myWorkshop->selection()->getIO(thePrs);
-      anOwner = new StdSelect_BRepOwner(aTDShape, anIO);
+      aOwnersList.Append(new StdSelect_BRepOwner(aTDShape, anIO));
       myPresentedObject = aResult;
     }
     else {
-      FeaturePtr aFeature = ModelAPI_Feature::feature(thePrs->object());
+      //FeaturePtr aFeature = ModelAPI_Feature::feature(thePrs->object());
+      FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(thePrs->object());
       if (aFeature.get()) {
         // Use feature as a reference to all its results
         myPresentedObject = aFeature;
@@ -130,14 +132,18 @@ bool ModuleBase_WidgetValidated::isValidInFilters(const ModuleBase_ViewerPrsPtr&
             std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttr);
           aSelectAttr->setValue(myPresentedObject, GeomShapePtr(), true);
           GeomShapePtr aShape = aSelectAttr->value();
+          if (!aShape.get() && aSelectAttr->contextFeature().get() &&
+            aSelectAttr->contextFeature()->firstResult().get()) {
+            aShape = aSelectAttr->contextFeature()->firstResult()->shape();
+          }
           if (aShape.get()) {
             const TopoDS_Shape aTDShape = aShape->impl<TopoDS_Shape>();
             Handle(AIS_InteractiveObject) anIO = myWorkshop->selection()->getIO(thePrs);
-            anOwner = new StdSelect_BRepOwner(aTDShape, anIO);
+            aOwnersList.Append(new StdSelect_BRepOwner(aTDShape, anIO));
           }
           else
             aValid = false;
-          aSelectAttr->setValue(ObjectPtr(), GeomShapePtr(), true);
+          //aSelectAttr->setValue(ObjectPtr(), GeomShapePtr(), true);
         }
         else {
           ResultPtr aResult = aFeature->firstResult();
@@ -145,18 +151,24 @@ bool ModuleBase_WidgetValidated::isValidInFilters(const ModuleBase_ViewerPrsPtr&
             GeomShapePtr aShapePtr = ModelAPI_Tools::shape(aResult);
             if (aShapePtr.get()) {
               const TopoDS_Shape aTDShape = aShapePtr->impl<TopoDS_Shape>();
-              Handle(AIS_InteractiveObject) anIO = myWorkshop->selection()->getIO(thePrs);
-              anOwner = new StdSelect_BRepOwner(aTDShape, anIO);
+              AISObjectPtr aIOPtr = myWorkshop->findPresentation(aResult);
+              if (aIOPtr.get()) {
+                Handle(AIS_InteractiveObject) anIO = aIOPtr->impl<Handle(AIS_InteractiveObject)>();
+                aOwnersList.Append(new StdSelect_BRepOwner(aTDShape, anIO));
+              }
+              else {
+                aOwnersList.Append(new StdSelect_BRepOwner(aTDShape));
+              }
             }
           }
-          aValid = !anOwner.IsNull(); // only results with a shape can be filtered
+          aValid = (aOwnersList.Size() > 0); // only results with a shape can be filtered
         }
       } else
         aValid = false; // only results with a shape can be filtered
     }
   }
   // checks the owner by the AIS context activated filters
-  if (!anOwner.IsNull()) {
+  if (aOwnersList.Size() > 0) {
     // the widget validator filter should be active, but during check by preselection
     // it is not yet activated, so we need to activate/deactivate it manually
     bool isActivated = isFilterActivated();
@@ -172,13 +184,19 @@ bool ModuleBase_WidgetValidated::isValidInFilters(const ModuleBase_ViewerPrsPtr&
     if (!aContext.IsNull()) {
       const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
       SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
+      AIS_NListOfEntityOwner::Iterator aOIt;
       for (; anIt.More() && aValid; anIt.Next()) {
         Handle(SelectMgr_Filter) aFilter = anIt.Value();
-        aValid = aFilter->IsOk(anOwner);
+        for (aOIt.Init(aOwnersList); aOIt.More(); aOIt.Next()) {
+          aValid = aFilter->IsOk(aOIt.Value());
+          if (!aValid)
+            break;
+        }
+        if (!aValid)
+          break;
       }
     }
-    if (!isActivated)
-    {
+    if (!isActivated) {
       // reset filters set in activateSelectionFilters above
       myWorkshop->selectionActivate()->updateSelectionFilters();
       clearValidatedCash();
@@ -186,10 +204,19 @@ bool ModuleBase_WidgetValidated::isValidInFilters(const ModuleBase_ViewerPrsPtr&
   }
 
   // removes created owner
-  if (!anOwner.IsNull() && anOwner != thePrs->owner()) {
-    anOwner.Nullify();
+  if (aOwnersList.Size() > 0 && thePrs->owner().IsNull()) {
     myPresentedObject = ObjectPtr();
   }
+  if (!aValid) {
+    // Clear attribute if it still has selection
+    AttributePtr anAttr = attribute();
+    std::string aType = anAttr->attributeType();
+    if (aType == ModelAPI_AttributeSelection::typeId()) {
+      AttributeSelectionPtr aSelectAttr =
+        std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttr);
+      aSelectAttr->removeTemporaryValues();
+    }
+  }
   return aValid;
 }
 
@@ -436,12 +463,9 @@ QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetValidated::getFilteredSelected()
 void ModuleBase_WidgetValidated::filterPresentations(QList<ModuleBase_ViewerPrsPtr>& theValues)
 {
   QList<ModuleBase_ViewerPrsPtr> aValidatedValues;
-
-  QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
-  bool isDone = false;
-  for (; anIt != aLast; anIt++) {
-    if (isValidInFilters(*anIt))
-      aValidatedValues.append(*anIt);
+  foreach(ModuleBase_ViewerPrsPtr aPrs, theValues) {
+    if (isValidInFilters(aPrs))
+      aValidatedValues.append(aPrs);
   }
   if (aValidatedValues.size() != theValues.size()) {
     theValues.clear();