Salome HOME
refs #550: fix crash when myObject is NULL
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ZLayers.cxx
index 2e9fad629670439b417130c7e1ed28070a8ac9d0..ac479434cafb04f231eb35d9a8c9053b4cedfd39 100644 (file)
@@ -1,12 +1,8 @@
-// Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
-//
-// Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
-// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
-//
+// Copyright (C) 2014-2015  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.
+// 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
 #include <PrsMgr_ModedPresentation.hxx>
 #include <TColStd_SequenceOfInteger.hxx>
 
-class PrsMgr_PresentationManager
+void SetZLayerForPrs( const Handle(PrsMgr_Presentation)& thePrs, int theLayerId );
+
+void SetPrsZLayer( const Handle_PrsMgr_PresentableObject& thePresentableObject,
+                   const int theMode, const int theLayerId )
 {
-public:
-  static void SetZLayer( const Handle_PrsMgr_PresentableObject& thePresentableObject,
-                         const Standard_Integer theMode,
-                         const Standard_Integer theLayerId )
+  PrsMgr_Presentations& aPresentations = thePresentableObject->Presentations();
+  for (Standard_Integer aIdx = 1; aIdx <= aPresentations.Length (); aIdx++)
   {
-    PrsMgr_Presentations& aPresentations = thePresentableObject->Presentations();
-    for (Standard_Integer aIdx = 1; aIdx <= aPresentations.Length (); aIdx++)
+    Handle(PrsMgr_Presentation) aPrs = aPresentations (aIdx).Presentation ();
+    if ( aPresentations (aIdx).Mode() == theMode )
     {
-      Handle(PrsMgr_Presentation) aPrs = aPresentations (aIdx).Presentation ();
-      if ( aPresentations (aIdx).Mode() == theMode )
-        aPrs->SetZLayer (theLayerId);
+      SetZLayerForPrs( aPrs, theLayerId );
     }
   }
-};
+}
 
-void SetPrsZLayer( const Handle_PrsMgr_PresentableObject& thePresentableObject,
-                   const int theMode, const int theLayerId )
+void SetZLayerSettings( const Handle_V3d_Viewer& theViewer3d, int theLayerId, bool theIsOrdered )
 {
-  PrsMgr_PresentationManager::SetZLayer( thePresentableObject, theMode, theLayerId );
+  if ( theViewer3d.IsNull() /*|| theLayerId < 0*/ )
+  {
+    return;
+  }
+
+  Graphic3d_ZLayerSettings aSettings = theViewer3d->ZLayerSettings( theLayerId );
+  // enable depth write
+  aSettings.EnableSetting( Graphic3d_ZLayerDepthWrite );
+  // disable depth clear
+  aSettings.DisableSetting( Graphic3d_ZLayerDepthClear );
+  if ( theIsOrdered ) {
+    // disable depth test
+    aSettings.DisableSetting( Graphic3d_ZLayerDepthTest );
+    // disable depth offset
+    aSettings.DisableSetting( Graphic3d_ZLayerDepthOffset );
+  } else {
+    // enable depth test
+    aSettings.EnableSetting( Graphic3d_ZLayerDepthTest );
+    // set depth offset
+    aSettings.SetDepthOffsetPositive();
+  }
+
+  // set new settings
+  theViewer3d->SetZLayerSettings( theLayerId, aSettings );
 }
 
+int CreateTopZLayer( const Handle_V3d_Viewer& theViewer3d )
+{
+  int aTopZLayer = -1;
+
+  if ( theViewer3d && !theViewer3d->AddZLayer( aTopZLayer ) ) {
+    aTopZLayer = -1;
+  }
+
+  return aTopZLayer;
+}
+
+
 HYDROGUI_ZLayersIterator::HYDROGUI_ZLayersIterator( const Handle_V3d_Viewer& theViewer )
   : myIndex( 0 ), myNewZLayer( -1 ), myViewer( theViewer )
 {
@@ -73,34 +102,36 @@ void HYDROGUI_ZLayersIterator::Init( const Handle_V3d_Viewer& theViewer )
 
 bool HYDROGUI_ZLayersIterator::More() const
 {
-  return myIndex <= (int)myZLayers.size() || myNewZLayer >= 0;
+  return myIndex < (int)myZLayers.size() || myNewZLayer >= 0;
 }
 
 void HYDROGUI_ZLayersIterator::Next()
 {
-  if( myIndex < (int)myZLayers.size() )
-    myIndex++;
-  else if( !myViewer.IsNull() )
-  {
-    bool isOK = myViewer->AddZLayer( myNewZLayer )==Standard_True;
-    if( !isOK )
-      myNewZLayer = -1;
+  myIndex++;
+
+  if( myIndex >= (int)myZLayers.size() && 
+      !myViewer.IsNull() ) {
+    myNewZLayer = CreateTopZLayer( myViewer );
   }
 }
 
-int HYDROGUI_ZLayersIterator::MaxLayer() const
+int HYDROGUI_ZLayersIterator::TopLayer() const
 {
-  int aMaxLayer = -1;
-  for( int i=0, n=myZLayers.size(); i<n; i++ )
-    if( myZLayers[i] > aMaxLayer )
-      aMaxLayer = myZLayers[i];
-  return aMaxLayer;
+  int aTopLayer = -1;
+
+  if ( myNewZLayer >= 0 ) {
+    aTopLayer = myNewZLayer;
+  } else if ( !myZLayers.empty() ) {
+    aTopLayer = myZLayers.back();
+  }
+
+  return aTopLayer;
 }
 
 
 int HYDROGUI_ZLayersIterator::LayerId() const
 {
-  if( More() )
+  if( myIndex < (int)myZLayers.size() )
     return myZLayers[myIndex];
   else
     return myNewZLayer;