Salome HOME
Harmonisation of GUI.
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_Displayer.cxx
index a99292ae0a714423475286734f15a47418d14626..7614db2affce28d64f1c85c1fbaad28ce8d2a5e2 100644 (file)
 #include "HYDROGUI_Prs.h"
 #include "HYDROGUI_PrsImageDriver.h"
 #include "HYDROGUI_PrsPolylineDriver.h"
+#include "HYDROGUI_PrsZoneDriver.h"
 #include "HYDROGUI_Tool.h"
 
+#include <CurveCreator_Utils.hxx>
+
+#include <LightApp_Application.h>
+#include <SVTK_ViewWindow.h>
+#include <OCCViewer_ViewWindow.h>
+#include <OCCViewer_ViewPort3d.h>
+#include <SUIT_ViewManager.h>
+
+#include <vtkRenderWindowInteractor.h>
+#include <vtkRenderer.h>
+#include <vtkWorldPointPicker.h>
+#include <vtkCamera.h>
+
 #include <GraphicsView_Viewer.h>
 #include <GraphicsView_ViewPort.h>
 
+const double LOCAL_SELECTION_TOLERANCE = 0.0001;
+
 HYDROGUI_Displayer::HYDROGUI_Displayer( HYDROGUI_Module* theModule )
-: myModule( theModule )
+: HYDROGUI_AbstractDisplayer( theModule ),
+  myXPosition( -1 ), myYPosition( -1 ), myIsPositionSaved( false )
 {
 }
 
@@ -44,7 +61,7 @@ HYDROGUI_Displayer::~HYDROGUI_Displayer()
 void HYDROGUI_Displayer::SetToUpdate( const HYDROData_SequenceOfObjects& theObjs,
                                       const int theViewerId )
 {
-  GraphicsView_Viewer* aViewer = myModule->getViewer( theViewerId );
+  GraphicsView_Viewer* aViewer = module()->getViewer( theViewerId );
   if( !aViewer )
     return;
 
@@ -55,7 +72,7 @@ void HYDROGUI_Displayer::SetToUpdate( const HYDROData_SequenceOfObjects& theObjs
   GraphicsView_ObjectList anObjectList = aViewPort->getObjects();
   for( int i = 1, n = theObjs.Length(); i <= n; i++ )
   {
-    Handle(HYDROData_Object) anObj = theObjs.Value( i );
+    Handle(HYDROData_Entity) anObj = theObjs.Value( i );
     if( anObj.IsNull() )
       continue;
 
@@ -64,18 +81,9 @@ void HYDROGUI_Displayer::SetToUpdate( const HYDROData_SequenceOfObjects& theObjs
   }
 }
 
-void HYDROGUI_Displayer::UpdateAll( const int theViewerId,
-                                    const bool theIsInit,
-                                    const bool theIsForced )
-{
-  if( theIsInit )
-    EraseAll( theViewerId );
-  DisplayAll( theViewerId, theIsForced );
-}
-
 void HYDROGUI_Displayer::EraseAll( const int theViewerId )
 {
-  GraphicsView_Viewer* aViewer = myModule->getViewer( theViewerId );
+  GraphicsView_Viewer* aViewer = module()->getViewer( theViewerId );
   if( !aViewer )
     return;
 
@@ -94,47 +102,10 @@ void HYDROGUI_Displayer::EraseAll( const int theViewerId )
   }
 }
 
-void HYDROGUI_Displayer::DisplayAll( const int theViewerId,
-                                     const bool theIsForced )
-{
-  HYDROGUI_DataModel* aModel = (HYDROGUI_DataModel*)myModule->dataModel();
-  if( aModel ) 
-  {
-    HYDROData_SequenceOfObjects aSeq;
-    HYDROGUI_Tool::GetPrsSubObjects( aModel, theViewerId, aSeq );
-    Update( aSeq, theViewerId, theIsForced );
-  }
-}
-
-void HYDROGUI_Displayer::Update( const HYDROData_SequenceOfObjects& theObjs,
-                                 const int theViewerId,
-                                 const bool theIsForced )
-{
-  // First of all, kill all bad presentations
-  purgeObjects( theViewerId );
-
-  // Now dig in the data model
-  HYDROData_SequenceOfObjects anObjectsToErase, anObjectsToDisplay;
-
-  for( int i = 1, n = theObjs.Length(); i <= n; i++ )
-  {
-    const Handle(HYDROData_Object)& anObj = theObjs.Value( i );
-    if( anObj.IsNull() )
-      anObjectsToErase.Append( anObj );
-    else
-      anObjectsToDisplay.Append( anObj );
-  }
-
-  if( anObjectsToErase.Length() )
-    Erase( anObjectsToErase, theViewerId );
-  if( anObjectsToDisplay.Length() )
-    Display( anObjectsToDisplay, theViewerId, theIsForced );
-}
-
 void HYDROGUI_Displayer::Erase( const HYDROData_SequenceOfObjects& theObjs,
                                 const int theViewerId )
 {
-  GraphicsView_Viewer* aViewer = myModule->getViewer( theViewerId );
+  GraphicsView_Viewer* aViewer = module()->getViewer( theViewerId );
   if( !aViewer )
     return;
 
@@ -142,17 +113,18 @@ void HYDROGUI_Displayer::Erase( const HYDROData_SequenceOfObjects& theObjs,
   if( !aViewPort )
     return;
 
-  HYDROGUI_DataModel* aModel = (HYDROGUI_DataModel*)myModule->dataModel();
+  HYDROGUI_DataModel* aModel = (HYDROGUI_DataModel*)module()->dataModel();
   if( aModel ) 
   {
     GraphicsView_ObjectList anObjectList = HYDROGUI_Tool::GetPrsList( aViewPort );
     for( int i = 1, n = theObjs.Length(); i <= n; i++ )
     {
       // the object may be null or dead
-      const Handle(HYDROData_Object)& anObj = theObjs.Value( i );
+      const Handle(HYDROData_Entity)& anObj = theObjs.Value( i );
       if( HYDROGUI_Prs* aPrs = HYDROGUI_Tool::GetPresentation( anObj, anObjectList ) )
       {
         aViewPort->removeItem( aPrs );
+        anObjectList.removeAll( aPrs );
         delete aPrs;
       }
     }
@@ -161,9 +133,10 @@ void HYDROGUI_Displayer::Erase( const HYDROData_SequenceOfObjects& theObjs,
 
 void HYDROGUI_Displayer::Display( const HYDROData_SequenceOfObjects& theObjs,
                                   const int theViewerId,
-                                  const bool theIsForced )
+                                  const bool theIsForced,
+                                  const bool theDoFitAll)
 {
-  GraphicsView_Viewer* aViewer = myModule->getViewer( theViewerId );
+  GraphicsView_Viewer* aViewer = module()->getViewer( theViewerId );
   if( !aViewer )
     return;
 
@@ -175,7 +148,7 @@ void HYDROGUI_Displayer::Display( const HYDROData_SequenceOfObjects& theObjs,
   GraphicsView_ObjectList anObjectList = aViewPort->getObjects();
   for( int i = 1, n = theObjs.Length(); i <= n; i++ )
   {
-    Handle(HYDROData_Object) anObj = theObjs.Value( i );
+    Handle(HYDROData_Entity) anObj = theObjs.Value( i );
     if( anObj.IsNull() )
       continue;
 
@@ -193,17 +166,21 @@ void HYDROGUI_Displayer::Display( const HYDROData_SequenceOfObjects& theObjs,
 
     if( aPrs )
     {
-      bool anIsVisible = anObj->GetVisibility();
+      bool anIsVisible = module()->isObjectVisible( (size_t)aViewer, anObj );
       aPrs->setVisible( anIsVisible );
     }
   }
 
-  aViewPort->fitAll();
+  aViewPort->onBoundingRectChanged(); // specific of HYDRO module
+  if ( theDoFitAll )
+  {
+    aViewPort->fitAll();
+  }
 }
 
 void HYDROGUI_Displayer::purgeObjects( const int theViewerId )
 {
-  GraphicsView_Viewer* aViewer = myModule->getViewer( theViewerId );
+  GraphicsView_Viewer* aViewer = module()->getViewer( theViewerId );
   if( !aViewer )
     return;
 
@@ -216,7 +193,7 @@ void HYDROGUI_Displayer::purgeObjects( const int theViewerId )
   {
     if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
     {
-      Handle(HYDROData_Object) anObject = aPrs->getObject();
+      Handle(HYDROData_Entity) anObject = aPrs->getObject();
       if( !anObject.IsNull() && anObject->IsRemoved() )
       {
         aViewPort->removeItem( aPrs );
@@ -226,7 +203,7 @@ void HYDROGUI_Displayer::purgeObjects( const int theViewerId )
   }
 }
 
-HYDROGUI_PrsDriver* HYDROGUI_Displayer::getDriver( const Handle(HYDROData_Object)& theObj )
+HYDROGUI_PrsDriver* HYDROGUI_Displayer::getDriver( const Handle(HYDROData_Entity)& theObj )
 {
   HYDROGUI_PrsDriver* aDriver = NULL;
   ObjectKind aKind = theObj->GetKind();
@@ -239,15 +216,145 @@ HYDROGUI_PrsDriver* HYDROGUI_Displayer::getDriver( const Handle(HYDROData_Object
     {
       case KIND_IMAGE:
         aDriver = new HYDROGUI_PrsImageDriver();
-        myPrsDriversMap[ aKind ] = aDriver;
         break;
-      case KIND_POLYLINE:
+      case KIND_POLYLINEXY:
         aDriver = new HYDROGUI_PrsPolylineDriver();
-        myPrsDriversMap[ aKind ] = aDriver;
+        break;
+      case KIND_ZONE:
+        aDriver = new HYDROGUI_PrsZoneDriver();
         break;
       default:
         break;
     }
+
+    if ( aDriver )
+      myPrsDriversMap[ aKind ] = aDriver;
   }
+
   return aDriver;
 }
+
+QString HYDROGUI_Displayer::GetType() const
+{
+  return GraphicsView_Viewer::Type();
+}
+
+void HYDROGUI_Displayer::SaveCursorViewPosition( SUIT_ViewWindow* theViewWindow )
+{
+  myIsPositionSaved = false;
+  myXPosition = 0;
+  myYPosition = 0;
+
+  SUIT_ViewWindow* aViewWindow = theViewWindow;
+  if ( !theViewWindow ) {
+    SUIT_ViewManager* aViewMgr = module()->getApp()->activeViewManager();
+    aViewWindow = aViewMgr ? aViewMgr->getActiveView() : 0;
+    if ( !aViewWindow )
+      return;
+  }
+  
+  OCCViewer_ViewWindow* anOCCViewWindow = 
+    dynamic_cast<OCCViewer_ViewWindow*>( aViewWindow );
+  if ( anOCCViewWindow ) {
+    // Get the selected point coordinates
+    OCCViewer_ViewPort3d* aViewPort = anOCCViewWindow->getViewPort();
+    if ( aViewPort ) {
+      QPoint aViewPos = aViewPort->mapFromGlobal( QCursor::pos() );
+      myXPosition = aViewPos.x();
+      myYPosition = aViewPos.y();
+      myIsPositionSaved = true;
+    }
+  }
+  else {
+    SVTK_ViewWindow* aVTKViewWindow = 
+      dynamic_cast<SVTK_ViewWindow*>(aViewWindow);
+    if ( aVTKViewWindow ) {
+      vtkRenderer* aRen = aVTKViewWindow->getRenderer();
+      if ( aRen )
+      {
+        vtkCamera* aCamera = aRen->GetActiveCamera();
+        double* aNormal = aCamera->GetViewPlaneNormal();
+        vtkRenderWindowInteractor* anInteractor = aVTKViewWindow->getInteractor();
+        if ( anInteractor )
+        {
+          anInteractor->GetLastEventPosition( myXPosition, myYPosition );
+          myIsPositionSaved = true;
+        }
+      }
+    }
+  }
+  if (!myIsPositionSaved)
+    int aValue = 0;
+}
+
+bool HYDROGUI_Displayer::GetCursorViewCoordinates( SUIT_ViewWindow* theViewWindow,
+                                                   double& theXCoordinate,
+                                                   double& theYCoordinate,
+                                                   double& theZCoordinate )
+{
+  theXCoordinate = 0;
+  theYCoordinate = 0;
+  theZCoordinate = 0;
+  bool doShow = false;
+  if ( !theViewWindow || !myIsPositionSaved )
+    return doShow;
+  
+  OCCViewer_ViewWindow* anOCCViewWindow = 
+    dynamic_cast<OCCViewer_ViewWindow*>(theViewWindow);
+  if ( anOCCViewWindow ) {
+    // Get the selected point coordinates
+    OCCViewer_ViewPort3d* aViewPort = anOCCViewWindow->getViewPort();
+    if ( !aViewPort ) {
+      return doShow;
+    }
+    gp_Pnt aPnt = CurveCreator_Utils::ConvertClickToPoint( myXPosition, myYPosition,
+                                                           aViewPort->getView() );
+    theXCoordinate = aPnt.X();
+    theYCoordinate = aPnt.Y();
+    doShow = true;
+  } 
+  else
+  {
+    SVTK_ViewWindow* aVTKViewWindow = 
+      dynamic_cast<SVTK_ViewWindow*>(theViewWindow);
+    if ( aVTKViewWindow ) {
+      vtkRenderer* aRen = aVTKViewWindow->getRenderer();
+      if ( aRen )
+      {
+        vtkCamera* aCamera = aRen->GetActiveCamera();
+        double* aNormal = aCamera->GetViewPlaneNormal();
+        myPicker->Pick( myXPosition, myYPosition, 0, aRen );
+        double* aCoords = myPicker->GetPickPosition();
+        /////////////////////// Use the same algorithm as for OCC
+        double X, Y, Z;
+        double aXp, aYp, aZp;
+        double Vx, Vy, Vz;
+        X = aCoords[0];
+        Y = aCoords[1];
+        Z = aCoords[2];
+        Vx = aNormal[0];
+        Vy = aNormal[1];
+        Vz = aNormal[2];
+        Standard_Real aPrec = LOCAL_SELECTION_TOLERANCE;
+        if ( fabs( Vz ) > aPrec ) {
+          double aT = -Z/Vz;
+          aXp = X + aT*Vx;
+          aYp = Y + aT*Vy;
+          aZp = Z + aT*Vz;
+        }
+        else { // Vz = 0 - the eyed plane is orthogonal to Z plane - XOZ, or YOZ
+          aXp = aYp = aZp = 0;
+          if ( fabs( Vy ) < aPrec ) // Vy = 0 - the YOZ plane
+            aYp = Y;
+          else if ( fabs( Vx ) < aPrec ) // Vx = 0 - the XOZ plane
+            aXp = X;
+        }
+        /////////////////////////
+        theXCoordinate = aXp;
+        theYCoordinate = aYp;
+        doShow = true;
+      }
+    } 
+  }
+  return doShow;
+}