Salome HOME
Porting to Qt4.
[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 #include "GEOMGUI_Selection.h"
21
22 #include "GeometryGUI.h"
23 #include "GEOM_Displayer.h"
24
25 #include <SalomeApp_Application.h>
26 #include <SalomeApp_Study.h>
27
28 #include <SUIT_Desktop.h>
29 #include <SUIT_Session.h>
30 #include <SUIT_ViewWindow.h>
31 #include <SUIT_ViewManager.h>
32
33 #include <SALOME_Prs.h>
34 #include <SALOME_InteractiveObject.hxx>
35
36 #include <SOCC_Prs.h>
37 #include <SVTK_Prs.h>
38 #include <SALOME_Actor.h>
39
40 #include <OCCViewer_ViewModel.h>
41 #include <SVTK_ViewModel.h>
42
43 #include "GEOMImpl_Types.hxx"
44
45 // OCCT Includes
46 #include <AIS.hxx>
47 #include <AIS_InteractiveObject.hxx>
48 #include <AIS_ListOfInteractive.hxx>
49
50 // VTK Includes
51 #include <vtkActorCollection.h>
52
53 GEOMGUI_Selection::GEOMGUI_Selection( const QString& client, LightApp_SelectionMgr* mgr )
54   :LightApp_Selection( client, mgr )
55 {
56 }
57
58 GEOMGUI_Selection::~GEOMGUI_Selection()
59 {
60 }
61
62 QVariant GEOMGUI_Selection::parameter( const QString& p ) const
63 {
64   if ( p == "isOCC" ) return QVariant( activeViewType() == OCCViewer_Viewer::Type() );
65   if ( p == "selectionmode" ){ 
66     return QVariant(selectionMode()); 
67   }
68   return LightApp_Selection::parameter( p );
69 }
70
71 QVariant GEOMGUI_Selection::parameter( const int ind, const QString& p ) const
72 {
73 //  if      ( p == "isVisible"   )    return QVariant( isVisible( ind ) );
74 // parameter isVisible is calculated in base SalomeApp_Selection
75 //  else
76   if( p == "type" )
77     return QVariant( typeName( ind ) );
78   else if ( p == "displaymode" )
79     return QVariant( displayMode( ind ) );
80   else
81     return LightApp_Selection::parameter( ind, p );
82 }
83
84 QString GEOMGUI_Selection::typeName( const int index ) const
85 {
86   if ( isComponent( index ) )
87     return "Component";
88   GEOM::GEOM_Object_var anObj = getObject( index );
89   if ( !CORBA::is_nil( anObj ) ) {
90     const int aGeomType = anObj->GetType();
91     if ( aGeomType == GEOM_GROUP )
92       return "Group";
93     else
94       return "Shape";
95   }
96   return "Unknown";
97 }
98
99 bool GEOMGUI_Selection::isVisible( const int index ) const
100 {
101   GEOM::GEOM_Object_var obj = getObject( index );
102   SALOME_View* view = GEOM_Displayer::GetActiveView();
103   if ( !CORBA::is_nil( obj ) && view ) {
104     Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( entry( index ).toLatin1(), "GEOM", "TEMP_IO" );
105     return view->isVisible( io );
106   }
107   return false;
108 }
109
110 QString GEOMGUI_Selection::displayMode( const int index ) const
111 {
112   SALOME_View* view = GEOM_Displayer::GetActiveView();
113   QString viewType = activeViewType();
114   if ( view /*fix for 9320==>*/&& ( viewType == OCCViewer_Viewer::Type() || viewType == SVTK_Viewer::Type() ) ) {
115     SALOME_Prs* prs = view->CreatePrs( entry( index ).toStdString().c_str() );
116     if ( prs ) {
117       if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
118         SOCC_Prs* occPrs = (SOCC_Prs*) prs;
119         AIS_ListOfInteractive lst;
120         occPrs->GetObjects( lst );
121         if ( lst.Extent() ) {
122           Handle(AIS_InteractiveObject) io = lst.First();
123           if ( !io.IsNull() ) {
124             int dm = io->DisplayMode();
125             if ( dm == AIS_WireFrame )
126               return "Wireframe";
127             else if ( dm == AIS_Shaded )
128               return "Shading";
129             else { // return default display mode of AIS_InteractiveContext
130               OCCViewer_Viewer* occViewer = (OCCViewer_Viewer*) SUIT_Session::session()->activeApplication()->desktop(
131                                             )->activeWindow()->getViewManager()->getViewModel();
132               Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
133               dm = ic->DisplayMode();
134               if ( dm == AIS_WireFrame )
135                 return "Wireframe";
136               else if ( dm == AIS_Shaded )
137                 return "Shading";
138             }
139           }
140         }
141       }
142       else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
143         SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
144         vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
145         if ( lst ) {
146           lst->InitTraversal();
147           vtkActor* actor = lst->GetNextActor();
148           if ( actor ) {
149             SALOME_Actor* salActor = dynamic_cast<SALOME_Actor*>( actor );
150             if ( salActor ) {
151               int dm = salActor->getDisplayMode();
152               if ( dm == 0 )
153                 return "Wireframe";
154               else if ( dm == 1 )
155                 return "Shading";
156             } // if ( salome actor )
157           } // if ( actor )
158         } // if ( lst == vtkPrs->GetObjects() )
159       } // if VTK
160     }
161   }
162   return "";
163 }
164
165 bool GEOMGUI_Selection::isComponent( const int index ) const
166 {
167   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
168     (SUIT_Session::session()->activeApplication()->activeStudy());
169
170   if ( appStudy && index >= 0 && index < count() )  {
171     _PTR(Study) study = appStudy->studyDS();
172     QString anEntry = entry( index );
173
174     if ( study && !anEntry.isNull() ) {
175       _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
176       if ( aSO && aSO->GetFatherComponent() )
177         return aSO->GetFatherComponent()->GetIOR() == aSO->GetIOR();
178     }
179   }
180   return false;
181 }
182
183 GEOM::GEOM_Object_ptr GEOMGUI_Selection::getObject( const int index ) const
184 {
185   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
186     (SUIT_Session::session()->activeApplication()->activeStudy());
187
188   if (appStudy && index >= 0 && index < count()) {
189     _PTR(Study) study = appStudy->studyDS();
190     QString anEntry = entry(index);
191
192     if (study && !anEntry.isNull()) {
193       _PTR(SObject) aSO (study->FindObjectID(anEntry.toStdString()));
194       if (aSO) {
195         CORBA::Object_var anObj = GeometryGUI::ClientSObjectToObject(aSO);
196         return GEOM::GEOM_Object::_narrow(anObj);
197       }
198     }
199   }
200   return GEOM::GEOM_Object::_nil();
201 }
202
203 QString GEOMGUI_Selection:: selectionMode() const
204
205   SalomeApp_Application* app = (SalomeApp_Application*)(SUIT_Session::session()->activeApplication());
206   if (app) {
207     GeometryGUI* aGeomGUI = dynamic_cast<GeometryGUI*>( app->module( "Geometry" ) );
208     if(aGeomGUI)
209       switch(aGeomGUI->getLocalSelectionMode())
210         {
211         case GEOM_POINT      : return "VERTEX";
212         case GEOM_EDGE       : return "EDGE";
213         case GEOM_WIRE       : return "WIRE";
214         case GEOM_FACE       : return "FACE";
215         case GEOM_SHELL      : return "SHELL";
216         case GEOM_SOLID      : return "SOLID";
217         case GEOM_COMPOUND   : return "COMPOUND";
218         case GEOM_ALLOBJECTS : return "ALL";
219         default: return "";
220         }
221   }
222   return "";
223 }