Salome HOME
Merge branch 'Dev_2.1.0' of salome:modules/shaper into Dev_2.1.0
[modules/shaper.git] / src / PartSet / PartSet_CustomPrs.cpp
index 2990969e94f9d4c6355deb77b5b44c6aadcc44b1..b2315bffb8d578dffb406e9742b6241f3be9e72d 100755 (executable)
@@ -5,6 +5,8 @@
 // Author:      Natalia ERMOLAEVA
 
 #include <PartSet_CustomPrs.h>
+#include <PartSet_Module.h>
+#include "PartSet_OperationPrs.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 <Config_PropManager.h>
 
 #include <AIS_InteractiveContext.hxx>
+#include <AIS_InteractiveObject.hxx>
+#include <Prs3d_PointAspect.hxx>
+
+//#define DO_NOT_VISUALIZE_CUSTOM_PRESENTATION
 
 #define OPERATION_PARAMETER_COLOR "255, 255, 0"
 
 PartSet_CustomPrs::PartSet_CustomPrs(ModuleBase_IWorkshop* theWorkshop)
-  : myWorkshop(theWorkshop)
+  : myWorkshop(theWorkshop), myIsActive(false)
 {
-  Handle(PartSet_OperationPrs) myOperationPrs = new PartSet_OperationPrs(); /// AIS presentation for the feature of operation
+  initPrs();
 }
 
-void PartSet_CustomPrs::setCustomized(const FeaturePtr& theFeature)
+bool PartSet_CustomPrs::isActive()
 {
+  return myIsActive;
+  /*Handle(PartSet_OperationPrs) anOperationPrs = getPresentation();
+  Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
 
-  myOperationPrs->setFeature(theFeature);
-  /*  QMap<ResultPtr, QList<GeomShapePtr> > aNewCustomized;
+  return !aContext.IsNull() && aContext->IsDisplayed(anOperationPrs);*/
+}
 
-  QList<GeomShapePtr> aShapeList;
-  ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
-  if (aResult.get()) {
-    aNewCustomized[aResult] = aShapeList;
-  }
-  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;
-            }
-          }
-        }
-      }
+bool PartSet_CustomPrs::activate(const FeaturePtr& theFeature, const bool theUpdateViewer)
+{
+#ifdef DO_NOT_VISUALIZE_CUSTOM_PRESENTATION
+  return false;
+#endif
+
+  bool isModified = false;
+  Handle(PartSet_OperationPrs) anOperationPrs = getPresentation();
+
+  if (anOperationPrs->canActivate(theFeature)) {
+    myIsActive = true;
+    anOperationPrs->setFeature(theFeature);
+    if (theFeature.get()) {
+      displayPresentation(theUpdateViewer);
+      isModified = true;
     }
   }
+  return isModified;
+}
+
+bool PartSet_CustomPrs::deactivate(const bool theUpdateViewer)
+{
+  myIsActive = false;
+  bool isModified = false;
+
+  Handle(PartSet_OperationPrs) anOperationPrs = getPresentation();
+  anOperationPrs->setFeature(FeaturePtr());
 
-  bool isDone = false;
-  XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
-  XGUI_Workshop* aWorkshop = aConnector->workshop();
-  XGUI_Displayer* aDisplayer = aWorkshop->displayer();
   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)) {
+    erasePresentation(theUpdateViewer);
+    isModified = true;
   }
 
-  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);
+  return isModified;
+}
+
+
+void PartSet_CustomPrs::displayPresentation(const bool theUpdateViewer)
+{
+  Handle(PartSet_OperationPrs) anOperationPrs = getPresentation();
+
+  Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
+  if (!aContext.IsNull() && !aContext->IsDisplayed(anOperationPrs)) {
+    if (anOperationPrs->hasShapes()) {
+      PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
+      XGUI_Workshop* aWorkshop = workshop();
+      aWorkshop->displayer()->displayAIS(myOperationPrs, false/*load object in selection*/, theUpdateViewer);
+      aContext->SetZLayer(anOperationPrs, aModule->getVisualLayerId());
     }
-    isDone = aDisplayer->customizeObject(aResult);
   }
-
-  // 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;
+  else {
+    if (!anOperationPrs->hasShapes())
+      erasePresentation(theUpdateViewer);
+    else {
+      anOperationPrs->Redisplay();
+      if (theUpdateViewer)
+        workshop()->displayer()->updateViewer();
+    }
   }
-  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)
+void PartSet_CustomPrs::erasePresentation(const bool theUpdateViewer)
 {
-  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);
-        }
-      }
+  XGUI_Workshop* aWorkshop = workshop();
+  aWorkshop->displayer()->eraseAIS(myOperationPrs, theUpdateViewer);
+}
+
+Handle(PartSet_OperationPrs) PartSet_CustomPrs::getPresentation()
+{
+  if (!myOperationPrs.get())
+    initPrs();
+  Handle(AIS_InteractiveObject) anAISIO = myOperationPrs->impl<Handle(AIS_InteractiveObject)>();
+  return Handle(PartSet_OperationPrs)::DownCast(anAISIO);
+}
+
+bool PartSet_CustomPrs::redisplay(const ObjectPtr& theObject, const bool theUpdateViewer)
+{
+#ifdef DO_NOT_VISUALIZE_CUSTOM_PRESENTATION
+  return false;
+#endif
+
+  bool isModified = false;
+  // the presentation should be recomputed if the previous AIS depend on the result
+  // [it should be hiddend] or the new AIS depend on it [it should be visualized]
+  Handle(PartSet_OperationPrs) anOperationPrs = getPresentation();
+  Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
+  if (!aContext.IsNull()) {
+    if (aContext->IsDisplayed(anOperationPrs)) {
+      // if there are performance poblems, to improve them, the necessity of redisplay can be checked
+      //bool aChanged = anOperationPrs->dependOn(theObject);
+      anOperationPrs->updateShapes();
+      //aChanged = aChanged || anOperationPrs->dependOn(theObject);
+      //if (aChanged)
+      anOperationPrs->Redisplay();
+      isModified = true;
+      if (theUpdateViewer)
+        workshop()->displayer()->updateViewer();
+    }
+    else {
+      anOperationPrs->updateShapes();
+      displayPresentation(theUpdateViewer);
     }
-    thePrs->setPointMarker(5, 5.); // Set point as a '+' symbol*+/
-  }
-  /*
-  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();
   }
-  return !aColor.empty() && thePrs->setColor(aColor[0], aColor[1], aColor[2]);*/
-  return isDone;
+  return isModified;
+}
+
+void PartSet_CustomPrs::clearPrs()
+{
+  Handle(PartSet_OperationPrs) anOperationPrs = getPresentation();
+  if (!anOperationPrs.IsNull())
+    anOperationPrs.Nullify();
+
+  myOperationPrs.reset();
+}
+
+void PartSet_CustomPrs::initPrs()
+{
+  myOperationPrs = AISObjectPtr(new GeomAPI_AISObject());
+  myOperationPrs->setImpl(new Handle(AIS_InteractiveObject)(new PartSet_OperationPrs(myWorkshop)));
+
+  std::vector<int> aColor = Config_PropManager::color("Visualization", "operation_parameter_color",
+                                                      OPERATION_PARAMETER_COLOR);
+  myOperationPrs->setColor(aColor[0], aColor[1], aColor[2]);
+
+  myOperationPrs->setPointMarker(5, 2.);
+  myOperationPrs->setWidth(1);
+}
+
+XGUI_Workshop* PartSet_CustomPrs::workshop() const
+{
+  XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
+  return aConnector->workshop();
 }