Salome HOME
Copyrights update
[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/
19 //
20 #include "GEOMGUI_Selection.h"
21
22 #include "GeometryGUI.h"
23 #include "GEOM_Displayer.h"
24
25 #include <LightApp_DataOwner.h>
26 #include <SalomeApp_Study.h>
27
28 #include <OCCViewer_ViewModel.h>
29
30 #include <SUIT_Session.h>
31 #include <SUIT_ViewWindow.h>
32 #include <SUIT_ViewManager.h>
33 #include <SUIT_ViewModel.h>
34
35 #include <SALOMEDSClient_SObject.hxx>
36 #include <SALOMEDSClient_Study.hxx>
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 {
60 }
61
62 GEOMGUI_Selection::~GEOMGUI_Selection()
63 {
64 }
65
66 QtxValue GEOMGUI_Selection::globalParam( const QString& p ) const
67 {
68   if ( p == "isOCC" ) return QtxValue( activeViewType() == OCCViewer_Viewer::Type() );
69  
70   return LightApp_Selection::globalParam( p );
71 }
72
73 QtxValue GEOMGUI_Selection::param( const int ind, const QString& p ) const
74 {
75 //  if      ( p == "isVisible"   )    return QtxValue( isVisible( ind ) );
76 // parameter isVisible is calculated in base SalomeApp_Selection
77 //  else
78   if( p == "type" )
79     return QtxValue( typeName( ind ) );
80   else if ( p == "displaymode" )
81     return QtxValue( displayMode( ind ) );
82   else
83     return LightApp_Selection::param( ind, p );
84 }
85
86 QString GEOMGUI_Selection::typeName( const int index ) const
87 {
88   if ( isComponent( index ) )
89     return "Component";
90   GEOM::GEOM_Object_var anObj = getObject( index );
91   if ( !CORBA::is_nil( anObj ) ) {
92     const int aGeomType = anObj->GetType();
93     if ( aGeomType == GEOM_GROUP )
94       return "Group";
95     else
96       return "Shape";
97   }
98   return "Unknown";
99 }
100
101 bool GEOMGUI_Selection::isVisible( const int index ) const
102 {
103   GEOM::GEOM_Object_var obj = getObject( index );
104   SALOME_View* view = GEOM_Displayer::GetActiveView();
105   if ( !CORBA::is_nil( obj ) && view ) {
106     Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( entry( index ).latin1(), "GEOM", "TEMP_IO" );
107     return view->isVisible( io );
108   }
109   return false;
110 }
111
112 QString GEOMGUI_Selection::displayMode( const int index ) const
113 {
114   SALOME_View* view = GEOM_Displayer::GetActiveView();
115   QString viewType = activeViewType();
116   if ( view /*fix for 9320==>*/&& ( viewType == OCCViewer_Viewer::Type() || viewType == SVTK_Viewer::Type() ) ) {
117     SALOME_Prs* prs = view->CreatePrs( entry( index ) );
118     if ( prs ) {
119       if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
120         SOCC_Prs* occPrs = (SOCC_Prs*) prs;
121         AIS_ListOfInteractive lst;
122         occPrs->GetObjects( lst );
123         if ( lst.Extent() ) {
124           Handle(AIS_InteractiveObject) io = lst.First();
125           if ( !io.IsNull() ) {
126             int dm = io->DisplayMode();
127             if ( dm == AIS_WireFrame )
128               return "Wireframe";
129             else if ( dm == AIS_Shaded )
130               return "Shading";
131             else { // return default display mode of AIS_InteractiveContext
132               OCCViewer_Viewer* occViewer = (OCCViewer_Viewer*) SUIT_Session::session()->activeApplication()->desktop(
133                                             )->activeWindow()->getViewManager()->getViewModel();
134               Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
135               dm = ic->DisplayMode();
136               if ( dm == AIS_WireFrame )
137                 return "Wireframe";
138               else if ( dm == AIS_Shaded )
139                 return "Shading";
140             }
141           }
142         }
143       }
144       else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
145         SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
146         vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
147         if ( lst ) {
148           lst->InitTraversal();
149           vtkActor* actor = lst->GetNextActor();
150           if ( actor ) {
151             SALOME_Actor* salActor = dynamic_cast<SALOME_Actor*>( actor );
152             if ( salActor ) {
153               int dm = salActor->getDisplayMode();
154               if ( dm == 0 )
155                 return "Wireframe";
156               else if ( dm == 1 )
157                 return "Shading";
158             } // if ( salome actor )
159           } // if ( actor )
160         } // if ( lst == vtkPrs->GetObjects() )
161       } // if VTK
162     }
163   }
164   return "";
165 }
166
167 bool GEOMGUI_Selection::isComponent( const int index ) const
168 {
169   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
170     (SUIT_Session::session()->activeApplication()->activeStudy());
171
172   if ( appStudy && index >= 0 && index < count() )  {
173     _PTR(Study) study = appStudy->studyDS();
174     QString anEntry = entry( index );
175
176     if ( study && !anEntry.isNull() ) {
177       _PTR(SObject) aSO( study->FindObjectID( anEntry.latin1() ) );
178       if ( aSO && aSO->GetFatherComponent() )
179         return aSO->GetFatherComponent()->GetIOR() == aSO->GetIOR();
180     }
181   }
182   return false;
183 }
184
185 GEOM::GEOM_Object_ptr GEOMGUI_Selection::getObject( const int index ) const
186 {
187   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
188     (SUIT_Session::session()->activeApplication()->activeStudy());
189
190   if (appStudy && index >= 0 && index < count()) {
191     _PTR(Study) study = appStudy->studyDS();
192     QString anEntry = entry(index);
193
194     if (study && !anEntry.isNull()) {
195       _PTR(SObject) aSO (study->FindObjectID(anEntry.latin1()));
196       if (aSO) {
197         CORBA::Object_var anObj = GeometryGUI::ClientSObjectToObject(aSO);
198         return GEOM::GEOM_Object::_narrow(anObj);
199       }
200     }
201   }
202   return GEOM::GEOM_Object::_nil();
203 }