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