Salome HOME
51dfa2f38e6a04d08a3132b10cae89a69c08e8d2
[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 HYDROGUI_OCCDisplayer::HYDROGUI_OCCDisplayer( HYDROGUI_Module* theModule )
39 : HYDROGUI_AbstractDisplayer( theModule )
40 {
41 }
42
43 HYDROGUI_OCCDisplayer::~HYDROGUI_OCCDisplayer()
44 {
45 }
46
47 void HYDROGUI_OCCDisplayer::SetToUpdate( const HYDROData_SequenceOfObjects& theObjs,
48                                          const int                          theViewerId )
49 {
50   OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
51   if( !aViewer )
52     return;
53
54   for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
55   {
56     Handle(HYDROData_Entity) anObj = theObjs.Value( i );
57     if( anObj.IsNull() )
58       continue;
59
60     HYDROGUI_Shape* anObjShape = module()->getObjectShape( (size_t)aViewer, anObj );
61     if ( !anObjShape )
62       continue;
63     
64     anObjShape->setIsToUpdate( true );
65   }
66 }
67
68 void HYDROGUI_OCCDisplayer::EraseAll( const int theViewerId )
69 {
70   OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
71   if( !aViewer )
72     return;
73
74   module()->removeViewShapes( (size_t)aViewer );
75 }
76
77 void HYDROGUI_OCCDisplayer::Erase( const HYDROData_SequenceOfObjects& theObjs,
78                                    const int                          theViewerId )
79 {
80   OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
81   if( !aViewer )
82     return;
83
84   for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
85   {
86     Handle(HYDROData_Entity) anObj = theObjs.Value( i );
87     if( anObj.IsNull() )
88       continue;
89
90     module()->removeObjectShape( (size_t)aViewer, anObj );
91   }
92 }
93
94 HYDROGUI_Shape* HYDROGUI_OCCDisplayer::createShape( const int                             theViewerId,
95                                                     const Handle(AIS_InteractiveContext)& theContext,
96                                                     const Handle(HYDROData_Entity)&       theObject )
97 {
98   HYDROGUI_Shape* aResShape = NULL;
99   if ( theContext.IsNull() || theObject.IsNull() )
100     return aResShape;
101
102   ObjectKind anObjectKind = theObject->GetKind();
103   if ( anObjectKind != KIND_IMAGE &&
104        anObjectKind != KIND_POLYLINEXY &&
105        anObjectKind != KIND_IMMERSIBLE_ZONE &&
106        anObjectKind != KIND_REGION &&
107        anObjectKind != KIND_ZONE &&
108        anObjectKind != KIND_OBSTACLE &&
109        anObjectKind != KIND_PROFILE )
110     return aResShape;
111
112   aResShape = new HYDROGUI_Shape( theContext, theObject );
113   module()->setObjectShape( theViewerId, theObject, aResShape );
114
115   return aResShape;
116 }
117
118 void HYDROGUI_OCCDisplayer::Display( const HYDROData_SequenceOfObjects& theObjs,
119                                      const int                          theViewerId,
120                                      const bool                         theIsForced,
121                                      const bool theDoFitAll )
122 {
123   OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
124   if( !aViewer )
125     return;
126
127   Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
128   if( aCtx.IsNull() )
129     return;
130
131   for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
132   {
133     Handle(HYDROData_Entity) anObj = theObjs.Value( i );
134     if ( anObj.IsNull() || anObj->IsRemoved() )
135       continue;
136
137     HYDROGUI_Shape* anObjShape = module()->getObjectShape( (size_t)aViewer, anObj );
138
139     if ( !anObjShape || anObjShape->getIsToUpdate() || theIsForced )
140     {
141       if ( !anObjShape )
142         anObjShape = createShape( (size_t)aViewer, aCtx, anObj );
143
144       if ( anObjShape )
145         anObjShape->update( false );
146     }
147
148     if ( anObjShape )
149     {
150       bool anIsVisible = module()->isObjectVisible( (size_t)aViewer, anObj );
151       anObjShape->setVisible( anIsVisible, false );
152     }
153   }
154
155   if ( theDoFitAll )
156   {
157     OCCViewer_ViewManager* aViewManager
158       = ::qobject_cast<OCCViewer_ViewManager*>( aViewer->getViewManager() );
159     if ( aViewManager )
160     {
161       OCCViewer_ViewWindow* aViewWindow = 
162         ::qobject_cast<OCCViewer_ViewWindow*>( aViewManager->getActiveView() );
163       if ( aViewWindow )
164       {
165         aViewWindow->onFitAll();
166       }
167     }
168   }
169 }
170
171 void HYDROGUI_OCCDisplayer::purgeObjects( const int theViewerId )
172 {
173   OCCViewer_Viewer* aViewer = module()->getOCCViewer( theViewerId );
174   if( !aViewer )
175     return;
176
177   Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
178   if( aCtx.IsNull() )
179     return;
180
181   AIS_ListOfInteractive aDisplayedObjects;
182   aCtx->DisplayedObjects( aDisplayedObjects );
183
184   AIS_ListIteratorOfListOfInteractive aListIter( aDisplayedObjects );
185   for ( ; aListIter.More(); aListIter.Next() )
186   {
187     Handle(AIS_InteractiveObject) aPrsObj = aListIter.Value();
188     if ( aPrsObj.IsNull() )
189       continue;
190
191     Handle(HYDROData_Entity) anOwnerObj = 
192       Handle(HYDROData_Entity)::DownCast( aPrsObj->GetOwner() );
193     if ( !anOwnerObj.IsNull() && anOwnerObj->IsRemoved() )
194       module()->removeObjectShape( (size_t)aViewer, anOwnerObj );
195   }
196 }
197
198 QString HYDROGUI_OCCDisplayer::GetType() const
199 {
200   return OCCViewer_Viewer::Type();
201 }