Salome HOME
feed7992d67883154a4baa224d5ff72824a839de
[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_Tool2.h"
24 #include <HYDROGUI_ShapeImage.h>
25 #include <HYDROGUI_ShapeBathymetry.h>
26 #include <HYDROGUI_ShapeLandCoverMap.h>
27 #include "HYDROGUI_Operation.h"
28 #include "HYDROGUI_DataObject.h"
29 #include "HYDROGUI_ZLayers.h"
30 #include "HYDROGUI_Polyline.h"
31
32 #include <HYDROData_Bathymetry.h>
33 #include <HYDROData_Image.h>
34 #include <HYDROData_LandCoverMap.h>
35 #include <HYDROData_StricklerTable.h>
36
37 #include <AIS_InteractiveContext.hxx>
38 #include <AIS_ListIteratorOfListOfInteractive.hxx>
39 #include <AIS_ListOfInteractive.hxx>
40 #include <AIS_ColorScale.hxx>
41
42 #include <TColStd_SequenceOfInteger.hxx>
43
44 #include <LightApp_Application.h>
45 #include <SUIT_Study.h>
46
47 #include <OCCViewer_ViewManager.h>
48 #include <OCCViewer_ViewModel.h>
49 #include <OCCViewer_ViewWindow.h>
50 #include <OCCViewer_ViewPort3d.h>
51
52 HYDROGUI_OCCDisplayer::HYDROGUI_OCCDisplayer( HYDROGUI_Module* theModule )
53 : HYDROGUI_AbstractDisplayer( theModule )
54 {
55   myToUpdateColorScale = false;
56 }
57
58 HYDROGUI_OCCDisplayer::~HYDROGUI_OCCDisplayer()
59 {
60 }
61
62 void HYDROGUI_OCCDisplayer::SetToUpdate( const HYDROData_SequenceOfObjects& theObjs,
63                                          const int                          theViewerId )
64 {
65   OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
66   if( !aViewer )
67     return;
68
69   for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
70   {
71     Handle(HYDROData_Entity) anObj = theObjs.Value( i );
72     if( anObj.IsNull() )
73       continue;
74
75     HYDROGUI_Shape* anObjShape = module()->getObjectShape( (size_t)aViewer, anObj );
76     if ( !anObjShape )
77       continue;
78     
79     anObjShape->setIsToUpdate( true );
80   }
81 }
82
83 int HYDROGUI_OCCDisplayer::AddPreviewZLayer( OCCViewer_ViewManager* theMgr )
84 {
85   int aLayer = -1;
86   OCCViewer_Viewer* aViewer = theMgr->getOCCViewer();
87   if ( !aViewer )
88     return aLayer;
89
90   aLayer = CreateTopZLayer( aViewer->getViewer3d() );
91   
92   // Hilight presentation should be on top
93   Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
94   if( !aCtx.IsNull() ) {
95     int aTopLayer = CreateTopZLayer( aViewer->getViewer3d() );
96     if ( aTopLayer > 0 ) {
97       UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, aTopLayer );
98     }
99   }
100
101   return aLayer;
102 }
103
104 void HYDROGUI_OCCDisplayer::RemoveZLayer( OCCViewer_ViewManager* theMgr,
105                                           const int theLayer )
106 {
107   if ( theLayer < 0 )
108     return;
109
110   OCCViewer_Viewer* aViewer = theMgr->getOCCViewer();
111   if ( !aViewer )
112     return;
113
114   // Get existing Z layers
115   TColStd_SequenceOfInteger anExistingZLayers;
116   aViewer->getViewer3d()->GetAllZLayers( anExistingZLayers );
117   int aNbLayers = anExistingZLayers.Length();
118   
119   if ( theLayer < aNbLayers )
120     aViewer->getViewer3d()->RemoveZLayer( theLayer );
121 }
122
123 void HYDROGUI_OCCDisplayer::EraseAll( const int theViewerId )
124 {
125   OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
126   if( !aViewer )
127     return;
128
129   module()->removeViewShapes( (size_t)aViewer );
130   UpdateColorScale( aViewer );
131 }
132
133 void HYDROGUI_OCCDisplayer::Erase( const HYDROData_SequenceOfObjects& theObjs,
134                                    const int                          theViewerId )
135 {
136   OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
137   if( !aViewer )
138     return;
139
140   for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
141   {
142     Handle(HYDROData_Entity) anObj = theObjs.Value( i );
143     if( anObj.IsNull() )
144       continue;
145
146     module()->removeObjectShape( (size_t)aViewer, anObj );
147   }
148   aViewer->update();
149   if ( !module()->isLandCoversScalarMapModeOn( (size_t)aViewer ) ) {
150     UpdateColorScale( aViewer );
151   }
152 }
153
154 HYDROGUI_Shape* HYDROGUI_OCCDisplayer::createShape( const int                             theViewerId,
155                                                     const Handle(AIS_InteractiveContext)& theContext,
156                                                     const Handle(HYDROData_Entity)&       theObject )
157 {
158   HYDROGUI_Shape* aResShape = NULL;
159   if ( theContext.IsNull() || theObject.IsNull() )
160     return aResShape;
161
162   if ( !HYDROGUI_Tool::IsObjectHasPresentation( theObject, OCCViewer_Viewer::Type() ) )
163     return aResShape;
164
165   if( theObject->IsKind( STANDARD_TYPE( HYDROData_Image ) ) )
166     aResShape = new HYDROGUI_ShapeImage( theContext, Handle(HYDROData_Image)::DownCast( theObject ) );
167   else if( theObject->IsKind( STANDARD_TYPE( HYDROData_Bathymetry ) ) )
168     aResShape = new HYDROGUI_ShapeBathymetry( this, theContext, Handle(HYDROData_Bathymetry)::DownCast( theObject ) );
169   else if( theObject->IsKind( STANDARD_TYPE( HYDROData_LandCoverMap ) ) ) {
170     bool isScalarMode = module()->isLandCoversScalarMapModeOn( theViewerId );
171     aResShape = new HYDROGUI_ShapeLandCoverMap( this, theContext, Handle(HYDROData_LandCoverMap)::DownCast( theObject ), -1, isScalarMode );
172   }
173   else
174     aResShape = new HYDROGUI_Shape( theContext, theObject );
175
176   module()->setObjectShape( theViewerId, theObject, aResShape );
177
178   return aResShape;
179 }
180
181 void HYDROGUI_OCCDisplayer::Display( const HYDROData_SequenceOfObjects& theObjs,
182                                      const int                          theViewerId,
183                                      const bool                         theIsForced,
184                                      const bool theDoFitAll )
185 {
186   // Get OCC viewer by id
187   OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
188   if( !aViewer )
189     return;
190
191   // Get interactive context
192   Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
193   if( aCtx.IsNull() )
194     return;
195
196   // Get the document
197   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( module()->getStudyId() );
198   if ( !aDoc )
199     return;
200   
201   // Assign Z layer indexes to the objects
202   aDoc->Show( theObjs );
203
204   // Sort objects by display order ( needed for Z layers assignment only )
205   HYDROData_SequenceOfObjects anUnorderedToDisplay = theObjs;
206   HYDROData_SequenceOfObjects anOrderedToDisplay;
207   HYDROData_SequenceOfObjects anAllOrderedObjects = aDoc->GetObjectsLayerOrder();
208
209   HYDROData_SequenceOfObjects::Iterator anAllOrderedIter( anAllOrderedObjects );
210   for ( ; anAllOrderedIter.More(); anAllOrderedIter.Next() ) {
211     QString anOrderedEntry = 
212       HYDROGUI_DataObject::dataObjectEntry( anAllOrderedIter.Value() );
213     
214     HYDROData_SequenceOfObjects::Iterator aToDisplayIter( anUnorderedToDisplay );
215     for ( ; aToDisplayIter.More(); aToDisplayIter.Next() ) {
216       Handle(HYDROData_Entity) anObjToDisplay = aToDisplayIter.Value();
217       QString anEntry = HYDROGUI_DataObject::dataObjectEntry( anObjToDisplay );
218       if ( anEntry == anOrderedEntry ) {
219         anOrderedToDisplay.Prepend( anObjToDisplay );
220         anUnorderedToDisplay.Remove( aToDisplayIter );
221         break;
222       }
223     }
224   }
225   
226   // Get 3d viewer
227   Handle(V3d_Viewer) aViewer3d = aViewer->getViewer3d();
228
229   // Display objects:
230   HYDROGUI_ZLayersIterator aZLayersIt( aViewer->getViewer3d() );
231   if ( !aZLayersIt.More() ) {
232     aZLayersIt.Next();
233   }
234
235   // 1. Display the ordered objects:
236   HYDROData_SequenceOfObjects::Iterator anOrderedIter( anOrderedToDisplay );
237   for ( ; anOrderedIter.More(); anOrderedIter.Next() ) {
238     Handle(HYDROData_Entity) anObj = anOrderedIter.Value();
239     if ( Display( anObj, aViewer, theIsForced ) ) {
240       // set Z layer ( one Z layer for each ordered object )
241       int aZLayerId = aZLayersIt.LayerId();
242       SetZLayer( aViewer, anObj, aZLayerId );
243       SetZLayerSettings( aViewer3d, aZLayerId, true );
244       aZLayersIt.Next();
245     }
246   }
247
248   // 2. Display the unordered objects:
249   bool isDisplayed = false;
250   int anUnorderedZLayerId = aZLayersIt.LayerId();
251   HYDROData_SequenceOfObjects::Iterator anUnorderedIter( anUnorderedToDisplay );
252   for ( ; anUnorderedIter.More(); anUnorderedIter.Next() ) {
253     Handle(HYDROData_Entity) anObj = anUnorderedIter.Value();
254     if ( Display( anObj, aViewer, theIsForced) ) {
255       // set Z layer ( one Z layer for all unordered objects )
256       SetZLayer( aViewer, anObj, anUnorderedZLayerId );
257       if ( !isDisplayed ) {
258         SetZLayerSettings( aViewer3d, anUnorderedZLayerId, false );
259       }
260       isDisplayed = true;
261     }
262   }
263   
264   // 3. Update the top Z layer index
265   if ( isDisplayed ) {
266     aZLayersIt.Next();
267   }
268
269   // Update Z layer of the active operation
270   int aPreviewZLayerId = aZLayersIt.LayerId();
271
272   HYDROGUI_Module* aModule = module();
273   SUIT_Operation* anOp = aModule->activeOperation();
274   HYDROGUI_Operation* aHOp = anOp ? dynamic_cast<HYDROGUI_Operation*>( anOp ) : 0;
275   if ( aHOp && aHOp->getPreviewZLayer() >= 0 ) {
276     aHOp->updatePreviewZLayer( aPreviewZLayerId );
277     aZLayersIt.Next();
278   }
279
280   // Update Z layer of hilight presentations
281   int aHilightLayer = aZLayersIt.TopLayer();
282   UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, aHilightLayer );
283
284   // Fit all / update selection
285   if ( theDoFitAll ) {
286     OCCViewer_ViewManager* aViewManager
287       = ::qobject_cast<OCCViewer_ViewManager*>( aViewer->getViewManager() );
288     if ( aViewManager ) {
289       OCCViewer_ViewWindow* aViewWindow = 
290         ::qobject_cast<OCCViewer_ViewWindow*>( aViewManager->getActiveView() );
291       if ( aViewWindow ) {
292         aViewWindow->onFitAll();
293       }
294     }
295   } 
296   else if ( !aCtx.IsNull() ) { // TODO: determine if this code is necessary (added as a fix for issue# 359)
297     aCtx->UpdateSelected();
298   }
299
300   UpdateColorScale( aViewer );
301 }
302
303 void HYDROGUI_OCCDisplayer::purgeObjects( const int theViewerId )
304 {
305   OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
306   if( !aViewer )
307     return;
308
309   Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
310   if( aCtx.IsNull() )
311     return;
312
313   AIS_ListOfInteractive aDisplayedObjects;
314   aCtx->DisplayedObjects( aDisplayedObjects );
315
316   AIS_ListIteratorOfListOfInteractive aListIter( aDisplayedObjects );
317   for ( ; aListIter.More(); aListIter.Next() )
318   {
319     Handle(AIS_InteractiveObject) aPrsObj = aListIter.Value();
320     if ( aPrsObj.IsNull() )
321       continue;
322
323     Handle(HYDROData_Entity) anOwnerObj = 
324       Handle(HYDROData_Entity)::DownCast( aPrsObj->GetOwner() );
325     if ( !anOwnerObj.IsNull() && anOwnerObj->IsRemoved() )
326       module()->removeObjectShape( (size_t)aViewer, anOwnerObj );
327   }
328 }
329
330 QString HYDROGUI_OCCDisplayer::GetType() const
331 {
332   return OCCViewer_Viewer::Type();
333 }
334
335 bool HYDROGUI_OCCDisplayer::Display( const Handle(HYDROData_Entity)& theObject,
336                                      const OCCViewer_Viewer* theViewer,
337                                      const bool theIsForced )
338 {
339   bool aRes = false;
340
341   if ( theObject.IsNull() || theObject->IsRemoved() || !theViewer ) {
342     return aRes;
343   }
344
345   // Get interactive context
346   Handle(AIS_InteractiveContext) aCtx = theViewer->getAISContext();
347   if( aCtx.IsNull() ) {
348     return aRes;
349   }
350
351   // Viewer id
352   size_t aViewerId = (size_t)theViewer;
353
354   // Object shape 
355   HYDROGUI_Shape* anObjShape = module()->getObjectShape( aViewerId, theObject );
356   // create if needed
357   if ( !anObjShape ) {
358     anObjShape = createShape( aViewerId, aCtx, theObject );
359     if ( anObjShape ) {
360       anObjShape->setIsToUpdate( true );
361     }
362   }
363   
364   // Process the shape
365   if ( anObjShape ) {
366     // update if needed
367     if ( anObjShape->getIsToUpdate() || theIsForced ) {
368       anObjShape->update( false, false );
369     }
370
371     // Set visibility
372     bool anIsVisible = module()->isObjectVisible( aViewerId, theObject );
373     anObjShape->setVisible( anIsVisible, false );
374
375     aRes = true;
376   }
377
378   return aRes;
379 }
380
381 void HYDROGUI_OCCDisplayer::SetZLayer( const OCCViewer_Viewer* theViewer,
382                                        const Handle(HYDROData_Entity)& theObject, 
383                                        const int theZLayerId )
384 {
385   if ( !theViewer /*|| ( theZLayerId < 0 )*/ ) {
386     return;
387   }
388   
389   // Get interactive context
390   Handle(AIS_InteractiveContext) aCtx = theViewer->getAISContext();
391   if( aCtx.IsNull() ) {
392     return;
393   }
394
395   // Get viewer id
396   size_t aViewerId = (size_t)theViewer;
397
398   // Get object shape 
399   HYDROGUI_Shape* anObjShape = module()->getObjectShape( aViewerId, theObject );
400
401   // Set Z layer
402   if ( anObjShape )
403   {
404     QList<Handle(AIS_InteractiveObject)> shapes = anObjShape->getAISObjects();
405     foreach( Handle(AIS_InteractiveObject) shape, shapes )
406       aCtx->SetZLayer( shape, theZLayerId );
407   }
408 }
409
410 void HYDROGUI_OCCDisplayer::SetToUpdateColorScale()
411 {
412   myToUpdateColorScale = true;
413 }
414
415 void HYDROGUI_OCCDisplayer::UpdateColorScale( const OCCViewer_Viewer* theViewer )
416 {
417   if( !myToUpdateColorScale || !theViewer )
418     return;
419   
420   OCCViewer_ViewWindow* aWnd = dynamic_cast<OCCViewer_ViewWindow*>( theViewer->getViewManager()->getActiveView() );
421   Handle(V3d_View) aView = aWnd->getViewPort()->getView();
422       
423   int aViewerId = (size_t)theViewer;//TODO: check if viewer id is correct
424   bool isLandCoverColoringOn = module()->isLandCoversScalarMapModeOn( aViewerId );
425     
426   QList<HYDROGUI_Shape*> aLandCoverMapShapes = module()->getObjectShapes( aViewerId, KIND_LAND_COVER_MAP );
427   QList<HYDROGUI_Shape*> aBathShapes = module()->getObjectShapes( aViewerId, KIND_BATHYMETRY );
428
429   bool isDisplayColorScale = !aBathShapes.empty() || isLandCoverColoringOn;
430
431   Standard_Real aColorScaleMin = 0, aColorScaleMax = 1;
432
433   // Get range
434   Handle(HYDROData_StricklerTable) aTable;
435   QStringList aTableTypes;
436   if ( isLandCoverColoringOn ) {
437     aTable = module()->getLandCoverColoringTable( aViewerId );
438     if ( !aTable.IsNull() ) {
439       // TODO: non-empty title leads to buggy behaviour
440       // aColorScaleTitle = TCollection_ExtendedString( aTable->GetName().toLatin1().constData() );
441       aTable->GetCoefficientRange( aColorScaleMin, aColorScaleMax );
442       aTableTypes = aTable->GetTypes();
443     }
444   } else {
445     Standard_Real aMin, aMax;
446     bool isFirst = true;
447     foreach( HYDROGUI_Shape* aShape, aBathShapes )
448     {
449       HYDROGUI_ShapeBathymetry* aBathShape = dynamic_cast<HYDROGUI_ShapeBathymetry*>( aShape );
450       if( !aBathShape || !aBathShape->isVisible() )
451         continue;
452
453       aBathShape->GetRange( aMin, aMax );
454
455       if( isFirst || aMin < aColorScaleMin )
456         aColorScaleMin = aMin;
457       if( isFirst || aMax > aColorScaleMax )
458         aColorScaleMax = aMax;
459
460       isFirst = false;
461     }
462   }
463
464   Handle(AIS_ColorScale) aColorScale = GetColorScale( aViewerId );
465   Handle(AIS_InteractiveContext) aCtx = theViewer->getAISContext();
466   if( isDisplayColorScale )
467   {
468     if( !aColorScale.IsNull() )
469     {
470       // Set color scale title
471       TCollection_ExtendedString aColorScaleTitle = ""; //TODO
472       aColorScale->SetTitle( aColorScaleTitle );
473       
474       // Set color scale range
475       aColorScale->SetRange( aColorScaleMin, aColorScaleMax );
476       
477       aColorScale->SetToUpdate();
478
479       if ( !isLandCoverColoringOn ) {
480         foreach( HYDROGUI_Shape* aShape, aBathShapes ) {
481           HYDROGUI_ShapeBathymetry* aBathShape = dynamic_cast<HYDROGUI_ShapeBathymetry*>( aShape );
482           if( !aBathShape || !aBathShape->isVisible() )
483             continue;
484
485           aBathShape->UpdateWithColorScale( aColorScale );
486         }
487       }
488       
489       if ( !aCtx.IsNull()/* && !aCtx->IsDisplayed( aColorScale ) */) {
490         if ( !aCtx->IsDisplayed( aColorScale ) ) {
491           aCtx->Display( aColorScale, Standard_False );
492         }
493
494         aCtx->Update( aColorScale );
495       }
496     }
497   }
498   else
499   {
500     if ( !aCtx.IsNull() && aCtx->IsDisplayed( aColorScale ) ) {
501       aCtx->Erase( aColorScale );
502     }
503   }
504
505   foreach( HYDROGUI_Shape* aShape, aLandCoverMapShapes ) {
506     HYDROGUI_ShapeLandCoverMap* aLandCoverMapShape = 
507       dynamic_cast<HYDROGUI_ShapeLandCoverMap*>( aShape );
508
509     if ( !aLandCoverMapShape || !aLandCoverMapShape->isVisible() ) {
510       continue;
511     }
512     
513     Handle(HYDROData_LandCoverMap) aLandCoverMap = 
514       Handle(HYDROData_LandCoverMap)::DownCast( aLandCoverMapShape->getObject() );
515
516     if ( aLandCoverMap.IsNull() ) {
517       continue;
518     }
519     
520     bool isScalarMode = aLandCoverMapShape->isScalarMapModeEnabled();
521     if( isScalarMode != isLandCoverColoringOn )
522     {
523       aLandCoverMapShape->setScalarMapModeEnabled( isLandCoverColoringOn );
524       theViewer->getAISContext()->Redisplay( aLandCoverMapShape->getAISObjects()[0], Standard_False );
525     }
526   }
527   
528   myToUpdateColorScale = false;
529 }
530
531 Handle(AIS_ColorScale) HYDROGUI_OCCDisplayer::GetColorScale( const int theViewerId )
532 {
533   Handle(AIS_ColorScale) aColorScale;
534
535   aColorScale = myColorScales.value( theViewerId, aColorScale );
536   if ( aColorScale.IsNull() ) {
537     // Create color scale
538     aColorScale = new AIS_ColorScale();
539
540     // Set properties
541     Standard_Integer anXPos = 50; //TODO
542     Standard_Integer anYPos = 100; //TODO
543     Standard_Integer aWidth = 100; //TODO
544     Standard_Integer aHeight = 350; //TODO
545     
546     Standard_Integer aTextHeight = 14; //TODO
547     Standard_Integer aNbIntervals = 20; //TODO
548
549     aColorScale->SetTransformPersistence( Graphic3d_TMF_2d, gp_Pnt( -1, -1, 0 ) );
550     
551     aColorScale->SetXPosition( anXPos );
552     aColorScale->SetYPosition( anYPos );
553     aColorScale->SetSize( aWidth, aHeight );
554
555     aColorScale->SetTextHeight( aTextHeight );
556     aColorScale->SetNumberOfIntervals( aNbIntervals );
557
558     // Put into the map
559     myColorScales.insert( theViewerId, aColorScale );
560   }
561
562   return aColorScale;
563 }
564
565 void HYDROGUI_OCCDisplayer::UpdatePolylines( int theViewerId, int theType, int theSize )
566 {
567   OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
568   if( !aViewer )
569     return;
570
571   // Get interactive context
572   Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
573   if( aCtx.IsNull() )
574     return;
575
576   AIS_ListOfInteractive objs;
577   aCtx->DisplayedObjects( objs );
578   AIS_ListOfInteractive::const_iterator it = objs.begin(), last = objs.end();
579   for( ; it!=last; it++ )
580   {
581     Handle(HYDROGUI_Arrow) arr = Handle(HYDROGUI_Arrow)::DownCast( *it );
582     if( !arr.IsNull() )
583     {
584       if( theType>=0 )
585         arr->SetType( (HYDROGUI_Arrow::Type)theType );
586       if( theSize>=0 )
587         arr->SetSize( theSize );
588       aCtx->Redisplay( arr, Standard_False );
589     }
590   }
591   aCtx->UpdateCurrentViewer();
592 }