Salome HOME
Merge branch 'master' of newgeom:newgeom.git
[modules/shaper.git] / src / XGUI / XGUI_Displayer.cpp
index a01a311291b73009ab8556a5441caf7aa0e83f06..e013befb7b2d0a48f4fcfd5d341c6214771eebcb 100644 (file)
@@ -6,15 +6,27 @@
 #include "XGUI_Viewer.h"
 #include "XGUI_Workshop.h"
 #include "XGUI_ViewerProxy.h"
+#include "ModuleBase_Tools.h"
 
 #include <ModelAPI_Document.h>
+#include <ModelAPI_Data.h>
+#include <ModelAPI_Object.h>
+
+#include <GeomAPI_Shape.h>
+#include <GeomAPI_IPresentable.h>
 
 #include <AIS_InteractiveContext.hxx>
+#include <AIS_LocalContext.hxx>
 #include <AIS_ListOfInteractive.hxx>
 #include <AIS_ListIteratorOfListOfInteractive.hxx>
+#include <AIS_DimensionSelectionMode.hxx>
 
 #include <AIS_Shape.hxx>
 
+#include <set>
+
+const int MOUSE_SENSITIVITY_IN_PIXEL = 10; ///< defines the local context mouse selection sensitivity
+
 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
 {
   myWorkshop = theWorkshop;
@@ -24,124 +36,215 @@ XGUI_Displayer::~XGUI_Displayer()
 {
 }
 
-bool XGUI_Displayer::IsVisible(boost::shared_ptr<ModelAPI_Feature> theFeature)
+bool XGUI_Displayer::isVisible(ObjectPtr theObject)
 {
-  return myFeature2AISObjectMap.find(theFeature) != myFeature2AISObjectMap.end();
+  return myResult2AISObjectMap.find(theObject) != myResult2AISObjectMap.end();
 }
 
-void XGUI_Displayer::Display(boost::shared_ptr<ModelAPI_Feature> theFeature,
-                             const bool isUpdateViewer)
+void XGUI_Displayer::display(ObjectPtr theObject, bool isUpdateViewer)
 {
+  if (isVisible(theObject)) {
+    redisplay(theObject, isUpdateViewer);
+  } else {
+    boost::shared_ptr<GeomAPI_AISObject> anAIS;
+
+    ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theObject);
+    if (aResult) {
+      boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModuleBase_Tools::shape(aResult);
+      if (aShapePtr) {
+        anAIS = boost::shared_ptr<GeomAPI_AISObject>(new GeomAPI_AISObject());
+        anAIS->createShape(aShapePtr);
+      }
+    } else {
+      GeomPresentablePtr aPrs = boost::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
+      if (aPrs) {
+        anAIS = aPrs->getAISObject(boost::shared_ptr<GeomAPI_AISObject>());
+      }
+    }
+    if (anAIS)
+      display(theObject, anAIS, isUpdateViewer);
+  }
 }
 
-void XGUI_Displayer::Display(boost::shared_ptr<ModelAPI_Feature> theFeature,
-                             const TopoDS_Shape& theShape, const bool isUpdateViewer)
+void XGUI_Displayer::display(ObjectPtr theObject,
+                             boost::shared_ptr<GeomAPI_AISObject> theAIS, bool isUpdateViewer)
 {
   Handle(AIS_InteractiveContext) aContext = AISContext();
 
-  Handle(AIS_Shape) anAIS = new AIS_Shape(theShape);
-  myFeature2AISObjectMap[theFeature] = anAIS;
-
-  aContext->Display(anAIS, Standard_False);
-  if (isUpdateViewer)
-    aContext->UpdateCurrentViewer();
+  Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
+  if (!anAISIO.IsNull()) {
+    myResult2AISObjectMap[theObject] = theAIS;
+    aContext->Display(anAISIO, isUpdateViewer);
+  }
 }
 
-boost::shared_ptr<ModelAPI_Feature> XGUI_Displayer::GetFeature(const TopoDS_Shape& theShape)
+
+
+void XGUI_Displayer::erase(ObjectPtr theObject, const bool isUpdateViewer)
 {
-  boost::shared_ptr<ModelAPI_Feature> aFeature;
+  if (!isVisible(theObject)) return;
 
-  FeatureToAISMap::const_iterator aFIt = myFeature2AISObjectMap.begin(),
-                                  aFLast = myFeature2AISObjectMap.end();
-  for (; aFIt != aFLast && !aFeature; aFIt++)
-  {
-    Handle(AIS_InteractiveObject) anAIS = (*aFIt).second;
-    Handle(AIS_Shape) anAISShape = Handle(AIS_Shape)::DownCast(anAIS);
-    if (!anAISShape.IsNull() && anAISShape->Shape() == theShape) {
-      aFeature = (*aFIt).first;
+  Handle(AIS_InteractiveContext) aContext = AISContext();
+  boost::shared_ptr<GeomAPI_AISObject> anObject = myResult2AISObjectMap[theObject];
+  if (anObject)  {
+    Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
+    if (!anAIS.IsNull()) {
+      aContext->Erase(anAIS, isUpdateViewer);
+      
     }
   }
-  return aFeature;
+  myResult2AISObjectMap.erase(theObject);
 }
 
-std::list<XGUI_ViewerPrs> XGUI_Displayer::GetViewerPrs
-                                                (const NCollection_List<TopoDS_Shape>& theShapes)
-{
-  std::list<XGUI_ViewerPrs> aPresentations;
-  if (theShapes.IsEmpty())
-    return aPresentations;
 
-  NCollection_List<TopoDS_Shape>::Iterator anIt(theShapes);
-  for (; anIt.More(); anIt.Next()) {
-    const TopoDS_Shape& aShape = anIt.Value();
-    if (aShape.IsNull())
-      continue;
-    boost::shared_ptr<ModelAPI_Feature> aFeature = GetFeature(aShape);
-    aPresentations.push_back(XGUI_ViewerPrs(aFeature, aShape));
+/*bool XGUI_Displayer::redisplay(ObjectPtr theObject,
+                               boost::shared_ptr<GeomAPI_AISObject> theAIS,
+                               const bool isUpdateViewer)
+{
+  bool isCreated = false;
+  Handle(AIS_InteractiveObject) anAIS = 
+    theAIS ? theAIS->impl<Handle(AIS_InteractiveObject)>() : Handle(AIS_InteractiveObject)();
+  Handle(AIS_InteractiveContext) aContext = AISContext();
+  // Open local context if there is no one
+  if (!aContext->HasOpenedContext()) {
+    aContext->ClearCurrents(false);
+    aContext->OpenLocalContext(false /use displayed objects/, true /allow shape decomposition/);
+    // set mouse sensitivity
+    //aContext->SetSensitivityMode(StdSelect_SM_WINDOW);
+    //aContext->SetPixelTolerance(MOUSE_SENSITIVITY_IN_PIXEL);
   }
+  // display or redisplay presentation
+  boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[theObject];
+  if (isVisible(theObject) && anObj && !anObj->empty()) {
+    aContext->Redisplay(anAIS, isUpdateViewer);
+    //aContext->RecomputeSelectionOnly(anAIS);
+  }
+  else {
+    myResult2AISObjectMap[theObject] = theAIS;
+    aContext->Display(anAIS, isUpdateViewer);
+    isCreated = true;
+  }
+  return isCreated;
+}*/
 
-  return aPresentations;
-}
-
-void XGUI_Displayer::Erase(boost::shared_ptr<ModelAPI_Feature> theFeature,
-                           const bool isUpdateViewer)
+void XGUI_Displayer::redisplay(ObjectPtr theObject, bool isUpdateViewer)
 {
-  if (myFeature2AISObjectMap.find(theFeature) == myFeature2AISObjectMap.end())
+  if (!isVisible(theObject))
     return;
 
-  Handle(AIS_InteractiveContext) aContext = AISContext();
-  Handle(AIS_InteractiveObject) anAIS = myFeature2AISObjectMap[theFeature];
-  Handle(AIS_Shape) anAISShape = Handle(AIS_Shape)::DownCast(anAIS);
-  if (!anAISShape.IsNull())
-  {
-    aContext->Erase(anAISShape);
+  ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theObject);
+  if (aResult) {
+    boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModuleBase_Tools::shape(aResult);
+    if (aShapePtr) {
+      boost::shared_ptr<GeomAPI_AISObject> aAISObj = getAISObject(theObject);
+      Handle(AIS_Shape) aAISShape = Handle(AIS_Shape)::DownCast(aAISObj->impl<Handle(AIS_InteractiveObject)>());
+      if (!aAISShape.IsNull()) {
+        aAISShape->Set(aShapePtr->impl<TopoDS_Shape>());
+        AISContext()->Redisplay(aAISShape, isUpdateViewer);
+      }
+    }
+  } else {
+    GeomPresentablePtr aPrs = boost::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
+    if (aPrs) {
+      boost::shared_ptr<GeomAPI_AISObject> aAISObj = aPrs->getAISObject(getAISObject(theObject));
+      Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
+      AISContext()->Redisplay(aAISIO, isUpdateViewer);
+    }
   }
-  myFeature2AISObjectMap.erase(theFeature);
-
-  if (isUpdateViewer)
-    aContext->UpdateCurrentViewer();
 }
 
-void XGUI_Displayer::RedisplayInLocalContext(boost::shared_ptr<ModelAPI_Feature> theFeature,
-                                             const TopoDS_Shape& theShape,
-                                             const std::list<int>& theModes, const bool isUpdateViewer)
+void XGUI_Displayer::activateInLocalContext(ResultPtr theResult,
+                                         const std::list<int>& theModes, const bool isUpdateViewer)
 {
   Handle(AIS_InteractiveContext) aContext = AISContext();
   // Open local context if there is no one
   if (!aContext->HasOpenedContext()) {
     aContext->ClearCurrents(false);
-    aContext->OpenLocalContext(false/*use displayed objects*/, /*true*/false/*use displayed objects*/,
-                         true/*allow shape decomposition*/);
+    aContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
   }
   // display or redisplay presentation
-  Handle(AIS_Shape) anAIS;
-  if (IsVisible(theFeature)) {
-    anAIS = Handle(AIS_Shape)::DownCast(myFeature2AISObjectMap[theFeature]);
-    if (!anAIS.IsNull()) {
-      anAIS->Set(theShape);
-      anAIS->Redisplay();
-    }
-  }
-  else {
-    anAIS = new AIS_Shape(theShape);
-    myFeature2AISObjectMap[theFeature] = anAIS;
-    aContext->Display(anAIS, false);
+  Handle(AIS_InteractiveObject) anAIS;
+  if (isVisible(theResult))
+  {
+    boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[theResult];
+    if (anObj)
+      anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
   }
+
   // Activate selection of objects from prs
   if (!anAIS.IsNull()) {
     aContext->Load(anAIS, -1, true/*allow decomposition*/);
+    aContext->Deactivate(anAIS);
+
     std::list<int>::const_iterator anIt = theModes.begin(), aLast = theModes.end();
     for (; anIt != aLast; anIt++)
     {
-      aContext->Activate(anAIS, AIS_Shape::SelectionMode((TopAbs_ShapeEnum)*anIt));
+      aContext->Activate(anAIS, (*anIt));
     }
   }
 
   if (isUpdateViewer)
-    aContext->UpdateCurrentViewer();
+    updateViewer();
 }
 
-void XGUI_Displayer::EraseAll(const bool isUpdateViewer)
+void XGUI_Displayer::stopSelection(const QResultList& theResults, const bool isStop,
+                                   const bool isUpdateViewer)
+{
+  Handle(AIS_InteractiveContext) aContext = AISContext();
+
+  Handle(AIS_Shape) anAIS;
+  QResultList::const_iterator anIt = theResults.begin(), aLast = theResults.end();
+  ResultPtr aFeature;
+  for (; anIt != aLast; anIt++) {
+    aFeature = *anIt;
+    if (isVisible(aFeature))
+      anAIS = Handle(AIS_Shape)::DownCast(myResult2AISObjectMap[aFeature]->impl<Handle(AIS_InteractiveObject)>());
+    if (anAIS.IsNull())
+      continue;
+
+    if (isStop) {
+      QColor aColor(Qt::white);
+      anAIS->SetColor(Quantity_Color(aColor.red()/255., aColor.green()/255., aColor.blue()/255., Quantity_TOC_RGB));
+      anAIS->Redisplay();
+    }
+    else {
+      QColor aColor(Qt::red);
+      anAIS->SetColor(Quantity_Color(aColor.red()/255., aColor.green()/255., aColor.blue()/255., Quantity_TOC_RGB));
+      anAIS->Redisplay();
+    }
+  }
+  if (isUpdateViewer)
+    updateViewer();
+}
+
+void XGUI_Displayer::setSelected(const QResultList& theResults, const bool isUpdateViewer)
+{
+  Handle(AIS_InteractiveContext) aContext = AISContext();
+  // we need to unhighligth objects manually in the current local context
+  // in couple with the selection clear (TODO)
+  Handle(AIS_LocalContext) aLocalContext = aContext->LocalContext();
+  if (!aLocalContext.IsNull())
+    aLocalContext->UnhilightLastDetected(myWorkshop->viewer()->activeView());
+
+  aContext->ClearSelected();
+  foreach(ResultPtr aResult, theResults) {
+    if (myResult2AISObjectMap.find(aResult) == myResult2AISObjectMap.end()) 
+      return;
+
+    boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[aResult];
+    if (anObj)
+    {
+      Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
+      if (!anAIS.IsNull())
+        aContext->AddOrRemoveSelected(anAIS, false);
+    }
+  }
+  if (isUpdateViewer)
+    updateViewer();
+}
+
+
+/*void XGUI_Displayer::EraseAll(const bool isUpdateViewer)
 {
   Handle(AIS_InteractiveContext) ic = AISContext();
 
@@ -156,55 +259,81 @@ void XGUI_Displayer::EraseAll(const bool isUpdateViewer)
     Handle(AIS_InteractiveObject) anIO = anIter.Value();
     ic->Erase(anIO, false);
   }
-  myFeature2AISObjectMap.clear();
+  myResult2AISObjectMap.clear();
   if (isUpdateViewer)
-    ic->UpdateCurrentViewer();
-}
+    updateViewer();
+}*/
 
-void XGUI_Displayer::EraseDeletedFeatures(const bool isUpdateViewer)
+void XGUI_Displayer::eraseDeletedResults(const bool isUpdateViewer)
 {
   Handle(AIS_InteractiveContext) aContext = AISContext();
 
-  FeatureToAISMap::const_iterator aFIt = myFeature2AISObjectMap.begin(),
-                                  aFLast = myFeature2AISObjectMap.end();
-  std::list<boost::shared_ptr<ModelAPI_Feature>> aRemoved;
+  ResultToAISMap::const_iterator aFIt = myResult2AISObjectMap.begin(),
+                                 aFLast = myResult2AISObjectMap.end();
+  std::list<ObjectPtr> aRemoved;
   for (; aFIt != aFLast; aFIt++)
   {
-    boost::shared_ptr<ModelAPI_Feature> aFeature = (*aFIt).first;
-    if (!aFeature) {
-      Handle(AIS_InteractiveObject) anAIS = (*aFIt).second;
+    ObjectPtr aFeature = (*aFIt).first;
+    if (!aFeature || !aFeature->data() || !aFeature->data()->isValid()) {
+      boost::shared_ptr<GeomAPI_AISObject> anObj = (*aFIt).second;
+      if (!anObj) continue;
+      Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
       if (!anAIS.IsNull()) {
         aContext->Erase(anAIS, false);
         aRemoved.push_back(aFeature);
       }
     }
   }
-  std::list<boost::shared_ptr<ModelAPI_Feature>>::const_iterator anIt = aRemoved.begin(),
+  std::list<ObjectPtr>::const_iterator anIt = aRemoved.begin(),
                                                                  aLast = aRemoved.end();
   for (; anIt != aLast; anIt++) {
-    myFeature2AISObjectMap.erase(myFeature2AISObjectMap.find(*anIt));
+    myResult2AISObjectMap.erase(myResult2AISObjectMap.find(*anIt));
   }
 
   if (isUpdateViewer)
-    aContext->UpdateCurrentViewer();
+    updateViewer();
 }
 
-void XGUI_Displayer::CloseLocalContexts(const bool isUpdateViewer)
+void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
 {
   closeAllContexts(true);
 }
 
+boost::shared_ptr<GeomAPI_AISObject> XGUI_Displayer::getAISObject(ObjectPtr theObject) const
+{
+  boost::shared_ptr<GeomAPI_AISObject> anIO;
+  if (myResult2AISObjectMap.find(theObject) != myResult2AISObjectMap.end())
+    anIO = (myResult2AISObjectMap.find(theObject))->second;
+  return anIO;
+}
+
+ObjectPtr XGUI_Displayer::getObject(Handle(AIS_InteractiveObject) theIO) const
+{
+  ObjectPtr aFeature;
+  ResultToAISMap::const_iterator aFIt = myResult2AISObjectMap.begin(),
+                                 aFLast = myResult2AISObjectMap.end();
+  for (; aFIt != aFLast && !aFeature; aFIt++) {
+    boost::shared_ptr<GeomAPI_AISObject> anObj = (*aFIt).second;
+    if (!anObj) continue;
+    Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
+    if (anAIS != theIO)
+      continue;
+    aFeature = (*aFIt).first;
+  }
+  return aFeature;
+}
+
 void XGUI_Displayer::closeAllContexts(const bool isUpdateViewer)
 {
   Handle(AIS_InteractiveContext) ic = AISContext();
   if (!ic.IsNull()) {
     ic->CloseAllContexts(false);
     if (isUpdateViewer)
-      ic->UpdateCurrentViewer();
+      updateViewer();
   }
 }
 
-void XGUI_Displayer::UpdateViewer()
+void XGUI_Displayer::updateViewer()
 {
   Handle(AIS_InteractiveContext) ic = AISContext();
   if (!ic.IsNull())