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 OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
161 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
166 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( module()->getStudyId() );
170 // Assign Z layer indexes
171 aDoc->Show( theObjs );
174 // Sort objects by display order ( needed for Z layers assignment only )
175 typedef QPair<Handle(HYDROData_Entity), bool> Object2ApplyZLayer;
176 QList<Object2ApplyZLayer> anOrderedToDisplay; // list of (object, is_z_layer_applicable)
177 HYDROData_SequenceOfObjects anObjectsToDisplay = theObjs;
178 HYDROData_SequenceOfObjects anOrderedObjects = aDoc->GetObjectsLayerOrder();
180 HYDROData_SequenceOfObjects::Iterator anOrderedIter( anOrderedObjects );
181 for ( ; anOrderedIter.More(); anOrderedIter.Next() ) {
182 QString anOrderedEntry = HYDROGUI_DataObject::dataObjectEntry( anOrderedIter.Value() );
184 HYDROData_SequenceOfObjects::Iterator aToDisplayIter( anObjectsToDisplay );
185 for ( ; aToDisplayIter.More(); aToDisplayIter.Next() ) {
186 Handle(HYDROData_Entity) anObjToDisplay = aToDisplayIter.Value();
187 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( anObjToDisplay );
188 if ( anEntry == anOrderedEntry ) {
189 anObjectsToDisplay.Remove( aToDisplayIter );
190 anOrderedToDisplay.prepend( Object2ApplyZLayer( anObjToDisplay, true ) );
195 // For the rest objects Z layer can't be applied
196 HYDROData_SequenceOfObjects::Iterator aRestObjsIter( anObjectsToDisplay );
197 for ( ; aRestObjsIter.More(); aRestObjsIter.Next() ) {
198 anOrderedToDisplay.prepend( Object2ApplyZLayer( aRestObjsIter.Value(), false ) );
201 // Get existing Z layers
202 TColStd_SequenceOfInteger anExistingZLayers;
203 aViewer->getViewer3d()->GetAllZLayers( anExistingZLayers );
204 int aNbLayers = anExistingZLayers.Length();
207 int aNextZLayerIndex = 2; // don't use the first default Z layer ( which index = 1 )
208 foreach ( const Object2ApplyZLayer& aPair, anOrderedToDisplay ) {
209 Handle(HYDROData_Entity) anObj = aPair.first;
210 if ( anObj.IsNull() || anObj->IsRemoved() )
213 HYDROGUI_Shape* anObjShape = module()->getObjectShape( (size_t)aViewer, anObj );
215 if ( !anObjShape || anObjShape->getIsToUpdate() || theIsForced )
218 anObjShape = createShape( (size_t)aViewer, aCtx, anObj );
221 anObjShape->update( false );
226 bool anIsVisible = module()->isObjectVisible( (size_t)aViewer, anObj );
227 anObjShape->setVisible( anIsVisible, false );
229 bool isZLayerApplicable = aPair.second;
231 // set Z layer if applicable
232 if ( isZLayerApplicable ) {
233 Standard_Integer aLayerId = -1;
234 if ( aNextZLayerIndex <= aNbLayers ) {
235 aLayerId = anExistingZLayers.Value( aNextZLayerIndex );
237 Standard_Integer aNewId = -1;
238 if ( aViewer->getViewer3d()->AddZLayer( aNewId ) ) {
242 if ( aLayerId >= 0 ) {
243 aCtx->SetZLayer( anObjShape->getAISShape(), aLayerId );
250 // Update Z layer of the active operation
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 Standard_Integer aLayerId = -1;
256 if ( aNextZLayerIndex <= aNbLayers )
257 aLayerId = anExistingZLayers.Value( aNextZLayerIndex );
259 Standard_Integer aNewId = -1;
260 if ( aViewer->getViewer3d()->AddZLayer( aNewId ) ) {
264 aHOp->updatePreviewZLayer( aLayerId );
269 OCCViewer_ViewManager* aViewManager
270 = ::qobject_cast<OCCViewer_ViewManager*>( aViewer->getViewManager() );
273 OCCViewer_ViewWindow* aViewWindow =
274 ::qobject_cast<OCCViewer_ViewWindow*>( aViewManager->getActiveView() );
277 aViewWindow->onFitAll();
281 else if ( aCtx.IsNull() )
283 aCtx->UpdateSelected();
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();