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 );
173 // TODO: implement sorting in another way
174 // Sort objects by display order ( needed for Z layers assignment only )
175 HYDROData_SequenceOfObjects anObjects = theObjs;
176 HYDROData_SequenceOfObjects anOrderedToDisplay;
177 HYDROData_SequenceOfObjects anAllOrdered = aDoc->GetObjectsLayerOrder();
178 for ( int i = 1, n = anAllOrdered.Length(); i <= n; i++ ) {
179 QString anAllEntry = HYDROGUI_DataObject::dataObjectEntry( anAllOrdered.Value( i ) );
181 for ( int j = 1; j <= anObjects.Length(); j++ ) {
182 Handle(HYDROData_Entity) anObj = anObjects.Value( j );
183 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( anObj );
184 if ( anEntry != anAllEntry ) {
187 anObjects.Remove( j );
188 anOrderedToDisplay.Prepend( anObj );
193 // Get existing Z layers
194 TColStd_SequenceOfInteger anExistingZLayers;
195 aViewer->getViewer3d()->GetAllZLayers( anExistingZLayers );
196 int aNbLayers = anExistingZLayers.Length();
200 for ( int n = anOrderedToDisplay.Length(); i <= n; i++ )
202 Handle(HYDROData_Entity) anObj = anOrderedToDisplay.Value( i );
203 if ( anObj.IsNull() || anObj->IsRemoved() )
206 HYDROGUI_Shape* anObjShape = module()->getObjectShape( (size_t)aViewer, anObj );
208 if ( !anObjShape || anObjShape->getIsToUpdate() || theIsForced )
211 anObjShape = createShape( (size_t)aViewer, aCtx, anObj );
214 anObjShape->update( false );
219 bool anIsVisible = module()->isObjectVisible( (size_t)aViewer, anObj );
220 anObjShape->setVisible( anIsVisible, false );
223 Standard_Integer aLayerId = -1;
224 if ( i <= aNbLayers ) {
225 aLayerId = anExistingZLayers.Value( i );
227 Standard_Integer aNewId = -1;
228 if ( aViewer->getViewer3d()->AddZLayer( aNewId ) ) {
232 if ( aLayerId >= 0 ) {
233 aCtx->SetZLayer( anObjShape->getAISShape(), aLayerId );
237 // update Z layer of the active operation
238 HYDROGUI_Module* aModule = module();
239 SUIT_Operation* anOp = aModule->activeOperation();
240 HYDROGUI_Operation* aHOp = anOp ? dynamic_cast<HYDROGUI_Operation*>( anOp ) : 0;
241 if ( aHOp && aHOp->getPreviewZLayer() >= 0 ) {
242 Standard_Integer aLayerId = -1;
243 if ( i <= aNbLayers )
244 aLayerId = anExistingZLayers.Value( i );
246 Standard_Integer aNewId = -1;
247 if ( aViewer->getViewer3d()->AddZLayer( aNewId ) ) {
251 aHOp->updatePreviewZLayer( aLayerId );
256 OCCViewer_ViewManager* aViewManager
257 = ::qobject_cast<OCCViewer_ViewManager*>( aViewer->getViewManager() );
260 OCCViewer_ViewWindow* aViewWindow =
261 ::qobject_cast<OCCViewer_ViewWindow*>( aViewManager->getActiveView() );
264 aViewWindow->onFitAll();
268 else if ( aCtx.IsNull() )
270 aCtx->UpdateSelected();
274 void HYDROGUI_OCCDisplayer::purgeObjects( const int theViewerId )
276 OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
280 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
284 AIS_ListOfInteractive aDisplayedObjects;
285 aCtx->DisplayedObjects( aDisplayedObjects );
287 AIS_ListIteratorOfListOfInteractive aListIter( aDisplayedObjects );
288 for ( ; aListIter.More(); aListIter.Next() )
290 Handle(AIS_InteractiveObject) aPrsObj = aListIter.Value();
291 if ( aPrsObj.IsNull() )
294 Handle(HYDROData_Entity) anOwnerObj =
295 Handle(HYDROData_Entity)::DownCast( aPrsObj->GetOwner() );
296 if ( !anOwnerObj.IsNull() && anOwnerObj->IsRemoved() )
297 module()->removeObjectShape( (size_t)aViewer, anOwnerObj );
301 QString HYDROGUI_OCCDisplayer::GetType() const
303 return OCCViewer_Viewer::Type();