Salome HOME
refs #327 - Polyline is not shown during creation
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_OCCDisplayer.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_OCCDisplayer.h"
24
25 #include "HYDROGUI_DataModel.h"
26 #include "HYDROGUI_Module.h"
27 #include "HYDROGUI_Tool.h"
28 #include "HYDROGUI_Shape.h"
29 #include "HYDROGUI_Operation.h"
30
31 #include <AIS_InteractiveContext.hxx>
32 #include <AIS_ListIteratorOfListOfInteractive.hxx>
33 #include <AIS_ListOfInteractive.hxx>
34
35 #include <TColStd_SequenceOfInteger.hxx>
36
37 #include <LightApp_Application.h>
38 #include <SUIT_Study.h>
39
40 #include <OCCViewer_ViewManager.h>
41 #include <OCCViewer_ViewModel.h>
42 #include <OCCViewer_ViewWindow.h>
43
44 HYDROGUI_OCCDisplayer::HYDROGUI_OCCDisplayer( HYDROGUI_Module* theModule )
45 : HYDROGUI_AbstractDisplayer( theModule )
46 {
47 }
48
49 HYDROGUI_OCCDisplayer::~HYDROGUI_OCCDisplayer()
50 {
51 }
52
53 void HYDROGUI_OCCDisplayer::SetToUpdate( const HYDROData_SequenceOfObjects& theObjs,
54                                          const int                          theViewerId )
55 {
56   OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
57   if( !aViewer )
58     return;
59
60   for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
61   {
62     Handle(HYDROData_Entity) anObj = theObjs.Value( i );
63     if( anObj.IsNull() )
64       continue;
65
66     HYDROGUI_Shape* anObjShape = module()->getObjectShape( (size_t)aViewer, anObj );
67     if ( !anObjShape )
68       continue;
69     
70     anObjShape->setIsToUpdate( true );
71   }
72 }
73
74 int HYDROGUI_OCCDisplayer::AddTopZLayer( OCCViewer_ViewManager* theMgr )
75 {
76   int aLayer = -1;
77   OCCViewer_Viewer* aViewer = theMgr->getOCCViewer();
78   if ( !aViewer )
79     return aLayer;
80
81   Standard_Integer aNewId = -1;
82   if ( aViewer->getViewer3d()->AddZLayer( aNewId ) )
83     aZLayer = aNewId;
84
85   return aZLayer;
86 }
87
88 void HYDROGUI_OCCDisplayer::RemoveZLayer( OCCViewer_ViewManager* theMgr,
89                                           const int theLayer )
90 {
91   if ( theLayer < 0 )
92     return;
93
94   OCCViewer_Viewer* aViewer = theMgr->getOCCViewer();
95   if ( !aViewer )
96     return;
97
98   // Get existing Z layers
99   TColStd_SequenceOfInteger anExistingZLayers;
100   aViewer->getViewer3d()->GetAllZLayers( anExistingZLayers );
101   int aNbLayers = anExistingZLayers.Length();
102   
103   if ( theLayer < aNbLayers )
104     aViewer->getViewer3d()->RemoveZLayer( theLayer );
105 }
106
107 void HYDROGUI_OCCDisplayer::EraseAll( const int theViewerId )
108 {
109   OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
110   if( !aViewer )
111     return;
112
113   module()->removeViewShapes( (size_t)aViewer );
114 }
115
116 void HYDROGUI_OCCDisplayer::Erase( const HYDROData_SequenceOfObjects& theObjs,
117                                    const int                          theViewerId )
118 {
119   OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
120   if( !aViewer )
121     return;
122
123   for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
124   {
125     Handle(HYDROData_Entity) anObj = theObjs.Value( i );
126     if( anObj.IsNull() )
127       continue;
128
129     module()->removeObjectShape( (size_t)aViewer, anObj );
130   }
131   aViewer->update();
132 }
133
134 HYDROGUI_Shape* HYDROGUI_OCCDisplayer::createShape( const int                             theViewerId,
135                                                     const Handle(AIS_InteractiveContext)& theContext,
136                                                     const Handle(HYDROData_Entity)&       theObject )
137 {
138   HYDROGUI_Shape* aResShape = NULL;
139   if ( theContext.IsNull() || theObject.IsNull() )
140     return aResShape;
141
142   if ( !HYDROGUI_Tool::IsObjectHasPresentation( theObject, OCCViewer_Viewer::Type() ) )
143     return aResShape;
144
145   aResShape = new HYDROGUI_Shape( theContext, theObject );
146   module()->setObjectShape( theViewerId, theObject, aResShape );
147
148   return aResShape;
149 }
150
151 void HYDROGUI_OCCDisplayer::Display( const HYDROData_SequenceOfObjects& theObjs,
152                                      const int                          theViewerId,
153                                      const bool                         theIsForced,
154                                      const bool theDoFitAll )
155 {
156   OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
157   if( !aViewer )
158     return;
159
160   Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
161   if( aCtx.IsNull() )
162     return;
163
164   // Sort objects by display order ( needed for Z layers assignment only )
165   HYDROData_SequenceOfObjects anObjects;
166   int aMaxIndex = -1;
167   for ( int i = 1, n = theObjs.Length(); i <= n; i++ ) {
168     Handle(HYDROData_Entity) anObj = theObjs.Value( i );
169     if ( anObj.IsNull() || anObj->IsRemoved() ) {
170       continue;
171     }
172
173     int aDisplayOrderIndex = module()->getObjectDisplayOrder( (size_t)aViewer, anObj );
174     if ( aDisplayOrderIndex > aMaxIndex ) {
175       anObjects.Append( anObj );
176       aMaxIndex = aDisplayOrderIndex;
177     } else {
178       anObjects.Prepend( anObj );
179     }
180   }
181
182   // Get existing Z layers
183   TColStd_SequenceOfInteger anExistingZLayers;
184   aViewer->getViewer3d()->GetAllZLayers( anExistingZLayers );
185   int aNbLayers = anExistingZLayers.Length();
186
187   // Display
188   int i = 1;
189   for ( int n = anObjects.Length(); i <= n; i++ )
190   {
191     Handle(HYDROData_Entity) anObj = anObjects.Value( i );
192     if ( anObj.IsNull() || anObj->IsRemoved() )
193       continue;
194
195     HYDROGUI_Shape* anObjShape = module()->getObjectShape( (size_t)aViewer, anObj );
196
197     if ( !anObjShape || anObjShape->getIsToUpdate() || theIsForced )
198     {
199       if ( !anObjShape )
200         anObjShape = createShape( (size_t)aViewer, aCtx, anObj );
201
202       if ( anObjShape )
203         anObjShape->update( false );
204     }
205
206     if ( anObjShape )
207     {
208       bool anIsVisible = module()->isObjectVisible( (size_t)aViewer, anObj );
209       anObjShape->setVisible( anIsVisible, false );
210
211       // Set Z layer
212       Standard_Integer aLayerId = -1;
213       if ( i <= aNbLayers ) {
214         aLayerId = anExistingZLayers.Value( i );
215       } else {
216         Standard_Integer aNewId = -1;
217         if ( aViewer->getViewer3d()->AddZLayer( aNewId ) ) {
218           aLayerId = aNewId;
219         }
220       }
221       if ( aLayerId >= 0 ) {
222         aCtx->SetZLayer( anObjShape->getAISShape(), aLayerId );
223       }
224     }
225   }
226   // update Z layer of the active operation
227   HYDROGUI_Module* aModule = module();
228   SUIT_Operation* anOp = aModule->activeOperation();
229   HYDROGUI_Operation* aHOp = anOp ? dynamic_cast<HYDROGUI_Operation*>( anOp ) : 0;
230   if ( aHOp && aHOp->getPreviewZLayer() >= 0 ) {
231     Standard_Integer aLayerId = -1;
232     if ( i <= aNbLayers )
233       aLayerId = anExistingZLayers.Value( i );
234     else {
235       Standard_Integer aNewId = -1;
236       if ( aViewer->getViewer3d()->AddZLayer( aNewId ) ) {
237         aLayerId = aNewId;
238       }
239     }
240     aHOp->updatePreviewZLayer( aLayerId );
241   }
242
243   if ( theDoFitAll )
244   {
245     OCCViewer_ViewManager* aViewManager
246       = ::qobject_cast<OCCViewer_ViewManager*>( aViewer->getViewManager() );
247     if ( aViewManager )
248     {
249       OCCViewer_ViewWindow* aViewWindow = 
250         ::qobject_cast<OCCViewer_ViewWindow*>( aViewManager->getActiveView() );
251       if ( aViewWindow )
252       {
253         aViewWindow->onFitAll();
254       }
255     }
256   }
257   else if ( aCtx.IsNull() )
258   {
259     aCtx->UpdateSelected();
260   }
261 }
262
263 void HYDROGUI_OCCDisplayer::purgeObjects( const int theViewerId )
264 {
265   OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
266   if( !aViewer )
267     return;
268
269   Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
270   if( aCtx.IsNull() )
271     return;
272
273   AIS_ListOfInteractive aDisplayedObjects;
274   aCtx->DisplayedObjects( aDisplayedObjects );
275
276   AIS_ListIteratorOfListOfInteractive aListIter( aDisplayedObjects );
277   for ( ; aListIter.More(); aListIter.Next() )
278   {
279     Handle(AIS_InteractiveObject) aPrsObj = aListIter.Value();
280     if ( aPrsObj.IsNull() )
281       continue;
282
283     Handle(HYDROData_Entity) anOwnerObj = 
284       Handle(HYDROData_Entity)::DownCast( aPrsObj->GetOwner() );
285     if ( !anOwnerObj.IsNull() && anOwnerObj->IsRemoved() )
286       module()->removeObjectShape( (size_t)aViewer, anOwnerObj );
287   }
288 }
289
290 QString HYDROGUI_OCCDisplayer::GetType() const
291 {
292   return OCCViewer_Viewer::Type();
293 }