1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HYDROGUI_OCCDisplayer.h"
25 #include "HYDROGUI_DataModel.h"
26 #include "HYDROGUI_Module.h"
27 #include "HYDROGUI_Tool.h"
28 #include <HYDROGUI_ShapeImage.h>
29 #include <HYDROGUI_ShapeBathymetry.h>
30 #include "HYDROGUI_Operation.h"
31 #include "HYDROGUI_DataObject.h"
32 #include "HYDROGUI_ZLayers.h"
33 #include <HYDROData_Bathymetry.h>
35 #include <AIS_InteractiveContext.hxx>
36 #include <AIS_ListIteratorOfListOfInteractive.hxx>
37 #include <AIS_ListOfInteractive.hxx>
39 #include <Aspect_ColorScale.hxx>
41 #include <TColStd_SequenceOfInteger.hxx>
43 #include <LightApp_Application.h>
44 #include <SUIT_Study.h>
46 #include <OCCViewer_ViewManager.h>
47 #include <OCCViewer_ViewModel.h>
48 #include <OCCViewer_ViewWindow.h>
49 #include <OCCViewer_ViewPort3d.h>
51 HYDROGUI_OCCDisplayer::HYDROGUI_OCCDisplayer( HYDROGUI_Module* theModule )
52 : HYDROGUI_AbstractDisplayer( theModule )
56 HYDROGUI_OCCDisplayer::~HYDROGUI_OCCDisplayer()
60 void HYDROGUI_OCCDisplayer::SetToUpdate( const HYDROData_SequenceOfObjects& theObjs,
61 const int theViewerId )
63 OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
67 for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
69 Handle(HYDROData_Entity) anObj = theObjs.Value( i );
73 HYDROGUI_Shape* anObjShape = module()->getObjectShape( (size_t)aViewer, anObj );
77 anObjShape->setIsToUpdate( true );
81 int HYDROGUI_OCCDisplayer::AddPreviewZLayer( OCCViewer_ViewManager* theMgr )
84 OCCViewer_Viewer* aViewer = theMgr->getOCCViewer();
88 aLayer = CreateTopZLayer( aViewer->getViewer3d() );
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 );
102 void HYDROGUI_OCCDisplayer::RemoveZLayer( OCCViewer_ViewManager* theMgr,
108 OCCViewer_Viewer* aViewer = theMgr->getOCCViewer();
112 // Get existing Z layers
113 TColStd_SequenceOfInteger anExistingZLayers;
114 aViewer->getViewer3d()->GetAllZLayers( anExistingZLayers );
115 int aNbLayers = anExistingZLayers.Length();
117 if ( theLayer < aNbLayers )
118 aViewer->getViewer3d()->RemoveZLayer( theLayer );
121 void HYDROGUI_OCCDisplayer::EraseAll( const int theViewerId )
123 OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
127 module()->removeViewShapes( (size_t)aViewer );
128 UpdateColorScale( aViewer );
131 void HYDROGUI_OCCDisplayer::Erase( const HYDROData_SequenceOfObjects& theObjs,
132 const int theViewerId )
134 OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
138 for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
140 Handle(HYDROData_Entity) anObj = theObjs.Value( i );
144 module()->removeObjectShape( (size_t)aViewer, anObj );
147 UpdateColorScale( aViewer );
150 HYDROGUI_Shape* HYDROGUI_OCCDisplayer::createShape( const int theViewerId,
151 const Handle(AIS_InteractiveContext)& theContext,
152 const Handle(HYDROData_Entity)& theObject )
154 HYDROGUI_Shape* aResShape = NULL;
155 if ( theContext.IsNull() || theObject.IsNull() )
158 if ( !HYDROGUI_Tool::IsObjectHasPresentation( theObject, OCCViewer_Viewer::Type() ) )
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( theContext, Handle_HYDROData_Bathymetry::DownCast( theObject ) );
166 aResShape = new HYDROGUI_Shape( theContext, theObject );
168 module()->setObjectShape( theViewerId, theObject, aResShape );
173 void HYDROGUI_OCCDisplayer::Display( const HYDROData_SequenceOfObjects& theObjs,
174 const int theViewerId,
175 const bool theIsForced,
176 const bool theDoFitAll )
178 // Get OCC viewer by id
179 OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
183 // Get interactive context
184 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
189 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( module()->getStudyId() );
193 // Assign Z layer indexes to the objects
194 aDoc->Show( theObjs );
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();
201 HYDROData_SequenceOfObjects::Iterator anAllOrderedIter( anAllOrderedObjects );
202 for ( ; anAllOrderedIter.More(); anAllOrderedIter.Next() ) {
203 QString anOrderedEntry =
204 HYDROGUI_DataObject::dataObjectEntry( anAllOrderedIter.Value() );
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 );
219 Handle(V3d_Viewer) aViewer3d = aViewer->getViewer3d();
222 HYDROGUI_ZLayersIterator aZLayersIt( aViewer->getViewer3d() );
223 if ( !aZLayersIt.More() ) {
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 );
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 );
256 // 3. Update the top Z layer index
261 // Update Z layer of the active operation
262 int aPreviewZLayerId = aZLayersIt.LayerId();
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 );
272 // Update Z layer of hilight presentations
273 int aHilightLayer = aZLayersIt.TopLayer();
274 UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, aHilightLayer );
276 // Fit all / update selection
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() );
284 aViewWindow->onFitAll();
288 else if ( !aCtx.IsNull() ) { // TODO: determine if this code is necessary (added as a fix for issue# 359)
289 aCtx->UpdateSelected();
292 UpdateColorScale( aViewer );
295 void HYDROGUI_OCCDisplayer::purgeObjects( const int theViewerId )
297 OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
301 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
305 AIS_ListOfInteractive aDisplayedObjects;
306 aCtx->DisplayedObjects( aDisplayedObjects );
308 AIS_ListIteratorOfListOfInteractive aListIter( aDisplayedObjects );
309 for ( ; aListIter.More(); aListIter.Next() )
311 Handle(AIS_InteractiveObject) aPrsObj = aListIter.Value();
312 if ( aPrsObj.IsNull() )
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 );
322 QString HYDROGUI_OCCDisplayer::GetType() const
324 return OCCViewer_Viewer::Type();
327 bool HYDROGUI_OCCDisplayer::Display( const Handle(HYDROData_Entity)& theObject,
328 const OCCViewer_Viewer* theViewer,
329 const bool theIsForced )
333 if ( theObject.IsNull() || theObject->IsRemoved() || !theViewer ) {
337 // Get interactive context
338 Handle(AIS_InteractiveContext) aCtx = theViewer->getAISContext();
339 if( aCtx.IsNull() ) {
344 size_t aViewerId = (size_t)theViewer;
347 HYDROGUI_Shape* anObjShape = module()->getObjectShape( aViewerId, theObject );
350 anObjShape = createShape( aViewerId, aCtx, theObject );
352 anObjShape->setIsToUpdate( true );
359 if ( anObjShape->getIsToUpdate() || theIsForced ) {
360 anObjShape->update( false, false );
364 bool anIsVisible = module()->isObjectVisible( aViewerId, theObject );
365 anObjShape->setVisible( anIsVisible, false );
370 UpdateColorScale( theViewer );
374 void HYDROGUI_OCCDisplayer::SetZLayer( const OCCViewer_Viewer* theViewer,
375 const Handle(HYDROData_Entity)& theObject,
376 const int theZLayerId )
378 if ( !theViewer || ( theZLayerId < 0 ) ) {
382 // Get interactive context
383 Handle(AIS_InteractiveContext) aCtx = theViewer->getAISContext();
384 if( aCtx.IsNull() ) {
389 size_t aViewerId = (size_t)theViewer;
392 HYDROGUI_Shape* anObjShape = module()->getObjectShape( aViewerId, theObject );
396 aCtx->SetZLayer( anObjShape->getAISObject(), theZLayerId );
400 void HYDROGUI_OCCDisplayer::UpdateColorScale( const OCCViewer_Viewer* theViewer )
402 OCCViewer_ViewWindow* aWnd = dynamic_cast<OCCViewer_ViewWindow*>( theViewer->getViewManager()->getActiveView() );
403 Handle(V3d_View) aView = aWnd->getViewPort()->getView();
405 int aViewerId = (size_t)theViewer;//TODO: check if viewer id is correct
406 QList<HYDROGUI_Shape*> aShapes = module()->getObjectShapes( aViewerId, KIND_BATHYMETRY );
408 bool isDisplayColorScale = !aShapes.empty();
409 Standard_Real anXPos = 0.05; //TODO
410 Standard_Real anYPos = 0.1; //TODO
411 Standard_Real aWidth = 0.2; //TODO
412 Standard_Real aHeight = 0.5; //TODO
413 Standard_Integer aTextHeight = 14; //TODO
414 Standard_Integer aNbIntervals = 20; //TODO
415 TCollection_ExtendedString aColorScaleTitle = "";//TODO
417 Standard_Real aColorScaleMin = 0, aColorScaleMax = 1, aMin, aMax;
419 foreach( HYDROGUI_Shape* aShape, aShapes )
421 HYDROGUI_ShapeBathymetry* aBathShape = dynamic_cast<HYDROGUI_ShapeBathymetry*>( aShape );
425 aBathShape->GetRange( aMin, aMax );
427 if( isFirst || aMin < aColorScaleMin )
428 aColorScaleMin = aMin;
429 if( isFirst || aMax > aColorScaleMax )
430 aColorScaleMax = aMax;
435 if( isDisplayColorScale )
437 Handle(Aspect_ColorScale) aColorScale = aView->ColorScale();
438 if( !aColorScale.IsNull() )
440 aColorScale->SetXPosition( anXPos );
441 aColorScale->SetYPosition( anYPos );
442 aColorScale->SetWidth( aWidth );
443 aColorScale->SetHeight( aHeight );
445 aColorScale->SetTextHeight( aTextHeight );
446 aColorScale->SetNumberOfIntervals( aNbIntervals );
448 aColorScale->SetTitle( aColorScaleTitle );
449 aColorScale->SetRange( aColorScaleMin, aColorScaleMax );
451 foreach( HYDROGUI_Shape* aShape, aShapes )
453 HYDROGUI_ShapeBathymetry* aBathShape = dynamic_cast<HYDROGUI_ShapeBathymetry*>( aShape );
457 aBathShape->UpdateWithColorScale( aColorScale );
460 if( !aView->ColorScaleIsDisplayed() )
461 aView->ColorScaleDisplay();
465 if( aView->ColorScaleIsDisplayed() )
466 aView->ColorScaleErase();