Salome HOME
Issue #3052: Conflicting constraint after trim ellipse
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetSelectionFilter.cpp
index 0abdd259c940e39d80340c12c3fa60fe00b72cdd..4b6871641c811f0d8af12addf410cd46508347e8 100644 (file)
@@ -43,6 +43,7 @@
 #include <StdSelect_BRepOwner.hxx>
 #include <TopoDS_Compound.hxx>
 #include <BRep_Builder.hxx>
+#include <TopExp_Explorer.hxx>
 
 #include <QLayout>
 #include <QPushButton>
@@ -52,6 +53,7 @@
 #include <QDialog>
 #include <QToolButton>
 #include <QCheckBox>
+#include <QDir>
 
 static FeaturePtr SelectorFeature;
 static std::string AttributeId;
@@ -98,6 +100,9 @@ ModuleBase_FilterStarter::ModuleBase_FilterStarter(const std::string& theFeature
 
 void ModuleBase_FilterStarter::onFiltersLaunch()
 {
+  static QString aHelpFileName = QString("FiltersPlugin") + QDir::separator() +
+    QString("FiltersPlugin.html");
+
   ModuleBase_Operation* aParentOp = myWorkshop->currentOperation();
   ModuleBase_OperationFeature* aFeatureOp = dynamic_cast<ModuleBase_OperationFeature*>(aParentOp);
   if (aFeatureOp)
@@ -124,6 +129,7 @@ void ModuleBase_FilterStarter::onFiltersLaunch()
   FiltersFeaturePtr aFilters = aAttrList->filters();
   if (aFilters.get())
     aFOperation->setFeature(aFilters);
+  aFOperation->setHelpFileName(aHelpFileName);
   myWorkshop->processLaunchOperation(aFOperation);
 }
 
@@ -214,18 +220,6 @@ void ModuleBase_FilterItem::onDelete()
   emit deleteItem(this);
 }
 
-QList<QWidget*> ModuleBase_FilterItem::getControls() const
-{
-  QList<QWidget*> aWidgetsList;
-  foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
-    QList<QWidget*> aSubList = aWgt->getControls();
-    foreach(QWidget* aSub, aSubList) {
-      aWidgetsList.append(aSub);
-    }
-  }
-  return aWidgetsList;
-}
-
 
 //*****************************************************************************
 //*****************************************************************************
@@ -315,17 +309,23 @@ ModuleBase_WidgetSelectionFilter::~ModuleBase_WidgetSelectionFilter()
     Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
     aCtx->Remove(myPreview, false);
     myPreview.Nullify();
-    if (myShowBtn->isChecked()) {
+    if (myListIO.Size() > 0) {
+      Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
       AIS_ListOfInteractive::const_iterator aIt;
       Handle(AIS_Shape) aShapeIO;
       for (aIt = myListIO.cbegin(); aIt != myListIO.cend(); aIt++) {
         aShapeIO = Handle(AIS_Shape)::DownCast(*aIt);
         if (!aShapeIO.IsNull()) {
           aCtx->Display(aShapeIO, false);
+          std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
+          anAISObj->setImpl(new Handle(AIS_InteractiveObject)(aShapeIO));
+          myWorkshop->applyCurrentSelectionModes(anAISObj);
         }
       }
+      myListIO.Clear();
+      myShowBtn->setChecked(false);
     }
-    aCtx->UpdateCurrentViewer();
+    myWorkshop->viewer()->update();
   }
   SelectorFeature = FeaturePtr();
   AttributeId = "";
@@ -345,7 +345,7 @@ void ModuleBase_WidgetSelectionFilter::onAddFilter(int theIndex)
       break;
     }
   }
-  onAddFilter(aFilter);
+  ModuleBase_FilterItem* aItem = onAddFilter(aFilter);
   FiltersFeaturePtr aFiltersFeature =
     std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
   aFiltersFeature->addFilter(aFilter);
@@ -353,16 +353,21 @@ void ModuleBase_WidgetSelectionFilter::onAddFilter(int theIndex)
   myFiltersCombo->setCurrentIndex(0);
   myFiltersCombo->removeItem(theIndex);
   updateObject(myFeature);
+
+  if (aItem && (aItem->widgets().size() > 0))
+    aItem->widgets().first()->emitFocusInWidget();
+  else
+    emitFocusInWidget();
 }
 
-void ModuleBase_WidgetSelectionFilter::onAddFilter(const std::string& theFilter)
+ModuleBase_FilterItem* ModuleBase_WidgetSelectionFilter::onAddFilter(const std::string& theFilter)
 {
   if (theFilter.length() == 0)
-    return;
+    return 0;
   std::list<std::string>::const_iterator aIt;
   for (aIt = myUseFilters.cbegin(); aIt != myUseFilters.cend(); aIt++) {
     if (theFilter == (*aIt))
-      return;
+      return 0;
   }
   myFilters.remove(theFilter);
   myUseFilters.push_back(theFilter);
@@ -376,6 +381,7 @@ void ModuleBase_WidgetSelectionFilter::onAddFilter(const std::string& theFilter)
   updateSelectBtn();
   clearCurrentSelection(true);
   updateNumberSelected();
+  return aItem;
 }
 
 void ModuleBase_WidgetSelectionFilter::onDeleteItem(ModuleBase_FilterItem* theItem)
@@ -404,7 +410,7 @@ void ModuleBase_WidgetSelectionFilter::onDeleteItem(ModuleBase_FilterItem* theIt
   myWorkshop->selectionActivate()->updateSelectionModes();
   myWorkshop->selectionActivate()->updateSelectionFilters();
   redisplayFeature();
-  myFiltersCombo->setFocus();
+  emitFocusInWidget();
   updateObject(myFeature);
 }
 
@@ -456,7 +462,7 @@ void ModuleBase_WidgetSelectionFilter::onSelect()
       if (!alreadyThere.Add(aTShape))
         continue;
       static SessionPtr aSession = ModelAPI_Session::get();
-      bool isValid = aSession->filters()->isValid(myFeature, aShape);
+      bool isValid = aSession->filters()->isValid(myFeature, aBody, aShape);
       if (isValid) {
         aBuilder.Add(aComp, aTShape);
         ModuleBase_ViewerPrsPtr aValue(new ModuleBase_ViewerPrs(aObj, aShape));
@@ -469,6 +475,7 @@ void ModuleBase_WidgetSelectionFilter::onSelect()
     updatePreview(aComp);
   updateNumberSelected();
   updateObject(myFeature);
+  onShowOnly(myShowBtn->isChecked());
 }
 
 void ModuleBase_WidgetSelectionFilter::updatePreview(const TopoDS_Shape& theShape)
@@ -507,9 +514,11 @@ void ModuleBase_WidgetSelectionFilter::onShowOnly(bool theShow)
   Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
 
   if (theShow) {
-    myListIO.Clear();
-    aCtx->DisplayedObjects(AIS_KOI_Shape, -1, myListIO);
-    myListIO.Remove(myPreview);
+    AIS_ListOfInteractive aList;
+    aCtx->DisplayedObjects(AIS_KOI_Shape, -1, aList);
+    aList.Remove(myPreview);
+    if (aList.Size() > 0)
+      myListIO = aList;
   }
   AIS_ListOfInteractive::const_iterator aIt;
   Handle(AIS_Shape) aShapeIO;
@@ -518,11 +527,15 @@ void ModuleBase_WidgetSelectionFilter::onShowOnly(bool theShow)
     if (!aShapeIO.IsNull()) {
       if (theShow)
         aCtx->Erase(aShapeIO, false);
-      else
+      else {
         aCtx->Display(aShapeIO, false);
+        std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
+        anAISObj->setImpl(new Handle(AIS_InteractiveObject)(aShapeIO));
+        myWorkshop->applyCurrentSelectionModes(anAISObj);
+      }
     }
   }
-  aCtx->UpdateCurrentViewer();
+  myWorkshop->viewer()->update();
 }
 
 void ModuleBase_WidgetSelectionFilter::updateSelectBtn()
@@ -546,13 +559,6 @@ void ModuleBase_WidgetSelectionFilter::updateNumberSelected()
 QList<QWidget*> ModuleBase_WidgetSelectionFilter::getControls() const
 {
   QList<QWidget*> aWidgets;
-  QList<ModuleBase_FilterItem*> aItems = myFiltersWgt->findChildren<ModuleBase_FilterItem*>();
-  foreach(ModuleBase_FilterItem* aItem, aItems) {
-    QList<QWidget*> aSubList = aItem->getControls();
-    foreach(QWidget* aWgt, aSubList) {
-      aWidgets.append(aWgt);
-    }
-  }
   aWidgets.append(myFiltersCombo);
   return aWidgets;
 }
@@ -567,12 +573,60 @@ void ModuleBase_WidgetSelectionFilter::clearCurrentSelection(bool toUpdate)
   }
 }
 
+void replaceSubShapesByResult(QList<ModuleBase_ViewerPrsPtr>& theResults, int theShapeType)
+{
+  QMap<ObjectPtr, QList<GeomShapePtr>> myResShapes;
+  // Sort sub-shapes by result
+  foreach (ModuleBase_ViewerPrsPtr aPrs, theResults) {
+    if (myResShapes.contains(aPrs->object()))
+      myResShapes[aPrs->object()].append(aPrs->shape());
+    else {
+      QList<GeomShapePtr> aShapes;
+      aShapes << aPrs->shape();
+      myResShapes[aPrs->object()] = aShapes;
+    }
+  }
+  // Find Results to replace by whole result
+  QList<GeomShapePtr> aShapes;
+  QList<ObjectPtr> aToReplace;
+  std::list<GeomShapePtr> aSubShapes;
+  foreach(ObjectPtr aObj, myResShapes.keys()) {
+    aShapes = myResShapes[aObj];
+    ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
+    TopTools_MapOfShape aShapesMap;
+    if (aRes.get()) {
+      GeomShapePtr aSubShape = aRes->shape();
+      const TopoDS_Shape& aShape = aSubShape->impl<TopoDS_Shape>();
+      for (TopExp_Explorer anExp(aShape, (TopAbs_ShapeEnum)theShapeType);
+        anExp.More(); anExp.Next()) {
+        aShapesMap.Add(anExp.Current());
+      }
+    }
+    if (aShapes.count() == aShapesMap.Size())
+      aToReplace.append(aObj);
+  }
+  // Replace the found results
+  QList<ModuleBase_ViewerPrsPtr>::iterator aIt;
+  foreach(ObjectPtr aObj, aToReplace) {
+    for (aIt = theResults.begin(); aIt != theResults.end(); aIt++) {
+      if ((*aIt)->object() == aObj) {
+        theResults.removeAll(*aIt);
+        aIt--;
+      }
+    }
+    ModuleBase_ViewerPrsPtr aValue(new ModuleBase_ViewerPrs(aObj));
+    theResults.append(aValue);
+  }
+}
+
 void ModuleBase_WidgetSelectionFilter::onFeatureAccepted()
 {
   AttributePtr aAttr = mySelectorFeature->attribute(mySelectorAttribute);
   AttributeSelectionListPtr aSelListAttr =
     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aAttr);
   aSelListAttr->clear();
+  if (aSelListAttr->isWholeResultAllowed())
+    replaceSubShapesByResult(myValues, selectionType(aSelListAttr->selectionType().c_str()));
   foreach(ModuleBase_ViewerPrsPtr aPrs, myValues) {
     aSelListAttr->append(aPrs->object(), aPrs->shape());
   }
@@ -630,7 +684,23 @@ QString ModuleBase_WidgetSelectionFilter::getError(const bool theValueStateCheck
 
 void ModuleBase_WidgetSelectionFilter::onObjectUpdated()
 {
+  myShowBtn->setChecked(false);
   clearCurrentSelection(true);
   updateNumberSelected();
+
+  QList<ModuleBase_FilterItem*> aItemsList = myFiltersWgt->findChildren<ModuleBase_FilterItem*>();
+  foreach(ModuleBase_FilterItem* aItem, aItemsList) {
+    QList<ModuleBase_ModelWidget*> aWidgetsList = aItem->widgets();
+    foreach(ModuleBase_ModelWidget* aWidget, aWidgetsList) {
+      if (!aWidget->feature().get())
+        aWidget->setFeature(myFeature);
+      aWidget->restoreValue();
+    }
+  }
   updateObject(myFeature);
+
+  // Redisplay the feature on order to Customize presentations from filters with selectors
+  static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
+  ModelAPI_EventCreator::get()->sendUpdated(myFeature, EVENT_DISP);
+  Events_Loop::loop()->flush(EVENT_DISP);
 }