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_Shape.h"
29 #include "HYDROGUI_Operation.h"
30 #include "HYDROGUI_DataObject.h"
31 #include "HYDROGUI_ZLayers.h"
33 #include <AIS_InteractiveContext.hxx>
34 #include <AIS_ListIteratorOfListOfInteractive.hxx>
35 #include <AIS_ListOfInteractive.hxx>
37 #include <Aspect_ColorScale.hxx>
39 #include <TColStd_SequenceOfInteger.hxx>
41 #include <LightApp_Application.h>
42 #include <SUIT_Study.h>
44 #include <OCCViewer_ViewManager.h>
45 #include <OCCViewer_ViewModel.h>
46 #include <OCCViewer_ViewWindow.h>
47 #include <OCCViewer_ViewPort3d.h>
49 HYDROGUI_OCCDisplayer::HYDROGUI_OCCDisplayer( HYDROGUI_Module* theModule )
50 : HYDROGUI_AbstractDisplayer( theModule )
54 HYDROGUI_OCCDisplayer::~HYDROGUI_OCCDisplayer()
58 void HYDROGUI_OCCDisplayer::SetToUpdate( const HYDROData_SequenceOfObjects& theObjs,
59 const int theViewerId )
61 OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
65 for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
67 Handle(HYDROData_Entity) anObj = theObjs.Value( i );
71 HYDROGUI_Shape* anObjShape = module()->getObjectShape( (size_t)aViewer, anObj );
75 anObjShape->setIsToUpdate( true );
79 int HYDROGUI_OCCDisplayer::AddPreviewZLayer( OCCViewer_ViewManager* theMgr )
82 OCCViewer_Viewer* aViewer = theMgr->getOCCViewer();
86 aLayer = CreateTopZLayer( aViewer->getViewer3d() );
88 // Hilight presentation should be on top
89 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
90 if( !aCtx.IsNull() ) {
91 int aTopLayer = CreateTopZLayer( aViewer->getViewer3d() );
92 if ( aTopLayer > 0 ) {
93 UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, aTopLayer );
100 void HYDROGUI_OCCDisplayer::RemoveZLayer( OCCViewer_ViewManager* theMgr,
106 OCCViewer_Viewer* aViewer = theMgr->getOCCViewer();
110 // Get existing Z layers
111 TColStd_SequenceOfInteger anExistingZLayers;
112 aViewer->getViewer3d()->GetAllZLayers( anExistingZLayers );
113 int aNbLayers = anExistingZLayers.Length();
115 if ( theLayer < aNbLayers )
116 aViewer->getViewer3d()->RemoveZLayer( theLayer );
119 void HYDROGUI_OCCDisplayer::EraseAll( const int theViewerId )
121 OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
125 module()->removeViewShapes( (size_t)aViewer );
126 UpdateColorScale( aViewer );
129 void HYDROGUI_OCCDisplayer::Erase( const HYDROData_SequenceOfObjects& theObjs,
130 const int theViewerId )
132 OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
136 for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
138 Handle(HYDROData_Entity) anObj = theObjs.Value( i );
142 module()->removeObjectShape( (size_t)aViewer, anObj );
145 UpdateColorScale( aViewer );
148 HYDROGUI_Shape* HYDROGUI_OCCDisplayer::createShape( const int theViewerId,
149 const Handle(AIS_InteractiveContext)& theContext,
150 const Handle(HYDROData_Entity)& theObject )
152 HYDROGUI_Shape* aResShape = NULL;
153 if ( theContext.IsNull() || theObject.IsNull() )
156 if ( !HYDROGUI_Tool::IsObjectHasPresentation( theObject, OCCViewer_Viewer::Type() ) )
159 aResShape = new HYDROGUI_Shape( theContext, theObject );
160 module()->setObjectShape( theViewerId, theObject, aResShape );
165 void HYDROGUI_OCCDisplayer::Display( const HYDROData_SequenceOfObjects& theObjs,
166 const int theViewerId,
167 const bool theIsForced,
168 const bool theDoFitAll )
170 // Get OCC viewer by id
171 OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
175 // Get interactive context
176 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
181 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( module()->getStudyId() );
185 // Assign Z layer indexes to the objects
186 aDoc->Show( theObjs );
188 // Sort objects by display order ( needed for Z layers assignment only )
189 HYDROData_SequenceOfObjects anUnorderedToDisplay = theObjs;
190 HYDROData_SequenceOfObjects anOrderedToDisplay;
191 HYDROData_SequenceOfObjects anAllOrderedObjects = aDoc->GetObjectsLayerOrder();
193 HYDROData_SequenceOfObjects::Iterator anAllOrderedIter( anAllOrderedObjects );
194 for ( ; anAllOrderedIter.More(); anAllOrderedIter.Next() ) {
195 QString anOrderedEntry =
196 HYDROGUI_DataObject::dataObjectEntry( anAllOrderedIter.Value() );
198 HYDROData_SequenceOfObjects::Iterator aToDisplayIter( anUnorderedToDisplay );
199 for ( ; aToDisplayIter.More(); aToDisplayIter.Next() ) {
200 Handle(HYDROData_Entity) anObjToDisplay = aToDisplayIter.Value();
201 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( anObjToDisplay );
202 if ( anEntry == anOrderedEntry ) {
203 anOrderedToDisplay.Prepend( anObjToDisplay );
204 anUnorderedToDisplay.Remove( aToDisplayIter );
211 Handle(V3d_Viewer) aViewer3d = aViewer->getViewer3d();
214 HYDROGUI_ZLayersIterator aZLayersIt( aViewer->getViewer3d() );
215 if ( !aZLayersIt.More() ) {
219 // 1. Display the ordered objects:
220 HYDROData_SequenceOfObjects::Iterator anOrderedIter( anOrderedToDisplay );
221 for ( ; anOrderedIter.More(); anOrderedIter.Next() ) {
222 Handle(HYDROData_Entity) anObj = anOrderedIter.Value();
223 if ( Display( anObj, aViewer, theIsForced ) ) {
224 // set Z layer ( one Z layer for each ordered object )
225 int aZLayerId = aZLayersIt.LayerId();
226 SetZLayer( aViewer, anObj, aZLayerId );
227 SetZLayerSettings( aViewer3d, aZLayerId, true );
232 // 2. Display the unordered objects:
233 bool isDisplayed = false;
234 int anUnorderedZLayerId = aZLayersIt.LayerId();
235 HYDROData_SequenceOfObjects::Iterator anUnorderedIter( anUnorderedToDisplay );
236 for ( ; anUnorderedIter.More(); anUnorderedIter.Next() ) {
237 Handle(HYDROData_Entity) anObj = anUnorderedIter.Value();
238 if ( Display( anObj, aViewer, theIsForced) ) {
239 // set Z layer ( one Z layer for all unordered objects )
240 SetZLayer( aViewer, anObj, anUnorderedZLayerId );
241 if ( !isDisplayed ) {
242 SetZLayerSettings( aViewer3d, anUnorderedZLayerId, false );
248 // 3. Update the top Z layer index
253 // Update Z layer of the active operation
254 int aPreviewZLayerId = aZLayersIt.LayerId();
256 HYDROGUI_Module* aModule = module();
257 SUIT_Operation* anOp = aModule->activeOperation();
258 HYDROGUI_Operation* aHOp = anOp ? dynamic_cast<HYDROGUI_Operation*>( anOp ) : 0;
259 if ( aHOp && aHOp->getPreviewZLayer() >= 0 ) {
260 aHOp->updatePreviewZLayer( aPreviewZLayerId );
264 // Update Z layer of hilight presentations
265 int aHilightLayer = aZLayersIt.TopLayer();
266 UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, aHilightLayer );
268 // Fit all / update selection
270 OCCViewer_ViewManager* aViewManager
271 = ::qobject_cast<OCCViewer_ViewManager*>( aViewer->getViewManager() );
272 if ( aViewManager ) {
273 OCCViewer_ViewWindow* aViewWindow =
274 ::qobject_cast<OCCViewer_ViewWindow*>( aViewManager->getActiveView() );
276 aViewWindow->onFitAll();
280 else if ( !aCtx.IsNull() ) { // TODO: determine if this code is necessary (added as a fix for issue# 359)
281 aCtx->UpdateSelected();
284 UpdateColorScale( aViewer );
287 void HYDROGUI_OCCDisplayer::purgeObjects( const int theViewerId )
289 OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
293 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
297 AIS_ListOfInteractive aDisplayedObjects;
298 aCtx->DisplayedObjects( aDisplayedObjects );
300 AIS_ListIteratorOfListOfInteractive aListIter( aDisplayedObjects );
301 for ( ; aListIter.More(); aListIter.Next() )
303 Handle(AIS_InteractiveObject) aPrsObj = aListIter.Value();
304 if ( aPrsObj.IsNull() )
307 Handle(HYDROData_Entity) anOwnerObj =
308 Handle(HYDROData_Entity)::DownCast( aPrsObj->GetOwner() );
309 if ( !anOwnerObj.IsNull() && anOwnerObj->IsRemoved() )
310 module()->removeObjectShape( (size_t)aViewer, anOwnerObj );
314 QString HYDROGUI_OCCDisplayer::GetType() const
316 return OCCViewer_Viewer::Type();
319 bool HYDROGUI_OCCDisplayer::Display( const Handle(HYDROData_Entity)& theObject,
320 const OCCViewer_Viewer* theViewer,
321 const bool theIsForced )
325 if ( theObject.IsNull() || theObject->IsRemoved() || !theViewer ) {
329 // Get interactive context
330 Handle(AIS_InteractiveContext) aCtx = theViewer->getAISContext();
331 if( aCtx.IsNull() ) {
336 size_t aViewerId = (size_t)theViewer;
339 HYDROGUI_Shape* anObjShape = module()->getObjectShape( aViewerId, theObject );
342 anObjShape = createShape( aViewerId, aCtx, theObject );
344 anObjShape->setIsToUpdate( true );
351 if ( anObjShape->getIsToUpdate() || theIsForced ) {
352 anObjShape->update( false );
356 bool anIsVisible = module()->isObjectVisible( aViewerId, theObject );
357 anObjShape->setVisible( anIsVisible, false );
362 UpdateColorScale( theViewer );
366 void HYDROGUI_OCCDisplayer::SetZLayer( const OCCViewer_Viewer* theViewer,
367 const Handle(HYDROData_Entity)& theObject,
368 const int theZLayerId )
370 if ( !theViewer || ( theZLayerId < 0 ) ) {
374 // Get interactive context
375 Handle(AIS_InteractiveContext) aCtx = theViewer->getAISContext();
376 if( aCtx.IsNull() ) {
381 size_t aViewerId = (size_t)theViewer;
384 HYDROGUI_Shape* anObjShape = module()->getObjectShape( aViewerId, theObject );
388 aCtx->SetZLayer( anObjShape->getAISObject(), theZLayerId );
392 void HYDROGUI_OCCDisplayer::UpdateColorScale( const OCCViewer_Viewer* theViewer )
394 OCCViewer_ViewWindow* aWnd = dynamic_cast<OCCViewer_ViewWindow*>( theViewer->getViewManager()->getActiveView() );
395 Handle(V3d_View) aView = aWnd->getViewPort()->getView();
397 int aViewerId = (size_t)theViewer;//TODO: check if viewer id is correct
398 QList<HYDROGUI_Shape*> aBaths = module()->getObjectShapes( aViewerId, KIND_BATHYMETRY );
400 bool isDisplayColorScale = !aBaths.empty();
401 Standard_Real anXPos = 0.05; //TODO
402 Standard_Real anYPos = 0.1; //TODO
403 Standard_Real aWidth = 0.2; //TODO
404 Standard_Real aHeight = 0.5; //TODO
405 Standard_Integer aTextHeight = 14; //TODO
406 Standard_Integer aNbIntervals = 20; //TODO
407 TCollection_ExtendedString aColorScaleTitle = "";//TODO
409 Standard_Real aColorScaleMin = 0, aColorScaleMax = 1, aMin, aMax;
411 foreach( HYDROGUI_Shape* aBathShape, aBaths )
413 aBathShape->GetRange( aMin, aMax );
415 if( isFirst || aMin < aColorScaleMin )
416 aColorScaleMin = aMin;
417 if( isFirst || aMax > aColorScaleMax )
418 aColorScaleMax = aMax;
423 if( isDisplayColorScale )
425 Handle(Aspect_ColorScale) aColorScale = aView->ColorScale();
426 if( !aColorScale.IsNull() )
428 aColorScale->SetXPosition( anXPos );
429 aColorScale->SetYPosition( anYPos );
430 aColorScale->SetWidth( aWidth );
431 aColorScale->SetHeight( aHeight );
433 aColorScale->SetTextHeight( aTextHeight );
434 aColorScale->SetNumberOfIntervals( aNbIntervals );
436 aColorScale->SetTitle( aColorScaleTitle );
437 aColorScale->SetRange( aColorScaleMin, aColorScaleMax );
439 foreach( HYDROGUI_Shape* aBathShape, aBaths )
440 aBathShape->UpdateWithColorScale( aColorScale );
442 if( !aView->ColorScaleIsDisplayed() )
443 aView->ColorScaleDisplay();
447 if( aView->ColorScaleIsDisplayed() )
448 aView->ColorScaleErase();