Salome HOME
Get rid of compilation warnings. Part I.
[modules/shaper.git] / src / XGUI / XGUI_FacesPanel.cpp
index b8fba2d25fe7cbbb858df23cf91b8d805613aa40..df5c2ddd796d062b33f4722fea73f2a1c6ebf59b 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2019  CEA/DEN, EDF R&D
+// Copyright (C) 2014-2020  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
@@ -42,7 +42,6 @@
 #include <GeomAPI_Shape.h>
 
 #include <ModelAPI_Events.h>
-#include <ModelAPI_ResultGroup.h>
 #include <ModelAPI_AttributeSelectionList.h>
 
 #include <QAction>
 #include <QGridLayout>
 #include <QListWidget>
 #include <QMainWindow>
+#include <QTimer>
 
 static const int LayoutMargin = 3;
 
+//********************************************************************
+bool getGroup(ModuleBase_ViewerPrsPtr thePrs, ResultGroupPtr& theResGroup,
+  FeaturePtr& theGroupFeature)
+{
+  ObjectPtr anObject = thePrs->object();
+  if (!anObject.get())
+    return false;
+
+  theResGroup = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(anObject);
+  if (theResGroup.get()) {
+    theGroupFeature = ModelAPI_Feature::feature(theResGroup);
+  }
+  else {
+    theGroupFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
+    if (theGroupFeature.get())
+      theResGroup = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(theGroupFeature->firstResult());
+  }
+  return theGroupFeature.get() && theResGroup.get();
+}
+
+//********************************************************************
+void updateHiddenShapes(Handle(ModuleBase_ResultPrs) thePrs, const TopoDS_ListOfShape& theShapes)
+{
+  TopoDS_ListOfShape aAlreadyHidden = thePrs->hiddenSubShapes();
+  TopoDS_ListOfShape::Iterator aShPIt(theShapes);
+  for (; aShPIt.More(); aShPIt.Next()) {
+    if (aAlreadyHidden.Contains(aShPIt.Value()))
+      aAlreadyHidden.Remove(aShPIt.Value());
+  }
+  thePrs->setSubShapeHidden(aAlreadyHidden);
+}
+
 //********************************************************************
 XGUI_FacesPanel::XGUI_FacesPanel(QWidget* theParent, XGUI_Workshop* theWorkshop)
-  : QDockWidget(theParent), myIsActive(false), myWorkshop(theWorkshop)
+  : QDockWidget(theParent), myWorkshop(theWorkshop), myIsActive(false)
 {
   setWindowTitle(tr("Hide Faces"));
-  QAction* aViewAct = toggleViewAction();
+  setObjectName("Hide Faces");
+
+  MAYBE_UNUSED QAction* aViewAct = toggleViewAction();
   setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
 
   QWidget* aContent = new QWidget(this);
@@ -78,6 +112,10 @@ XGUI_FacesPanel::XGUI_FacesPanel(QWidget* theParent, XGUI_Workshop* theWorkshop)
 
   myListView->getControl()->setFocusPolicy(Qt::StrongFocus);
   myListView->getControl()->viewport()->installEventFilter(this);
+
+  XGUI_Displayer* aDisplayer = myWorkshop->displayer();
+  connect(aDisplayer, SIGNAL(objectDisplayed(ObjectPtr, AISObjectPtr)),
+    SLOT(onObjectDisplay(ObjectPtr, AISObjectPtr)));
 }
 
 //********************************************************************
@@ -88,7 +126,7 @@ void XGUI_FacesPanel::reset(const bool isToFlushRedisplay)
 
   std::map<ObjectPtr, TopoDS_ListOfShape> anObjectToShapes;
   std::map<ObjectPtr, Handle(ModuleBase_ResultPrs) > anObjectToPrs;
-  QMap<int, std::shared_ptr<ModuleBase_ViewerPrs> >::const_iterator aIt;
+  QMap<int, ModuleBase_ViewerPrsPtr >::const_iterator aIt;
   for (aIt = myItems.cbegin(); aIt != myItems.cend(); aIt++) {
     getObjectsMapFromPrs(aIt.value(), anObjectToShapes, anObjectToPrs);
   }
@@ -100,7 +138,7 @@ void XGUI_FacesPanel::reset(const bool isToFlushRedisplay)
     aObjects.insert(aPrsIt->first);
     aPrsIt->second->setSubShapeHidden(anEmpty);
   }
-  std::set<std::shared_ptr<ModelAPI_Object> >::const_iterator aGrpIt;
+  std::set<ObjectPtr >::const_iterator aGrpIt;
   for (aGrpIt = myHiddenGroups.cbegin(); aGrpIt != myHiddenGroups.cend(); aGrpIt++)
     (*aGrpIt)->setDisplayed(true);
   myHiddenGroups.clear();
@@ -202,14 +240,9 @@ void XGUI_FacesPanel::restoreObjects(const std::set<ObjectPtr>& theHiddenObjects
   }
 
   // remove from myItes container
-  for (std::set<int>::const_iterator aToBeRemovedIt = anIndicesToBeRemoved.begin();
-    aToBeRemovedIt != anIndicesToBeRemoved.end(); aToBeRemovedIt++)
-  {
-    myItems.remove(*aToBeRemovedIt);
-  }
+  removeItems(anIndicesToBeRemoved);
   if (!anIndicesToBeRemoved.empty()) // means that myItems has been changed
     updateProcessedObjects(myItems, myItemObjects);
-  myListView->removeItems(anIndicesToBeRemoved);
 
   // remove from container of hidden objects
   for (std::set<ObjectPtr>::const_iterator aHiddenIt = theHiddenObjects.begin();
@@ -239,66 +272,84 @@ bool XGUI_FacesPanel::processAction(ModuleBase_ActionType theActionType)
 }
 
 //********************************************************************
-void XGUI_FacesPanel::getObjectsMapFromPrs(ModuleBase_ViewerPrsPtr thePrs,
+void XGUI_FacesPanel::getObjectsMapFromResult(ResultGroupPtr theResGroup,
+  FeaturePtr theGroupFeature,
   std::map<ObjectPtr, TopoDS_ListOfShape>& theObjectToShapes,
   std::map<ObjectPtr, Handle(ModuleBase_ResultPrs) >& theObjectToPrs)
 {
-  ObjectPtr anObject = thePrs->object();
-  if (!anObject.get())
-    return;
-
   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
-  ResultGroupPtr aResGroup = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(anObject);
-  if (aResGroup.get()) {
-    // Process a grouip result
-    FeaturePtr aGroupFeature = ModelAPI_Feature::feature(aResGroup);
-    AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list");
-    AISObjectPtr aPrs;
-    for (int i = 0; i < aSelectionList->size(); i++) {
-      AttributeSelectionPtr aSelection = aSelectionList->value(i);
-      ResultPtr aRes = aSelection->context();
-      aPrs = aDisplayer->getAISObject(aRes);
-      if (aPrs.get()) {
-        Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
-          aPrs->impl<Handle(AIS_InteractiveObject)>());
-        if (!aResultPrs.IsNull()) {
-          GeomShapePtr aShape = aSelection->value();
-          if (theObjectToShapes.find(aRes) != theObjectToShapes.end())
-            theObjectToShapes.at(aRes).Append(aShape->impl<TopoDS_Shape>());
-          else {
-            TopoDS_ListOfShape aListOfShapes;
-            aListOfShapes.Append(aShape->impl<TopoDS_Shape>());
-            theObjectToShapes[aRes] = aListOfShapes;
-            theObjectToPrs[aRes] = aResultPrs;
-          }
+  // Process a grouip result
+  AttributeSelectionListPtr aSelectionList = theGroupFeature->selectionList("group_list");
+  AISObjectPtr aPrs;
+  for (int i = 0; i < aSelectionList->size(); i++) {
+    AttributeSelectionPtr aSelection = aSelectionList->value(i);
+    ResultPtr aRes = aSelection->context();
+    aPrs = aDisplayer->getAISObject(aRes);
+    if (aPrs.get()) {
+      Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
+        aPrs->impl<Handle(AIS_InteractiveObject)>());
+      if (!aResultPrs.IsNull()) {
+        GeomShapePtr aShape = aSelection->value();
+        if (theObjectToShapes.find(aRes) != theObjectToShapes.end())
+          theObjectToShapes.at(aRes).Append(aShape->impl<TopoDS_Shape>());
+        else {
+          TopoDS_ListOfShape aListOfShapes;
+          aListOfShapes.Append(aShape->impl<TopoDS_Shape>());
+          theObjectToShapes[aRes] = aListOfShapes;
+          theObjectToPrs[aRes] = aResultPrs;
         }
       }
     }
   }
+}
+
+//********************************************************************
+void objectsMapFromPrs(ModuleBase_ViewerPrsPtr thePrs,
+  std::map<ObjectPtr, TopoDS_ListOfShape>& theObjectToShapes,
+  std::map<ObjectPtr, Handle(ModuleBase_ResultPrs) >& theObjectToPrs)
+{
+  ObjectPtr anObject = thePrs->object();
+  if (!anObject.get())
+    return;
+
+  // Process bodies
+  Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
+    thePrs->interactive());
+  if (aResultPrs.IsNull())
+    return;
+
+  if (theObjectToShapes.find(anObject) != theObjectToShapes.end())
+    theObjectToShapes.at(anObject).Append(ModuleBase_Tools::getSelectedShape(thePrs));
   else {
-    // Process bodies
-    Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
-      thePrs->interactive());
-    if (aResultPrs.IsNull())
-      return;
-
-    if (theObjectToShapes.find(anObject) != theObjectToShapes.end())
-      theObjectToShapes.at(anObject).Append(ModuleBase_Tools::getSelectedShape(thePrs));
-    else {
-      TopoDS_ListOfShape aListOfShapes;
-      aListOfShapes.Append(ModuleBase_Tools::getSelectedShape(thePrs));
-      theObjectToShapes[anObject] = aListOfShapes;
-      theObjectToPrs[anObject] = aResultPrs;
-    }
+    TopoDS_ListOfShape aListOfShapes;
+    aListOfShapes.Append(ModuleBase_Tools::getSelectedShape(thePrs));
+    theObjectToShapes[anObject] = aListOfShapes;
+    theObjectToPrs[anObject] = aResultPrs;
   }
 }
 
+//********************************************************************
+void XGUI_FacesPanel::getObjectsMapFromPrs(ModuleBase_ViewerPrsPtr thePrs,
+  std::map<ObjectPtr, TopoDS_ListOfShape>& theObjectToShapes,
+  std::map<ObjectPtr, Handle(ModuleBase_ResultPrs) >& theObjectToPrs)
+{
+  ResultGroupPtr aResGroup;
+  FeaturePtr aGroupFeature;
+  if (getGroup(thePrs, aResGroup, aGroupFeature))
+    getObjectsMapFromResult(aResGroup, aGroupFeature, theObjectToShapes, theObjectToPrs);
+  else
+    objectsMapFromPrs(thePrs, theObjectToShapes, theObjectToPrs);
+}
+
 //********************************************************************
 void XGUI_FacesPanel::processSelection()
 {
   QList<ModuleBase_ViewerPrsPtr> aSelected =
     myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Viewer);
 
+  if (aSelected.size() == 0)
+    return;
+
   bool isModified = false;
   static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
 
@@ -312,23 +363,35 @@ void XGUI_FacesPanel::processSelection()
     if (!anObject.get())
       continue;
 
-    ResultGroupPtr aResGroup = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(anObject);
-    if (!aResGroup.get()) {
+    ResultGroupPtr aResGroup;
+    FeaturePtr aGroupFeature;
+    if (getGroup(aPrs, aResGroup, aGroupFeature)) {
+      AttributeSelectionListPtr aSelectionListAttr =
+        aGroupFeature->data()->selectionList("group_list");
+      std::string aType = aSelectionListAttr->selectionType();
+      if (aType != "Faces")
+        continue;
+    }
+    else {
       GeomShapePtr aShapePtr = aPrs->shape();
       if (!aShapePtr.get() || !aShapePtr->isFace())
-        return;
+        continue;
     }
 
     QString aItemName = aResGroup.get()?
       aResGroup->data()->name().c_str() : XGUI_Tools::generateName(aPrs);
     if (myListView->hasItem(aItemName))
-      return;
+      continue;
 
-    getObjectsMapFromPrs(aPrs, anObjectToShapes, anObjectToPrs);
-    if (aResGroup.get() && aResGroup->isDisplayed()) {
-      aResGroup->setDisplayed(false);
-      myHiddenGroups.insert(aResGroup);
+    if (aResGroup.get()) {
+      if (aResGroup->isDisplayed()) {
+        aResGroup->setDisplayed(false);
+        myHiddenGroups.insert(aResGroup);
+      }
+      getObjectsMapFromResult(aResGroup, aGroupFeature, anObjectToShapes, anObjectToPrs);
     }
+    else
+      objectsMapFromPrs(aPrs, anObjectToShapes, anObjectToPrs);
 
     // The code is dedicated to remove already selected items if they are selected twice
     // It can happen in case of groups selection
@@ -388,12 +451,7 @@ void XGUI_FacesPanel::processSelection()
   }
 
   // Remove duplicate items
-  if (aToRemove.size() > 0) {
-    myListView->removeItems(aToRemove);
-    std::set<int>::const_iterator aIntIt;
-    for (aIntIt = aToRemove.cbegin(); aIntIt != aToRemove.cend(); aIntIt++)
-      myItems.remove(*aIntIt);
-  }
+  removeItems(aToRemove);
   if (isModified) {
     updateProcessedObjects(myItems, myItemObjects);
     flushRedisplay();
@@ -411,7 +469,6 @@ bool XGUI_FacesPanel::processDelete()
   if (aSelectedIds.empty())
     return false;
 
-  bool isModified = false;
   std::set<ModuleBase_ViewerPrsPtr> aRestored;
   std::set<int>::const_iterator anIt;
   for (anIt = aSelectedIds.begin(); anIt != aSelectedIds.end(); anIt++) {
@@ -419,7 +476,6 @@ bool XGUI_FacesPanel::processDelete()
     if (aRestored.find(aPrs) == aRestored.end()) {
       aRestored.insert(aPrs);
       myItems.remove(*anIt);
-      isModified = true;
     }
   }
   std::map<ObjectPtr, TopoDS_ListOfShape> anObjectToShapes;
@@ -428,9 +484,10 @@ bool XGUI_FacesPanel::processDelete()
   std::set<ModuleBase_ViewerPrsPtr>::const_iterator aIt;
   for (aIt = aRestored.cbegin(); aIt != aRestored.cend(); aIt++) {
     getObjectsMapFromPrs((*aIt), anObjectToShapes, anObjectToPrs);
-    ResultGroupPtr aResGroup = std::dynamic_pointer_cast<ModelAPI_ResultGroup>((*aIt)->object());
-    if (aResGroup.get()) {
-      std::set<std::shared_ptr<ModelAPI_Object> >::iterator aGrpIt = myHiddenGroups.find(aResGroup);
+    ResultGroupPtr aResGroup;
+    FeaturePtr aGroupFeature;
+    if (getGroup((*aIt), aResGroup, aGroupFeature)) {
+      std::set<ObjectPtr >::iterator aGrpIt = myHiddenGroups.find(aResGroup);
       if (aGrpIt != myHiddenGroups.end()) {
         aResGroup->setDisplayed(true);
         myHiddenGroups.erase(aGrpIt);
@@ -461,7 +518,7 @@ bool XGUI_FacesPanel::processDelete()
 
 //********************************************************************
 bool XGUI_FacesPanel::redisplayObjects(
-  const std::set<std::shared_ptr<ModelAPI_Object> >& theObjects)
+  const std::set<ObjectPtr >& theObjects)
 {
   if (theObjects.empty())
     return false;
@@ -488,24 +545,21 @@ void XGUI_FacesPanel::updateProcessedObjects(QMap<int, ModuleBase_ViewerPrsPtr>
   for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anIt = theItems.begin();
        anIt != theItems.end(); anIt++) {
     ModuleBase_ViewerPrsPtr aPrs = anIt.value();
-    ObjectPtr anObject = aPrs.get() ? aPrs->object() : ObjectPtr();
-    if (anObject.get()) {
-      ResultGroupPtr aResGroup = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(anObject);
-      if (aResGroup.get()) {
-        FeaturePtr aGroupFeature = ModelAPI_Feature::feature(aResGroup);
-        AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list");
-        for (int i = 0; i < aSelectionList->size(); i++) {
-          AttributeSelectionPtr aSelection = aSelectionList->value(i);
-          ResultPtr aRes = aSelection->context();
-          if (theObjects.find(aRes) == theObjects.end())
-            theObjects.insert(aRes);
-        }
-      }
-      else {
-        if (theObjects.find(anObject) == theObjects.end())
-          theObjects.insert(anObject);
+    ResultGroupPtr aResGroup;
+    FeaturePtr aGroupFeature;
+    if (getGroup(aPrs, aResGroup, aGroupFeature)) {
+      AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list");
+      for (int i = 0; i < aSelectionList->size(); i++) {
+        AttributeSelectionPtr aSelection = aSelectionList->value(i);
+        ResultPtr aRes = aSelection->context();
+        if (theObjects.find(aRes) == theObjects.end())
+          theObjects.insert(aRes);
       }
     }
+    else {
+      if (theObjects.find(aPrs->object()) == theObjects.end())
+        theObjects.insert(aPrs->object());
+    }
   }
 }
 
@@ -516,13 +570,6 @@ void XGUI_FacesPanel::closeEvent(QCloseEvent* theEvent)
   emit closed();
 }
 
-//********************************************************************
-bool XGUI_FacesPanel::customizeObject(const ObjectPtr& theObject,
-  const AISObjectPtr& thePresentation)
-{
-  return myItems.size() > 0;
-}
-
 //********************************************************************
 void XGUI_FacesPanel::onDeleteItem()
 {
@@ -534,7 +581,7 @@ void XGUI_FacesPanel::onTransparencyChanged()
 {
   std::map<ObjectPtr, TopoDS_ListOfShape> anObjectToShapes;
   std::map<ObjectPtr, Handle(ModuleBase_ResultPrs) > anObjectToPrs;
-  QMap<int, std::shared_ptr<ModuleBase_ViewerPrs> >::const_iterator aIt;
+  QMap<int, ModuleBase_ViewerPrsPtr >::const_iterator aIt;
   for (aIt = myItems.cbegin(); aIt != myItems.cend(); aIt++) {
     getObjectsMapFromPrs(aIt.value(), anObjectToShapes, anObjectToPrs);
   }
@@ -567,3 +614,97 @@ void XGUI_FacesPanel::flushRedisplay() const
     anObjectBrowser->updateAllIndexes();
   myWorkshop->viewer()->update();
 }
+
+
+//********************************************************************
+void XGUI_FacesPanel::onObjectDisplay(ObjectPtr theObject, AISObjectPtr theAIS)
+{
+  bool aContains = false;
+  QMap<int, ModuleBase_ViewerPrsPtr>::iterator aIt;
+  for (aIt = myItems.begin(); aIt != myItems.end(); aIt++) {
+    ModuleBase_ViewerPrsPtr aPrs = aIt.value();
+    if (aPrs->object() == theObject) {
+      aContains = true;
+      break;
+    }
+  }
+  if (aContains) {
+    ResultGroupPtr aResGroup;
+    FeaturePtr aGroupFeature;
+    std::map<ObjectPtr, TopoDS_ListOfShape> aObjectToShapes;
+    std::map<ObjectPtr, Handle(ModuleBase_ResultPrs)> aObjectToPrs;
+    std::set<ObjectPtr> aObjects;
+    std::set<int> aIdsToRem;
+
+    TopoDS_ListOfShape aHideShapes;
+    std::map<ObjectPtr, TopoDS_ListOfShape>::const_iterator aSIt;
+    for (aIt = myItems.begin(); aIt != myItems.end(); aIt++) {
+      ModuleBase_ViewerPrsPtr aPrs = aIt.value();
+      if (getGroup(aPrs, aResGroup, aGroupFeature)) {
+        getObjectsMapFromResult(aResGroup, aGroupFeature, aObjectToShapes, aObjectToPrs);
+        if (aResGroup == theObject) {
+          // If group is displayed it means that it has to be deleted from the Faces list and all
+          // corresponded faces have been restored
+          for (aSIt = aObjectToShapes.begin(); aSIt != aObjectToShapes.end(); aSIt++) {
+            TopoDS_ListOfShape aShapes = aSIt->second;
+            Handle(ModuleBase_ResultPrs) aPrs = aObjectToPrs[aSIt->first];
+            TopoDS_ListOfShape aAlreadyHidden = aPrs->hiddenSubShapes();
+            TopoDS_ListOfShape::Iterator aShPIt(aShapes);
+            for (; aShPIt.More(); aShPIt.Next()) {
+              if (aAlreadyHidden.Contains(aShPIt.Value()))
+                aAlreadyHidden.Remove(aShPIt.Value());
+            }
+            aPrs->setSubShapeHidden(aAlreadyHidden);
+            aObjects.insert(aSIt->first);
+          }
+          aIdsToRem.insert(aIt.key());
+        }
+        else {
+          std::map<ObjectPtr, Handle(ModuleBase_ResultPrs)>::iterator aPIt =
+            aObjectToPrs.find(theObject);
+          if (aPIt != aObjectToPrs.end()) {
+            ObjectPtr aObj = aPIt->first;
+            if (aObj == theObject) {
+              Handle(ModuleBase_ResultPrs) aPrs = aPIt->second;
+              TopoDS_ListOfShape aShapes = aObjectToShapes[aObj];
+              aHideShapes.Append(aShapes);
+              aObjects.insert(aObj);
+            }
+          }
+        }
+      }
+      else {
+        if (aPrs->object() == theObject) {
+          TopoDS_Shape aShape = aPrs->shape()->impl<TopoDS_Shape>();
+          if (!aShape.IsNull())
+            aHideShapes.Append(aShape);
+          aPrs->setInteractive(theAIS->impl<Handle(AIS_InteractiveObject)>());
+        }
+      }
+    }
+    double aTransp = transparency();
+    if (aHideShapes.Size() > 0) {
+      Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
+        theAIS->impl<Handle(AIS_InteractiveObject)>());
+      if (!aResultPrs.IsNull()) {
+        aResultPrs->setSubShapeHidden(aHideShapes);
+        aResultPrs->setHiddenSubShapeTransparency(aTransp);
+        aObjects.insert(theObject);
+      }
+    }
+    removeItems(aIdsToRem);
+    myWorkshop->selector()->clearSelection();
+    if (redisplayObjects(aObjects))
+      QTimer::singleShot(50, this, SLOT(flushRedisplay()));
+  }
+}
+
+void XGUI_FacesPanel::removeItems(std::set<int> theIds)
+{
+  if (theIds.empty())
+    return;
+  myListView->removeItems(theIds);
+  std::set<int>::const_iterator aRIt;
+  for (aRIt = theIds.begin(); aRIt != theIds.end(); aRIt++)
+    myItems.remove(*aRIt);
+}