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