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