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 <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>
46 HYDROGUI_OCCDisplayer::HYDROGUI_OCCDisplayer( HYDROGUI_Module* theModule )
47 : HYDROGUI_AbstractDisplayer( theModule )
51 HYDROGUI_OCCDisplayer::~HYDROGUI_OCCDisplayer()
55 void HYDROGUI_OCCDisplayer::SetToUpdate( const HYDROData_SequenceOfObjects& theObjs,
56 const int theViewerId )
58 OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
62 for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
64 Handle(HYDROData_Entity) anObj = theObjs.Value( i );
68 HYDROGUI_Shape* anObjShape = module()->getObjectShape( (size_t)aViewer, anObj );
72 anObjShape->setIsToUpdate( true );
76 int HYDROGUI_OCCDisplayer::AddPreviewZLayer( OCCViewer_ViewManager* theMgr )
79 OCCViewer_Viewer* aViewer = theMgr->getOCCViewer();
83 aLayer = CreateTopZLayer( aViewer->getViewer3d() );
85 // Hilight presentation should be on top
86 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
87 if( !aCtx.IsNull() ) {
88 int aTopLayer = CreateTopZLayer( aViewer->getViewer3d() );
89 if ( aTopLayer > 0 ) {
90 UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, aTopLayer );
97 void HYDROGUI_OCCDisplayer::RemoveZLayer( OCCViewer_ViewManager* theMgr,
103 OCCViewer_Viewer* aViewer = theMgr->getOCCViewer();
107 // Get existing Z layers
108 TColStd_SequenceOfInteger anExistingZLayers;
109 aViewer->getViewer3d()->GetAllZLayers( anExistingZLayers );
110 int aNbLayers = anExistingZLayers.Length();
112 if ( theLayer < aNbLayers )
113 aViewer->getViewer3d()->RemoveZLayer( theLayer );
116 void HYDROGUI_OCCDisplayer::EraseAll( const int theViewerId )
118 OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
122 module()->removeViewShapes( (size_t)aViewer );
125 void HYDROGUI_OCCDisplayer::Erase( const HYDROData_SequenceOfObjects& theObjs,
126 const int theViewerId )
128 OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
132 for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
134 Handle(HYDROData_Entity) anObj = theObjs.Value( i );
138 module()->removeObjectShape( (size_t)aViewer, anObj );
143 HYDROGUI_Shape* HYDROGUI_OCCDisplayer::createShape( const int theViewerId,
144 const Handle(AIS_InteractiveContext)& theContext,
145 const Handle(HYDROData_Entity)& theObject )
147 HYDROGUI_Shape* aResShape = NULL;
148 if ( theContext.IsNull() || theObject.IsNull() )
151 if ( !HYDROGUI_Tool::IsObjectHasPresentation( theObject, OCCViewer_Viewer::Type() ) )
154 aResShape = new HYDROGUI_Shape( theContext, theObject );
155 module()->setObjectShape( theViewerId, theObject, aResShape );
160 void HYDROGUI_OCCDisplayer::Display( const HYDROData_SequenceOfObjects& theObjs,
161 const int theViewerId,
162 const bool theIsForced,
163 const bool theDoFitAll )
165 // Get OCC viewer by id
166 OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
170 // Get interactive context
171 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
176 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( module()->getStudyId() );
180 // Assign Z layer indexes to the objects
181 aDoc->Show( theObjs );
183 // Sort objects by display order ( needed for Z layers assignment only )
184 HYDROData_SequenceOfObjects anUnorderedToDisplay = theObjs;
185 HYDROData_SequenceOfObjects anOrderedToDisplay;
186 HYDROData_SequenceOfObjects anAllOrderedObjects = aDoc->GetObjectsLayerOrder();
188 HYDROData_SequenceOfObjects::Iterator anAllOrderedIter( anAllOrderedObjects );
189 for ( ; anAllOrderedIter.More(); anAllOrderedIter.Next() ) {
190 QString anOrderedEntry =
191 HYDROGUI_DataObject::dataObjectEntry( anAllOrderedIter.Value() );
193 HYDROData_SequenceOfObjects::Iterator aToDisplayIter( anUnorderedToDisplay );
194 for ( ; aToDisplayIter.More(); aToDisplayIter.Next() ) {
195 Handle(HYDROData_Entity) anObjToDisplay = aToDisplayIter.Value();
196 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( anObjToDisplay );
197 if ( anEntry == anOrderedEntry ) {
198 anOrderedToDisplay.Prepend( anObjToDisplay );
199 anUnorderedToDisplay.Remove( aToDisplayIter );
206 Handle(V3d_Viewer) aViewer3d = aViewer->getViewer3d();
209 HYDROGUI_ZLayersIterator aZLayersIt( aViewer->getViewer3d() );
210 if ( !aZLayersIt.More() ) {
214 // 1. Display the ordered objects:
215 HYDROData_SequenceOfObjects::Iterator anOrderedIter( anOrderedToDisplay );
216 for ( ; anOrderedIter.More(); anOrderedIter.Next() ) {
217 Handle(HYDROData_Entity) anObj = anOrderedIter.Value();
218 if ( Display( anObj, aViewer, theIsForced ) ) {
219 // set Z layer ( one Z layer for each ordered object )
220 int aZLayerId = aZLayersIt.LayerId();
221 SetZLayer( aViewer, anObj, aZLayerId );
222 SetZLayerSettings( aViewer3d, aZLayerId, true );
227 // 2. Display the unordered objects:
228 bool isDisplayed = false;
229 int anUnorderedZLayerId = aZLayersIt.LayerId();
230 HYDROData_SequenceOfObjects::Iterator anUnorderedIter( anUnorderedToDisplay );
231 for ( ; anUnorderedIter.More(); anUnorderedIter.Next() ) {
232 Handle(HYDROData_Entity) anObj = anUnorderedIter.Value();
233 if ( Display( anObj, aViewer, theIsForced) ) {
234 // set Z layer ( one Z layer for all unordered objects )
235 SetZLayer( aViewer, anObj, anUnorderedZLayerId );
236 if ( !isDisplayed ) {
237 SetZLayerSettings( aViewer3d, anUnorderedZLayerId, false );
243 // 3. Update the top Z layer index
248 // Update Z layer of the active operation
249 int aPreviewZLayerId = aZLayersIt.LayerId();
251 HYDROGUI_Module* aModule = module();
252 SUIT_Operation* anOp = aModule->activeOperation();
253 HYDROGUI_Operation* aHOp = anOp ? dynamic_cast<HYDROGUI_Operation*>( anOp ) : 0;
254 if ( aHOp && aHOp->getPreviewZLayer() >= 0 ) {
255 aHOp->updatePreviewZLayer( aPreviewZLayerId );
259 // Update Z layer of hilight presentations
260 int aHilightLayer = aZLayersIt.TopLayer();
261 UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, aHilightLayer );
263 // Fit all / update selection
265 OCCViewer_ViewManager* aViewManager
266 = ::qobject_cast<OCCViewer_ViewManager*>( aViewer->getViewManager() );
267 if ( aViewManager ) {
268 OCCViewer_ViewWindow* aViewWindow =
269 ::qobject_cast<OCCViewer_ViewWindow*>( aViewManager->getActiveView() );
271 aViewWindow->onFitAll();
275 else if ( !aCtx.IsNull() ) { // TODO: determine if this code is necessary (added as a fix for issue# 359)
276 aCtx->UpdateSelected();
280 void HYDROGUI_OCCDisplayer::purgeObjects( const int theViewerId )
282 OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
286 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
290 AIS_ListOfInteractive aDisplayedObjects;
291 aCtx->DisplayedObjects( aDisplayedObjects );
293 AIS_ListIteratorOfListOfInteractive aListIter( aDisplayedObjects );
294 for ( ; aListIter.More(); aListIter.Next() )
296 Handle(AIS_InteractiveObject) aPrsObj = aListIter.Value();
297 if ( aPrsObj.IsNull() )
300 Handle(HYDROData_Entity) anOwnerObj =
301 Handle(HYDROData_Entity)::DownCast( aPrsObj->GetOwner() );
302 if ( !anOwnerObj.IsNull() && anOwnerObj->IsRemoved() )
303 module()->removeObjectShape( (size_t)aViewer, anOwnerObj );
307 QString HYDROGUI_OCCDisplayer::GetType() const
309 return OCCViewer_Viewer::Type();
312 bool HYDROGUI_OCCDisplayer::Display( const Handle(HYDROData_Entity)& theObject,
313 const OCCViewer_Viewer* theViewer,
314 const bool theIsForced )
318 if ( theObject.IsNull() || theObject->IsRemoved() || !theViewer ) {
322 // Get interactive context
323 Handle(AIS_InteractiveContext) aCtx = theViewer->getAISContext();
324 if( aCtx.IsNull() ) {
329 size_t aViewerId = (size_t)theViewer;
332 HYDROGUI_Shape* anObjShape = module()->getObjectShape( aViewerId, theObject );
335 anObjShape = createShape( aViewerId, aCtx, theObject );
337 anObjShape->setIsToUpdate( true );
344 if ( anObjShape->getIsToUpdate() || theIsForced ) {
345 anObjShape->update( false );
349 bool anIsVisible = module()->isObjectVisible( aViewerId, theObject );
350 anObjShape->setVisible( anIsVisible, false );
358 void HYDROGUI_OCCDisplayer::SetZLayer( const OCCViewer_Viewer* theViewer,
359 const Handle(HYDROData_Entity)& theObject,
360 const int theZLayerId )
362 if ( !theViewer || ( theZLayerId < 0 ) ) {
366 // Get interactive context
367 Handle(AIS_InteractiveContext) aCtx = theViewer->getAISContext();
368 if( aCtx.IsNull() ) {
373 size_t aViewerId = (size_t)theViewer;
376 HYDROGUI_Shape* anObjShape = module()->getObjectShape( aViewerId, theObject );
380 aCtx->SetZLayer( anObjShape->getAISShape(), theZLayerId );