Salome HOME
Remove references to land cover object from data model and GUI in order to have a...
[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 // TODO
27 //#include <HYDROGUI_ShapeLandCoverMap.h>
28 #include "HYDROGUI_Operation.h"
29 #include "HYDROGUI_DataObject.h"
30 #include "HYDROGUI_ZLayers.h"
31
32 #include <HYDROData_Bathymetry.h>
33 #include <HYDROData_LandCoverMap.h>
34 #include <HYDROData_StricklerTable.h>
35
36 #include <AIS_InteractiveContext.hxx>
37 #include <AIS_ListIteratorOfListOfInteractive.hxx>
38 #include <AIS_ListOfInteractive.hxx>
39
40 #include <Aspect_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     /* TODO
172     aResShape = new HYDROGUI_ShapeLandCover( this, theContext, Handle_HYDROData_LandCoverMap::DownCast( theObject ), -1, isScalarMode );
173     */
174   }
175   else
176     aResShape = new HYDROGUI_Shape( theContext, theObject );
177
178   module()->setObjectShape( theViewerId, theObject, aResShape );
179
180   return aResShape;
181 }
182
183 void HYDROGUI_OCCDisplayer::Display( const HYDROData_SequenceOfObjects& theObjs,
184                                      const int                          theViewerId,
185                                      const bool                         theIsForced,
186                                      const bool theDoFitAll )
187 {
188   // Get OCC viewer by id
189   OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
190   if( !aViewer )
191     return;
192
193   // Get interactive context
194   Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
195   if( aCtx.IsNull() )
196     return;
197
198   // Get the document
199   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( module()->getStudyId() );
200   if ( !aDoc )
201     return;
202   
203   // Assign Z layer indexes to the objects
204   aDoc->Show( theObjs );
205
206   // Sort objects by display order ( needed for Z layers assignment only )
207   HYDROData_SequenceOfObjects anUnorderedToDisplay = theObjs;
208   HYDROData_SequenceOfObjects anOrderedToDisplay;
209   HYDROData_SequenceOfObjects anAllOrderedObjects = aDoc->GetObjectsLayerOrder();
210
211   HYDROData_SequenceOfObjects::Iterator anAllOrderedIter( anAllOrderedObjects );
212   for ( ; anAllOrderedIter.More(); anAllOrderedIter.Next() ) {
213     QString anOrderedEntry = 
214       HYDROGUI_DataObject::dataObjectEntry( anAllOrderedIter.Value() );
215     
216     HYDROData_SequenceOfObjects::Iterator aToDisplayIter( anUnorderedToDisplay );
217     for ( ; aToDisplayIter.More(); aToDisplayIter.Next() ) {
218       Handle(HYDROData_Entity) anObjToDisplay = aToDisplayIter.Value();
219       QString anEntry = HYDROGUI_DataObject::dataObjectEntry( anObjToDisplay );
220       if ( anEntry == anOrderedEntry ) {
221         anOrderedToDisplay.Prepend( anObjToDisplay );
222         anUnorderedToDisplay.Remove( aToDisplayIter );
223         break;
224       }
225     }
226   }
227   
228   // Get 3d viewer
229   Handle(V3d_Viewer) aViewer3d = aViewer->getViewer3d();
230
231   // Display objects:
232   HYDROGUI_ZLayersIterator aZLayersIt( aViewer->getViewer3d() );
233   if ( !aZLayersIt.More() ) {
234     aZLayersIt.Next();
235   }
236
237   // 1. Display the ordered objects:
238   HYDROData_SequenceOfObjects::Iterator anOrderedIter( anOrderedToDisplay );
239   for ( ; anOrderedIter.More(); anOrderedIter.Next() ) {
240     Handle(HYDROData_Entity) anObj = anOrderedIter.Value();
241     if ( Display( anObj, aViewer, theIsForced ) ) {
242       // set Z layer ( one Z layer for each ordered object )
243       int aZLayerId = aZLayersIt.LayerId();
244       SetZLayer( aViewer, anObj, aZLayerId );
245       SetZLayerSettings( aViewer3d, aZLayerId, true );
246       aZLayersIt.Next();
247     }
248   }
249
250   // 2. Display the unordered objects:
251   bool isDisplayed = false;
252   int anUnorderedZLayerId = aZLayersIt.LayerId();
253   HYDROData_SequenceOfObjects::Iterator anUnorderedIter( anUnorderedToDisplay );
254   for ( ; anUnorderedIter.More(); anUnorderedIter.Next() ) {
255     Handle(HYDROData_Entity) anObj = anUnorderedIter.Value();
256     if ( Display( anObj, aViewer, theIsForced) ) {
257       // set Z layer ( one Z layer for all unordered objects )
258       SetZLayer( aViewer, anObj, anUnorderedZLayerId );
259       if ( !isDisplayed ) {
260         SetZLayerSettings( aViewer3d, anUnorderedZLayerId, false );
261       }
262       isDisplayed = true;
263     }
264   }
265   
266   // 3. Update the top Z layer index
267   if ( isDisplayed ) {
268     aZLayersIt.Next();
269   }
270
271   // Update Z layer of the active operation
272   int aPreviewZLayerId = aZLayersIt.LayerId();
273
274   HYDROGUI_Module* aModule = module();
275   SUIT_Operation* anOp = aModule->activeOperation();
276   HYDROGUI_Operation* aHOp = anOp ? dynamic_cast<HYDROGUI_Operation*>( anOp ) : 0;
277   if ( aHOp && aHOp->getPreviewZLayer() >= 0 ) {
278     aHOp->updatePreviewZLayer( aPreviewZLayerId );
279     aZLayersIt.Next();
280   }
281
282   // Update Z layer of hilight presentations
283   int aHilightLayer = aZLayersIt.TopLayer();
284   UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, aHilightLayer );
285
286   // Fit all / update selection
287   if ( theDoFitAll ) {
288     OCCViewer_ViewManager* aViewManager
289       = ::qobject_cast<OCCViewer_ViewManager*>( aViewer->getViewManager() );
290     if ( aViewManager ) {
291       OCCViewer_ViewWindow* aViewWindow = 
292         ::qobject_cast<OCCViewer_ViewWindow*>( aViewManager->getActiveView() );
293       if ( aViewWindow ) {
294         aViewWindow->onFitAll();
295       }
296     }
297   } 
298   else if ( !aCtx.IsNull() ) { // TODO: determine if this code is necessary (added as a fix for issue# 359)
299     aCtx->UpdateSelected();
300   }
301
302   UpdateColorScale( aViewer );
303 }
304
305 void HYDROGUI_OCCDisplayer::purgeObjects( const int theViewerId )
306 {
307   OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
308   if( !aViewer )
309     return;
310
311   Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
312   if( aCtx.IsNull() )
313     return;
314
315   AIS_ListOfInteractive aDisplayedObjects;
316   aCtx->DisplayedObjects( aDisplayedObjects );
317
318   AIS_ListIteratorOfListOfInteractive aListIter( aDisplayedObjects );
319   for ( ; aListIter.More(); aListIter.Next() )
320   {
321     Handle(AIS_InteractiveObject) aPrsObj = aListIter.Value();
322     if ( aPrsObj.IsNull() )
323       continue;
324
325     Handle(HYDROData_Entity) anOwnerObj = 
326       Handle(HYDROData_Entity)::DownCast( aPrsObj->GetOwner() );
327     if ( !anOwnerObj.IsNull() && anOwnerObj->IsRemoved() )
328       module()->removeObjectShape( (size_t)aViewer, anOwnerObj );
329   }
330 }
331
332 QString HYDROGUI_OCCDisplayer::GetType() const
333 {
334   return OCCViewer_Viewer::Type();
335 }
336
337 bool HYDROGUI_OCCDisplayer::Display( const Handle(HYDROData_Entity)& theObject,
338                                      const OCCViewer_Viewer* theViewer,
339                                      const bool theIsForced )
340 {
341   bool aRes = false;
342
343   if ( theObject.IsNull() || theObject->IsRemoved() || !theViewer ) {
344     return aRes;
345   }
346
347   // Get interactive context
348   Handle(AIS_InteractiveContext) aCtx = theViewer->getAISContext();
349   if( aCtx.IsNull() ) {
350     return aRes;
351   }
352
353   // Viewer id
354   size_t aViewerId = (size_t)theViewer;
355
356   // Object shape 
357   HYDROGUI_Shape* anObjShape = module()->getObjectShape( aViewerId, theObject );
358   // create if needed
359   if ( !anObjShape ) {
360     anObjShape = createShape( aViewerId, aCtx, theObject );
361     if ( anObjShape ) {
362       anObjShape->setIsToUpdate( true );
363     }
364   }
365   
366   // Process the shape
367   if ( anObjShape ) {
368     // update if needed
369     if ( anObjShape->getIsToUpdate() || theIsForced ) {
370       anObjShape->update( false, false );
371     }
372
373     // Set visibility
374     bool anIsVisible = module()->isObjectVisible( aViewerId, theObject );
375     anObjShape->setVisible( anIsVisible, false );
376
377     aRes = true;
378   }
379
380   return aRes;
381 }
382
383 void HYDROGUI_OCCDisplayer::SetZLayer( const OCCViewer_Viewer* theViewer,
384                                        const Handle(HYDROData_Entity)& theObject, 
385                                        const int theZLayerId )
386 {
387   if ( !theViewer /*|| ( theZLayerId < 0 )*/ ) {
388     return;
389   }
390   
391   // Get interactive context
392   Handle(AIS_InteractiveContext) aCtx = theViewer->getAISContext();
393   if( aCtx.IsNull() ) {
394     return;
395   }
396
397   // Get viewer id
398   size_t aViewerId = (size_t)theViewer;
399
400   // Get object shape 
401   HYDROGUI_Shape* anObjShape = module()->getObjectShape( aViewerId, theObject );
402
403   // Set Z layer
404   if ( anObjShape ) {
405     aCtx->SetZLayer( anObjShape->getAISObject(), theZLayerId );
406   }
407 }
408
409 void HYDROGUI_OCCDisplayer::SetToUpdateColorScale()
410 {
411   myToUpdateColorScale = true;
412 }
413
414 void HYDROGUI_OCCDisplayer::UpdateColorScale( const OCCViewer_Viewer* theViewer )
415 {
416   if( !myToUpdateColorScale || !theViewer )
417     return;
418   
419   OCCViewer_ViewWindow* aWnd = dynamic_cast<OCCViewer_ViewWindow*>( theViewer->getViewManager()->getActiveView() );
420   Handle(V3d_View) aView = aWnd->getViewPort()->getView();
421       
422   int aViewerId = (size_t)theViewer;//TODO: check if viewer id is correct
423   bool isLandCoverColoringOn = module()->isLandCoversScalarMapModeOn( aViewerId );
424     
425   QList<HYDROGUI_Shape*> aLandCoverShapes = module()->getObjectShapes( aViewerId, KIND_LAND_COVER_MAP );
426   QList<HYDROGUI_Shape*> aBathShapes = module()->getObjectShapes( aViewerId, KIND_BATHYMETRY );
427
428   bool isDisplayColorScale = !aBathShapes.empty() || isLandCoverColoringOn;
429   Standard_Real anXPos = 0.05; //TODO
430   Standard_Real anYPos = 0.1; //TODO
431   Standard_Real aWidth = 0.2; //TODO
432   Standard_Real aHeight = 0.5; //TODO
433   Standard_Integer aTextHeight = 14; //TODO
434   Standard_Integer aNbIntervals = 20; //TODO
435   TCollection_ExtendedString aColorScaleTitle = "";//TODO
436
437   Standard_Real aColorScaleMin = 0, aColorScaleMax = 1;
438
439   // Get range
440   Handle(HYDROData_StricklerTable) aTable;
441   QStringList aTableTypes;
442   if ( isLandCoverColoringOn ) {
443     aTable = module()->getLandCoverColoringTable( aViewerId );
444     if ( !aTable.IsNull() ) {
445       // TODO: non-empty title leads to buggy behaviour
446       // aColorScaleTitle = TCollection_ExtendedString( aTable->GetName().toLatin1().constData() );
447       aTable->GetCoefficientRange( aColorScaleMin, aColorScaleMax );
448       aTableTypes = aTable->GetTypes();
449     }
450   } else {
451     Standard_Real aMin, aMax;
452     bool isFirst = true;
453     foreach( HYDROGUI_Shape* aShape, aBathShapes )
454     {
455       HYDROGUI_ShapeBathymetry* aBathShape = dynamic_cast<HYDROGUI_ShapeBathymetry*>( aShape );
456       if( !aBathShape || !aBathShape->isVisible() )
457         continue;
458
459       aBathShape->GetRange( aMin, aMax );
460
461       if( isFirst || aMin < aColorScaleMin )
462         aColorScaleMin = aMin;
463       if( isFirst || aMax > aColorScaleMax )
464         aColorScaleMax = aMax;
465
466       isFirst = false;
467     }
468   }
469
470   Handle(Aspect_ColorScale) aColorScale;
471   if( isDisplayColorScale )
472   {
473     aColorScale = aView->ColorScale();
474     if( !aColorScale.IsNull() )
475     {
476       aColorScale->SetXPosition( anXPos );
477       aColorScale->SetYPosition( anYPos );
478       aColorScale->SetWidth( aWidth );
479       aColorScale->SetHeight( aHeight );
480
481       aColorScale->SetTextHeight( aTextHeight );
482       aColorScale->SetNumberOfIntervals( aNbIntervals );
483
484       aColorScale->SetTitle( aColorScaleTitle );
485       aColorScale->SetRange( aColorScaleMin, aColorScaleMax );
486
487       if ( !isLandCoverColoringOn ) {
488         foreach( HYDROGUI_Shape* aShape, aBathShapes ) {
489           HYDROGUI_ShapeBathymetry* aBathShape = dynamic_cast<HYDROGUI_ShapeBathymetry*>( aShape );
490           if( !aBathShape || !aBathShape->isVisible() )
491             continue;
492
493           aBathShape->UpdateWithColorScale( aColorScale );
494         }
495       }
496     }
497     if( !aView->ColorScaleIsDisplayed() )
498       aView->ColorScaleDisplay();
499   }
500   else
501   {
502     if( aView->ColorScaleIsDisplayed() )
503       aView->ColorScaleErase();
504   }
505
506   /* TODO
507   foreach( HYDROGUI_Shape* aShape, aLandCoverShapes ) {
508     HYDROGUI_ShapeLandCover* aLandCoverShape = 
509       dynamic_cast<HYDROGUI_ShapeLandCover*>( aShape );
510
511     if ( !aLandCoverShape || !aLandCoverShape->isVisible() ) {
512       continue;
513     }
514     
515     QColor aColor;    
516     Handle(HYDROData_LandCover) aLandCover = 
517       Handle(HYDROData_LandCover)::DownCast( aLandCoverShape->getObject() );
518
519     if ( aLandCover.IsNull() ) {
520       continue;
521     }
522     
523     QColor aUndefinedColor( Qt::gray );
524     QColor aColor = isLandCoverColoringOn ? aUndefinedColor : aLandCover->GetFillingColor();
525     
526     if ( isLandCoverColoringOn && !aTable.IsNull() ) {
527       QString aStricklerType = 
528         aLandCover->GetStricklerType().toLatin1().constData();
529      
530       if ( aTable->HasType( aStricklerType ) ) {
531         double aStricklerCoeff = aTable->Get( aStricklerType, 0 );
532         Quantity_Color aShapeColor;
533         if ( aColorScale->FindColor( aStricklerCoeff, aShapeColor ) ) {
534           aColor = QColor( aShapeColor.Red() * 255, 
535                            aShapeColor.Green() * 255,
536                            aShapeColor.Blue() * 255 );
537         }
538       }
539     }    
540     
541     aLandCoverShape->setFillingColor( aColor, true, true );
542     aLandCoverShape->setScalarMapModeEnabled( isLandCoverColoringOn );
543     theViewer->getAISContext()->Redisplay( aLandCoverShape->getAISObject() );
544   }
545   */
546
547   myToUpdateColorScale = false;
548 }