Salome HOME
bug #237: fatal error on profile
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_VTKPrsDisplayer.cxx
index 101176f88f6d5d39b97216b0b6f2baf22f10fa29..8b373317e81242cbaae2fafeddba21582cf7f389 100644 (file)
 #include "HYDROGUI_Module.h"
 #include "HYDROGUI_VTKPrs.h"
 #include "HYDROGUI_VTKPrsBathymetryDriver.h"
+#include "HYDROGUI_VTKPrsShapeDriver.h"
 #include "HYDROGUI_Tool.h"
 
+#include "HYDROData_Tool.h"
 #include <SVTK_ViewModel.h>
 #include <SVTK_ViewWindow.h>
 #include <SALOME_ListIO.hxx>
 #include <SUIT_ViewManager.h>
 #include <SUIT_Accel.h>
 
+#include <vtkLookupTable.h>
+#include <vtkRenderer.h>
+#include <vtkTextProperty.h>
+#include <vtkWindow.h>
+#include <vtkActor2DCollection.h>
+
 #include <QVector>
 
+#define NB_COLORS 32
+
+// Saturation of blue
+//#define HUE_START 0.69 
+//#define HUE_END   0.41
+//#define SATURATION_START 1.0 
+//#define SATURATION_END   0.4
+
+#define HUE_START 0.7
+#define HUE_END   0.0 
+#define SATURATION_START 1.0 
+#define SATURATION_END   1.0
+
 HYDROGUI_VTKPrsDisplayer::HYDROGUI_VTKPrsDisplayer( HYDROGUI_Module* theModule )
-: HYDROGUI_AbstractDisplayer( theModule ), myDriver( NULL )
+: HYDROGUI_AbstractDisplayer( theModule ), myDriver( NULL ), myShapeDriver( NULL )
 {
 }
 
@@ -79,6 +100,31 @@ void HYDROGUI_VTKPrsDisplayer::EraseAll( const int theViewerId )
   }
 }
 
+void HYDROGUI_VTKPrsDisplayer::DeleteScalarBar( const int theViewerId )
+{
+  SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
+  if( aViewer )
+  {
+    if ( myScalarBars.contains( (size_t)aViewer ) )
+    {
+      SUIT_ViewManager* aViewMgr = dynamic_cast<SUIT_ViewManager*>( aViewer->getViewManager() );
+      if ( aViewMgr && aViewMgr->getViewsCount() > 0 )
+      {
+        SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>( aViewMgr->getActiveView() );
+        if ( aView )
+        {
+          vtkScalarBarActor* aScalarBar = myScalarBars[ (size_t)aViewer ];
+          if ( aView->getRenderer()->HasViewProp( aScalarBar ) )
+          {
+            aView->getRenderer()->RemoveActor2D( aScalarBar );
+          }
+        }
+      }
+      myScalarBars.remove( (size_t)aViewer );
+    }
+  }
+}
+
 void HYDROGUI_VTKPrsDisplayer::Erase( const HYDROData_SequenceOfObjects& theObjs,
                                 const int theViewerId )
 {
@@ -97,7 +143,10 @@ void HYDROGUI_VTKPrsDisplayer::Erase( const HYDROData_SequenceOfObjects& theObjs
       {
         aViewer->Erase( aPrs, true );
       }
-      module()->removeObjectVTKPrs( (size_t)aViewer, anObj );
+      if ( anObj->IsRemoved() )
+      {
+        module()->removeObjectVTKPrs( (size_t)aViewer, anObj );
+      }
     }
   }
 }
@@ -110,6 +159,29 @@ void HYDROGUI_VTKPrsDisplayer::Display( const HYDROData_SequenceOfObjects& theOb
   SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
   if( aViewer )
   {
+    // Hide colors legend bar
+    SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(
+      aViewer->getViewManager()->getActiveView() );
+    vtkScalarBarActor* aScalarBar = 0;
+    if ( aView )
+    {
+      if ( !myScalarBars.contains( (size_t)aViewer ) )
+      {
+        createScalarBar( (size_t)aViewer );
+      }
+      aScalarBar = myScalarBars[ (size_t)aViewer ];
+
+      if ( aView->getRenderer()->HasViewProp( aScalarBar ) )
+      {
+        aView->getRenderer()->RemoveActor2D( aScalarBar );
+      }
+    }
+
+    // Invalidate global Z range
+    double anInvalidRange[2] = { HYDROGUI_VTKPrs::InvalidZValue(), HYDROGUI_VTKPrs::InvalidZValue() };
+    SetZRange( (size_t)aViewer, anInvalidRange );
+
+    int anInvalidZ = HYDROGUI_VTKPrs::InvalidZValue();
     bool isChanged = false;
     HYDROGUI_VTKPrs* aPrs;
     for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
@@ -117,39 +189,76 @@ void HYDROGUI_VTKPrsDisplayer::Display( const HYDROData_SequenceOfObjects& theOb
       Handle(HYDROData_Entity) anObj = theObjs.Value( i );
       if( !anObj.IsNull() )
       {
-        bool anIsVisible = module()->isObjectVisible( (size_t)(aViewer->getViewManager()->getViewModel()), anObj );
+        bool anIsVisible = module()->isObjectVisible( (size_t)aViewer, anObj );
         aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anObj );
 
         bool anIsInserted = ( aPrs != 0 );
         if( anIsVisible && ( !aPrs || aPrs->getIsToUpdate() || theIsForced ) )
         {
-          if( HYDROGUI_VTKPrsDriver* aDriver = getDriver( anObj ) )
+          if( HYDROGUI_VTKPrsDriver* aDriver = getDriver( (size_t)aViewer, anObj ) )
           {
             if( aDriver->Update( anObj, aPrs ) && aPrs && !anIsInserted )
             {
-              module()->setObjectVTKPrs( theViewerId, anObj, aPrs );
-              aViewer->Display( aPrs );
-              isChanged = true;
+              module()->setObjectVTKPrs( (size_t)aViewer, anObj, aPrs );
             }
           }
         }
 
-        if( aPrs && !anIsVisible )
+        if( aPrs )
         {
-          aViewer->Erase( aPrs );
+          if ( anIsVisible )
+          {
+
+            // Extend the global Z range if necessary
+            double* aGlobalRange = GetZRange( (size_t)aViewer );
+            double* aRange = aPrs->getInternalZRange();
+            bool anIsUpdate = false;
+            if ( aRange[0] < aGlobalRange[0] || ValuesEquals( aGlobalRange[0], anInvalidZ ) )
+            {
+              aGlobalRange[0] = aRange[0];
+              anIsUpdate = true;
+            }
+            if ( aRange[1] > aGlobalRange[1] || ValuesEquals( aGlobalRange[1], anInvalidZ ) )
+            {
+              aGlobalRange[1] = aRange[1];
+              anIsUpdate = true;
+            }
+
+            if ( anIsUpdate )
+            {
+              module()->updateVTKZRange( (size_t)aViewer, aGlobalRange );
+            }
+
+            aViewer->Display( aPrs );
+
+          }
+          else
+          {
+            aViewer->Erase( aPrs );
+          }
           isChanged = true;
         }
       }
     }
 
-    if ( theDoFitAll )
+    if ( aView ) 
     {
-      // Repaint is done inside OnFitAll()
-      aViewer->getViewManager()->getActiveView()->onAccelAction( SUIT_Accel::ZoomFit );
-    } 
-    else if ( isChanged )
-    {
-      aViewer->Repaint();
+      if ( isChanged && aScalarBar )
+      {
+        // Show colors legend bar
+          aView->getRenderer()->AddActor2D( aScalarBar );
+      }
+
+      // Refresh the view
+      if ( theDoFitAll )
+      {
+        // Repaint is done inside OnFitAll()
+        aView->onAccelAction( SUIT_Accel::ZoomFit );
+      } 
+      else if ( isChanged )
+      {
+        aView->Repaint();
+      }
     }
   }
 }
@@ -185,7 +294,7 @@ void HYDROGUI_VTKPrsDisplayer::purgeObjects( const int theViewerId )
   }
 }
 
-HYDROGUI_VTKPrsDriver* HYDROGUI_VTKPrsDisplayer::getDriver( const Handle(HYDROData_Entity)& theObj )
+HYDROGUI_VTKPrsDriver* HYDROGUI_VTKPrsDisplayer::getDriver( const int theViewId, const Handle(HYDROData_Entity)& theObj )
 {
   HYDROGUI_VTKPrsDriver* aDriver = NULL;
   ObjectKind aKind = theObj->GetKind();
@@ -193,10 +302,18 @@ HYDROGUI_VTKPrsDriver* HYDROGUI_VTKPrsDisplayer::getDriver( const Handle(HYDRODa
   {
     if ( !myDriver )
     {
-      myDriver = new HYDROGUI_VTKPrsBathymetryDriver();
+      myDriver = new HYDROGUI_VTKPrsBathymetryDriver( myScalarBars[ theViewId ] );
     }
     aDriver = myDriver;
   }
+  else
+  {
+    if ( !myShapeDriver )
+    {
+      myShapeDriver = new HYDROGUI_VTKPrsShapeDriver( myScalarBars[ theViewId ] );
+    }
+    aDriver = myShapeDriver;
+  }
 
   return aDriver;
 }
@@ -205,3 +322,37 @@ QString HYDROGUI_VTKPrsDisplayer::GetType() const
 {
   return SVTK_Viewer::Type();
 }
+
+void HYDROGUI_VTKPrsDisplayer::SetZRange( const int theViewId, double theRange[] )
+{
+  myScalarBars[ theViewId ]->GetLookupTable()->SetRange( theRange );
+}
+
+double* HYDROGUI_VTKPrsDisplayer::GetZRange( const int theViewId ) const
+{
+  return myScalarBars[ theViewId ]->GetLookupTable()->GetRange();
+}
+
+void HYDROGUI_VTKPrsDisplayer::createScalarBar( const int theViewId )
+{
+  if ( !myScalarBars.contains( theViewId ) )
+  {
+    // The invalid value is used to identify the case when the table range is not initialized yet.
+    double anInvalidValue = HYDROGUI_VTKPrs::InvalidZValue();
+    vtkLookupTable* aTable = vtkLookupTable::New();
+    aTable->SetHueRange( HUE_START, HUE_END );
+    aTable->SetSaturationRange( SATURATION_START, SATURATION_END );
+    aTable->SetTableRange( anInvalidValue, anInvalidValue );
+    aTable->SetValueRange( 1.0, 1.0 );
+    aTable->SetAlphaRange( 1.0, 1.0 );
+    aTable->SetNumberOfColors( NB_COLORS );
+    aTable->Build();
+    vtkSmartPointer<vtkScalarBarActor> aScalarBar = vtkScalarBarActor::New();
+    aScalarBar->SetLookupTable( aTable );
+    aScalarBar->SetNumberOfLabels( NB_COLORS * 0.75 );
+    aScalarBar->SetWidth( aScalarBar->GetWidth() / 1.5 );
+    aScalarBar->SetTextureGridWidth( aScalarBar->GetTextureGridWidth() * 4. );
+    aTable->Delete();
+    myScalarBars.insert( theViewId, aScalarBar );
+  }
+}