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