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"
31 #include <AIS_InteractiveContext.hxx>
32 #include <AIS_ListIteratorOfListOfInteractive.hxx>
33 #include <AIS_ListOfInteractive.hxx>
35 #include <TColStd_SequenceOfInteger.hxx>
37 #include <LightApp_Application.h>
38 #include <SUIT_Study.h>
40 #include <OCCViewer_ViewManager.h>
41 #include <OCCViewer_ViewModel.h>
42 #include <OCCViewer_ViewWindow.h>
44 HYDROGUI_OCCDisplayer::HYDROGUI_OCCDisplayer( HYDROGUI_Module* theModule )
45 : HYDROGUI_AbstractDisplayer( theModule )
49 HYDROGUI_OCCDisplayer::~HYDROGUI_OCCDisplayer()
53 void HYDROGUI_OCCDisplayer::SetToUpdate( const HYDROData_SequenceOfObjects& theObjs,
54 const int theViewerId )
56 OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
60 for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
62 Handle(HYDROData_Entity) anObj = theObjs.Value( i );
66 HYDROGUI_Shape* anObjShape = module()->getObjectShape( (size_t)aViewer, anObj );
70 anObjShape->setIsToUpdate( true );
74 int HYDROGUI_OCCDisplayer::AddTopZLayer( OCCViewer_ViewManager* theMgr )
77 OCCViewer_Viewer* aViewer = theMgr->getOCCViewer();
81 Standard_Integer aNewId = -1;
82 if ( aViewer->getViewer3d()->AddZLayer( aNewId ) )
88 void HYDROGUI_OCCDisplayer::RemoveZLayer( OCCViewer_ViewManager* theMgr,
94 OCCViewer_Viewer* aViewer = theMgr->getOCCViewer();
98 // Get existing Z layers
99 TColStd_SequenceOfInteger anExistingZLayers;
100 aViewer->getViewer3d()->GetAllZLayers( anExistingZLayers );
101 int aNbLayers = anExistingZLayers.Length();
103 if ( theLayer < aNbLayers )
104 aViewer->getViewer3d()->RemoveZLayer( theLayer );
107 void HYDROGUI_OCCDisplayer::EraseAll( const int theViewerId )
109 OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
113 module()->removeViewShapes( (size_t)aViewer );
116 void HYDROGUI_OCCDisplayer::Erase( const HYDROData_SequenceOfObjects& theObjs,
117 const int theViewerId )
119 OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
123 for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
125 Handle(HYDROData_Entity) anObj = theObjs.Value( i );
129 module()->removeObjectShape( (size_t)aViewer, anObj );
134 HYDROGUI_Shape* HYDROGUI_OCCDisplayer::createShape( const int theViewerId,
135 const Handle(AIS_InteractiveContext)& theContext,
136 const Handle(HYDROData_Entity)& theObject )
138 HYDROGUI_Shape* aResShape = NULL;
139 if ( theContext.IsNull() || theObject.IsNull() )
142 if ( !HYDROGUI_Tool::IsObjectHasPresentation( theObject, OCCViewer_Viewer::Type() ) )
145 aResShape = new HYDROGUI_Shape( theContext, theObject );
146 module()->setObjectShape( theViewerId, theObject, aResShape );
151 void HYDROGUI_OCCDisplayer::Display( const HYDROData_SequenceOfObjects& theObjs,
152 const int theViewerId,
153 const bool theIsForced,
154 const bool theDoFitAll )
156 OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
160 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
164 // Sort objects by display order ( needed for Z layers assignment only )
165 HYDROData_SequenceOfObjects anObjects;
167 for ( int i = 1, n = theObjs.Length(); i <= n; i++ ) {
168 Handle(HYDROData_Entity) anObj = theObjs.Value( i );
169 if ( anObj.IsNull() || anObj->IsRemoved() ) {
173 int aDisplayOrderIndex = module()->getObjectDisplayOrder( (size_t)aViewer, anObj );
174 if ( aDisplayOrderIndex > aMaxIndex ) {
175 anObjects.Append( anObj );
176 aMaxIndex = aDisplayOrderIndex;
178 anObjects.Prepend( anObj );
182 // Get existing Z layers
183 TColStd_SequenceOfInteger anExistingZLayers;
184 aViewer->getViewer3d()->GetAllZLayers( anExistingZLayers );
185 int aNbLayers = anExistingZLayers.Length();
189 for ( int n = anObjects.Length(); i <= n; i++ )
191 Handle(HYDROData_Entity) anObj = anObjects.Value( i );
192 if ( anObj.IsNull() || anObj->IsRemoved() )
195 HYDROGUI_Shape* anObjShape = module()->getObjectShape( (size_t)aViewer, anObj );
197 if ( !anObjShape || anObjShape->getIsToUpdate() || theIsForced )
200 anObjShape = createShape( (size_t)aViewer, aCtx, anObj );
203 anObjShape->update( false );
208 bool anIsVisible = module()->isObjectVisible( (size_t)aViewer, anObj );
209 anObjShape->setVisible( anIsVisible, false );
212 Standard_Integer aLayerId = -1;
213 if ( i <= aNbLayers ) {
214 aLayerId = anExistingZLayers.Value( i );
216 Standard_Integer aNewId = -1;
217 if ( aViewer->getViewer3d()->AddZLayer( aNewId ) ) {
221 if ( aLayerId >= 0 ) {
222 aCtx->SetZLayer( anObjShape->getAISShape(), aLayerId );
226 // update Z layer of the active operation
227 HYDROGUI_Module* aModule = module();
228 SUIT_Operation* anOp = aModule->activeOperation();
229 HYDROGUI_Operation* aHOp = anOp ? dynamic_cast<HYDROGUI_Operation*>( anOp ) : 0;
230 if ( aHOp && aHOp->getPreviewZLayer() >= 0 ) {
231 Standard_Integer aLayerId = -1;
232 if ( i <= aNbLayers )
233 aLayerId = anExistingZLayers.Value( i );
235 Standard_Integer aNewId = -1;
236 if ( aViewer->getViewer3d()->AddZLayer( aNewId ) ) {
240 aHOp->updatePreviewZLayer( aLayerId );
245 OCCViewer_ViewManager* aViewManager
246 = ::qobject_cast<OCCViewer_ViewManager*>( aViewer->getViewManager() );
249 OCCViewer_ViewWindow* aViewWindow =
250 ::qobject_cast<OCCViewer_ViewWindow*>( aViewManager->getActiveView() );
253 aViewWindow->onFitAll();
257 else if ( aCtx.IsNull() )
259 aCtx->UpdateSelected();
263 void HYDROGUI_OCCDisplayer::purgeObjects( const int theViewerId )
265 OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
269 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
273 AIS_ListOfInteractive aDisplayedObjects;
274 aCtx->DisplayedObjects( aDisplayedObjects );
276 AIS_ListIteratorOfListOfInteractive aListIter( aDisplayedObjects );
277 for ( ; aListIter.More(); aListIter.Next() )
279 Handle(AIS_InteractiveObject) aPrsObj = aListIter.Value();
280 if ( aPrsObj.IsNull() )
283 Handle(HYDROData_Entity) anOwnerObj =
284 Handle(HYDROData_Entity)::DownCast( aPrsObj->GetOwner() );
285 if ( !anOwnerObj.IsNull() && anOwnerObj->IsRemoved() )
286 module()->removeObjectShape( (size_t)aViewer, anOwnerObj );
290 QString HYDROGUI_OCCDisplayer::GetType() const
292 return OCCViewer_Viewer::Type();