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"
32 #include <AIS_InteractiveContext.hxx>
33 #include <AIS_ListIteratorOfListOfInteractive.hxx>
34 #include <AIS_ListOfInteractive.hxx>
36 #include <TColStd_SequenceOfInteger.hxx>
38 #include <LightApp_Application.h>
39 #include <SUIT_Study.h>
41 #include <OCCViewer_ViewManager.h>
42 #include <OCCViewer_ViewModel.h>
43 #include <OCCViewer_ViewWindow.h>
45 HYDROGUI_OCCDisplayer::HYDROGUI_OCCDisplayer( HYDROGUI_Module* theModule )
46 : HYDROGUI_AbstractDisplayer( theModule )
50 HYDROGUI_OCCDisplayer::~HYDROGUI_OCCDisplayer()
54 void HYDROGUI_OCCDisplayer::SetToUpdate( const HYDROData_SequenceOfObjects& theObjs,
55 const int theViewerId )
57 OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
61 for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
63 Handle(HYDROData_Entity) anObj = theObjs.Value( i );
67 HYDROGUI_Shape* anObjShape = module()->getObjectShape( (size_t)aViewer, anObj );
71 anObjShape->setIsToUpdate( true );
75 int HYDROGUI_OCCDisplayer::AddTopZLayer( OCCViewer_ViewManager* theMgr )
78 OCCViewer_Viewer* aViewer = theMgr->getOCCViewer();
82 Standard_Integer aNewId = -1;
83 if ( aViewer->getViewer3d()->AddZLayer( aNewId ) )
89 void HYDROGUI_OCCDisplayer::RemoveZLayer( OCCViewer_ViewManager* theMgr,
95 OCCViewer_Viewer* aViewer = theMgr->getOCCViewer();
99 // Get existing Z layers
100 TColStd_SequenceOfInteger anExistingZLayers;
101 aViewer->getViewer3d()->GetAllZLayers( anExistingZLayers );
102 int aNbLayers = anExistingZLayers.Length();
104 if ( theLayer < aNbLayers )
105 aViewer->getViewer3d()->RemoveZLayer( theLayer );
108 void HYDROGUI_OCCDisplayer::EraseAll( const int theViewerId )
110 OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
114 module()->removeViewShapes( (size_t)aViewer );
117 void HYDROGUI_OCCDisplayer::Erase( const HYDROData_SequenceOfObjects& theObjs,
118 const int theViewerId )
120 OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
124 for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
126 Handle(HYDROData_Entity) anObj = theObjs.Value( i );
130 module()->removeObjectShape( (size_t)aViewer, anObj );
135 HYDROGUI_Shape* HYDROGUI_OCCDisplayer::createShape( const int theViewerId,
136 const Handle(AIS_InteractiveContext)& theContext,
137 const Handle(HYDROData_Entity)& theObject )
139 HYDROGUI_Shape* aResShape = NULL;
140 if ( theContext.IsNull() || theObject.IsNull() )
143 if ( !HYDROGUI_Tool::IsObjectHasPresentation( theObject, OCCViewer_Viewer::Type() ) )
146 aResShape = new HYDROGUI_Shape( theContext, theObject );
147 module()->setObjectShape( theViewerId, theObject, aResShape );
152 void HYDROGUI_OCCDisplayer::Display( const HYDROData_SequenceOfObjects& theObjs,
153 const int theViewerId,
154 const bool theIsForced,
155 const bool theDoFitAll )
157 // Get OCC viewer by id
158 OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
162 // Get interactive context
163 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
168 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( module()->getStudyId() );
172 // Assign Z layer indexes to the objects
173 aDoc->Show( theObjs );
175 // Sort objects by display order ( needed for Z layers assignment only )
176 HYDROData_SequenceOfObjects anUnorderedToDisplay = theObjs;
177 HYDROData_SequenceOfObjects anOrderedToDisplay;
178 HYDROData_SequenceOfObjects anAllOrderedObjects = aDoc->GetObjectsLayerOrder();
180 HYDROData_SequenceOfObjects::Iterator anAllOrderedIter( anAllOrderedObjects );
181 for ( ; anAllOrderedIter.More(); anAllOrderedIter.Next() ) {
182 QString anOrderedEntry =
183 HYDROGUI_DataObject::dataObjectEntry( anAllOrderedIter.Value() );
185 HYDROData_SequenceOfObjects::Iterator aToDisplayIter( anUnorderedToDisplay );
186 for ( ; aToDisplayIter.More(); aToDisplayIter.Next() ) {
187 Handle(HYDROData_Entity) anObjToDisplay = aToDisplayIter.Value();
188 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( anObjToDisplay );
189 if ( anEntry == anOrderedEntry ) {
190 anOrderedToDisplay.Prepend( anObjToDisplay );
191 anUnorderedToDisplay.Remove( aToDisplayIter );
198 Handle(V3d_Viewer) aViewer3d = aViewer->getViewer3d();
200 // Get existing Z layers
201 TColStd_SequenceOfInteger anExistingZLayers;
202 aViewer3d->GetAllZLayers( anExistingZLayers );
203 int aNbLayers = anExistingZLayers.Length();
206 int aNextZLayerIndex = 2; // don't use the first default Z layer ( which index = 1 )
208 // 1. Display the ordered objects:
209 HYDROData_SequenceOfObjects::Iterator anOrderedIter( anOrderedToDisplay );
210 for ( ; anOrderedIter.More(); anOrderedIter.Next() ) {
211 Handle(HYDROData_Entity) anObj = anOrderedIter.Value();
212 if ( Display( anObj, aViewer, theIsForced, aNextZLayerIndex, true ) ) {
217 // 2. Display the unordered objects:
218 bool isDisplayed = false;
219 HYDROData_SequenceOfObjects::Iterator anUnorderedIter( anUnorderedToDisplay );
220 for ( ; anUnorderedIter.More(); anUnorderedIter.Next() ) {
221 Handle(HYDROData_Entity) anObj = anUnorderedIter.Value();
222 if ( Display( anObj, aViewer, theIsForced, aNextZLayerIndex, false ) ) {
227 // 3. Update the top Z layer index
232 // Update Z layer of the active operation
233 aViewer3d->GetAllZLayers( anExistingZLayers );
235 HYDROGUI_Module* aModule = module();
236 SUIT_Operation* anOp = aModule->activeOperation();
237 HYDROGUI_Operation* aHOp = anOp ? dynamic_cast<HYDROGUI_Operation*>( anOp ) : 0;
238 if ( aHOp && aHOp->getPreviewZLayer() >= 0 ) {
239 Standard_Integer aLayerId = -1;
240 if ( aNextZLayerIndex <= aNbLayers )
241 aLayerId = anExistingZLayers.Value( aNextZLayerIndex );
243 Standard_Integer aNewId = -1;
244 if ( aViewer->getViewer3d()->AddZLayer( aNewId ) ) {
248 aHOp->updatePreviewZLayer( aLayerId );
253 OCCViewer_ViewManager* aViewManager
254 = ::qobject_cast<OCCViewer_ViewManager*>( aViewer->getViewManager() );
257 OCCViewer_ViewWindow* aViewWindow =
258 ::qobject_cast<OCCViewer_ViewWindow*>( aViewManager->getActiveView() );
261 aViewWindow->onFitAll();
265 else if ( !aCtx.IsNull() ) // TODO: determine if this code is necessary (added as a fix for issue# 359)
267 aCtx->UpdateSelected();
271 void HYDROGUI_OCCDisplayer::purgeObjects( const int theViewerId )
273 OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
277 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
281 AIS_ListOfInteractive aDisplayedObjects;
282 aCtx->DisplayedObjects( aDisplayedObjects );
284 AIS_ListIteratorOfListOfInteractive aListIter( aDisplayedObjects );
285 for ( ; aListIter.More(); aListIter.Next() )
287 Handle(AIS_InteractiveObject) aPrsObj = aListIter.Value();
288 if ( aPrsObj.IsNull() )
291 Handle(HYDROData_Entity) anOwnerObj =
292 Handle(HYDROData_Entity)::DownCast( aPrsObj->GetOwner() );
293 if ( !anOwnerObj.IsNull() && anOwnerObj->IsRemoved() )
294 module()->removeObjectShape( (size_t)aViewer, anOwnerObj );
298 QString HYDROGUI_OCCDisplayer::GetType() const
300 return OCCViewer_Viewer::Type();
303 bool HYDROGUI_OCCDisplayer::Display( const Handle(HYDROData_Entity)& theObject,
304 const OCCViewer_Viewer* theViewer,
305 const bool theIsForced,
306 const int theZLayerIndex,
307 const bool theIsOrdered )
311 if ( theObject.IsNull() || theObject->IsRemoved() || !theViewer ) {
315 // Get interactive context
316 Handle(AIS_InteractiveContext) aCtx = theViewer->getAISContext();
317 if( aCtx.IsNull() ) {
322 size_t aViewerId = (size_t)theViewer;
325 HYDROGUI_Shape* anObjShape = module()->getObjectShape( aViewerId, theObject );
328 anObjShape = createShape( aViewerId, aCtx, theObject );
330 anObjShape->setIsToUpdate( true );
337 if ( anObjShape->getIsToUpdate() || theIsForced ) {
338 anObjShape->update( false );
342 bool anIsVisible = module()->isObjectVisible( aViewerId, theObject );
343 anObjShape->setVisible( anIsVisible, false );
346 Handle(V3d_Viewer) aViewer3d = theViewer->getViewer3d();
347 if ( !aViewer3d.IsNull() ) {
348 // Get existing layers
349 TColStd_SequenceOfInteger anExistingZLayers;
350 aViewer3d->GetAllZLayers( anExistingZLayers );
351 int aNbLayers = anExistingZLayers.Length();
353 // Get or create the appropriate layer
354 Standard_Integer aLayerId = -1;
355 if ( theZLayerIndex <= aNbLayers ) {
356 aLayerId = anExistingZLayers.Value( theZLayerIndex );
358 Standard_Integer aNewId = -1;
359 if ( aViewer3d->AddZLayer( aNewId ) ) {
364 if ( aLayerId >= 0 ) {
366 aCtx->SetZLayer( anObjShape->getAISShape(), aLayerId );
368 // set Z layer settings for the 3d viewer:
370 Graphic3d_ZLayerSettings aSettings = aViewer3d->ZLayerSettings( aLayerId );
371 // enable depth write
372 aSettings.EnableSetting( Graphic3d_ZLayerDepthWrite );
373 // disable depth clear
374 aSettings.DisableSetting( Graphic3d_ZLayerDepthClear );
375 if ( theIsOrdered ) {
376 // disable depth test
377 aSettings.DisableSetting( Graphic3d_ZLayerDepthTest );
378 // disable depth offset
379 aSettings.DisableSetting( Graphic3d_ZLayerDepthOffset );
382 aSettings.EnableSetting( Graphic3d_ZLayerDepthTest );
384 aSettings.SetDepthOffsetPositive();
388 aViewer3d->SetZLayerSettings( aLayerId, aSettings );