Salome HOME
Adjust unit tests
[modules/shaper.git] / src / XGUI / XGUI_FacesPanel.cpp
index 5f9c9d5833d503d7cf5909ad1ad668d5e82b367c..a815c49ec9632cfb4e820515c131ceaa8ed1d9f6 100644 (file)
@@ -26,6 +26,7 @@
 
 #include <ModelAPI_Events.h>
 
+#include <ModuleBase_IModule.h>
 #include <ModuleBase_ISelection.h>
 #include "ModuleBase_IWorkshop.h"
 #include "ModuleBase_ListView.h"
@@ -33,7 +34,8 @@
 #include "ModuleBase_Tools.h"
 #include "ModuleBase_ViewerPrs.h"
 
-#include "XGUI_Displayer.h"
+#include "XGUI_SelectionMgr.h"
+#include "XGUI_SelectionFilterType.h"
 #include "XGUI_Tools.h"
 #include "XGUI_Workshop.h"
 
@@ -60,6 +62,8 @@ XGUI_FacesPanel::XGUI_FacesPanel(QWidget* theParent, ModuleBase_IWorkshop* theWo
   setWidget(aContent);
 
   myHiddenOrTransparent = new QCheckBox(tr("Transparent"), aContent);
+  connect(myHiddenOrTransparent, SIGNAL(toggled(bool)), SLOT(onTransparencyChanged()));
+
   myListView = new ModuleBase_ListView(aContent, "", "Hidden/transparent faces in 3D view");
   connect(myListView, SIGNAL(deleteActionClicked()), SLOT(onDeleteItem()));
 
@@ -73,23 +77,46 @@ XGUI_FacesPanel::XGUI_FacesPanel(QWidget* theParent, ModuleBase_IWorkshop* theWo
 //********************************************************************
 void XGUI_FacesPanel::reset(const bool isToFlushRedisplay)
 {
-  // restore presentation state
-  bool isModified = false;
-  std::set<ObjectPtr> aRestoredObjects;
-  for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anIt = myItems.begin();
-    anIt != myItems.end(); anIt++) {
-    if (aRestoredObjects.find(anIt.value()->object()) == aRestoredObjects.end())
-      aRestoredObjects.insert(anIt.value()->object());
-  }
+  if (myLastItemIndex == 0) // do nothing because there was no activity in the pane after reset
+    return;
+
   // clear internal containers
   myListView->getControl()->clear();
-  myLastItemIndex = 0;
   myItems.clear();
 
-  isModified = redisplayObjects(aRestoredObjects, isToFlushRedisplay);
+  // restore previous view of presentations
+  bool isModified = redisplayObjects(myItemObjects, false);
+  std::set<std::shared_ptr<ModelAPI_Object> > aHiddenObjects = myHiddenObjects;
+  isModified = displayHiddenObjects(aHiddenObjects, myHiddenObjects, false) || isModified;
+  if (isModified)// && isToFlushRedisplay) // flush signal immediatelly until container is filled
+    Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
+
+  updateProcessedObjects(myItems, myItemObjects);
+  myHiddenObjects.clear();
+  myLastItemIndex = 0; // it should be after redisplay as flag used in customize
+}
+
+//********************************************************************
+bool XGUI_FacesPanel::isEmpty() const
+{
+  return myItems.size() == 0;
+}
+
+//********************************************************************
+void XGUI_FacesPanel::selectionModes(QIntList& theModes)
+{
+  theModes.append(TopAbs_FACE);
+}
+
+//********************************************************************
+void XGUI_FacesPanel::selectionFilters(SelectMgr_ListOfFilter& theSelectionFilters)
+{
+  ModuleBase_IModule* aModule = myWorkshop->module();
+  QIntList aModuleSelectionFilters = myWorkshop->module()->selectionFilters();
 
-  if (isToFlushRedisplay && isModified)
-    XGUI_Tools::workshop(myWorkshop)->displayer()->updateViewer();
+  theSelectionFilters.Append(aModule->selectionFilter(SF_GlobalFilter));
+  theSelectionFilters.Append(aModule->selectionFilter(SF_FilterInfinite));
+  theSelectionFilters.Append(aModule->selectionFilter(SF_ResultGroupNameFilter));
 }
 
 //********************************************************************
@@ -114,18 +141,15 @@ void XGUI_FacesPanel::setActivePanel(const bool theIsActive)
   ModuleBase_Tools::setShadowEffect(myListView->getControl(), theIsActive);
   myIsActive = theIsActive;
 
-  if (myIsActive)
-  {
+  if (myIsActive) {
     emit activated();
-    // selection should be activated after emit signal, that deactivates current widget(selection)
-    activateSelection(theIsActive);
+    // selection should be cleared after emit of signal to do not process selection change
+    // event by the previous selector
+    // the selection is cleared by activating selection control
+    XGUI_Tools::workshop(myWorkshop)->selector()->clearSelection();
   }
   else
-  {
-    // selection should be activated after emit signal, that deactivates current widget(selection)
-    activateSelection(theIsActive);
     emit deactivated();
-  }
 }
 
 //********************************************************************
@@ -151,15 +175,18 @@ 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);
-
+  }
+  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();
     aHiddenIt != theHiddenObjects.end(); aHiddenIt++)
   {
-    if (myHiddenObjects.find(*aHiddenIt) != myHiddenObjects.end()) ///< found objects
+    if (myHiddenObjects.find(*aHiddenIt) != myHiddenObjects.end()) /// found objects
       myHiddenObjects.erase(*aHiddenIt);
   }
 }
@@ -188,24 +215,57 @@ void XGUI_FacesPanel::processSelection()
   QList<ModuleBase_ViewerPrsPtr> aSelected = myWorkshop->selection()->getSelected(
                                                        ModuleBase_ISelection::Viewer);
   bool isModified = false;
+  static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
+
+  std::map<ObjectPtr, NCollection_List<TopoDS_Shape> > anObjectToShapes;
+  std::map<ObjectPtr, Handle(ModuleBase_ResultPrs) > anObjectToPrs;
   for (int i = 0; i < aSelected.size(); i++) {
     ModuleBase_ViewerPrsPtr aPrs = aSelected[i];
     ObjectPtr anObject = aPrs->object();
     if (!anObject.get())
       continue;
-
     if (ModuleBase_Tools::getSelectedShape(aPrs).ShapeType() != TopAbs_FACE)
       continue;
 
+    Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
+      aPrs->interactive());
+    if (aResultPrs.IsNull())
+      continue;
+
     myItems.insert(myLastItemIndex, aPrs);
     myListView->addItem(generateName(aPrs), myLastItemIndex);
-    isModified = hideFace(myLastItemIndex) || isModified;
-
     myLastItemIndex++;
+    isModified = true;
+
+    if (anObjectToShapes.find(anObject) != anObjectToShapes.end())
+      anObjectToShapes.at(anObject).Append(ModuleBase_Tools::getSelectedShape(aPrs));
+    else {
+      NCollection_List<TopoDS_Shape> aListOfShapes;
+      aListOfShapes.Append(ModuleBase_Tools::getSelectedShape(aPrs));
+      anObjectToShapes[anObject] = aListOfShapes;
+      anObjectToPrs[anObject] = aResultPrs;
+    }
   }
+  for (std::map<ObjectPtr, NCollection_List<TopoDS_Shape> >::const_iterator
+    anIt = anObjectToShapes.begin(); anIt != anObjectToShapes.end(); anIt++) {
+    ObjectPtr anObject = anIt->first;
+    if (!anObject.get() || anObjectToPrs.find(anObject) == anObjectToPrs.end())
+      continue;
+    Handle(ModuleBase_ResultPrs) aResultPrs = anObjectToPrs.at(anObject);
 
+    if (aResultPrs->hasSubShapeVisible(anIt->second) || useTransparency()) // redisplay
+      ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
+    else { // erase object because it is entirely hidden
+      anObject->setDisplayed(false);
+      myHiddenObjects.insert(anObject);
+      ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
+    }
+  }
   if (isModified)
-    XGUI_Tools::workshop(myWorkshop)->displayer()->updateViewer();
+  {
+    updateProcessedObjects(myItems, myItemObjects);
+    Events_Loop::loop()->flush(aDispEvent);
+  }
 }
 
 //********************************************************************
@@ -221,20 +281,25 @@ bool XGUI_FacesPanel::processDelete()
 
   bool isModified = false;
   std::set<ObjectPtr> aRestoredObjects;
-  XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(myWorkshop)->displayer();
   for (std::set<int>::const_iterator anIt = aSelectedIds.begin(); anIt != aSelectedIds.end();
        anIt++) {
     ModuleBase_ViewerPrsPtr aPrs = myItems[*anIt];
     if (aRestoredObjects.find(aPrs->object()) == aRestoredObjects.end())
       aRestoredObjects.insert(aPrs->object());
     myItems.remove(*anIt);
+    isModified = true;
+  }
+  if (isModified) {
+    bool isRedisplayed = redisplayObjects(aRestoredObjects, false);
+    isRedisplayed = displayHiddenObjects(aRestoredObjects, myHiddenObjects, false)
+                    || isRedisplayed;
+    if (isRedisplayed)
+      Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
+    // should be after flush of redisplay to have items object to be updated
+    updateProcessedObjects(myItems, myItemObjects);
   }
-  myListView->removeSelectedItems();
-
-  isModified = redisplayObjects(aRestoredObjects, true) || isModified;
-  if (isModified)
-    XGUI_Tools::workshop(myWorkshop)->displayer()->updateViewer();
 
+  myListView->removeSelectedItems();
   // Restore selection
   myListView->restoreSelection(anIndices);
   //appendSelectionInHistory();
@@ -246,89 +311,122 @@ bool XGUI_FacesPanel::redisplayObjects(
   const std::set<std::shared_ptr<ModelAPI_Object> >& theObjects,
   const bool isToFlushRedisplay)
 {
+  if (theObjects.empty())
+    return false;
+
   bool isModified = false;
-  XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(myWorkshop)->displayer();
   static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
-
   for (std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(); anIt != theObjects.end();
        anIt++)
   {
     ObjectPtr anObject = *anIt;
-    if (!anObject->isDisplayed()) {
-      // if the object was hidden by this panel
-      if (myHiddenObjects.find(anObject) != myHiddenObjects.end())
-        myHiddenObjects.erase(anObject);
-      anObject->setDisplayed(true); // it means that the object is hidden by hide all faces
-      ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
-      isModified = true;
-      //isModified = aDisplayer->display(anObject, false) || isModified;
-    }
-    else {
-      ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
-      isModified = true;
-    }
+    if (!anObject->isDisplayed())
+      continue;
+    ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
+    isModified = true;
   }
-  if (isToFlushRedisplay)
+  if (isModified && isToFlushRedisplay)
     Events_Loop::loop()->flush(aDispEvent);
   return isModified;
 }
 
 //********************************************************************
-bool XGUI_FacesPanel::hideFace(const int theIndex)
+bool XGUI_FacesPanel::displayHiddenObjects(
+  const std::set<std::shared_ptr<ModelAPI_Object> >& theObjects,
+  std::set<std::shared_ptr<ModelAPI_Object> >& theHiddenObjects,
+  const bool isToFlushRedisplay)
 {
-  XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(myWorkshop)->displayer();
-
-  if (!myItems.contains(theIndex))
+  if (theObjects.empty())
     return false;
 
-  ModuleBase_ViewerPrsPtr aPrs = myItems[theIndex];
-
-  AISObjectPtr aAISObj = aDisplayer->getAISObject(aPrs->object());
-  if (aAISObj.get() == NULL)
-    return false;
-  Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
-    aAISObj->impl<Handle(AIS_InteractiveObject)>());
-  if (aResultPrs.IsNull())
-    return false;
-  // set shape hidden to check whether the presentation should be erased from the viewer
   bool isModified = false;
-  if (aResultPrs->hasSubShapeVisible(ModuleBase_Tools::getSelectedShape(aPrs)))
-    isModified = aDisplayer->redisplay(aPrs->object(), false) || isModified;
-  else
+  static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
+
+  for (std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(); anIt != theObjects.end();
+       anIt++)
   {
-    ObjectPtr anObject = aPrs->object();
-    myHiddenObjects.insert(anObject);
-    static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
-    anObject->setDisplayed(false);
-    isModified = aDisplayer->erase(anObject, false) || isModified;
+    ObjectPtr anObject = *anIt;
+    // if the object was hidden by this panel
+    if (anObject->isDisplayed() || theHiddenObjects.find(anObject) == theHiddenObjects.end())
+      continue;
+    theHiddenObjects.erase(anObject);
+    anObject->setDisplayed(true); // it means that the object is hidden by hide all faces
     ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
-    Events_Loop::loop()->flush(aDispEvent);
+    isModified = true;
   }
+
+  if (isModified && isToFlushRedisplay)
+    Events_Loop::loop()->flush(aDispEvent);
   return isModified;
 }
 
 //********************************************************************
-void XGUI_FacesPanel::closeEvent(QCloseEvent* theEvent)
+bool XGUI_FacesPanel::hideEmptyObjects()
 {
-  QDockWidget::closeEvent(theEvent);
-  emit closed();
+  bool isModified = false;
+  static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
+  std::map<ObjectPtr, NCollection_List<TopoDS_Shape> > anObjectToShapes;
+  std::map<ObjectPtr, Handle(ModuleBase_ResultPrs) > anObjectToPrs;
+
+  for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anIt = myItems.begin();
+       anIt != myItems.end(); anIt++) {
+    ModuleBase_ViewerPrsPtr aPrs = anIt.value();
+    ObjectPtr anObject = aPrs->object();
+    if (!anObject.get() || !anObject->isDisplayed())
+      continue;
+
+    Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
+      aPrs->interactive());
+    if (aResultPrs.IsNull())
+      continue;
+
+    if (anObjectToShapes.find(anObject) != anObjectToShapes.end())
+      anObjectToShapes.at(anObject).Append(ModuleBase_Tools::getSelectedShape(aPrs));
+    else {
+      NCollection_List<TopoDS_Shape> aListOfShapes;
+      aListOfShapes.Append(ModuleBase_Tools::getSelectedShape(aPrs));
+      anObjectToShapes[anObject] = aListOfShapes;
+      anObjectToPrs[anObject] = aResultPrs;
+    }
+  }
+  for (std::map<ObjectPtr, NCollection_List<TopoDS_Shape> >::const_iterator
+    anIt = anObjectToShapes.begin(); anIt != anObjectToShapes.end(); anIt++) {
+    ObjectPtr anObject = anIt->first;
+    if (!anObject.get() || anObjectToPrs.find(anObject) == anObjectToPrs.end())
+      continue;
+    Handle(ModuleBase_ResultPrs) aResultPrs = anObjectToPrs.at(anObject);
+
+    if (!aResultPrs->hasSubShapeVisible(anIt->second)) {
+      // erase object because it is entirely hidden
+      anObject->setDisplayed(false);
+      myHiddenObjects.insert(anObject);
+      ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
+      isModified = true;
+    }
+  }
+  return isModified;
 }
 
 //********************************************************************
-void XGUI_FacesPanel::activateSelection(bool toActivate)
+void XGUI_FacesPanel::updateProcessedObjects(QMap<int, ModuleBase_ViewerPrsPtr> theItems,
+                                             std::set<ObjectPtr>& theObjects)
 {
-  QIntList aShapeTypes;
-  aShapeTypes.append(TopAbs_FACE);
-
-  if (toActivate) {
-    myWorkshop->activateSubShapesSelection(aShapeTypes);
-  } else {
-    myWorkshop->deactivateSubShapesSelection();
+  theObjects.clear();
+  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() && theObjects.find(anObject) != theObjects.end())
+      continue;
+    theObjects.insert(anObject);
   }
-  if (toActivate)
-    activateSelectionFilters();
-  else
-    deactivateSelectionFilters();
+}
+
+//********************************************************************
+void XGUI_FacesPanel::closeEvent(QCloseEvent* theEvent)
+{
+  QDockWidget::closeEvent(theEvent);
+  emit closed();
 }
 
 //********************************************************************
@@ -357,19 +455,16 @@ QString XGUI_FacesPanel::generateName(const ModuleBase_ViewerPrsPtr& thePrs)
 }
 
 //********************************************************************
-bool XGUI_FacesPanel::customizeObject(const ObjectPtr& theObject, const bool isDisplayed)
+bool XGUI_FacesPanel::customizeObject(const ObjectPtr& theObject,
+                                      const AISObjectPtr& thePresentation)
 {
-  if (isDisplayed && myItems.isEmpty())
+  if (myLastItemIndex == 0) // do nothing because there was no activity in the pane after reset
     return false;
 
-  XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(myWorkshop)->displayer();
-
-  AISObjectPtr aAISObj = aDisplayer->getAISObject(theObject);
-  if (aAISObj.get() == NULL)
+  if (thePresentation.get() == NULL)
     return false;
-  Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
-    aAISObj->impl<Handle(AIS_InteractiveObject)>());
-  if (aResultPrs.IsNull())
+
+  if (myItemObjects.find(theObject) == myItemObjects.end()) // not found
     return false;
 
   // if the object is displayed, the hidden faces are collected and set to the presentation
@@ -384,10 +479,17 @@ bool XGUI_FacesPanel::customizeObject(const ObjectPtr& theObject, const bool isD
     if (!aHiddenSubShapes.Contains(aShape))
       aHiddenSubShapes.Append(aShape);
   }
+
+  Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
+    thePresentation->impl<Handle(AIS_InteractiveObject)>());
+  if (aResultPrs.IsNull())
+    return false;
+
   isModified = aResultPrs->setSubShapeHidden(aHiddenSubShapes);
+
   double aTransparency = !useTransparency() ? 1
     : Config_PropManager::real("Visualization", "hidden_face_transparency");
-  aResultPrs->setHiddenSubShapeTransparency(aTransparency);
+  isModified = aResultPrs->setHiddenSubShapeTransparency(aTransparency) || isModified;
 
   return isModified;
 }
@@ -398,8 +500,26 @@ void XGUI_FacesPanel::onDeleteItem()
   processDelete();
 }
 
+//********************************************************************
+void XGUI_FacesPanel::onTransparencyChanged()
+{
+  bool isModified = false;
+  if (useTransparency()) {
+    std::set<std::shared_ptr<ModelAPI_Object> > aHiddenObjects = myHiddenObjects;
+    isModified = displayHiddenObjects(aHiddenObjects, myHiddenObjects, false);
+  }
+  else
+    isModified = hideEmptyObjects();
+
+  isModified = redisplayObjects(myItemObjects, false) || isModified;
+  if (isModified)
+    Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
+
+}
+
 //********************************************************************
 void XGUI_FacesPanel::onClosed()
 {
+  setActivePanel(false);
   reset(true);
 }