Salome HOME
the qstring should be converted to char* with help of toLatin1().constData()
[modules/geom.git] / src / GEOMGUI / GEOMGUI_Selection.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 // File   : GEOMGUI_Selection.cxx
21 // Author : Alexander SOLOVYOV, Open CASCADE S.A.S. (alexander.solovyov@opencascade.com)
22 //
23
24 #include "GEOMGUI_Selection.h"
25
26 #include "GeometryGUI.h"
27 #include "GEOM_Displayer.h"
28
29 #include <SalomeApp_Application.h>
30 #include <SalomeApp_Study.h>
31
32 #include <SUIT_Desktop.h>
33 #include <SUIT_Session.h>
34 #include <SUIT_ViewWindow.h>
35 #include <SUIT_ViewManager.h>
36
37 #include <SALOME_Prs.h>
38 #include <SALOME_InteractiveObject.hxx>
39
40 #include <SOCC_Prs.h>
41 #include <SVTK_Prs.h>
42 #include <SALOME_Actor.h>
43
44 #include <OCCViewer_ViewModel.h>
45 #include <SVTK_ViewModel.h>
46
47 #include <GEOMImpl_Types.hxx>
48
49 // OCCT Includes
50 #include <AIS.hxx>
51 #include <AIS_InteractiveObject.hxx>
52 #include <AIS_ListOfInteractive.hxx>
53
54 // VTK Includes
55 #include <vtkActorCollection.h>
56
57 GEOMGUI_Selection::GEOMGUI_Selection( const QString& client, LightApp_SelectionMgr* mgr )
58 : LightApp_Selection( client, mgr )
59 {
60 }
61
62 GEOMGUI_Selection::~GEOMGUI_Selection()
63 {
64 }
65
66 QVariant GEOMGUI_Selection::parameter( const QString& p ) const
67 {
68   if ( p == "isOCC" ) return QVariant( activeViewType() == OCCViewer_Viewer::Type() );
69   if ( p == "selectionmode" ){ 
70     return QVariant(selectionMode()); 
71   }
72   return LightApp_Selection::parameter( p );
73 }
74
75 QVariant GEOMGUI_Selection::parameter( const int ind, const QString& p ) const
76 {
77 //  if      ( p == "isVisible"   )    return QVariant( isVisible( ind ) );
78 // parameter isVisible is calculated in base SalomeApp_Selection
79 //  else
80   if( p == "type" )
81     return QVariant( typeName( ind ) );
82   else if ( p == "displaymode" )
83     return QVariant( displayMode( ind ) );
84   else
85     return LightApp_Selection::parameter( ind, p );
86 }
87
88 QString GEOMGUI_Selection::typeName( const int index ) const
89 {
90   if ( isComponent( index ) )
91     return "Component";
92   GEOM::GEOM_Object_var anObj = getObject( index );
93   if ( !CORBA::is_nil( anObj ) ) {
94     const int aGeomType = anObj->GetType();
95     if ( aGeomType == GEOM_GROUP )
96       return "Group";
97     else
98       return "Shape";
99   }
100   return "Unknown";
101 }
102
103 bool GEOMGUI_Selection::isVisible( const int index ) const
104 {
105   GEOM::GEOM_Object_var obj = getObject( index );
106   SALOME_View* view = GEOM_Displayer::GetActiveView();
107   if ( !CORBA::is_nil( obj ) && view ) {
108     Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( entry( index ).toLatin1().constData(), "GEOM", "TEMP_IO" );
109     return view->isVisible( io );
110   }
111   return false;
112 }
113
114 QString GEOMGUI_Selection::displayMode( const int index ) const
115 {
116   SALOME_View* view = GEOM_Displayer::GetActiveView();
117   QString viewType = activeViewType();
118   if ( view /*fix for 9320==>*/&& ( viewType == OCCViewer_Viewer::Type() || viewType == SVTK_Viewer::Type() ) ) {
119     SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
120     if ( prs ) {
121       if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
122         SOCC_Prs* occPrs = (SOCC_Prs*) prs;
123         AIS_ListOfInteractive lst;
124         occPrs->GetObjects( lst );
125         if ( lst.Extent() ) {
126           Handle(AIS_InteractiveObject) io = lst.First();
127           if ( !io.IsNull() ) {
128             int dm = io->DisplayMode();
129             if ( dm == AIS_WireFrame )
130               return "Wireframe";
131             else if ( dm == AIS_Shaded )
132               return "Shading";
133             else { // return default display mode of AIS_InteractiveContext
134               OCCViewer_Viewer* occViewer = (OCCViewer_Viewer*) SUIT_Session::session()->activeApplication()->desktop(
135                                             )->activeWindow()->getViewManager()->getViewModel();
136               Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
137               dm = ic->DisplayMode();
138               if ( dm == AIS_WireFrame )
139                 return "Wireframe";
140               else if ( dm == AIS_Shaded )
141                 return "Shading";
142             }
143           }
144         }
145       }
146       else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
147         SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
148         vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
149         if ( lst ) {
150           lst->InitTraversal();
151           vtkActor* actor = lst->GetNextActor();
152           if ( actor ) {
153             SALOME_Actor* salActor = dynamic_cast<SALOME_Actor*>( actor );
154             if ( salActor ) {
155               int dm = salActor->getDisplayMode();
156               if ( dm == 0 )
157                 return "Wireframe";
158               else if ( dm == 1 )
159                 return "Shading";
160             } // if ( salome actor )
161           } // if ( actor )
162         } // if ( lst == vtkPrs->GetObjects() )
163       } // if VTK
164     }
165   }
166   return "";
167 }
168
169 bool GEOMGUI_Selection::isComponent( const int index ) const
170 {
171   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
172     (SUIT_Session::session()->activeApplication()->activeStudy());
173
174   if ( appStudy && index >= 0 && index < count() )  {
175     _PTR(Study) study = appStudy->studyDS();
176     QString anEntry = entry( index );
177
178     if ( study && !anEntry.isNull() ) {
179       _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
180       if ( aSO && aSO->GetFatherComponent() )
181         return aSO->GetFatherComponent()->GetIOR() == aSO->GetIOR();
182     }
183   }
184   return false;
185 }
186
187 GEOM::GEOM_Object_ptr GEOMGUI_Selection::getObject( const int index ) const
188 {
189   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
190     (SUIT_Session::session()->activeApplication()->activeStudy());
191
192   if (appStudy && index >= 0 && index < count()) {
193     _PTR(Study) study = appStudy->studyDS();
194     QString anEntry = entry(index);
195
196     if (study && !anEntry.isNull()) {
197       _PTR(SObject) aSO (study->FindObjectID(anEntry.toStdString()));
198       if (aSO) {
199         CORBA::Object_var anObj = GeometryGUI::ClientSObjectToObject(aSO);
200         return GEOM::GEOM_Object::_narrow(anObj);
201       }
202     }
203   }
204   return GEOM::GEOM_Object::_nil();
205 }
206
207 QString GEOMGUI_Selection:: selectionMode() const
208
209   SalomeApp_Application* app = (SalomeApp_Application*)(SUIT_Session::session()->activeApplication());
210   if (app) {
211     GeometryGUI* aGeomGUI = dynamic_cast<GeometryGUI*>( app->module( "Geometry" ) );
212     if(aGeomGUI)
213       switch(aGeomGUI->getLocalSelectionMode())
214         {
215         case GEOM_POINT      : return "VERTEX";
216         case GEOM_EDGE       : return "EDGE";
217         case GEOM_WIRE       : return "WIRE";
218         case GEOM_FACE       : return "FACE";
219         case GEOM_SHELL      : return "SHELL";
220         case GEOM_SOLID      : return "SOLID";
221         case GEOM_COMPOUND   : return "COMPOUND";
222         case GEOM_ALLOBJECTS : return "ALL";
223         default: return "";
224         }
225   }
226   return "";
227 }