Salome HOME
IPAL21267 SIGSEGV on Show Mesh in VTK viewer in GEOM module. Regress.
[modules/geom.git] / src / GEOMGUI / GEOMGUI_Selection.cxx
1 //  Copyright (C) 2007-2008  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 // File   : GEOMGUI_Selection.cxx
23 // Author : Alexander SOLOVYOV, Open CASCADE S.A.S. (alexander.solovyov@opencascade.com)
24 //
25 #include "GEOMGUI_Selection.h"
26
27 #include "GeometryGUI.h"
28 #include "GEOM_Displayer.h"
29
30 #include <SalomeApp_Application.h>
31 #include <SalomeApp_Study.h>
32
33 #include <SUIT_Desktop.h>
34 #include <SUIT_Session.h>
35 #include <SUIT_ViewWindow.h>
36 #include <SUIT_ViewManager.h>
37
38 #include <SALOME_Prs.h>
39 #include <SALOME_InteractiveObject.hxx>
40
41 #include <SOCC_Prs.h>
42 #include <SVTK_Prs.h>
43 #include <SALOME_Actor.h>
44 #include <GEOM_Actor.h>
45
46 #include <OCCViewer_ViewModel.h>
47 #include <SVTK_ViewModel.h>
48
49 #include <GEOMImpl_Types.hxx>
50
51 #include <GEOM_AISShape.hxx>
52
53 // OCCT Includes
54 #include <AIS.hxx>
55 #include <AIS_InteractiveObject.hxx>
56 #include <AIS_ListOfInteractive.hxx>
57
58 // VTK Includes
59 #include <vtkActorCollection.h>
60
61 GEOMGUI_Selection::GEOMGUI_Selection()
62 : LightApp_Selection()
63 {
64 }
65
66 GEOMGUI_Selection::~GEOMGUI_Selection()
67 {
68 }
69
70 QVariant GEOMGUI_Selection::parameter( const QString& p ) const
71 {
72   if ( p == "isOCC" ) return QVariant( activeViewType() == OCCViewer_Viewer::Type() );
73   if ( p == "selectionmode" ){ 
74     return QVariant(selectionMode()); 
75   }
76   return LightApp_Selection::parameter( p );
77 }
78
79 QVariant GEOMGUI_Selection::parameter( const int ind, const QString& p ) const
80 {
81 //  if      ( p == "isVisible"   )    return QVariant( isVisible( ind ) );
82 // parameter isVisible is calculated in base SalomeApp_Selection
83 //  else
84   if( p == "type" )
85     return QVariant( typeName( ind ) );
86   else if ( p == "displaymode" )
87     return QVariant( displayMode( ind ) );
88   else if ( p == "isAutoColor" )
89     return QVariant( isAutoColor( ind ) );
90   else if ( p == "isVectorsMode" )
91     return QVariant( isVectorsMode( ind ) );
92   else
93     return LightApp_Selection::parameter( ind, p );
94 }
95
96 QString GEOMGUI_Selection::typeName( const int index ) const
97 {
98   if ( isComponent( index ) )
99     return "Component";
100   GEOM::GEOM_Object_var anObj = getObject( index );
101   if ( !CORBA::is_nil( anObj ) ) {
102     const int aGeomType = anObj->GetType();
103     if ( aGeomType == GEOM_GROUP )
104       return "Group";
105     else
106       return "Shape";
107   }
108   return "Unknown";
109 }
110
111 bool GEOMGUI_Selection::isVisible( const int index ) const
112 {
113   GEOM::GEOM_Object_var obj = getObject( index );
114   SALOME_View* view = GEOM_Displayer::GetActiveView();
115   if ( !CORBA::is_nil( obj ) && view ) {
116     Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( entry( index ).toLatin1().constData(), "GEOM", "TEMP_IO" );
117     return view->isVisible( io );
118   }
119   return false;
120 }
121
122 bool GEOMGUI_Selection::isAutoColor( const int index ) const
123 {
124   GEOM::GEOM_Object_var obj = getObject( index );
125   if ( !CORBA::is_nil( obj ) )
126     return obj->GetAutoColor();
127   return false;
128 }
129
130 QString GEOMGUI_Selection::displayMode( const int index ) const
131 {
132   SALOME_View* view = GEOM_Displayer::GetActiveView();
133   QString viewType = activeViewType();
134   if ( view /*fix for 9320==>*/&& ( viewType == OCCViewer_Viewer::Type() || viewType == SVTK_Viewer::Type() ) ) {
135     SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
136     if ( prs ) {
137       if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
138         SOCC_Prs* occPrs = (SOCC_Prs*) prs;
139         AIS_ListOfInteractive lst;
140         occPrs->GetObjects( lst );
141         if ( lst.Extent() ) {
142           Handle(AIS_InteractiveObject) io = lst.First();
143           if ( !io.IsNull() ) {
144             int dm = io->DisplayMode();
145             if ( dm == AIS_WireFrame )
146               return "Wireframe";
147             else if ( dm == AIS_Shaded )
148               return "Shading";
149             else { // return default display mode of AIS_InteractiveContext
150               OCCViewer_Viewer* occViewer = (OCCViewer_Viewer*) SUIT_Session::session()->activeApplication()->desktop(
151                                             )->activeWindow()->getViewManager()->getViewModel();
152               Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
153               dm = ic->DisplayMode();
154               if ( dm == AIS_WireFrame )
155                 return "Wireframe";
156               else if ( dm == AIS_Shaded )
157                 return "Shading";
158             }
159           }
160         }
161       }
162       else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
163         SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
164         vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
165         if ( lst ) {
166           lst->InitTraversal();
167           vtkActor* actor = lst->GetNextActor();
168           if ( actor ) {
169             SALOME_Actor* salActor = dynamic_cast<SALOME_Actor*>( actor );
170             if ( salActor ) {
171               int dm = salActor->getDisplayMode();
172               if ( dm == 0 )
173                 return "Wireframe";
174               else if ( dm == 1 )
175                 return "Shading";
176             } // if ( salome actor )
177           } // if ( actor )
178         } // if ( lst == vtkPrs->GetObjects() )
179       } // if VTK
180     }
181   }
182   return "";
183 }
184
185 bool GEOMGUI_Selection::isVectorsMode( const int index ) const
186 {
187   bool ret = false;
188   SALOME_View* view = GEOM_Displayer::GetActiveView();
189   QString viewType = activeViewType();
190   if ( view && ( viewType == OCCViewer_Viewer::Type() || viewType == SVTK_Viewer::Type() ) ) {
191     SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
192     if ( prs ) {
193       if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
194         SOCC_Prs* occPrs = (SOCC_Prs*) prs;
195         AIS_ListOfInteractive lst;
196         occPrs->GetObjects( lst );
197         if ( lst.Extent() ) {
198           Handle(AIS_InteractiveObject) io = lst.First();
199           if ( !io.IsNull() ) {
200             Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
201             ret = aSh->isShowVectors();
202           }
203         }
204       } else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
205         SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
206         vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
207         if ( lst ) {
208           lst->InitTraversal();
209           vtkActor* actor = lst->GetNextActor();
210           if ( actor ) {
211             GEOM_Actor* aGeomActor = GEOM_Actor::SafeDownCast(actor);
212             if ( aGeomActor )
213               ret = aGeomActor->GetVectorMode();
214           }
215         }
216       }
217     }
218   }
219   return ret;
220 }
221
222 bool GEOMGUI_Selection::isComponent( const int index ) const
223 {
224   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
225     (SUIT_Session::session()->activeApplication()->activeStudy());
226
227   if ( appStudy && index >= 0 && index < count() )  {
228     _PTR(Study) study = appStudy->studyDS();
229     QString anEntry = entry( index );
230
231     if ( study && !anEntry.isNull() ) {
232       _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
233       if ( aSO && aSO->GetFatherComponent() )
234         return aSO->GetFatherComponent()->GetIOR() == aSO->GetIOR();
235     }
236   }
237   return false;
238 }
239
240 GEOM::GEOM_Object_ptr GEOMGUI_Selection::getObject( const int index ) const
241 {
242   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
243     (SUIT_Session::session()->activeApplication()->activeStudy());
244
245   if (appStudy && index >= 0 && index < count()) {
246     _PTR(Study) study = appStudy->studyDS();
247     QString anEntry = entry(index);
248
249     if (study && !anEntry.isNull()) {
250       _PTR(SObject) aSO (study->FindObjectID(anEntry.toStdString()));
251       if (aSO) {
252         CORBA::Object_var anObj = GeometryGUI::ClientSObjectToObject(aSO);
253         return GEOM::GEOM_Object::_narrow(anObj);
254       }
255     }
256   }
257   return GEOM::GEOM_Object::_nil();
258 }
259
260 QString GEOMGUI_Selection::selectionMode() const
261 {
262   SalomeApp_Application* app = (SalomeApp_Application*)(SUIT_Session::session()->activeApplication());
263   if (app) {
264     GeometryGUI* aGeomGUI = dynamic_cast<GeometryGUI*>( app->module( "Geometry" ) );
265     if (aGeomGUI) {
266       switch (aGeomGUI->getLocalSelectionMode())
267       {
268         case GEOM_POINT      : return "VERTEX";
269         case GEOM_EDGE       : return "EDGE";
270         case GEOM_WIRE       : return "WIRE";
271         case GEOM_FACE       : return "FACE";
272         case GEOM_SHELL      : return "SHELL";
273         case GEOM_SOLID      : return "SOLID";
274         case GEOM_COMPOUND   : return "COMPOUND";
275         case GEOM_ALLOBJECTS : return "ALL";
276         default: return "";
277       }
278     }
279   }
280   return "";
281 }