Salome HOME
43df09486392ad354db7731045661da9e197cf33
[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 #include "HYDROGUI_ZLayers.h"
32
33 #include <AIS_InteractiveContext.hxx>
34 #include <AIS_ListIteratorOfListOfInteractive.hxx>
35 #include <AIS_ListOfInteractive.hxx>
36
37 #include <Aspect_ColorScale.hxx>
38
39 #include <TColStd_SequenceOfInteger.hxx>
40
41 #include <LightApp_Application.h>
42 #include <SUIT_Study.h>
43
44 #include <OCCViewer_ViewManager.h>
45 #include <OCCViewer_ViewModel.h>
46 #include <OCCViewer_ViewWindow.h>
47 #include <OCCViewer_ViewPort3d.h>
48
49 HYDROGUI_OCCDisplayer::HYDROGUI_OCCDisplayer( HYDROGUI_Module* theModule )
50 : HYDROGUI_AbstractDisplayer( theModule )
51 {
52 }
53
54 HYDROGUI_OCCDisplayer::~HYDROGUI_OCCDisplayer()
55 {
56 }
57
58 void HYDROGUI_OCCDisplayer::SetToUpdate( const HYDROData_SequenceOfObjects& theObjs,
59                                          const int                          theViewerId )
60 {
61   OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
62   if( !aViewer )
63     return;
64
65   for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
66   {
67     Handle(HYDROData_Entity) anObj = theObjs.Value( i );
68     if( anObj.IsNull() )
69       continue;
70
71     HYDROGUI_Shape* anObjShape = module()->getObjectShape( (size_t)aViewer, anObj );
72     if ( !anObjShape )
73       continue;
74     
75     anObjShape->setIsToUpdate( true );
76   }
77 }
78
79 int HYDROGUI_OCCDisplayer::AddPreviewZLayer( OCCViewer_ViewManager* theMgr )
80 {
81   int aLayer = -1;
82   OCCViewer_Viewer* aViewer = theMgr->getOCCViewer();
83   if ( !aViewer )
84     return aLayer;
85
86   aLayer = CreateTopZLayer( aViewer->getViewer3d() );
87   
88   // Hilight presentation should be on top
89   Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
90   if( !aCtx.IsNull() ) {
91     int aTopLayer = CreateTopZLayer( aViewer->getViewer3d() );
92     if ( aTopLayer > 0 ) {
93       UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, aTopLayer );
94     }
95   }
96
97   return aLayer;
98 }
99
100 void HYDROGUI_OCCDisplayer::RemoveZLayer( OCCViewer_ViewManager* theMgr,
101                                           const int theLayer )
102 {
103   if ( theLayer < 0 )
104     return;
105
106   OCCViewer_Viewer* aViewer = theMgr->getOCCViewer();
107   if ( !aViewer )
108     return;
109
110   // Get existing Z layers
111   TColStd_SequenceOfInteger anExistingZLayers;
112   aViewer->getViewer3d()->GetAllZLayers( anExistingZLayers );
113   int aNbLayers = anExistingZLayers.Length();
114   
115   if ( theLayer < aNbLayers )
116     aViewer->getViewer3d()->RemoveZLayer( theLayer );
117 }
118
119 void HYDROGUI_OCCDisplayer::EraseAll( const int theViewerId )
120 {
121   OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
122   if( !aViewer )
123     return;
124
125   module()->removeViewShapes( (size_t)aViewer );
126   UpdateColorScale( aViewer );
127 }
128
129 void HYDROGUI_OCCDisplayer::Erase( const HYDROData_SequenceOfObjects& theObjs,
130                                    const int                          theViewerId )
131 {
132   OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
133   if( !aViewer )
134     return;
135
136   for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
137   {
138     Handle(HYDROData_Entity) anObj = theObjs.Value( i );
139     if( anObj.IsNull() )
140       continue;
141
142     module()->removeObjectShape( (size_t)aViewer, anObj );
143   }
144   aViewer->update();
145   UpdateColorScale( aViewer );
146 }
147
148 HYDROGUI_Shape* HYDROGUI_OCCDisplayer::createShape( const int                             theViewerId,
149                                                     const Handle(AIS_InteractiveContext)& theContext,
150                                                     const Handle(HYDROData_Entity)&       theObject )
151 {
152   HYDROGUI_Shape* aResShape = NULL;
153   if ( theContext.IsNull() || theObject.IsNull() )
154     return aResShape;
155
156   if ( !HYDROGUI_Tool::IsObjectHasPresentation( theObject, OCCViewer_Viewer::Type() ) )
157     return aResShape;
158
159   aResShape = new HYDROGUI_Shape( theContext, theObject );
160   module()->setObjectShape( theViewerId, theObject, aResShape );
161
162   return aResShape;
163 }
164
165 void HYDROGUI_OCCDisplayer::Display( const HYDROData_SequenceOfObjects& theObjs,
166                                      const int                          theViewerId,
167                                      const bool                         theIsForced,
168                                      const bool theDoFitAll )
169 {
170   // Get OCC viewer by id
171   OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
172   if( !aViewer )
173     return;
174
175   // Get interactive context
176   Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
177   if( aCtx.IsNull() )
178     return;
179
180   // Get the document
181   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( module()->getStudyId() );
182   if ( !aDoc )
183     return;
184   
185   // Assign Z layer indexes to the objects
186   aDoc->Show( theObjs );
187
188   // Sort objects by display order ( needed for Z layers assignment only )
189   HYDROData_SequenceOfObjects anUnorderedToDisplay = theObjs;
190   HYDROData_SequenceOfObjects anOrderedToDisplay;
191   HYDROData_SequenceOfObjects anAllOrderedObjects = aDoc->GetObjectsLayerOrder();
192
193   HYDROData_SequenceOfObjects::Iterator anAllOrderedIter( anAllOrderedObjects );
194   for ( ; anAllOrderedIter.More(); anAllOrderedIter.Next() ) {
195     QString anOrderedEntry = 
196       HYDROGUI_DataObject::dataObjectEntry( anAllOrderedIter.Value() );
197     
198     HYDROData_SequenceOfObjects::Iterator aToDisplayIter( anUnorderedToDisplay );
199     for ( ; aToDisplayIter.More(); aToDisplayIter.Next() ) {
200       Handle(HYDROData_Entity) anObjToDisplay = aToDisplayIter.Value();
201       QString anEntry = HYDROGUI_DataObject::dataObjectEntry( anObjToDisplay );
202       if ( anEntry == anOrderedEntry ) {
203         anOrderedToDisplay.Prepend( anObjToDisplay );
204         anUnorderedToDisplay.Remove( aToDisplayIter );
205         break;
206       }
207     }
208   }
209   
210   // Get 3d viewer
211   Handle(V3d_Viewer) aViewer3d = aViewer->getViewer3d();
212
213   // Display objects:
214   HYDROGUI_ZLayersIterator aZLayersIt( aViewer->getViewer3d() );
215   if ( !aZLayersIt.More() ) {
216     aZLayersIt.Next();
217   }
218
219   // 1. Display the ordered objects:
220   HYDROData_SequenceOfObjects::Iterator anOrderedIter( anOrderedToDisplay );
221   for ( ; anOrderedIter.More(); anOrderedIter.Next() ) {
222     Handle(HYDROData_Entity) anObj = anOrderedIter.Value();
223     if ( Display( anObj, aViewer, theIsForced ) ) {
224       // set Z layer ( one Z layer for each ordered object )
225       int aZLayerId = aZLayersIt.LayerId();
226       SetZLayer( aViewer, anObj, aZLayerId );
227       SetZLayerSettings( aViewer3d, aZLayerId, true );
228       aZLayersIt.Next();
229     }
230   }
231
232   // 2. Display the unordered objects:
233   bool isDisplayed = false;
234   int anUnorderedZLayerId = aZLayersIt.LayerId();
235   HYDROData_SequenceOfObjects::Iterator anUnorderedIter( anUnorderedToDisplay );
236   for ( ; anUnorderedIter.More(); anUnorderedIter.Next() ) {
237     Handle(HYDROData_Entity) anObj = anUnorderedIter.Value();
238     if ( Display( anObj, aViewer, theIsForced) ) {
239       // set Z layer ( one Z layer for all unordered objects )
240       SetZLayer( aViewer, anObj, anUnorderedZLayerId );
241       if ( !isDisplayed ) {
242         SetZLayerSettings( aViewer3d, anUnorderedZLayerId, false );
243       }
244       isDisplayed = true;
245     }
246   }
247   
248   // 3. Update the top Z layer index
249   if ( isDisplayed ) {
250     aZLayersIt.Next();
251   }
252
253   // Update Z layer of the active operation
254   int aPreviewZLayerId = aZLayersIt.LayerId();
255
256   HYDROGUI_Module* aModule = module();
257   SUIT_Operation* anOp = aModule->activeOperation();
258   HYDROGUI_Operation* aHOp = anOp ? dynamic_cast<HYDROGUI_Operation*>( anOp ) : 0;
259   if ( aHOp && aHOp->getPreviewZLayer() >= 0 ) {
260     aHOp->updatePreviewZLayer( aPreviewZLayerId );
261     aZLayersIt.Next();
262   }
263
264   // Update Z layer of hilight presentations
265   int aHilightLayer = aZLayersIt.TopLayer();
266   UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, aHilightLayer );
267
268   // Fit all / update selection
269   if ( theDoFitAll ) {
270     OCCViewer_ViewManager* aViewManager
271       = ::qobject_cast<OCCViewer_ViewManager*>( aViewer->getViewManager() );
272     if ( aViewManager ) {
273       OCCViewer_ViewWindow* aViewWindow = 
274         ::qobject_cast<OCCViewer_ViewWindow*>( aViewManager->getActiveView() );
275       if ( aViewWindow ) {
276         aViewWindow->onFitAll();
277       }
278     }
279   } 
280   else if ( !aCtx.IsNull() ) { // TODO: determine if this code is necessary (added as a fix for issue# 359)
281     aCtx->UpdateSelected();
282   }
283
284   UpdateColorScale( aViewer );
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 }
318
319 bool HYDROGUI_OCCDisplayer::Display( const Handle(HYDROData_Entity)& theObject,
320                                      const OCCViewer_Viewer* theViewer,
321                                      const bool theIsForced )
322 {
323   bool aRes = false;
324
325   if ( theObject.IsNull() || theObject->IsRemoved() || !theViewer ) {
326     return aRes;
327   }
328
329   // Get interactive context
330   Handle(AIS_InteractiveContext) aCtx = theViewer->getAISContext();
331   if( aCtx.IsNull() ) {
332     return aRes;
333   }
334
335   // Viewer id
336   size_t aViewerId = (size_t)theViewer;
337
338   // Object shape 
339   HYDROGUI_Shape* anObjShape = module()->getObjectShape( aViewerId, theObject );
340   // create if needed
341   if ( !anObjShape ) {
342     anObjShape = createShape( aViewerId, aCtx, theObject );
343     if ( anObjShape ) {
344       anObjShape->setIsToUpdate( true );
345     }
346   }
347   
348   // Process the shape
349   if ( anObjShape ) {
350     // update if needed
351     if ( anObjShape->getIsToUpdate() || theIsForced ) {
352       anObjShape->update( false );
353     }
354
355     // Set visibility
356     bool anIsVisible = module()->isObjectVisible( aViewerId, theObject );
357     anObjShape->setVisible( anIsVisible, false );
358
359     aRes = true;
360   }
361
362   UpdateColorScale( theViewer );
363   return aRes;
364 }
365
366 void HYDROGUI_OCCDisplayer::SetZLayer( const OCCViewer_Viewer* theViewer,
367                                        const Handle(HYDROData_Entity)& theObject, 
368                                        const int theZLayerId )
369 {
370   if ( !theViewer || ( theZLayerId < 0 ) ) {
371     return;
372   }
373   
374   // Get interactive context
375   Handle(AIS_InteractiveContext) aCtx = theViewer->getAISContext();
376   if( aCtx.IsNull() ) {
377     return;
378   }
379
380   // Get viewer id
381   size_t aViewerId = (size_t)theViewer;
382
383   // Get object shape 
384   HYDROGUI_Shape* anObjShape = module()->getObjectShape( aViewerId, theObject );
385
386   // Set Z layer
387   if ( anObjShape ) {
388     aCtx->SetZLayer( anObjShape->getAISObject(), theZLayerId );
389   }
390 }
391
392 void HYDROGUI_OCCDisplayer::UpdateColorScale( const OCCViewer_Viewer* theViewer )
393 {
394   OCCViewer_ViewWindow* aWnd = dynamic_cast<OCCViewer_ViewWindow*>( theViewer->getViewManager()->getActiveView() );
395   Handle(V3d_View) aView = aWnd->getViewPort()->getView();
396
397   int aViewerId = (size_t)theViewer;//TODO: check if viewer id is correct
398   QList<HYDROGUI_Shape*> aBaths = module()->getObjectShapes( aViewerId, KIND_BATHYMETRY );
399
400   bool isDisplayColorScale = !aBaths.empty();
401   Standard_Real anXPos = 0.05; //TODO
402   Standard_Real anYPos = 0.1; //TODO
403   Standard_Real aWidth = 0.2; //TODO
404   Standard_Real aHeight = 0.5; //TODO
405   Standard_Integer aTextHeight = 14; //TODO
406   Standard_Integer aNbIntervals = 20; //TODO
407   TCollection_ExtendedString aColorScaleTitle = "";//TODO
408
409   Standard_Real aColorScaleMin = 0, aColorScaleMax = 1, aMin, aMax;
410   bool isFirst = true;
411   foreach( HYDROGUI_Shape* aBathShape, aBaths )
412   {
413     aBathShape->GetRange( aMin, aMax );
414
415     if( isFirst || aMin < aColorScaleMin )
416       aColorScaleMin = aMin;
417     if( isFirst || aMax > aColorScaleMax )
418       aColorScaleMax = aMax;
419
420     isFirst = false;
421   }
422
423   if( isDisplayColorScale )
424   {
425     Handle(Aspect_ColorScale) aColorScale = aView->ColorScale();
426     if( !aColorScale.IsNull() )
427     {
428       aColorScale->SetXPosition( anXPos );
429       aColorScale->SetYPosition( anYPos );
430       aColorScale->SetWidth( aWidth );
431       aColorScale->SetHeight( aHeight );
432
433       aColorScale->SetTextHeight( aTextHeight );
434       aColorScale->SetNumberOfIntervals( aNbIntervals );
435
436       aColorScale->SetTitle( aColorScaleTitle );
437       aColorScale->SetRange( aColorScaleMin, aColorScaleMax );
438
439       foreach( HYDROGUI_Shape* aBathShape, aBaths )
440         aBathShape->UpdateWithColorScale( aColorScale );
441     }
442     if( !aView->ColorScaleIsDisplayed() )
443       aView->ColorScaleDisplay();
444   }
445   else
446   {
447     if( aView->ColorScaleIsDisplayed() )
448       aView->ColorScaleErase();
449   }
450 }