Salome HOME
d1655f3c411ba05c8726579c0b87e5d4a8951283
[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 <OCCViewer_ViewManager.h>
35 #include <OCCViewer_ViewModel.h>
36 #include <OCCViewer_ViewWindow.h>
37
38 #include <QApplication>
39
40 HYDROGUI_OCCDisplayer::HYDROGUI_OCCDisplayer( HYDROGUI_Module* theModule )
41 : myModule( 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 = myModule->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 = myModule->getObjectShape( (size_t)aViewer, anObj );
63     if ( !anObjShape )
64       continue;
65     
66     anObjShape->setIsToUpdate( true );
67   }
68 }
69
70 void HYDROGUI_OCCDisplayer::UpdateAll( const int  theViewerId,
71                                        const bool theIsInit,
72                                        const bool theIsForced )
73 {
74   if ( theIsInit )
75     EraseAll( theViewerId );
76
77   DisplayAll( theViewerId, theIsForced );
78 }
79
80 void HYDROGUI_OCCDisplayer::EraseAll( const int theViewerId )
81 {
82   OCCViewer_Viewer* aViewer = myModule->getOCCViewer( theViewerId );
83   if( !aViewer )
84     return;
85
86   myModule->removeViewShapes( (size_t)aViewer );
87 }
88
89 void HYDROGUI_OCCDisplayer::DisplayAll( const int  theViewerId,
90                                         const bool theIsForced )
91 {
92   HYDROData_SequenceOfObjects aSeq;
93   HYDROGUI_Tool::GetPrsSubObjects( myModule, aSeq );
94   Update( aSeq, theViewerId, theIsForced );
95 }
96
97 void HYDROGUI_OCCDisplayer::Update( const HYDROData_SequenceOfObjects& theObjs,
98                                     const int                          theViewerId,
99                                     const bool                         theIsForced )
100 {
101   // First of all, kill all bad presentations
102   purgeObjects( theViewerId );
103
104   // Now dig in the data model
105   HYDROData_SequenceOfObjects anObjectsToErase, anObjectsToDisplay;
106
107   for( int i = 1, n = theObjs.Length(); i <= n; i++ )
108   {
109     const Handle(HYDROData_Entity)& anObj = theObjs.Value( i );
110     if( anObj.IsNull() )
111       anObjectsToErase.Append( anObj );
112     else
113       anObjectsToDisplay.Append( anObj );
114   }
115
116   if( anObjectsToErase.Length() )
117     Erase( anObjectsToErase, theViewerId );
118   if( anObjectsToDisplay.Length() )
119     Display( anObjectsToDisplay, theViewerId, theIsForced );
120 }
121
122 void HYDROGUI_OCCDisplayer::Erase( const HYDROData_SequenceOfObjects& theObjs,
123                                    const int                          theViewerId )
124 {
125   OCCViewer_Viewer* aViewer = myModule->getOCCViewer( theViewerId );
126   if( !aViewer )
127     return;
128
129   for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
130   {
131     Handle(HYDROData_Entity) anObj = theObjs.Value( i );
132     if( anObj.IsNull() )
133       continue;
134
135     myModule->removeObjectShape( (size_t)aViewer, anObj );
136   }
137 }
138
139 HYDROGUI_Shape* HYDROGUI_OCCDisplayer::createShape( const int                             theViewerId,
140                                                     const Handle(AIS_InteractiveContext)& theContext,
141                                                     const Handle(HYDROData_Entity)&       theObject )
142 {
143   HYDROGUI_Shape* aResShape = NULL;
144   if ( theContext.IsNull() || theObject.IsNull() )
145     return aResShape;
146
147   ObjectKind anObjectKind = theObject->GetKind();
148   if ( anObjectKind != KIND_IMAGE &&
149        anObjectKind != KIND_POLYLINE &&
150        anObjectKind != KIND_IMMERSIBLE_ZONE &&
151        anObjectKind != KIND_REGION &&
152        anObjectKind != KIND_ZONE )
153     return aResShape;
154
155   aResShape = new HYDROGUI_Shape( theContext, theObject );
156   myModule->setObjectShape( theViewerId, theObject, aResShape );
157
158   return aResShape;
159 }
160
161 void HYDROGUI_OCCDisplayer::Display( const HYDROData_SequenceOfObjects& theObjs,
162                                      const int                          theViewerId,
163                                      const bool                         theIsForced )
164 {
165   OCCViewer_Viewer* aViewer = myModule->getOCCViewer( theViewerId );
166   if( !aViewer )
167     return;
168
169   Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
170   if( aCtx.IsNull() )
171     return;
172
173   for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
174   {
175     Handle(HYDROData_Entity) anObj = theObjs.Value( i );
176     if ( anObj.IsNull() || anObj->IsRemoved() )
177       continue;
178
179     HYDROGUI_Shape* anObjShape = myModule->getObjectShape( (size_t)aViewer, anObj );
180
181     if ( !anObjShape || anObjShape->getIsToUpdate() || theIsForced )
182     {
183       if ( !anObjShape )
184         anObjShape = createShape( (size_t)aViewer, aCtx, anObj );
185
186       if ( anObjShape )
187         anObjShape->update( false );
188     }
189
190     if ( anObjShape )
191     {
192       bool anIsVisible = myModule->isObjectVisible( (size_t)aViewer, anObj );
193       anObjShape->setVisible( anIsVisible, false );
194     }
195   }
196
197   OCCViewer_ViewManager* aViewManager
198     = ::qobject_cast<OCCViewer_ViewManager*>( aViewer->getViewManager() );
199   if ( aViewManager )
200   {
201     OCCViewer_ViewWindow* aViewWindow = 
202       ::qobject_cast<OCCViewer_ViewWindow*>( aViewManager->getActiveView() );
203     if ( aViewWindow )
204     {
205       //RKV: QApplication::processEvents(); //Process the draw events for viewer
206       aViewWindow->onFitAll();
207     }
208   }
209 }
210
211 void HYDROGUI_OCCDisplayer::purgeObjects( const int theViewerId )
212 {
213   OCCViewer_Viewer* aViewer = myModule->getOCCViewer( theViewerId );
214   if( !aViewer )
215     return;
216
217   Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
218   if( aCtx.IsNull() )
219     return;
220
221   AIS_ListOfInteractive aDisplayedObjects;
222   aCtx->DisplayedObjects( aDisplayedObjects );
223
224   AIS_ListIteratorOfListOfInteractive aListIter( aDisplayedObjects );
225   for ( ; aListIter.More(); aListIter.Next() )
226   {
227     Handle(AIS_InteractiveObject) aPrsObj = aListIter.Value();
228     if ( aPrsObj.IsNull() )
229       continue;
230
231     Handle(HYDROData_Entity) anOwnerObj = 
232       Handle(HYDROData_Entity)::DownCast( aPrsObj->GetOwner() );
233     if ( !anOwnerObj.IsNull() && anOwnerObj->IsRemoved() )
234       myModule->removeObjectShape( (size_t)aViewer, anOwnerObj );
235   }
236 }
237
238