Salome HOME
Exlude the user input during process events by application (Bug #325).
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_OCCDisplayer.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROGUI_OCCDisplayer.h"
24
25 #include "HYDROGUI_DataModel.h"
26 #include "HYDROGUI_Module.h"
27 #include "HYDROGUI_Tool.h"
28 #include "HYDROGUI_Shape.h"
29
30 #include <AIS_InteractiveContext.hxx>
31 #include <AIS_ListIteratorOfListOfInteractive.hxx>
32 #include <AIS_ListOfInteractive.hxx>
33
34 #include <TColStd_SequenceOfInteger.hxx>
35
36 #include <OCCViewer_ViewManager.h>
37 #include <OCCViewer_ViewModel.h>
38 #include <OCCViewer_ViewWindow.h>
39
40 HYDROGUI_OCCDisplayer::HYDROGUI_OCCDisplayer( HYDROGUI_Module* theModule )
41 : HYDROGUI_AbstractDisplayer( theModule )
42 {
43 }
44
45 HYDROGUI_OCCDisplayer::~HYDROGUI_OCCDisplayer()
46 {
47 }
48
49 void HYDROGUI_OCCDisplayer::SetToUpdate( const HYDROData_SequenceOfObjects& theObjs,
50                                          const int                          theViewerId )
51 {
52   OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
53   if( !aViewer )
54     return;
55
56   for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
57   {
58     Handle(HYDROData_Entity) anObj = theObjs.Value( i );
59     if( anObj.IsNull() )
60       continue;
61
62     HYDROGUI_Shape* anObjShape = module()->getObjectShape( (size_t)aViewer, anObj );
63     if ( !anObjShape )
64       continue;
65     
66     anObjShape->setIsToUpdate( true );
67   }
68 }
69
70 void HYDROGUI_OCCDisplayer::EraseAll( const int theViewerId )
71 {
72   OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
73   if( !aViewer )
74     return;
75
76   module()->removeViewShapes( (size_t)aViewer );
77 }
78
79 void HYDROGUI_OCCDisplayer::Erase( const HYDROData_SequenceOfObjects& theObjs,
80                                    const int                          theViewerId )
81 {
82   OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
83   if( !aViewer )
84     return;
85
86   for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
87   {
88     Handle(HYDROData_Entity) anObj = theObjs.Value( i );
89     if( anObj.IsNull() )
90       continue;
91
92     module()->removeObjectShape( (size_t)aViewer, anObj );
93   }
94   aViewer->update();
95 }
96
97 HYDROGUI_Shape* HYDROGUI_OCCDisplayer::createShape( const int                             theViewerId,
98                                                     const Handle(AIS_InteractiveContext)& theContext,
99                                                     const Handle(HYDROData_Entity)&       theObject )
100 {
101   HYDROGUI_Shape* aResShape = NULL;
102   if ( theContext.IsNull() || theObject.IsNull() )
103     return aResShape;
104
105   if ( !HYDROGUI_Tool::IsObjectHasPresentation( theObject, OCCViewer_Viewer::Type() ) )
106     return aResShape;
107
108   aResShape = new HYDROGUI_Shape( theContext, theObject );
109   module()->setObjectShape( theViewerId, theObject, aResShape );
110
111   return aResShape;
112 }
113
114 void HYDROGUI_OCCDisplayer::Display( const HYDROData_SequenceOfObjects& theObjs,
115                                      const int                          theViewerId,
116                                      const bool                         theIsForced,
117                                      const bool theDoFitAll )
118 {
119   OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
120   if( !aViewer )
121     return;
122
123   Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
124   if( aCtx.IsNull() )
125     return;
126
127   // Sort objects by display order ( needed for Z layers assignment only )
128   HYDROData_SequenceOfObjects anObjects;
129   int aMaxIndex = -1;
130   for ( int i = 1, n = theObjs.Length(); i <= n; i++ ) {
131     Handle(HYDROData_Entity) anObj = theObjs.Value( i );
132     if ( anObj.IsNull() || anObj->IsRemoved() ) {
133       continue;
134     }
135
136     int aDisplayOrderIndex = module()->getObjectDisplayOrder( (size_t)aViewer, anObj );
137     if ( aDisplayOrderIndex > aMaxIndex ) {
138       anObjects.Append( anObj );
139       aMaxIndex = aDisplayOrderIndex;
140     } else {
141       anObjects.Prepend( anObj );
142     }
143   }
144
145   // Get existing Z layers
146   TColStd_SequenceOfInteger anExistingZLayers;
147   aViewer->getViewer3d()->GetAllZLayers( anExistingZLayers );
148   int aNbLayers = anExistingZLayers.Length();
149
150   // Display
151   for ( int i = 1, n = anObjects.Length(); i <= n; i++ )
152   {
153     Handle(HYDROData_Entity) anObj = anObjects.Value( i );
154     if ( anObj.IsNull() || anObj->IsRemoved() )
155       continue;
156
157     HYDROGUI_Shape* anObjShape = module()->getObjectShape( (size_t)aViewer, anObj );
158
159     if ( !anObjShape || anObjShape->getIsToUpdate() || theIsForced )
160     {
161       if ( !anObjShape )
162         anObjShape = createShape( (size_t)aViewer, aCtx, anObj );
163
164       if ( anObjShape )
165         anObjShape->update( false );
166     }
167
168     if ( anObjShape )
169     {
170       bool anIsVisible = module()->isObjectVisible( (size_t)aViewer, anObj );
171       anObjShape->setVisible( anIsVisible, false );
172
173       // Set Z layer
174       Standard_Integer aLayerId = -1;
175       if ( i <= aNbLayers ) {
176         aLayerId = anExistingZLayers.Value( i );
177       } else {
178         Standard_Integer aNewId = -1;
179         if ( aViewer->getViewer3d()->AddZLayer( aNewId ) ) {
180           aLayerId = aNewId;
181         }
182       }
183       if ( aLayerId >= 0 ) {
184         aCtx->SetZLayer( anObjShape->getAISShape(), aLayerId );
185       }
186     }
187   }
188
189   if ( theDoFitAll )
190   {
191     OCCViewer_ViewManager* aViewManager
192       = ::qobject_cast<OCCViewer_ViewManager*>( aViewer->getViewManager() );
193     if ( aViewManager )
194     {
195       OCCViewer_ViewWindow* aViewWindow = 
196         ::qobject_cast<OCCViewer_ViewWindow*>( aViewManager->getActiveView() );
197       if ( aViewWindow )
198       {
199         aViewWindow->onFitAll();
200       }
201     }
202   }
203 }
204
205 void HYDROGUI_OCCDisplayer::purgeObjects( const int theViewerId )
206 {
207   OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
208   if( !aViewer )
209     return;
210
211   Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
212   if( aCtx.IsNull() )
213     return;
214
215   AIS_ListOfInteractive aDisplayedObjects;
216   aCtx->DisplayedObjects( aDisplayedObjects );
217
218   AIS_ListIteratorOfListOfInteractive aListIter( aDisplayedObjects );
219   for ( ; aListIter.More(); aListIter.Next() )
220   {
221     Handle(AIS_InteractiveObject) aPrsObj = aListIter.Value();
222     if ( aPrsObj.IsNull() )
223       continue;
224
225     Handle(HYDROData_Entity) anOwnerObj = 
226       Handle(HYDROData_Entity)::DownCast( aPrsObj->GetOwner() );
227     if ( !anOwnerObj.IsNull() && anOwnerObj->IsRemoved() )
228       module()->removeObjectShape( (size_t)aViewer, anOwnerObj );
229   }
230 }
231
232 QString HYDROGUI_OCCDisplayer::GetType() const
233 {
234   return OCCViewer_Viewer::Type();
235 }