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_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 #include <HYDROData_Bathymetry.h>
31 #include <AIS_InteractiveContext.hxx>
32 #include <AIS_ListIteratorOfListOfInteractive.hxx>
33 #include <AIS_ListOfInteractive.hxx>
35 #include <Aspect_ColorScale.hxx>
37 #include <TColStd_SequenceOfInteger.hxx>
39 #include <LightApp_Application.h>
40 #include <SUIT_Study.h>
42 #include <OCCViewer_ViewManager.h>
43 #include <OCCViewer_ViewModel.h>
44 #include <OCCViewer_ViewWindow.h>
45 #include <OCCViewer_ViewPort3d.h>
47 HYDROGUI_OCCDisplayer::HYDROGUI_OCCDisplayer( HYDROGUI_Module* theModule )
48 : HYDROGUI_AbstractDisplayer( theModule )
50 myToUpdateColorScale = false;
53 HYDROGUI_OCCDisplayer::~HYDROGUI_OCCDisplayer()
57 void HYDROGUI_OCCDisplayer::SetToUpdate( const HYDROData_SequenceOfObjects& theObjs,
58 const int theViewerId )
60 OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
64 for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
66 Handle(HYDROData_Entity) anObj = theObjs.Value( i );
70 HYDROGUI_Shape* anObjShape = module()->getObjectShape( (size_t)aViewer, anObj );
74 anObjShape->setIsToUpdate( true );
78 int HYDROGUI_OCCDisplayer::AddPreviewZLayer( OCCViewer_ViewManager* theMgr )
81 OCCViewer_Viewer* aViewer = theMgr->getOCCViewer();
85 aLayer = CreateTopZLayer( aViewer->getViewer3d() );
87 // Hilight presentation should be on top
88 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
89 if( !aCtx.IsNull() ) {
90 int aTopLayer = CreateTopZLayer( aViewer->getViewer3d() );
91 if ( aTopLayer > 0 ) {
92 UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, aTopLayer );
99 void HYDROGUI_OCCDisplayer::RemoveZLayer( OCCViewer_ViewManager* theMgr,
105 OCCViewer_Viewer* aViewer = theMgr->getOCCViewer();
109 // Get existing Z layers
110 TColStd_SequenceOfInteger anExistingZLayers;
111 aViewer->getViewer3d()->GetAllZLayers( anExistingZLayers );
112 int aNbLayers = anExistingZLayers.Length();
114 if ( theLayer < aNbLayers )
115 aViewer->getViewer3d()->RemoveZLayer( theLayer );
118 void HYDROGUI_OCCDisplayer::EraseAll( const int theViewerId )
120 OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
124 module()->removeViewShapes( (size_t)aViewer );
125 UpdateColorScale( aViewer );
128 void HYDROGUI_OCCDisplayer::Erase( const HYDROData_SequenceOfObjects& theObjs,
129 const int theViewerId )
131 OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
135 for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
137 Handle(HYDROData_Entity) anObj = theObjs.Value( i );
141 module()->removeObjectShape( (size_t)aViewer, anObj );
144 UpdateColorScale( aViewer );
147 HYDROGUI_Shape* HYDROGUI_OCCDisplayer::createShape( const int theViewerId,
148 const Handle(AIS_InteractiveContext)& theContext,
149 const Handle(HYDROData_Entity)& theObject )
151 HYDROGUI_Shape* aResShape = NULL;
152 if ( theContext.IsNull() || theObject.IsNull() )
155 if ( !HYDROGUI_Tool::IsObjectHasPresentation( theObject, OCCViewer_Viewer::Type() ) )
158 if( theObject->IsKind( STANDARD_TYPE( HYDROData_Image ) ) )
159 aResShape = new HYDROGUI_ShapeImage( theContext, Handle_HYDROData_Image::DownCast( theObject ) );
160 else if( theObject->IsKind( STANDARD_TYPE( HYDROData_Bathymetry ) ) )
161 aResShape = new HYDROGUI_ShapeBathymetry( this, theContext, Handle_HYDROData_Bathymetry::DownCast( theObject ) );
163 aResShape = new HYDROGUI_Shape( theContext, theObject );
165 module()->setObjectShape( theViewerId, theObject, aResShape );
170 void HYDROGUI_OCCDisplayer::Display( const HYDROData_SequenceOfObjects& theObjs,
171 const int theViewerId,
172 const bool theIsForced,
173 const bool theDoFitAll )
175 // Get OCC viewer by id
176 OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
180 // Get interactive context
181 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
186 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( module()->getStudyId() );
190 // Assign Z layer indexes to the objects
191 aDoc->Show( theObjs );
193 // Sort objects by display order ( needed for Z layers assignment only )
194 HYDROData_SequenceOfObjects anUnorderedToDisplay = theObjs;
195 HYDROData_SequenceOfObjects anOrderedToDisplay;
196 HYDROData_SequenceOfObjects anAllOrderedObjects = aDoc->GetObjectsLayerOrder();
198 HYDROData_SequenceOfObjects::Iterator anAllOrderedIter( anAllOrderedObjects );
199 for ( ; anAllOrderedIter.More(); anAllOrderedIter.Next() ) {
200 QString anOrderedEntry =
201 HYDROGUI_DataObject::dataObjectEntry( anAllOrderedIter.Value() );
203 HYDROData_SequenceOfObjects::Iterator aToDisplayIter( anUnorderedToDisplay );
204 for ( ; aToDisplayIter.More(); aToDisplayIter.Next() ) {
205 Handle(HYDROData_Entity) anObjToDisplay = aToDisplayIter.Value();
206 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( anObjToDisplay );
207 if ( anEntry == anOrderedEntry ) {
208 anOrderedToDisplay.Prepend( anObjToDisplay );
209 anUnorderedToDisplay.Remove( aToDisplayIter );
216 Handle(V3d_Viewer) aViewer3d = aViewer->getViewer3d();
219 HYDROGUI_ZLayersIterator aZLayersIt( aViewer->getViewer3d() );
220 if ( !aZLayersIt.More() ) {
224 // 1. Display the ordered objects:
225 HYDROData_SequenceOfObjects::Iterator anOrderedIter( anOrderedToDisplay );
226 for ( ; anOrderedIter.More(); anOrderedIter.Next() ) {
227 Handle(HYDROData_Entity) anObj = anOrderedIter.Value();
228 if ( Display( anObj, aViewer, theIsForced ) ) {
229 // set Z layer ( one Z layer for each ordered object )
230 int aZLayerId = aZLayersIt.LayerId();
231 SetZLayer( aViewer, anObj, aZLayerId );
232 SetZLayerSettings( aViewer3d, aZLayerId, true );
237 // 2. Display the unordered objects:
238 bool isDisplayed = false;
239 int anUnorderedZLayerId = aZLayersIt.LayerId();
240 HYDROData_SequenceOfObjects::Iterator anUnorderedIter( anUnorderedToDisplay );
241 for ( ; anUnorderedIter.More(); anUnorderedIter.Next() ) {
242 Handle(HYDROData_Entity) anObj = anUnorderedIter.Value();
243 if ( Display( anObj, aViewer, theIsForced) ) {
244 // set Z layer ( one Z layer for all unordered objects )
245 SetZLayer( aViewer, anObj, anUnorderedZLayerId );
246 if ( !isDisplayed ) {
247 SetZLayerSettings( aViewer3d, anUnorderedZLayerId, false );
253 // 3. Update the top Z layer index
258 // Update Z layer of the active operation
259 int aPreviewZLayerId = aZLayersIt.LayerId();
261 HYDROGUI_Module* aModule = module();
262 SUIT_Operation* anOp = aModule->activeOperation();
263 HYDROGUI_Operation* aHOp = anOp ? dynamic_cast<HYDROGUI_Operation*>( anOp ) : 0;
264 if ( aHOp && aHOp->getPreviewZLayer() >= 0 ) {
265 aHOp->updatePreviewZLayer( aPreviewZLayerId );
269 // Update Z layer of hilight presentations
270 int aHilightLayer = aZLayersIt.TopLayer();
271 UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, aHilightLayer );
273 // Fit all / update selection
275 OCCViewer_ViewManager* aViewManager
276 = ::qobject_cast<OCCViewer_ViewManager*>( aViewer->getViewManager() );
277 if ( aViewManager ) {
278 OCCViewer_ViewWindow* aViewWindow =
279 ::qobject_cast<OCCViewer_ViewWindow*>( aViewManager->getActiveView() );
281 aViewWindow->onFitAll();
285 else if ( !aCtx.IsNull() ) { // TODO: determine if this code is necessary (added as a fix for issue# 359)
286 aCtx->UpdateSelected();
289 UpdateColorScale( aViewer );
292 void HYDROGUI_OCCDisplayer::purgeObjects( const int theViewerId )
294 OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
298 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
302 AIS_ListOfInteractive aDisplayedObjects;
303 aCtx->DisplayedObjects( aDisplayedObjects );
305 AIS_ListIteratorOfListOfInteractive aListIter( aDisplayedObjects );
306 for ( ; aListIter.More(); aListIter.Next() )
308 Handle(AIS_InteractiveObject) aPrsObj = aListIter.Value();
309 if ( aPrsObj.IsNull() )
312 Handle(HYDROData_Entity) anOwnerObj =
313 Handle(HYDROData_Entity)::DownCast( aPrsObj->GetOwner() );
314 if ( !anOwnerObj.IsNull() && anOwnerObj->IsRemoved() )
315 module()->removeObjectShape( (size_t)aViewer, anOwnerObj );
319 QString HYDROGUI_OCCDisplayer::GetType() const
321 return OCCViewer_Viewer::Type();
324 bool HYDROGUI_OCCDisplayer::Display( const Handle(HYDROData_Entity)& theObject,
325 const OCCViewer_Viewer* theViewer,
326 const bool theIsForced )
330 if ( theObject.IsNull() || theObject->IsRemoved() || !theViewer ) {
334 // Get interactive context
335 Handle(AIS_InteractiveContext) aCtx = theViewer->getAISContext();
336 if( aCtx.IsNull() ) {
341 size_t aViewerId = (size_t)theViewer;
344 HYDROGUI_Shape* anObjShape = module()->getObjectShape( aViewerId, theObject );
347 anObjShape = createShape( aViewerId, aCtx, theObject );
349 anObjShape->setIsToUpdate( true );
356 if ( anObjShape->getIsToUpdate() || theIsForced ) {
357 anObjShape->update( false, false );
361 bool anIsVisible = module()->isObjectVisible( aViewerId, theObject );
362 anObjShape->setVisible( anIsVisible, false );
370 void HYDROGUI_OCCDisplayer::SetZLayer( const OCCViewer_Viewer* theViewer,
371 const Handle(HYDROData_Entity)& theObject,
372 const int theZLayerId )
374 if ( !theViewer /*|| ( theZLayerId < 0 )*/ ) {
378 // Get interactive context
379 Handle(AIS_InteractiveContext) aCtx = theViewer->getAISContext();
380 if( aCtx.IsNull() ) {
385 size_t aViewerId = (size_t)theViewer;
388 HYDROGUI_Shape* anObjShape = module()->getObjectShape( aViewerId, theObject );
392 aCtx->SetZLayer( anObjShape->getAISObject(), theZLayerId );
396 void HYDROGUI_OCCDisplayer::SetToUpdateColorScale()
398 myToUpdateColorScale = true;
401 void HYDROGUI_OCCDisplayer::UpdateColorScale( const OCCViewer_Viewer* theViewer )
403 if( !myToUpdateColorScale )
406 OCCViewer_ViewWindow* aWnd = dynamic_cast<OCCViewer_ViewWindow*>( theViewer->getViewManager()->getActiveView() );
407 Handle(V3d_View) aView = aWnd->getViewPort()->getView();
409 int aViewerId = (size_t)theViewer;//TODO: check if viewer id is correct
410 QList<HYDROGUI_Shape*> aShapes = module()->getObjectShapes( aViewerId, KIND_BATHYMETRY );
412 bool isDisplayColorScale = !aShapes.empty();
413 Standard_Real anXPos = 0.05; //TODO
414 Standard_Real anYPos = 0.1; //TODO
415 Standard_Real aWidth = 0.2; //TODO
416 Standard_Real aHeight = 0.5; //TODO
417 Standard_Integer aTextHeight = 14; //TODO
418 Standard_Integer aNbIntervals = 20; //TODO
419 TCollection_ExtendedString aColorScaleTitle = "";//TODO
421 Standard_Real aColorScaleMin = 0, aColorScaleMax = 1, aMin, aMax;
423 foreach( HYDROGUI_Shape* aShape, aShapes )
425 HYDROGUI_ShapeBathymetry* aBathShape = dynamic_cast<HYDROGUI_ShapeBathymetry*>( aShape );
426 if( !aBathShape || !aBathShape->isVisible() )
429 aBathShape->GetRange( aMin, aMax );
431 if( isFirst || aMin < aColorScaleMin )
432 aColorScaleMin = aMin;
433 if( isFirst || aMax > aColorScaleMax )
434 aColorScaleMax = aMax;
439 if( isDisplayColorScale )
441 Handle(Aspect_ColorScale) aColorScale = aView->ColorScale();
442 if( !aColorScale.IsNull() )
444 aColorScale->SetXPosition( anXPos );
445 aColorScale->SetYPosition( anYPos );
446 aColorScale->SetWidth( aWidth );
447 aColorScale->SetHeight( aHeight );
449 aColorScale->SetTextHeight( aTextHeight );
450 aColorScale->SetNumberOfIntervals( aNbIntervals );
452 aColorScale->SetTitle( aColorScaleTitle );
453 aColorScale->SetRange( aColorScaleMin, aColorScaleMax );
455 foreach( HYDROGUI_Shape* aShape, aShapes )
457 HYDROGUI_ShapeBathymetry* aBathShape = dynamic_cast<HYDROGUI_ShapeBathymetry*>( aShape );
458 if( !aBathShape || !aBathShape->isVisible() )
461 aBathShape->UpdateWithColorScale( aColorScale );
464 if( !aView->ColorScaleIsDisplayed() )
465 aView->ColorScaleDisplay();
469 if( aView->ColorScaleIsDisplayed() )
470 aView->ColorScaleErase();
473 myToUpdateColorScale = false;