]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROGUI/HYDROGUI_OCCDisplayer.cxx
Salome HOME
Fix bug: objects which has no 2D prs are not displayed.
[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: to optimize
174   // Sort objects by display order ( needed for Z layers assignment only )
175   typedef QPair<Handle(HYDROData_Entity), bool> Object2ApplyZLayer;
176   QList<Object2ApplyZLayer> anOrderedToDisplay; // list of (object, is_z_layer_applicable)
177   HYDROData_SequenceOfObjects anObjectsToDisplay = theObjs;
178   HYDROData_SequenceOfObjects anOrderedObjects = aDoc->GetObjectsLayerOrder();
179
180   HYDROData_SequenceOfObjects::Iterator anOrderedIter( anOrderedObjects );
181   for ( ; anOrderedIter.More(); anOrderedIter.Next() ) {
182     QString anOrderedEntry = HYDROGUI_DataObject::dataObjectEntry( anOrderedIter.Value() );
183     
184     HYDROData_SequenceOfObjects::Iterator aToDisplayIter( anObjectsToDisplay );
185     for ( ; aToDisplayIter.More(); aToDisplayIter.Next() ) {
186       Handle(HYDROData_Entity) anObjToDisplay = aToDisplayIter.Value();
187       QString anEntry = HYDROGUI_DataObject::dataObjectEntry( anObjToDisplay );
188       if ( anEntry == anOrderedEntry ) {
189         anObjectsToDisplay.Remove( aToDisplayIter );
190         anOrderedToDisplay.prepend( Object2ApplyZLayer( anObjToDisplay, true ) );
191         break;
192       }
193     }
194   }
195   // For the rest objects Z layer can't be applied
196   HYDROData_SequenceOfObjects::Iterator aRestObjsIter( anObjectsToDisplay );
197   for ( ; aRestObjsIter.More(); aRestObjsIter.Next() ) {
198     anOrderedToDisplay.prepend( Object2ApplyZLayer( aRestObjsIter.Value(), false ) );
199   }
200
201   // Get existing Z layers
202   TColStd_SequenceOfInteger anExistingZLayers;
203   aViewer->getViewer3d()->GetAllZLayers( anExistingZLayers );
204   int aNbLayers = anExistingZLayers.Length();
205
206   // Display
207   int aNextZLayerIndex = 2; // don't use the first default Z layer ( which index = 1 )
208   foreach ( const Object2ApplyZLayer& aPair, anOrderedToDisplay ) {
209     Handle(HYDROData_Entity) anObj = aPair.first;
210     if ( anObj.IsNull() || anObj->IsRemoved() )
211       continue;
212
213     HYDROGUI_Shape* anObjShape = module()->getObjectShape( (size_t)aViewer, anObj );
214
215     if ( !anObjShape || anObjShape->getIsToUpdate() || theIsForced )
216     {
217       if ( !anObjShape )
218         anObjShape = createShape( (size_t)aViewer, aCtx, anObj );
219
220       if ( anObjShape )
221         anObjShape->update( false );
222     }
223
224     if ( anObjShape )
225     {
226       bool anIsVisible = module()->isObjectVisible( (size_t)aViewer, anObj );
227       anObjShape->setVisible( anIsVisible, false );
228
229       bool isZLayerApplicable = aPair.second;
230
231       // set Z layer if applicable
232       if ( isZLayerApplicable ) {
233         Standard_Integer aLayerId = -1;
234         if ( aNextZLayerIndex <= aNbLayers ) {
235           aLayerId = anExistingZLayers.Value( aNextZLayerIndex );
236         } else {
237           Standard_Integer aNewId = -1;
238           if ( aViewer->getViewer3d()->AddZLayer( aNewId ) ) {
239             aLayerId = aNewId;
240           }
241         }
242         if ( aLayerId >= 0 ) {
243           aCtx->SetZLayer( anObjShape->getAISShape(), aLayerId );
244         }
245         aNextZLayerIndex++;
246       }
247     }
248   }
249
250   // Update Z layer of the active operation
251   HYDROGUI_Module* aModule = module();
252   SUIT_Operation* anOp = aModule->activeOperation();
253   HYDROGUI_Operation* aHOp = anOp ? dynamic_cast<HYDROGUI_Operation*>( anOp ) : 0;
254   if ( aHOp && aHOp->getPreviewZLayer() >= 0 ) {
255     Standard_Integer aLayerId = -1;
256     if ( aNextZLayerIndex <= aNbLayers )
257       aLayerId = anExistingZLayers.Value( aNextZLayerIndex );
258     else {
259       Standard_Integer aNewId = -1;
260       if ( aViewer->getViewer3d()->AddZLayer( aNewId ) ) {
261         aLayerId = aNewId;
262       }
263     }
264     aHOp->updatePreviewZLayer( aLayerId );
265   }
266
267   if ( theDoFitAll )
268   {
269     OCCViewer_ViewManager* aViewManager
270       = ::qobject_cast<OCCViewer_ViewManager*>( aViewer->getViewManager() );
271     if ( aViewManager )
272     {
273       OCCViewer_ViewWindow* aViewWindow = 
274         ::qobject_cast<OCCViewer_ViewWindow*>( aViewManager->getActiveView() );
275       if ( aViewWindow )
276       {
277         aViewWindow->onFitAll();
278       }
279     }
280   }
281   else if ( aCtx.IsNull() )
282   {
283     aCtx->UpdateSelected();
284   }
285 }
286
287 void HYDROGUI_OCCDisplayer::purgeObjects( const int theViewerId )
288 {
289   OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
290   if( !aViewer )
291     return;
292
293   Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
294   if( aCtx.IsNull() )
295     return;
296
297   AIS_ListOfInteractive aDisplayedObjects;
298   aCtx->DisplayedObjects( aDisplayedObjects );
299
300   AIS_ListIteratorOfListOfInteractive aListIter( aDisplayedObjects );
301   for ( ; aListIter.More(); aListIter.Next() )
302   {
303     Handle(AIS_InteractiveObject) aPrsObj = aListIter.Value();
304     if ( aPrsObj.IsNull() )
305       continue;
306
307     Handle(HYDROData_Entity) anOwnerObj = 
308       Handle(HYDROData_Entity)::DownCast( aPrsObj->GetOwner() );
309     if ( !anOwnerObj.IsNull() && anOwnerObj->IsRemoved() )
310       module()->removeObjectShape( (size_t)aViewer, anOwnerObj );
311   }
312 }
313
314 QString HYDROGUI_OCCDisplayer::GetType() const
315 {
316   return OCCViewer_Viewer::Type();
317 }