Salome HOME
Corrections of examples path after install with scbi
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ZLayers.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include <HYDROGUI_ZLayers.h>
20 #include <PrsMgr_PresentableObject.hxx>
21 #include <PrsMgr_Presentations.hxx>
22 #include <TColStd_SequenceOfInteger.hxx>
23 #include <Graphic3d_ZLayerId.hxx>
24
25 //#define _DEVDEBUG_
26 #include "HYDRO_trace.hxx"
27
28 void SetZLayerForPrs( const Handle(PrsMgr_Presentation)& thePrs, int theLayerId );
29
30 void SetPrsZLayer( const Handle(PrsMgr_PresentableObject)& thePresentableObject,
31                    const int theMode, const int theLayerId )
32 {
33   PrsMgr_Presentations& aPresentations = thePresentableObject->Presentations();
34   for (Standard_Integer aIdx = 1; aIdx <= aPresentations.Length (); aIdx++)
35   {
36     Handle(PrsMgr_Presentation) aPrs = aPresentations (aIdx);
37     if ( aPrs->Mode() == theMode )
38     {
39       SetZLayerForPrs( aPrs, theLayerId );
40     }
41   }
42 }
43
44 void SetZLayerSettings( const Handle(V3d_Viewer)& theViewer3d, int theLayerId, bool theIsOrdered )
45 {
46   if ( theViewer3d.IsNull() /*|| theLayerId < 0*/ )
47   {
48     return;
49   }
50
51   Graphic3d_ZLayerSettings aSettings = theViewer3d->ZLayerSettings( theLayerId );
52   // enable depth write
53   aSettings.EnableSetting( Graphic3d_ZLayerDepthWrite );
54   // disable depth clear
55   aSettings.DisableSetting( Graphic3d_ZLayerDepthClear );
56   if ( theIsOrdered ) {
57     // disable depth test
58     aSettings.DisableSetting( Graphic3d_ZLayerDepthTest );
59     // disable depth offset
60     aSettings.DisableSetting( Graphic3d_ZLayerDepthOffset );
61   } else {
62     // enable depth test
63     aSettings.EnableSetting( Graphic3d_ZLayerDepthTest );
64     // set depth offset
65     aSettings.SetDepthOffsetPositive();
66   }
67
68   // set new settings
69   theViewer3d->SetZLayerSettings( theLayerId, aSettings );
70 }
71
72 int CreateTopZLayer( const Handle(V3d_Viewer)& theViewer3d )
73 {
74   DEBTRACE("CreateTopZLayer");
75   int aTopZLayer = Graphic3d_ZLayerId_Top;
76
77   if ( theViewer3d ) //&& !theViewer3d->AddZLayer( aTopZLayer ) ) {
78     {
79       try
80       {
81          theViewer3d->AddZLayer( aTopZLayer );
82       }
83       catch (...)
84       {
85         DEBTRACE("Add ZLayer not possible");
86         aTopZLayer = Graphic3d_ZLayerId_Top;
87       }
88     }
89   DEBTRACE("  aTopZLayer: " << aTopZLayer);
90   return aTopZLayer;
91 }
92
93
94 HYDROGUI_ZLayersIterator::HYDROGUI_ZLayersIterator( const Handle(V3d_Viewer)& theViewer )
95   : myIndex( 0 ), myNewZLayer( Graphic3d_ZLayerId_Top ), myViewer( theViewer )
96 {
97   Init( theViewer );
98 }
99
100 HYDROGUI_ZLayersIterator::~HYDROGUI_ZLayersIterator()
101 {
102 }
103
104 void HYDROGUI_ZLayersIterator::Init( const Handle(V3d_Viewer)& theViewer )
105 {
106   TColStd_SequenceOfInteger anExistingZLayers;
107   theViewer->GetAllZLayers( anExistingZLayers );
108
109   int n = anExistingZLayers.Length();
110   for( int i=1; i<=n; i++ ) {
111     int aLayerId = anExistingZLayers( i );
112     if ( aLayerId >= 0 ) {
113       myZLayers.push_back( aLayerId );
114     }
115   }
116
117   myIndex = 0;
118 }
119
120 bool HYDROGUI_ZLayersIterator::More() const
121 {
122   return myIndex < (int)myZLayers.size() || myNewZLayer >= 0;
123 }
124
125 void HYDROGUI_ZLayersIterator::Next()
126 {
127   myIndex++;
128
129   if( myIndex >= (int)myZLayers.size() &&
130       !myViewer.IsNull() ) {
131     myNewZLayer = CreateTopZLayer( myViewer );
132   }
133 }
134
135 int HYDROGUI_ZLayersIterator::TopLayer() const
136 {
137   int aTopLayer = Graphic3d_ZLayerId_Top;
138
139   if ( myNewZLayer >= 0 ) {
140     aTopLayer = myNewZLayer;
141   } else if ( !myZLayers.empty() ) {
142     aTopLayer = myZLayers.back();
143   }
144
145   return aTopLayer;
146 }
147
148
149 int HYDROGUI_ZLayersIterator::LayerId() const
150 {
151   if( myIndex < (int)myZLayers.size() )
152     return myZLayers[myIndex];
153   else
154     return myNewZLayer;
155 }