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