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>
53 #include "HYDRO_trace.hxx"
55 HYDROGUI_OCCDisplayer::HYDROGUI_OCCDisplayer( HYDROGUI_Module* theModule )
56 : HYDROGUI_AbstractDisplayer( theModule )
58 DEBTRACE("HYDROGUI_OCCDisplayer");
59 myToUpdateColorScale = false;
62 HYDROGUI_OCCDisplayer::~HYDROGUI_OCCDisplayer()
66 void HYDROGUI_OCCDisplayer::SetToUpdate( const HYDROData_SequenceOfObjects& theObjs,
67 const size_t theViewerId )
69 OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
73 for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
75 Handle(HYDROData_Entity) anObj = theObjs.Value( i );
79 HYDROGUI_Shape* anObjShape = module()->getObjectShape( (size_t)aViewer, anObj );
83 anObjShape->setIsToUpdate( true );
87 int HYDROGUI_OCCDisplayer::AddPreviewZLayer( OCCViewer_ViewManager* theMgr )
89 DEBTRACE("AddPreviewZLayer");
91 OCCViewer_Viewer* aViewer = theMgr->getOCCViewer();
95 aLayer = CreateTopZLayer( aViewer->getViewer3d() );
97 // Hilight presentation should be on top
98 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
99 if( !aCtx.IsNull() ) {
100 int aTopLayer = CreateTopZLayer( aViewer->getViewer3d() );
101 if ( aTopLayer > 0 ) {
102 UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, aTopLayer );
109 void HYDROGUI_OCCDisplayer::RemoveZLayer( OCCViewer_ViewManager* theMgr,
113 DEBTRACE("RemoveZLayer " << theLayer << " " << isClearAll);
120 OCCViewer_Viewer* aViewer = theMgr->getOCCViewer();
124 // Get existing Z layers
125 TColStd_SequenceOfInteger anExistingZLayers;
126 aViewer->getViewer3d()->GetAllZLayers( anExistingZLayers );
127 int aNbLayers = anExistingZLayers.Length();
131 for ( int i = 1; i <= aNbLayers; i++ )
133 int val = anExistingZLayers.Value( i );
135 aViewer->getViewer3d()->RemoveZLayer( val, true );
139 if ( theLayer < aNbLayers )
140 aViewer->getViewer3d()->RemoveZLayer( theLayer );
143 void HYDROGUI_OCCDisplayer::EraseAll( const size_t theViewerId )
145 OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
149 module()->removeViewShapes( (size_t)aViewer );
150 UpdateColorScale( aViewer );
153 void HYDROGUI_OCCDisplayer::Erase( const HYDROData_SequenceOfObjects& theObjs,
154 const size_t theViewerId )
156 OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
160 for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
162 Handle(HYDROData_Entity) anObj = theObjs.Value( i );
166 module()->removeObjectShape( (size_t)aViewer, anObj );
169 if ( !module()->isLandCoversScalarMapModeOn( (size_t)aViewer ) ) {
170 UpdateColorScale( aViewer );
174 HYDROGUI_Shape* HYDROGUI_OCCDisplayer::createShape( const size_t theViewerId,
175 const Handle(AIS_InteractiveContext)& theContext,
176 const Handle(HYDROData_Entity)& theObject )
178 DEBTRACE("createShape " << theViewerId);
179 HYDROGUI_Shape* aResShape = NULL;
180 if ( theContext.IsNull() || theObject.IsNull() )
183 if ( !HYDROGUI_Tool::IsObjectHasPresentation( theObject, OCCViewer_Viewer::Type() ) )
186 if( theObject->IsKind( STANDARD_TYPE( HYDROData_Image ) ) )
187 aResShape = new HYDROGUI_ShapeImage( theContext, Handle(HYDROData_Image)::DownCast( theObject ) );
188 else if( theObject->IsKind( STANDARD_TYPE( HYDROData_Bathymetry ) ) )
189 aResShape = new HYDROGUI_ShapeBathymetry( this, theContext, Handle(HYDROData_Bathymetry)::DownCast( theObject ) );
190 else if( theObject->IsKind( STANDARD_TYPE( HYDROData_LandCoverMap ) ) ) {
191 bool isScalarMode = module()->isLandCoversScalarMapModeOn( theViewerId );
192 aResShape = new HYDROGUI_ShapeLandCoverMap( this, theContext, Handle(HYDROData_LandCoverMap)::DownCast( theObject ), -1, isScalarMode );
195 aResShape = new HYDROGUI_Shape( theContext, theObject );
197 module()->setObjectShape( theViewerId, theObject, aResShape );
202 void HYDROGUI_OCCDisplayer::Display( const HYDROData_SequenceOfObjects& theObjs,
203 const size_t theViewerId,
204 const bool theIsForced,
205 const bool theDoFitAll )
207 // Get OCC viewer by id
208 OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
212 // Get interactive context
213 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
218 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document();
222 // Assign Z layer indexes to the objects
223 aDoc->Show( theObjs );
225 // Sort objects by display order ( needed for Z layers assignment only )
226 HYDROData_SequenceOfObjects anUnorderedToDisplay = theObjs;
227 HYDROData_SequenceOfObjects anOrderedToDisplay;
228 HYDROData_SequenceOfObjects anAllOrderedObjects = aDoc->GetObjectsLayerOrder();
230 HYDROData_SequenceOfObjects::Iterator anAllOrderedIter( anAllOrderedObjects );
231 for ( ; anAllOrderedIter.More(); anAllOrderedIter.Next() ) {
232 QString anOrderedEntry =
233 HYDROGUI_DataObject::dataObjectEntry( anAllOrderedIter.Value() );
235 HYDROData_SequenceOfObjects::Iterator aToDisplayIter( anUnorderedToDisplay );
236 for ( ; aToDisplayIter.More(); aToDisplayIter.Next() ) {
237 Handle(HYDROData_Entity) anObjToDisplay = aToDisplayIter.Value();
238 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( anObjToDisplay );
239 if ( anEntry == anOrderedEntry ) {
240 anOrderedToDisplay.Prepend( anObjToDisplay );
241 anUnorderedToDisplay.Remove( aToDisplayIter );
248 Handle(V3d_Viewer) aViewer3d = aViewer->getViewer3d();
251 HYDROGUI_ZLayersIterator aZLayersIt( aViewer->getViewer3d() );
252 if ( !aZLayersIt.More() ) {
256 // 1. Display the ordered objects:
257 HYDROData_SequenceOfObjects::Iterator anOrderedIter( anOrderedToDisplay );
258 for ( ; anOrderedIter.More(); anOrderedIter.Next() ) {
259 Handle(HYDROData_Entity) anObj = anOrderedIter.Value();
260 if ( Display( anObj, aViewer, theIsForced ) ) {
261 // set Z layer ( one Z layer for each ordered object )
262 int aZLayerId = aZLayersIt.LayerId();
263 SetZLayer( aViewer, anObj, aZLayerId );
264 SetZLayerSettings( aViewer3d, aZLayerId, true );
269 // 2. Display the unordered objects:
270 bool isDisplayed = false;
271 int anUnorderedZLayerId = aZLayersIt.LayerId();
272 HYDROData_SequenceOfObjects::Iterator anUnorderedIter( anUnorderedToDisplay );
273 for ( ; anUnorderedIter.More(); anUnorderedIter.Next() ) {
274 Handle(HYDROData_Entity) anObj = anUnorderedIter.Value();
275 if ( Display( anObj, aViewer, theIsForced) ) {
276 // set Z layer ( one Z layer for all unordered objects )
277 SetZLayer( aViewer, anObj, anUnorderedZLayerId );
278 if ( !isDisplayed ) {
279 SetZLayerSettings( aViewer3d, anUnorderedZLayerId, false );
285 // 3. Update the top Z layer index
290 // Update Z layer of the active operation
291 int aPreviewZLayerId = aZLayersIt.LayerId();
293 HYDROGUI_Module* aModule = module();
294 SUIT_Operation* anOp = aModule->activeOperation();
295 HYDROGUI_Operation* aHOp = anOp ? dynamic_cast<HYDROGUI_Operation*>( anOp ) : 0;
296 if ( aHOp && aHOp->getPreviewZLayer() >= 0 ) {
297 aHOp->updatePreviewZLayer( aPreviewZLayerId );
301 // Update Z layer of hilight presentations
302 int aHilightLayer = aZLayersIt.TopLayer();
303 UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, aHilightLayer );
305 // Fit all / update selection
307 OCCViewer_ViewManager* aViewManager
308 = ::qobject_cast<OCCViewer_ViewManager*>( aViewer->getViewManager() );
309 if ( aViewManager ) {
310 OCCViewer_ViewWindow* aViewWindow =
311 ::qobject_cast<OCCViewer_ViewWindow*>( aViewManager->getActiveView() );
313 aViewWindow->onFitAll();
317 else if ( !aCtx.IsNull() ) { // TODO: determine if this code is necessary (added as a fix for issue# 359)
318 aCtx->UpdateSelected(true);
321 UpdateColorScale( aViewer );
324 void HYDROGUI_OCCDisplayer::purgeObjects( const size_t theViewerId )
326 OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
330 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
334 AIS_ListOfInteractive aDisplayedObjects;
335 aCtx->DisplayedObjects( aDisplayedObjects );
337 AIS_ListIteratorOfListOfInteractive aListIter( aDisplayedObjects );
338 for ( ; aListIter.More(); aListIter.Next() )
340 Handle(AIS_InteractiveObject) aPrsObj = aListIter.Value();
341 if ( aPrsObj.IsNull() )
344 Handle(HYDROData_Entity) anOwnerObj =
345 Handle(HYDROData_Entity)::DownCast( aPrsObj->GetOwner() );
346 if ( !anOwnerObj.IsNull() && anOwnerObj->IsRemoved() )
347 module()->removeObjectShape( (size_t)aViewer, anOwnerObj );
349 UpdateColorScale( aViewer );
352 QString HYDROGUI_OCCDisplayer::GetType() const
354 return OCCViewer_Viewer::Type();
357 bool HYDROGUI_OCCDisplayer::Display( const Handle(HYDROData_Entity)& theObject,
358 const OCCViewer_Viewer* theViewer,
359 const bool theIsForced )
364 if ( theObject.IsNull() || theObject->IsRemoved() || !theViewer ) {
368 // Get interactive context
369 Handle(AIS_InteractiveContext) aCtx = theViewer->getAISContext();
370 if( aCtx.IsNull() ) {
375 size_t aViewerId = (size_t)theViewer;
378 HYDROGUI_Shape* anObjShape = module()->getObjectShape( aViewerId, theObject );
381 anObjShape = createShape( aViewerId, aCtx, theObject );
383 anObjShape->setIsToUpdate( true );
390 if ( anObjShape->getIsToUpdate() || theIsForced ) {
391 anObjShape->update( false, false );
395 bool anIsVisible = module()->isObjectVisible( aViewerId, theObject );
396 anObjShape->setVisible( anIsVisible, false );
404 void HYDROGUI_OCCDisplayer::SetZLayer( const OCCViewer_Viewer* theViewer,
405 const Handle(HYDROData_Entity)& theObject,
406 const int theZLayerId )
408 if ( !theViewer /*|| ( theZLayerId < 0 )*/ ) {
412 // Get interactive context
413 Handle(AIS_InteractiveContext) aCtx = theViewer->getAISContext();
414 if( aCtx.IsNull() ) {
419 size_t aViewerId = (size_t)theViewer;
422 HYDROGUI_Shape* anObjShape = module()->getObjectShape( aViewerId, theObject );
427 QList<Handle(AIS_InteractiveObject)> shapes = anObjShape->getAISObjects();
428 foreach( Handle(AIS_InteractiveObject) shape, shapes )
429 aCtx->SetZLayer( shape, theZLayerId );
433 void HYDROGUI_OCCDisplayer::SetToUpdateColorScale()
435 myToUpdateColorScale = true;
438 void HYDROGUI_OCCDisplayer::UpdateColorScale( const OCCViewer_Viewer* theViewer )
440 if( !myToUpdateColorScale || !theViewer )
443 OCCViewer_ViewWindow* aWnd = dynamic_cast<OCCViewer_ViewWindow*>( theViewer->getViewManager()->getActiveView() );
444 Handle(V3d_View) aView = aWnd->getViewPort()->getView();
447 HYDROGUI_Module* aModule = module();
448 size_t aViewerId = (size_t)theViewer;//TODO: check if viewer id is correct
449 bool isLandCoverColoringOn = aModule->isLandCoversScalarMapModeOn( aViewerId );
451 QList<HYDROGUI_Shape*> aLandCoverMapShapes = aModule->getObjectShapes( aViewerId, KIND_LAND_COVER_MAP );
452 QList<HYDROGUI_Shape*> aBathShapes = aModule->getObjectShapes( aViewerId, KIND_BATHYMETRY );
454 bool isDisplayColorScale = false;
455 foreach (HYDROGUI_Shape* shape, aLandCoverMapShapes)
457 if (aModule->isObjectVisible(aViewerId, shape->getObject()))
459 isDisplayColorScale = true;
463 if (!isDisplayColorScale)
464 foreach (HYDROGUI_Shape* shape, aBathShapes)
466 if (aModule->isObjectVisible(aViewerId, shape->getObject()))
468 isDisplayColorScale = true;
473 Standard_Real aColorScaleMin = 0, aColorScaleMax = 1;
476 Handle(HYDROData_StricklerTable) aTable;
477 QStringList aTableTypes;
478 if ( isLandCoverColoringOn ) {
479 aTable = module()->getLandCoverColoringTable( aViewerId );
480 if ( !aTable.IsNull() ) {
481 // TODO: non-empty title leads to buggy behaviour
482 // aColorScaleTitle = TCollection_ExtendedString( aTable->GetName().toLatin1().constData() );
483 aTable->GetCoefficientRange( aColorScaleMin, aColorScaleMax );
484 aTableTypes = aTable->GetTypes();
487 Standard_Real aMin, aMax;
489 foreach( HYDROGUI_Shape* aShape, aBathShapes )
491 HYDROGUI_ShapeBathymetry* aBathShape = dynamic_cast<HYDROGUI_ShapeBathymetry*>( aShape );
492 if( !aBathShape || !aBathShape->isVisible() )
495 aBathShape->GetRange( aMin, aMax );
497 if( isFirst || aMin < aColorScaleMin )
498 aColorScaleMin = aMin;
499 if( isFirst || aMax > aColorScaleMax )
500 aColorScaleMax = aMax;
506 Handle(AIS_ColorScale) aColorScale = GetColorScale( aViewerId );
507 Handle(AIS_InteractiveContext) aCtx = theViewer->getAISContext();
508 if( isDisplayColorScale )
510 if( !aColorScale.IsNull() )
512 // Set color scale title
513 TCollection_ExtendedString aColorScaleTitle = ""; //TODO
514 aColorScale->SetTitle( aColorScaleTitle );
516 // Set color scale range
517 aColorScale->SetRange( aColorScaleMin, aColorScaleMax );
519 aColorScale->SetToUpdate();
521 if ( !isLandCoverColoringOn ) {
522 foreach( HYDROGUI_Shape* aShape, aBathShapes ) {
523 HYDROGUI_ShapeBathymetry* aBathShape = dynamic_cast<HYDROGUI_ShapeBathymetry*>( aShape );
524 if( !aBathShape || !aBathShape->isVisible() )
527 aBathShape->UpdateWithColorScale( aColorScale );
531 if ( !aCtx.IsNull()/* && !aCtx->IsDisplayed( aColorScale ) */) {
532 if ( !aCtx->IsDisplayed( aColorScale ) ) {
533 aCtx->Display( aColorScale, Standard_False );
536 aCtx->Update( aColorScale, true );
542 if ( !aCtx.IsNull() && aCtx->IsDisplayed( aColorScale ) ) {
543 aCtx->Erase( aColorScale, true );
547 foreach( HYDROGUI_Shape* aShape, aLandCoverMapShapes ) {
548 HYDROGUI_ShapeLandCoverMap* aLandCoverMapShape =
549 dynamic_cast<HYDROGUI_ShapeLandCoverMap*>( aShape );
551 if ( !aLandCoverMapShape || !aLandCoverMapShape->isVisible() ) {
555 Handle(HYDROData_LandCoverMap) aLandCoverMap =
556 Handle(HYDROData_LandCoverMap)::DownCast( aLandCoverMapShape->getObject() );
558 if ( aLandCoverMap.IsNull() ) {
562 bool isScalarMode = aLandCoverMapShape->isScalarMapModeEnabled();
563 if( isScalarMode != isLandCoverColoringOn )
565 aLandCoverMapShape->setScalarMapModeEnabled( isLandCoverColoringOn );
566 theViewer->getAISContext()->Redisplay( aLandCoverMapShape->getAISObjects()[0], Standard_False );
570 myToUpdateColorScale = false;
573 Handle(AIS_ColorScale) HYDROGUI_OCCDisplayer::GetColorScale( const size_t theViewerId )
575 Handle(AIS_ColorScale) aColorScale;
577 aColorScale = myColorScales.value( theViewerId, aColorScale );
578 if ( aColorScale.IsNull() ) {
579 // Create color scale
580 aColorScale = new AIS_ColorScale();
583 Standard_Integer anXPos = 50; //TODO
584 Standard_Integer anYPos = 100; //TODO
585 Standard_Integer aWidth = 100; //TODO
586 Standard_Integer aHeight = 350; //TODO
588 Standard_Integer aTextHeight = 14; //TODO
589 Standard_Integer aNbIntervals = 20; //TODO
591 aColorScale->SetTransformPersistence( Graphic3d_TMF_2d, gp_Pnt( -1, -1, 0 ) );
593 aColorScale->SetXPosition( anXPos );
594 aColorScale->SetYPosition( anYPos );
595 aColorScale->SetSize( aWidth, aHeight );
597 aColorScale->SetTextHeight( aTextHeight );
598 aColorScale->SetNumberOfIntervals( aNbIntervals );
601 myColorScales.insert( theViewerId, aColorScale );
607 void HYDROGUI_OCCDisplayer::UpdatePolylines( size_t theViewerId, int theType, int theSize )
609 OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
613 // Get interactive context
614 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
618 AIS_ListOfInteractive objs;
619 aCtx->DisplayedObjects( objs );
620 AIS_ListOfInteractive::const_iterator it = objs.begin(), last = objs.end();
621 for( ; it!=last; it++ )
623 Handle(HYDROGUI_Arrow) arr = Handle(HYDROGUI_Arrow)::DownCast( *it );
627 arr->SetType( (HYDROGUI_Arrow::Type)theType );
629 arr->SetSize( theSize );
630 aCtx->Redisplay( arr, Standard_False );
633 aCtx->UpdateCurrentViewer();