Salome HOME
#1624 Translation/Rotation/Mirror geometrical constraint are always visible
[modules/shaper.git] / src / PartSet / PartSet_CustomPrs.cpp
index fdb2a0f3b109478dbf225b25e81ea3004f78f18e..604e593c2d711f41d2dadf84d650090edde09517 100755 (executable)
@@ -7,6 +7,7 @@
 #include <PartSet_CustomPrs.h>
 #include <PartSet_Module.h>
 #include "PartSet_OperationPrs.h"
+#include "PartSet_OverconstraintListener.h"
 
 #include <XGUI_ModuleConnector.h>
 #include <XGUI_Workshop.h>
@@ -17,6 +18,8 @@
 #include <ModuleBase_Tools.h>
 
 #include <Config_PropManager.h>
+#include <Events_Loop.h>
+#include <ModelAPI_Events.h>
 
 #include <AIS_InteractiveContext.hxx>
 #include <AIS_InteractiveObject.hxx>
 //#define DO_NOT_VISUALIZE_CUSTOM_PRESENTATION
 
 PartSet_CustomPrs::PartSet_CustomPrs(ModuleBase_IWorkshop* theWorkshop)
-  : myWorkshop(theWorkshop), myFeature(FeaturePtr())
+  : myWorkshop(theWorkshop), myFeature(FeaturePtr()), myPresentationIsEmpty(false)
 {
+  Events_Loop* aLoop = Events_Loop::loop();
+  aLoop->registerListener(this, Events_Loop::eventByName(EVENT_EMPTY_OPERATION_PRESENTATION));
+
   initPresentation(ModuleBase_IModule::CustomizeArguments);
   initPresentation(ModuleBase_IModule::CustomizeResults);
   initPresentation(ModuleBase_IModule::CustomizeHighlightedObjects);
@@ -54,9 +60,7 @@ bool PartSet_CustomPrs::activate(const FeaturePtr& theFeature,
 
   bool isModified = false;
   if (theFeature.get()) {
-    displayPresentation(ModuleBase_IModule::CustomizeArguments, theUpdateViewer);
-    displayPresentation(ModuleBase_IModule::CustomizeResults, theUpdateViewer);
-    displayPresentation(ModuleBase_IModule::CustomizeHighlightedObjects, theUpdateViewer);
+    displayPresentation(theFlag, theUpdateViewer);
     isModified = true;
   }
   return isModified;
@@ -66,14 +70,8 @@ bool PartSet_CustomPrs::deactivate(const ModuleBase_IModule::ModuleBase_Customiz
                                    const bool theUpdateViewer)
 {
   myIsActive[theFlag] = false;
-  bool isModified = false;
-
-  erasePresentation(ModuleBase_IModule::CustomizeArguments, theUpdateViewer);
-  erasePresentation(ModuleBase_IModule::CustomizeResults, theUpdateViewer);
-  erasePresentation(ModuleBase_IModule::CustomizeHighlightedObjects, theUpdateViewer);
-  isModified = true;
-
-  return isModified;
+  erasePresentation(theFlag, theUpdateViewer);
+  return true;
 }
 
 bool PartSet_CustomPrs::displayPresentation(
@@ -83,26 +81,35 @@ bool PartSet_CustomPrs::displayPresentation(
   bool isModified = false;
 
   // update the AIS objects content
-  Handle(PartSet_OperationPrs) anOperationPrs = getPresentation(theFlag);
+  AISObjectPtr aPresentation = getPresentation(theFlag, true);
+  Handle(AIS_InteractiveObject) anAISIO = aPresentation->impl<Handle(AIS_InteractiveObject)>();
+  Handle(PartSet_OperationPrs) anOperationPrs = Handle(PartSet_OperationPrs)::DownCast(anAISIO);
+
   // do nothing if the feature can not be displayed [is moved from presentation, to be checked]
   if (!myFeature.get())
     return isModified;
 
+  QMap<ObjectPtr, QList<GeomShapePtr> > aFeatureShapes;
   switch (theFlag) {
     case ModuleBase_IModule::CustomizeArguments:
-      PartSet_OperationPrs::getFeatureShapes(myFeature, myWorkshop, anOperationPrs->featureShapes());
+      PartSet_OperationPrs::getFeatureShapes(myFeature, myWorkshop, aFeatureShapes);
       break;
     case ModuleBase_IModule::CustomizeResults:
-      PartSet_OperationPrs::getResultShapes(myFeature, myWorkshop, anOperationPrs->featureShapes());
+      PartSet_OperationPrs::getResultShapes(myFeature, myWorkshop, aFeatureShapes);
       break;
     case ModuleBase_IModule::CustomizeHighlightedObjects:
-      PartSet_OperationPrs::getHighlightedShapes(myWorkshop, anOperationPrs->featureShapes());
+      PartSet_OperationPrs::getHighlightedShapes(myWorkshop, aFeatureShapes);
       break;
     default:
       return isModified;
   }
+  NCollection_DataMap<TopoDS_Shape, Handle_AIS_InteractiveObject>& aShapeMap =
+                                                                 anOperationPrs->shapesMap();
+  PartSet_OperationPrs::fillShapeList(aFeatureShapes, myWorkshop, aShapeMap);
 
+  myPresentationIsEmpty = false;
   // redisplay AIS objects
+  bool aRedisplayed = false;
   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
   if (!aContext.IsNull() && !aContext->IsDisplayed(anOperationPrs)) {
     // when the feature can not be visualized in the module, the operation preview should not
@@ -114,8 +121,8 @@ bool PartSet_CustomPrs::displayPresentation(
 
       PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
       XGUI_Workshop* aWorkshop = workshop();
-      aWorkshop->displayer()->displayAIS(myPresentations[theFlag], false/*load object in selection*/,
-                                         theUpdateViewer);
+      aRedisplayed = aWorkshop->displayer()->displayAIS(myPresentations[theFlag],
+                                         false/*load object in selection*/, false);
       aContext->SetZLayer(anOperationPrs, aModule->getVisualLayerId());
       isModified = true;
     }
@@ -124,46 +131,64 @@ bool PartSet_CustomPrs::displayPresentation(
     // when the feature can not be visualized in the module, the operation preview should not
     // be visualized also
     if (!anOperationPrs->hasShapes() || !myWorkshop->module()->canDisplayObject(myFeature)) {
-      erasePresentation(theFlag, theUpdateViewer);
+      aRedisplayed = erasePresentation(theFlag, false);
       isModified = true;
     }
     else {
       anOperationPrs->Redisplay();
       isModified = true;
-      if (theUpdateViewer)
-        workshop()->displayer()->updateViewer();
+      aRedisplayed = true;
     }
   }
+  if (myPresentationIsEmpty) {
+    aRedisplayed = erasePresentation(theFlag, false);
+  }
+  if (aRedisplayed && theUpdateViewer)
+    workshop()->displayer()->updateViewer();
+
   return isModified;
 }
 
-void PartSet_CustomPrs::erasePresentation(const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
+bool PartSet_CustomPrs::erasePresentation(const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
                                           const bool theUpdateViewer)
 {
+  bool isErased = false;
   XGUI_Workshop* aWorkshop = workshop();
-  aWorkshop->displayer()->eraseAIS(myPresentations[theFlag], theUpdateViewer);
+  if (myPresentations.contains(theFlag))
+    isErased = aWorkshop->displayer()->eraseAIS(myPresentations[theFlag], theUpdateViewer);
+  return isErased;
 }
 
 void PartSet_CustomPrs::clearPresentation(const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag)
 {
-  Handle(PartSet_OperationPrs) anOperationPrs = getPresentation(theFlag);
-  if (!anOperationPrs.IsNull())
-    anOperationPrs.Nullify();
+  AISObjectPtr aPresentation = getPresentation(theFlag, false);
+  if (aPresentation.get()) {
+    Handle(AIS_InteractiveObject) anAISIO = aPresentation->impl<Handle(AIS_InteractiveObject)>();
+    Handle(PartSet_OperationPrs) anOperationPrs = Handle(PartSet_OperationPrs)::DownCast(anAISIO);
+
+    anOperationPrs->shapesMap().Clear();
+    if (!anOperationPrs.IsNull())
+      anOperationPrs.Nullify();
+    myPresentations.remove(theFlag);
+  }
 }
 
-Handle(PartSet_OperationPrs) PartSet_CustomPrs::getPresentation(
-                                         const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag)
+AISObjectPtr PartSet_CustomPrs::getPresentation(
+                                       const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
+                                       const bool theToCreate)
 {
   Handle(PartSet_OperationPrs) aPresentation;
 
-  if (myPresentations.contains(theFlag)) {
-    AISObjectPtr anOperationPrs = myPresentations[theFlag];
-    if (!anOperationPrs.get())
-      initPresentation(theFlag);
-    Handle(AIS_InteractiveObject) anAISIO = anOperationPrs->impl<Handle(AIS_InteractiveObject)>();
-    aPresentation = Handle(PartSet_OperationPrs)::DownCast(anAISIO);
+  AISObjectPtr anOperationPrs;
+  if (myPresentations.contains(theFlag))
+    anOperationPrs = myPresentations[theFlag];
+  
+  if (!anOperationPrs.get() && theToCreate) {
+    initPresentation(theFlag);
+    anOperationPrs = myPresentations[theFlag];
   }
-  return aPresentation;
+
+  return anOperationPrs;
 }
 
 bool PartSet_CustomPrs::redisplay(const ObjectPtr& theObject,
@@ -173,7 +198,11 @@ bool PartSet_CustomPrs::redisplay(const ObjectPtr& theObject,
 #ifdef DO_NOT_VISUALIZE_CUSTOM_PRESENTATION
   return false;
 #endif
-  return displayPresentation(theFlag, theUpdateViewer);
+  bool aRedisplayed = false;
+  if (myIsActive[theFlag])
+    aRedisplayed = displayPresentation(theFlag, theUpdateViewer);
+  
+  return aRedisplayed;
 }
 
 void PartSet_CustomPrs::clearPrs()
@@ -183,6 +212,12 @@ void PartSet_CustomPrs::clearPrs()
   clearPresentation(ModuleBase_IModule::CustomizeHighlightedObjects);
 }
 
+void PartSet_CustomPrs::processEvent(const std::shared_ptr<Events_Message>& theMessage)
+{
+  if (theMessage->eventID() == Events_Loop::eventByName(EVENT_EMPTY_OPERATION_PRESENTATION))
+    myPresentationIsEmpty = true; /// store state to analize it after display/erase is finished
+}
+
 void PartSet_CustomPrs::initPresentation(const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag)
 {
   AISObjectPtr anOperationPrs = AISObjectPtr(new GeomAPI_AISObject());