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.
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.
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
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROGUI_OCCDisplayer.h"
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"
32 #include <HYDROData_Bathymetry.h>
33 #include <HYDROData_Image.h>
34 #include <HYDROData_LandCoverMap.h>
35 #include <HYDROData_StricklerTable.h>
37 #include <AIS_InteractiveContext.hxx>
38 #include <AIS_ListIteratorOfListOfInteractive.hxx>
39 #include <AIS_ListOfInteractive.hxx>
40 #include <AIS_ColorScale.hxx>
42 #include <TColStd_SequenceOfInteger.hxx>
44 #include <LightApp_Application.h>
45 #include <SUIT_Study.h>
47 #include <OCCViewer_ViewManager.h>
48 #include <OCCViewer_ViewModel.h>
49 #include <OCCViewer_ViewWindow.h>
50 #include <OCCViewer_ViewPort3d.h>
52 HYDROGUI_OCCDisplayer::HYDROGUI_OCCDisplayer( HYDROGUI_Module* theModule )
53 : HYDROGUI_AbstractDisplayer( theModule )
55 myToUpdateColorScale = false;
58 HYDROGUI_OCCDisplayer::~HYDROGUI_OCCDisplayer()
62 void HYDROGUI_OCCDisplayer::SetToUpdate( const HYDROData_SequenceOfObjects& theObjs,
63 const int theViewerId )
65 OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
69 for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
71 Handle(HYDROData_Entity) anObj = theObjs.Value( i );
75 HYDROGUI_Shape* anObjShape = module()->getObjectShape( (size_t)aViewer, anObj );
79 anObjShape->setIsToUpdate( true );
83 int HYDROGUI_OCCDisplayer::AddPreviewZLayer( OCCViewer_ViewManager* theMgr )
86 OCCViewer_Viewer* aViewer = theMgr->getOCCViewer();
90 aLayer = CreateTopZLayer( aViewer->getViewer3d() );
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 );
104 void HYDROGUI_OCCDisplayer::RemoveZLayer( OCCViewer_ViewManager* theMgr,
110 OCCViewer_Viewer* aViewer = theMgr->getOCCViewer();
114 // Get existing Z layers
115 TColStd_SequenceOfInteger anExistingZLayers;
116 aViewer->getViewer3d()->GetAllZLayers( anExistingZLayers );
117 int aNbLayers = anExistingZLayers.Length();
119 if ( theLayer < aNbLayers )
120 aViewer->getViewer3d()->RemoveZLayer( theLayer );
123 void HYDROGUI_OCCDisplayer::EraseAll( const int theViewerId )
125 OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
129 module()->removeViewShapes( (size_t)aViewer );
130 UpdateColorScale( aViewer );
133 void HYDROGUI_OCCDisplayer::Erase( const HYDROData_SequenceOfObjects& theObjs,
134 const int theViewerId )
136 OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
140 for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
142 Handle(HYDROData_Entity) anObj = theObjs.Value( i );
146 module()->removeObjectShape( (size_t)aViewer, anObj );
149 if ( !module()->isLandCoversScalarMapModeOn( (size_t)aViewer ) ) {
150 UpdateColorScale( aViewer );
154 HYDROGUI_Shape* HYDROGUI_OCCDisplayer::createShape( const int theViewerId,
155 const Handle(AIS_InteractiveContext)& theContext,
156 const Handle(HYDROData_Entity)& theObject )
158 HYDROGUI_Shape* aResShape = NULL;
159 if ( theContext.IsNull() || theObject.IsNull() )
162 if ( !HYDROGUI_Tool::IsObjectHasPresentation( theObject, OCCViewer_Viewer::Type() ) )
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 );
174 aResShape = new HYDROGUI_Shape( theContext, theObject );
176 module()->setObjectShape( theViewerId, theObject, aResShape );
181 void HYDROGUI_OCCDisplayer::Display( const HYDROData_SequenceOfObjects& theObjs,
182 const int theViewerId,
183 const bool theIsForced,
184 const bool theDoFitAll )
186 // Get OCC viewer by id
187 OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
191 // Get interactive context
192 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
197 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( module()->getStudyId() );
201 // Assign Z layer indexes to the objects
202 aDoc->Show( theObjs );
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();
209 HYDROData_SequenceOfObjects::Iterator anAllOrderedIter( anAllOrderedObjects );
210 for ( ; anAllOrderedIter.More(); anAllOrderedIter.Next() ) {
211 QString anOrderedEntry =
212 HYDROGUI_DataObject::dataObjectEntry( anAllOrderedIter.Value() );
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 );
227 Handle(V3d_Viewer) aViewer3d = aViewer->getViewer3d();
230 HYDROGUI_ZLayersIterator aZLayersIt( aViewer->getViewer3d() );
231 if ( !aZLayersIt.More() ) {
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 );
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 );
264 // 3. Update the top Z layer index
269 // Update Z layer of the active operation
270 int aPreviewZLayerId = aZLayersIt.LayerId();
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 );
280 // Update Z layer of hilight presentations
281 int aHilightLayer = aZLayersIt.TopLayer();
282 UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, aHilightLayer );
284 // Fit all / update selection
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() );
292 aViewWindow->onFitAll();
296 else if ( !aCtx.IsNull() ) { // TODO: determine if this code is necessary (added as a fix for issue# 359)
297 aCtx->UpdateSelected(true);
300 UpdateColorScale( aViewer );
303 void HYDROGUI_OCCDisplayer::purgeObjects( const int theViewerId )
305 OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
309 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
313 AIS_ListOfInteractive aDisplayedObjects;
314 aCtx->DisplayedObjects( aDisplayedObjects );
316 AIS_ListIteratorOfListOfInteractive aListIter( aDisplayedObjects );
317 for ( ; aListIter.More(); aListIter.Next() )
319 Handle(AIS_InteractiveObject) aPrsObj = aListIter.Value();
320 if ( aPrsObj.IsNull() )
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 );
328 UpdateColorScale( aViewer );
331 QString HYDROGUI_OCCDisplayer::GetType() const
333 return OCCViewer_Viewer::Type();
336 bool HYDROGUI_OCCDisplayer::Display( const Handle(HYDROData_Entity)& theObject,
337 const OCCViewer_Viewer* theViewer,
338 const bool theIsForced )
342 if ( theObject.IsNull() || theObject->IsRemoved() || !theViewer ) {
346 // Get interactive context
347 Handle(AIS_InteractiveContext) aCtx = theViewer->getAISContext();
348 if( aCtx.IsNull() ) {
353 size_t aViewerId = (size_t)theViewer;
356 HYDROGUI_Shape* anObjShape = module()->getObjectShape( aViewerId, theObject );
359 anObjShape = createShape( aViewerId, aCtx, theObject );
361 anObjShape->setIsToUpdate( true );
368 if ( anObjShape->getIsToUpdate() || theIsForced ) {
369 anObjShape->update( false, false );
373 bool anIsVisible = module()->isObjectVisible( aViewerId, theObject );
374 anObjShape->setVisible( anIsVisible, false );
382 void HYDROGUI_OCCDisplayer::SetZLayer( const OCCViewer_Viewer* theViewer,
383 const Handle(HYDROData_Entity)& theObject,
384 const int theZLayerId )
386 if ( !theViewer /*|| ( theZLayerId < 0 )*/ ) {
390 // Get interactive context
391 Handle(AIS_InteractiveContext) aCtx = theViewer->getAISContext();
392 if( aCtx.IsNull() ) {
397 size_t aViewerId = (size_t)theViewer;
400 HYDROGUI_Shape* anObjShape = module()->getObjectShape( aViewerId, theObject );
405 QList<Handle(AIS_InteractiveObject)> shapes = anObjShape->getAISObjects();
406 foreach( Handle(AIS_InteractiveObject) shape, shapes )
407 aCtx->SetZLayer( shape, theZLayerId );
411 void HYDROGUI_OCCDisplayer::SetToUpdateColorScale()
413 myToUpdateColorScale = true;
416 void HYDROGUI_OCCDisplayer::UpdateColorScale( const OCCViewer_Viewer* theViewer )
418 if( !myToUpdateColorScale || !theViewer )
421 OCCViewer_ViewWindow* aWnd = dynamic_cast<OCCViewer_ViewWindow*>( theViewer->getViewManager()->getActiveView() );
422 Handle(V3d_View) aView = aWnd->getViewPort()->getView();
425 HYDROGUI_Module* aModule = module();
426 int aViewerId = (size_t)theViewer;//TODO: check if viewer id is correct
427 bool isLandCoverColoringOn = aModule->isLandCoversScalarMapModeOn( aViewerId );
429 QList<HYDROGUI_Shape*> aLandCoverMapShapes = aModule->getObjectShapes( aViewerId, KIND_LAND_COVER_MAP );
430 QList<HYDROGUI_Shape*> aBathShapes = aModule->getObjectShapes( aViewerId, KIND_BATHYMETRY );
432 bool isDisplayColorScale = false;
433 foreach (HYDROGUI_Shape* shape, aLandCoverMapShapes)
435 if (aModule->isObjectVisible(aViewerId, shape->getObject()))
437 isDisplayColorScale = true;
441 if (!isDisplayColorScale)
442 foreach (HYDROGUI_Shape* shape, aBathShapes)
444 if (aModule->isObjectVisible(aViewerId, shape->getObject()))
446 isDisplayColorScale = true;
451 Standard_Real aColorScaleMin = 0, aColorScaleMax = 1;
454 Handle(HYDROData_StricklerTable) aTable;
455 QStringList aTableTypes;
456 if ( isLandCoverColoringOn ) {
457 aTable = module()->getLandCoverColoringTable( aViewerId );
458 if ( !aTable.IsNull() ) {
459 // TODO: non-empty title leads to buggy behaviour
460 // aColorScaleTitle = TCollection_ExtendedString( aTable->GetName().toLatin1().constData() );
461 aTable->GetCoefficientRange( aColorScaleMin, aColorScaleMax );
462 aTableTypes = aTable->GetTypes();
465 Standard_Real aMin, aMax;
467 foreach( HYDROGUI_Shape* aShape, aBathShapes )
469 HYDROGUI_ShapeBathymetry* aBathShape = dynamic_cast<HYDROGUI_ShapeBathymetry*>( aShape );
470 if( !aBathShape || !aBathShape->isVisible() )
473 aBathShape->GetRange( aMin, aMax );
475 if( isFirst || aMin < aColorScaleMin )
476 aColorScaleMin = aMin;
477 if( isFirst || aMax > aColorScaleMax )
478 aColorScaleMax = aMax;
484 Handle(AIS_ColorScale) aColorScale = GetColorScale( aViewerId );
485 Handle(AIS_InteractiveContext) aCtx = theViewer->getAISContext();
486 if( isDisplayColorScale )
488 if( !aColorScale.IsNull() )
490 // Set color scale title
491 TCollection_ExtendedString aColorScaleTitle = ""; //TODO
492 aColorScale->SetTitle( aColorScaleTitle );
494 // Set color scale range
495 aColorScale->SetRange( aColorScaleMin, aColorScaleMax );
497 aColorScale->SetToUpdate();
499 if ( !isLandCoverColoringOn ) {
500 foreach( HYDROGUI_Shape* aShape, aBathShapes ) {
501 HYDROGUI_ShapeBathymetry* aBathShape = dynamic_cast<HYDROGUI_ShapeBathymetry*>( aShape );
502 if( !aBathShape || !aBathShape->isVisible() )
505 aBathShape->UpdateWithColorScale( aColorScale );
509 if ( !aCtx.IsNull()/* && !aCtx->IsDisplayed( aColorScale ) */) {
510 if ( !aCtx->IsDisplayed( aColorScale ) ) {
511 aCtx->Display( aColorScale, Standard_False );
514 aCtx->Update( aColorScale, true );
520 if ( !aCtx.IsNull() && aCtx->IsDisplayed( aColorScale ) ) {
521 aCtx->Erase( aColorScale, true );
525 foreach( HYDROGUI_Shape* aShape, aLandCoverMapShapes ) {
526 HYDROGUI_ShapeLandCoverMap* aLandCoverMapShape =
527 dynamic_cast<HYDROGUI_ShapeLandCoverMap*>( aShape );
529 if ( !aLandCoverMapShape || !aLandCoverMapShape->isVisible() ) {
533 Handle(HYDROData_LandCoverMap) aLandCoverMap =
534 Handle(HYDROData_LandCoverMap)::DownCast( aLandCoverMapShape->getObject() );
536 if ( aLandCoverMap.IsNull() ) {
540 bool isScalarMode = aLandCoverMapShape->isScalarMapModeEnabled();
541 if( isScalarMode != isLandCoverColoringOn )
543 aLandCoverMapShape->setScalarMapModeEnabled( isLandCoverColoringOn );
544 theViewer->getAISContext()->Redisplay( aLandCoverMapShape->getAISObjects()[0], Standard_False );
548 myToUpdateColorScale = false;
551 Handle(AIS_ColorScale) HYDROGUI_OCCDisplayer::GetColorScale( const int theViewerId )
553 Handle(AIS_ColorScale) aColorScale;
555 aColorScale = myColorScales.value( theViewerId, aColorScale );
556 if ( aColorScale.IsNull() ) {
557 // Create color scale
558 aColorScale = new AIS_ColorScale();
561 Standard_Integer anXPos = 50; //TODO
562 Standard_Integer anYPos = 100; //TODO
563 Standard_Integer aWidth = 100; //TODO
564 Standard_Integer aHeight = 350; //TODO
566 Standard_Integer aTextHeight = 14; //TODO
567 Standard_Integer aNbIntervals = 20; //TODO
569 aColorScale->SetTransformPersistence( Graphic3d_TMF_2d, gp_Pnt( -1, -1, 0 ) );
571 aColorScale->SetXPosition( anXPos );
572 aColorScale->SetYPosition( anYPos );
573 aColorScale->SetSize( aWidth, aHeight );
575 aColorScale->SetTextHeight( aTextHeight );
576 aColorScale->SetNumberOfIntervals( aNbIntervals );
579 myColorScales.insert( theViewerId, aColorScale );
585 void HYDROGUI_OCCDisplayer::UpdatePolylines( int theViewerId, int theType, int theSize )
587 OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
591 // Get interactive context
592 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
596 AIS_ListOfInteractive objs;
597 aCtx->DisplayedObjects( objs );
598 AIS_ListOfInteractive::const_iterator it = objs.begin(), last = objs.end();
599 for( ; it!=last; it++ )
601 Handle(HYDROGUI_Arrow) arr = Handle(HYDROGUI_Arrow)::DownCast( *it );
605 arr->SetType( (HYDROGUI_Arrow::Type)theType );
607 arr->SetSize( theSize );
608 aCtx->Redisplay( arr, Standard_False );
611 aCtx->UpdateCurrentViewer();