Salome HOME
Merge remote-tracking branch 'origin/BR_IMPROVEMENTS' into BR_v14_rc
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_OCCDisplayer.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROGUI_OCCDisplayer.h"
20
21 #include "HYDROGUI_DataModel.h"
22 #include "HYDROGUI_Module.h"
23 #include "HYDROGUI_Tool.h"
24 #include <HYDROGUI_ShapeImage.h>
25 #include <HYDROGUI_ShapeBathymetry.h>
26 #include "HYDROGUI_Operation.h"
27 #include "HYDROGUI_DataObject.h"
28 #include "HYDROGUI_ZLayers.h"
29 #include <HYDROData_Bathymetry.h>
30
31 #include <AIS_InteractiveContext.hxx>
32 #include <AIS_ListIteratorOfListOfInteractive.hxx>
33 #include <AIS_ListOfInteractive.hxx>
34
35 #include <Aspect_ColorScale.hxx>
36
37 #include <TColStd_SequenceOfInteger.hxx>
38
39 #include <LightApp_Application.h>
40 #include <SUIT_Study.h>
41
42 #include <OCCViewer_ViewManager.h>
43 #include <OCCViewer_ViewModel.h>
44 #include <OCCViewer_ViewWindow.h>
45 #include <OCCViewer_ViewPort3d.h>
46
47 HYDROGUI_OCCDisplayer::HYDROGUI_OCCDisplayer( HYDROGUI_Module* theModule )
48 : HYDROGUI_AbstractDisplayer( theModule )
49 {
50   myToUpdateColorScale = false;
51 }
52
53 HYDROGUI_OCCDisplayer::~HYDROGUI_OCCDisplayer()
54 {
55 }
56
57 void HYDROGUI_OCCDisplayer::SetToUpdate( const HYDROData_SequenceOfObjects& theObjs,
58                                          const int                          theViewerId )
59 {
60   OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
61   if( !aViewer )
62     return;
63
64   for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
65   {
66     Handle(HYDROData_Entity) anObj = theObjs.Value( i );
67     if( anObj.IsNull() )
68       continue;
69
70     HYDROGUI_Shape* anObjShape = module()->getObjectShape( (size_t)aViewer, anObj );
71     if ( !anObjShape )
72       continue;
73     
74     anObjShape->setIsToUpdate( true );
75   }
76 }
77
78 int HYDROGUI_OCCDisplayer::AddPreviewZLayer( OCCViewer_ViewManager* theMgr )
79 {
80   int aLayer = -1;
81   OCCViewer_Viewer* aViewer = theMgr->getOCCViewer();
82   if ( !aViewer )
83     return aLayer;
84
85   aLayer = CreateTopZLayer( aViewer->getViewer3d() );
86   
87   // Hilight presentation should be on top
88   Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
89   if( !aCtx.IsNull() ) {
90     int aTopLayer = CreateTopZLayer( aViewer->getViewer3d() );
91     if ( aTopLayer > 0 ) {
92       UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, aTopLayer );
93     }
94   }
95
96   return aLayer;
97 }
98
99 void HYDROGUI_OCCDisplayer::RemoveZLayer( OCCViewer_ViewManager* theMgr,
100                                           const int theLayer )
101 {
102   if ( theLayer < 0 )
103     return;
104
105   OCCViewer_Viewer* aViewer = theMgr->getOCCViewer();
106   if ( !aViewer )
107     return;
108
109   // Get existing Z layers
110   TColStd_SequenceOfInteger anExistingZLayers;
111   aViewer->getViewer3d()->GetAllZLayers( anExistingZLayers );
112   int aNbLayers = anExistingZLayers.Length();
113   
114   if ( theLayer < aNbLayers )
115     aViewer->getViewer3d()->RemoveZLayer( theLayer );
116 }
117
118 void HYDROGUI_OCCDisplayer::EraseAll( const int theViewerId )
119 {
120   OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
121   if( !aViewer )
122     return;
123
124   module()->removeViewShapes( (size_t)aViewer );
125   UpdateColorScale( aViewer );
126 }
127
128 void HYDROGUI_OCCDisplayer::Erase( const HYDROData_SequenceOfObjects& theObjs,
129                                    const int                          theViewerId )
130 {
131   OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
132   if( !aViewer )
133     return;
134
135   for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
136   {
137     Handle(HYDROData_Entity) anObj = theObjs.Value( i );
138     if( anObj.IsNull() )
139       continue;
140
141     module()->removeObjectShape( (size_t)aViewer, anObj );
142   }
143   aViewer->update();
144   UpdateColorScale( aViewer );
145 }
146
147 HYDROGUI_Shape* HYDROGUI_OCCDisplayer::createShape( const int                             theViewerId,
148                                                     const Handle(AIS_InteractiveContext)& theContext,
149                                                     const Handle(HYDROData_Entity)&       theObject )
150 {
151   HYDROGUI_Shape* aResShape = NULL;
152   if ( theContext.IsNull() || theObject.IsNull() )
153     return aResShape;
154
155   if ( !HYDROGUI_Tool::IsObjectHasPresentation( theObject, OCCViewer_Viewer::Type() ) )
156     return aResShape;
157
158   if( theObject->IsKind( STANDARD_TYPE( HYDROData_Image ) ) )
159     aResShape = new HYDROGUI_ShapeImage( theContext, Handle_HYDROData_Image::DownCast( theObject ) );
160   else if( theObject->IsKind( STANDARD_TYPE( HYDROData_Bathymetry ) ) )
161     aResShape = new HYDROGUI_ShapeBathymetry( this, theContext, Handle_HYDROData_Bathymetry::DownCast( theObject ) );
162   else
163     aResShape = new HYDROGUI_Shape( theContext, theObject );
164
165   module()->setObjectShape( theViewerId, theObject, aResShape );
166
167   return aResShape;
168 }
169
170 void HYDROGUI_OCCDisplayer::Display( const HYDROData_SequenceOfObjects& theObjs,
171                                      const int                          theViewerId,
172                                      const bool                         theIsForced,
173                                      const bool theDoFitAll )
174 {
175   // Get OCC viewer by id
176   OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
177   if( !aViewer )
178     return;
179
180   // Get interactive context
181   Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
182   if( aCtx.IsNull() )
183     return;
184
185   // Get the document
186   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( module()->getStudyId() );
187   if ( !aDoc )
188     return;
189   
190   // Assign Z layer indexes to the objects
191   aDoc->Show( theObjs );
192
193   // Sort objects by display order ( needed for Z layers assignment only )
194   HYDROData_SequenceOfObjects anUnorderedToDisplay = theObjs;
195   HYDROData_SequenceOfObjects anOrderedToDisplay;
196   HYDROData_SequenceOfObjects anAllOrderedObjects = aDoc->GetObjectsLayerOrder();
197
198   HYDROData_SequenceOfObjects::Iterator anAllOrderedIter( anAllOrderedObjects );
199   for ( ; anAllOrderedIter.More(); anAllOrderedIter.Next() ) {
200     QString anOrderedEntry = 
201       HYDROGUI_DataObject::dataObjectEntry( anAllOrderedIter.Value() );
202     
203     HYDROData_SequenceOfObjects::Iterator aToDisplayIter( anUnorderedToDisplay );
204     for ( ; aToDisplayIter.More(); aToDisplayIter.Next() ) {
205       Handle(HYDROData_Entity) anObjToDisplay = aToDisplayIter.Value();
206       QString anEntry = HYDROGUI_DataObject::dataObjectEntry( anObjToDisplay );
207       if ( anEntry == anOrderedEntry ) {
208         anOrderedToDisplay.Prepend( anObjToDisplay );
209         anUnorderedToDisplay.Remove( aToDisplayIter );
210         break;
211       }
212     }
213   }
214   
215   // Get 3d viewer
216   Handle(V3d_Viewer) aViewer3d = aViewer->getViewer3d();
217
218   // Display objects:
219   HYDROGUI_ZLayersIterator aZLayersIt( aViewer->getViewer3d() );
220   if ( !aZLayersIt.More() ) {
221     aZLayersIt.Next();
222   }
223
224   // 1. Display the ordered objects:
225   HYDROData_SequenceOfObjects::Iterator anOrderedIter( anOrderedToDisplay );
226   for ( ; anOrderedIter.More(); anOrderedIter.Next() ) {
227     Handle(HYDROData_Entity) anObj = anOrderedIter.Value();
228     if ( Display( anObj, aViewer, theIsForced ) ) {
229       // set Z layer ( one Z layer for each ordered object )
230       int aZLayerId = aZLayersIt.LayerId();
231       SetZLayer( aViewer, anObj, aZLayerId );
232       SetZLayerSettings( aViewer3d, aZLayerId, true );
233       aZLayersIt.Next();
234     }
235   }
236
237   // 2. Display the unordered objects:
238   bool isDisplayed = false;
239   int anUnorderedZLayerId = aZLayersIt.LayerId();
240   HYDROData_SequenceOfObjects::Iterator anUnorderedIter( anUnorderedToDisplay );
241   for ( ; anUnorderedIter.More(); anUnorderedIter.Next() ) {
242     Handle(HYDROData_Entity) anObj = anUnorderedIter.Value();
243     if ( Display( anObj, aViewer, theIsForced) ) {
244       // set Z layer ( one Z layer for all unordered objects )
245       SetZLayer( aViewer, anObj, anUnorderedZLayerId );
246       if ( !isDisplayed ) {
247         SetZLayerSettings( aViewer3d, anUnorderedZLayerId, false );
248       }
249       isDisplayed = true;
250     }
251   }
252   
253   // 3. Update the top Z layer index
254   if ( isDisplayed ) {
255     aZLayersIt.Next();
256   }
257
258   // Update Z layer of the active operation
259   int aPreviewZLayerId = aZLayersIt.LayerId();
260
261   HYDROGUI_Module* aModule = module();
262   SUIT_Operation* anOp = aModule->activeOperation();
263   HYDROGUI_Operation* aHOp = anOp ? dynamic_cast<HYDROGUI_Operation*>( anOp ) : 0;
264   if ( aHOp && aHOp->getPreviewZLayer() >= 0 ) {
265     aHOp->updatePreviewZLayer( aPreviewZLayerId );
266     aZLayersIt.Next();
267   }
268
269   // Update Z layer of hilight presentations
270   int aHilightLayer = aZLayersIt.TopLayer();
271   UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, aHilightLayer );
272
273   // Fit all / update selection
274   if ( theDoFitAll ) {
275     OCCViewer_ViewManager* aViewManager
276       = ::qobject_cast<OCCViewer_ViewManager*>( aViewer->getViewManager() );
277     if ( aViewManager ) {
278       OCCViewer_ViewWindow* aViewWindow = 
279         ::qobject_cast<OCCViewer_ViewWindow*>( aViewManager->getActiveView() );
280       if ( aViewWindow ) {
281         aViewWindow->onFitAll();
282       }
283     }
284   } 
285   else if ( !aCtx.IsNull() ) { // TODO: determine if this code is necessary (added as a fix for issue# 359)
286     aCtx->UpdateSelected();
287   }
288
289   UpdateColorScale( aViewer );
290 }
291
292 void HYDROGUI_OCCDisplayer::purgeObjects( const int theViewerId )
293 {
294   OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
295   if( !aViewer )
296     return;
297
298   Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
299   if( aCtx.IsNull() )
300     return;
301
302   AIS_ListOfInteractive aDisplayedObjects;
303   aCtx->DisplayedObjects( aDisplayedObjects );
304
305   AIS_ListIteratorOfListOfInteractive aListIter( aDisplayedObjects );
306   for ( ; aListIter.More(); aListIter.Next() )
307   {
308     Handle(AIS_InteractiveObject) aPrsObj = aListIter.Value();
309     if ( aPrsObj.IsNull() )
310       continue;
311
312     Handle(HYDROData_Entity) anOwnerObj = 
313       Handle(HYDROData_Entity)::DownCast( aPrsObj->GetOwner() );
314     if ( !anOwnerObj.IsNull() && anOwnerObj->IsRemoved() )
315       module()->removeObjectShape( (size_t)aViewer, anOwnerObj );
316   }
317 }
318
319 QString HYDROGUI_OCCDisplayer::GetType() const
320 {
321   return OCCViewer_Viewer::Type();
322 }
323
324 bool HYDROGUI_OCCDisplayer::Display( const Handle(HYDROData_Entity)& theObject,
325                                      const OCCViewer_Viewer* theViewer,
326                                      const bool theIsForced )
327 {
328   bool aRes = false;
329
330   if ( theObject.IsNull() || theObject->IsRemoved() || !theViewer ) {
331     return aRes;
332   }
333
334   // Get interactive context
335   Handle(AIS_InteractiveContext) aCtx = theViewer->getAISContext();
336   if( aCtx.IsNull() ) {
337     return aRes;
338   }
339
340   // Viewer id
341   size_t aViewerId = (size_t)theViewer;
342
343   // Object shape 
344   HYDROGUI_Shape* anObjShape = module()->getObjectShape( aViewerId, theObject );
345   // create if needed
346   if ( !anObjShape ) {
347     anObjShape = createShape( aViewerId, aCtx, theObject );
348     if ( anObjShape ) {
349       anObjShape->setIsToUpdate( true );
350     }
351   }
352   
353   // Process the shape
354   if ( anObjShape ) {
355     // update if needed
356     if ( anObjShape->getIsToUpdate() || theIsForced ) {
357       anObjShape->update( false, false );
358     }
359
360     // Set visibility
361     bool anIsVisible = module()->isObjectVisible( aViewerId, theObject );
362     anObjShape->setVisible( anIsVisible, false );
363
364     aRes = true;
365   }
366
367   return aRes;
368 }
369
370 void HYDROGUI_OCCDisplayer::SetZLayer( const OCCViewer_Viewer* theViewer,
371                                        const Handle(HYDROData_Entity)& theObject, 
372                                        const int theZLayerId )
373 {
374   if ( !theViewer || ( theZLayerId < 0 ) ) {
375     return;
376   }
377   
378   // Get interactive context
379   Handle(AIS_InteractiveContext) aCtx = theViewer->getAISContext();
380   if( aCtx.IsNull() ) {
381     return;
382   }
383
384   // Get viewer id
385   size_t aViewerId = (size_t)theViewer;
386
387   // Get object shape 
388   HYDROGUI_Shape* anObjShape = module()->getObjectShape( aViewerId, theObject );
389
390   // Set Z layer
391   if ( anObjShape ) {
392     aCtx->SetZLayer( anObjShape->getAISObject(), theZLayerId );
393   }
394 }
395
396 void HYDROGUI_OCCDisplayer::SetToUpdateColorScale()
397 {
398   myToUpdateColorScale = true;
399 }
400
401 void HYDROGUI_OCCDisplayer::UpdateColorScale( const OCCViewer_Viewer* theViewer )
402 {
403   if( !myToUpdateColorScale )
404     return;
405
406   OCCViewer_ViewWindow* aWnd = dynamic_cast<OCCViewer_ViewWindow*>( theViewer->getViewManager()->getActiveView() );
407   Handle(V3d_View) aView = aWnd->getViewPort()->getView();
408
409   int aViewerId = (size_t)theViewer;//TODO: check if viewer id is correct
410   QList<HYDROGUI_Shape*> aShapes = module()->getObjectShapes( aViewerId, KIND_BATHYMETRY );
411
412   bool isDisplayColorScale = !aShapes.empty();
413   Standard_Real anXPos = 0.05; //TODO
414   Standard_Real anYPos = 0.1; //TODO
415   Standard_Real aWidth = 0.2; //TODO
416   Standard_Real aHeight = 0.5; //TODO
417   Standard_Integer aTextHeight = 14; //TODO
418   Standard_Integer aNbIntervals = 20; //TODO
419   TCollection_ExtendedString aColorScaleTitle = "";//TODO
420
421   Standard_Real aColorScaleMin = 0, aColorScaleMax = 1, aMin, aMax;
422   bool isFirst = true;
423   foreach( HYDROGUI_Shape* aShape, aShapes )
424   {
425     HYDROGUI_ShapeBathymetry* aBathShape = dynamic_cast<HYDROGUI_ShapeBathymetry*>( aShape );
426     if( !aBathShape || !aBathShape->isVisible() )
427       continue;
428
429     aBathShape->GetRange( aMin, aMax );
430
431     if( isFirst || aMin < aColorScaleMin )
432       aColorScaleMin = aMin;
433     if( isFirst || aMax > aColorScaleMax )
434       aColorScaleMax = aMax;
435
436     isFirst = false;
437   }
438
439   if( isDisplayColorScale )
440   {
441     Handle(Aspect_ColorScale) aColorScale = aView->ColorScale();
442     if( !aColorScale.IsNull() )
443     {
444       aColorScale->SetXPosition( anXPos );
445       aColorScale->SetYPosition( anYPos );
446       aColorScale->SetWidth( aWidth );
447       aColorScale->SetHeight( aHeight );
448
449       aColorScale->SetTextHeight( aTextHeight );
450       aColorScale->SetNumberOfIntervals( aNbIntervals );
451
452       aColorScale->SetTitle( aColorScaleTitle );
453       aColorScale->SetRange( aColorScaleMin, aColorScaleMax );
454
455       foreach( HYDROGUI_Shape* aShape, aShapes )
456       {
457         HYDROGUI_ShapeBathymetry* aBathShape = dynamic_cast<HYDROGUI_ShapeBathymetry*>( aShape );
458         if( !aBathShape || !aBathShape->isVisible() )
459           continue;
460
461         aBathShape->UpdateWithColorScale( aColorScale );
462       }
463     }
464     if( !aView->ColorScaleIsDisplayed() )
465       aView->ColorScaleDisplay();
466   }
467   else
468   {
469     if( aView->ColorScaleIsDisplayed() )
470       aView->ColorScaleErase();
471   }
472
473   myToUpdateColorScale = false;
474 }