Salome HOME
Task #2997: Add button "Show DOF"
[modules/shaper.git] / src / PartSet / PartSet_CustomPrs.cpp
old mode 100755 (executable)
new mode 100644 (file)
index 3ae8460..21465eb
@@ -1,10 +1,26 @@
-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
-
-// File:        PartSet_CustomPrs.cpp
-// Created:     30 Jun 2015
-// Author:      Natalia ERMOLAEVA
+// 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
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// 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
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
 
 #include <PartSet_CustomPrs.h>
+#include <PartSet_Module.h>
+#include "PartSet_OperationPrs.h"
+#include "PartSet_OverconstraintListener.h"
 
 #include <XGUI_ModuleConnector.h>
 #include <XGUI_Workshop.h>
 
 #include <ModuleBase_IWorkshop.h>
 #include <ModuleBase_IViewer.h>
-
-#include <GeomValidators_Tools.h>
-
-#include <ModelAPI_Attribute.h>
-#include <ModelAPI_AttributeSelectionList.h>
+#include <ModuleBase_Tools.h>
 
 #include <Config_PropManager.h>
+#include <Events_Loop.h>
+#include <ModelAPI_Events.h>
 
 #include <AIS_InteractiveContext.hxx>
+#include <AIS_InteractiveObject.hxx>
+#include <Prs3d_PointAspect.hxx>
 
-#define OPERATION_PARAMETER_COLOR "255, 255, 0"
+//#define DO_NOT_VISUALIZE_CUSTOM_PRESENTATION
 
 PartSet_CustomPrs::PartSet_CustomPrs(ModuleBase_IWorkshop* theWorkshop)
-  : myWorkshop(theWorkshop)
+  : myWorkshop(theWorkshop), myFeature(FeaturePtr()), myPresentationIsEmpty(false),
+  myDisabledMode(-1)
+{
+  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);
+
+  myIsActive[ModuleBase_IModule::CustomizeArguments] = false;
+  myIsActive[ModuleBase_IModule::CustomizeResults] = false;
+  myIsActive[ModuleBase_IModule::CustomizeHighlightedObjects] = false;
+}
+
+bool PartSet_CustomPrs::isActive(const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag)
 {
+  return myIsActive[theFlag];
 }
 
-void PartSet_CustomPrs::setCustomized(const ObjectPtr& theObject)
+bool PartSet_CustomPrs::activate(const FeaturePtr& theFeature,
+                                 const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
+                                 const bool theUpdateViewer)
 {
-/*  QMap<ResultPtr, QList<GeomShapePtr> > aNewCustomized;
+#ifdef DO_NOT_VISUALIZE_CUSTOM_PRESENTATION
+  return false;
+#endif
 
-  QList<GeomShapePtr> aShapeList;
-  ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
-  if (aResult.get()) {
-    aNewCustomized[aResult] = aShapeList;
+  myIsActive[theFlag] = true;
+  myFeature = theFeature;
+
+  bool isModified = false;
+  if (theFeature.get()) {
+    displayPresentation(theFlag, theUpdateViewer);
+    isModified = true;
   }
-  else {
-    FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
-    if (aFeature.get()) {
-      std::list<AttributePtr> anAttributes = aFeature->data()->attributes("");
-      std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
-      for (; anIt != aLast; anIt++) {
-        AttributePtr anAttribute = *anIt;
-        ObjectPtr anObject = GeomValidators_Tools::getObject(anAttribute);
-        if (anObject.get()) {
-          ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
-          if (aResult.get())
-            aNewCustomized[aResult] = aShapeList;
-        }
-        else if (anAttribute->attributeType() == ModelAPI_AttributeSelectionList::typeId()) {
-          std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList = 
-                  std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
-          for(int i = 0; i < aCurSelList->size(); i++) {
-            std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
-            ObjectPtr anObject = GeomValidators_Tools::getObject(aSelAttribute);
-            if (anObject.get()) {
-              ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
-              if (aResult.get())
-                aNewCustomized[aResult] = aShapeList;
-            }
-          }
-        }
-      }
-    }
+  return isModified;
+}
+
+bool PartSet_CustomPrs::deactivate(const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
+                                   const bool theUpdateViewer)
+{
+  myIsActive[theFlag] = false;
+  erasePresentation(theFlag, theUpdateViewer);
+  return true;
+}
+
+bool PartSet_CustomPrs::displayPresentation(
+                                  const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
+                                  const bool theUpdateViewer)
+{
+  bool isModified = false;
+
+  if (myDisabledMode == theFlag)
+    return isModified;
+
+  // update the AIS objects content
+  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, aFeatureShapes);
+      break;
+    case ModuleBase_IModule::CustomizeResults:
+      PartSet_OperationPrs::getResultShapes(myFeature, myWorkshop, aFeatureShapes);
+      PartSet_OperationPrs::getPresentationShapes(myFeature, myWorkshop, aFeatureShapes, false);
+      break;
+    case ModuleBase_IModule::CustomizeHighlightedObjects:
+      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);
 
-  bool isDone = false;
-  XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
-  XGUI_Workshop* aWorkshop = aConnector->workshop();
-  XGUI_Displayer* aDisplayer = aWorkshop->displayer();
+  myPresentationIsEmpty = false;
+  // redisplay AIS objects
+  bool aRedisplayed = false;
   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
-  // find objects which are not customized anymore
-  QMap<ResultPtr, QList<GeomShapePtr> > aNotCustomized;
-  QMap<ResultPtr, QList<GeomShapePtr> >::const_iterator anIt = myCustomized.begin(),
-                                                        aLast = myCustomized.end();
-  for (; anIt != aLast; anIt++) {
-    ResultPtr aResult = anIt.key();
-    if (!aNewCustomized.contains(aResult))
-      aNotCustomized[aResult] = aShapeList;
-  }
+  if (!aContext.IsNull() && !aContext->IsDisplayed(anOperationPrs)) {
+    // 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)) {
+      // set color here because it can be changed in preferences
+      Quantity_Color aShapeColor = getShapeColor(theFlag);
+      anOperationPrs->setShapeColor(aShapeColor);
 
-  myCustomized.clear();
-  // restore the previous state of the object if there is no such object in the new map
-  for (anIt = aNotCustomized.begin(), aLast = aNotCustomized.end(); anIt != aLast; anIt++) {
-    ResultPtr aResult = anIt.key();
-    AISObjectPtr anAISObj = aDisplayer->getAISObject(aResult);
-    if (anAISObj.get()) {
-      Handle(AIS_InteractiveObject) anAISIO = anAISObj->impl<Handle(AIS_InteractiveObject)>();
-      aContext->Redisplay(anAISIO, false);
+      PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
+      XGUI_Workshop* aWorkshop = workshop();
+      aRedisplayed = aWorkshop->displayer()->displayAIS(myPresentations[theFlag],
+                                         false/*load object in selection*/, 0, false);
+      aContext->SetZLayer(anOperationPrs, aModule->getVisualLayerId());
+      isModified = true;
     }
-    isDone = aDisplayer->customizeObject(aResult);
   }
+  else {
+    // 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)) {
+      aRedisplayed = erasePresentation(theFlag, false);
+      isModified = true;
+    }
+    else {
+      anOperationPrs->Redisplay();
+      isModified = true;
+      aRedisplayed = true;
+    }
+  }
+  if (myPresentationIsEmpty) {
+    aRedisplayed = erasePresentation(theFlag, false);
+  }
+  if (aRedisplayed && theUpdateViewer)
+    workshop()->displayer()->updateViewer();
 
-  // set customized for the new objects
-  myCustomized = aNewCustomized;
-  for (anIt = myCustomized.begin(), aLast = myCustomized.end(); anIt != aLast; anIt++) {
-    ResultPtr aResult = anIt.key();
-    AISObjectPtr anAISObj = aDisplayer->getAISObject(aResult);
-    if (anAISObj.get())
-      isDone = customisePresentation(aResult, anAISObj, 0) || isDone;
+  return isModified;
+}
+
+bool PartSet_CustomPrs::erasePresentation(
+                          const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
+                          const bool theUpdateViewer)
+{
+  bool isErased = false;
+  XGUI_Workshop* aWorkshop = workshop();
+  if (myPresentations.contains(theFlag))
+    isErased = aWorkshop->displayer()->eraseAIS(myPresentations[theFlag], theUpdateViewer);
+  return isErased;
+}
+
+void PartSet_CustomPrs::clearPresentation(
+  const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag)
+{
+  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);
   }
-  if (isDone)
-    aDisplayer->updateViewer();*/
 }
 
-/*#include <AIS_InteractiveObject.hxx>
-#include <AIS_Shape.hxx>
-#include <TopExp_Explorer.hxx>
-#include <TopoDS_Vertex.hxx>
-#include <StdPrs_ShadedShape.hxx>
-#include <StdPrs_WFDeflectionShape.hxx>*/
-bool PartSet_CustomPrs::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
-                                              std::shared_ptr<GeomAPI_ICustomPrs> theCustomPrs)
+AISObjectPtr PartSet_CustomPrs::getPresentation(
+                                       const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
+                                       const bool theToCreate)
 {
-  bool isDone = false;
-  if (myCustomized.contains(theResult)) {
-    std::vector<int> aColor = Config_PropManager::color("Visualization", "operation_parameter_color",
-                                                        OPERATION_PARAMETER_COLOR);
-    isDone = thePrs->setColor(aColor[0], aColor[1], aColor[2]);
-    /*
-    Handle(AIS_InteractiveObject) anAISIO = thePrs->impl<Handle(AIS_InteractiveObject)>();
-
-    const Handle(Prs3d_Presentation)& aPresentation = anAISIO->Presentation();
-    if (!aPresentation.IsNull()) {
-      Handle(AIS_Shape) aShapeAIS = Handle(AIS_Shape)::DownCast(anAISIO);
-      if (!aShapeAIS.IsNull()) {
-        TopExp_Explorer anExp(aShapeAIS->Shape(), TopAbs_VERTEX);
-        Handle(Prs3d_Drawer) aDrawer = aShapeAIS->Attributes();
-        for (; anExp.More(); anExp.Next()) {
-          const TopoDS_Vertex& aVertex = (const TopoDS_Vertex&)anExp.Current();
-          StdPrs_WFDeflectionShape::Add(aPresentation, aVertex, aDrawer);
-        }
-      }
-    }
-    thePrs->setPointMarker(5, 5.); // Set point as a '+' symbol*/
+  Handle(PartSet_OperationPrs) aPresentation;
+
+  AISObjectPtr anOperationPrs;
+  if (myPresentations.contains(theFlag))
+    anOperationPrs = myPresentations[theFlag];
+
+  if (!anOperationPrs.get() && theToCreate) {
+    initPresentation(theFlag);
+    anOperationPrs = myPresentations[theFlag];
+  }
+
+  return anOperationPrs;
+}
+
+bool PartSet_CustomPrs::redisplay(const ObjectPtr& theObject,
+                                  const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
+                                  const bool theUpdateViewer)
+{
+#ifdef DO_NOT_VISUALIZE_CUSTOM_PRESENTATION
+  return false;
+#endif
+  bool aRedisplayed = false;
+  if (myIsActive[theFlag])
+    aRedisplayed = displayPresentation(theFlag, theUpdateViewer);
+
+  return aRedisplayed;
+}
+
+void PartSet_CustomPrs::clearPrs()
+{
+  clearPresentation(ModuleBase_IModule::CustomizeArguments);
+  clearPresentation(ModuleBase_IModule::CustomizeResults);
+  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());
+  Handle(PartSet_OperationPrs) anAISPrs = new PartSet_OperationPrs(myWorkshop);
+  anOperationPrs->setImpl(new Handle(AIS_InteractiveObject)(anAISPrs));
+  if (theFlag == ModuleBase_IModule::CustomizeArguments ||
+      theFlag == ModuleBase_IModule::CustomizeResults) {
+    anOperationPrs->setPointMarker(5, 2.);
+    anOperationPrs->setWidth((theFlag == ModuleBase_IModule::CustomizeHighlightedObjects)? 2 : 1);
   }
-  /*
-  std::vector<int> aColor;
-
-  getResultColor(theResult, aColor);
-
-  SessionPtr aMgr = ModelAPI_Session::get();
-  if (aMgr->activeDocument() != theResult->document()) {
-    QColor aQColor(aColor[0], aColor[1], aColor[2]);
-    QColor aNewColor = QColor::fromHsvF(aQColor.hueF(), aQColor.saturationF()/3., aQColor.valueF());
-    aColor[0] = aNewColor.red();
-    aColor[1] = aNewColor.green();
-    aColor[2] = aNewColor.blue();
+  else if (theFlag == ModuleBase_IModule::CustomizeHighlightedObjects)
+    anAISPrs->useAISWidth();
+
+  if (anOperationPrs.get())
+    myPresentations[theFlag] = anOperationPrs;
+}
+
+Quantity_Color PartSet_CustomPrs::getShapeColor(
+                                  const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag)
+{
+  Quantity_Color aColor;
+  switch(theFlag) {
+    case ModuleBase_IModule::CustomizeArguments:
+      aColor = ModuleBase_Tools::color("Visualization", "operation_parameter_color");
+    break;
+    case ModuleBase_IModule::CustomizeResults:
+      aColor = ModuleBase_Tools::color("Visualization", "operation_result_color");
+    break;
+    case ModuleBase_IModule::CustomizeHighlightedObjects:
+      aColor = ModuleBase_Tools::color("Visualization", "operation_highlight_color");
+    break;
+    default:
+    break;
   }
-  return !aColor.empty() && thePrs->setColor(aColor[0], aColor[1], aColor[2]);*/
-  return isDone;
+  return aColor;
+}
+
+XGUI_Workshop* PartSet_CustomPrs::workshop() const
+{
+  XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
+  return aConnector->workshop();
 }