Salome HOME
PAL9391
[modules/geom.git] / src / GEOMGUI / GEOMGUI_Selection.cxx
1 #include "GEOMGUI_Selection.h"
2
3 #include "GeometryGUI.h"
4 #include "GEOM_Displayer.h"
5
6 #include <SalomeApp_DataOwner.h>
7 #include <SalomeApp_Study.h>
8
9 #include <OCCViewer_ViewModel.h>
10
11 #include <SUIT_Session.h>
12 #include <SUIT_ViewWindow.h>
13 #include <SUIT_ViewManager.h>
14 #include <SUIT_ViewModel.h>
15
16 #include <SALOMEDSClient_SObject.hxx>
17 #include <SALOMEDSClient_Study.hxx>
18
19 #include <SALOME_Prs.h>
20 #include <SALOME_InteractiveObject.hxx>
21
22 #include <SOCC_Prs.h>
23 #include <SVTK_Prs.h>
24 #include <SALOME_Actor.h>
25
26 #include <OCCViewer_ViewModel.h>
27 #include <VTKViewer_ViewModel.h>
28
29 #include "GEOMImpl_Types.hxx"
30
31 // OCCT Includes
32 #include <AIS.hxx>
33 #include <AIS_InteractiveObject.hxx>
34 #include <AIS_ListOfInteractive.hxx>
35
36 // VTK Includes
37 #include <vtkActorCollection.h>
38
39 GEOMGUI_Selection::GEOMGUI_Selection()
40 {
41 }
42
43 GEOMGUI_Selection::~GEOMGUI_Selection()
44 {
45 }
46
47 QtxValue GEOMGUI_Selection::globalParam( const QString& p ) const
48 {
49   if ( p == "isOCC" ) return QtxValue( activeViewType() == OCCViewer_Viewer::Type() );
50
51   return SalomeApp_Selection::globalParam( p );
52 }
53
54 QtxValue GEOMGUI_Selection::param( const int ind, const QString& p ) const
55 {
56 //  if      ( p == "isVisible"   )    return QtxValue( isVisible( ind ) );
57 // parameter isVisible is calculated in base SalomeApp_Selection
58 //  else
59   if( p == "type" )
60     return QtxValue( typeName( ind ) );
61   else if ( p == "displaymode" )
62     return QtxValue( displayMode( ind ) );
63   else
64     return SalomeApp_Selection::param( ind, p );
65 }
66
67 QString GEOMGUI_Selection::typeName( const int index ) const
68 {
69   if ( isComponent( index ) )
70     return "Component";
71   GEOM::GEOM_Object_var anObj = getObject( index );
72   if ( !CORBA::is_nil( anObj ) ) {
73     const int aGeomType = anObj->GetType();
74     if ( aGeomType == GEOM_GROUP )
75       return "Group";
76     else
77       return "Shape";
78   }
79   return "Unknown";
80 }
81
82 bool GEOMGUI_Selection::isVisible( const int index ) const
83 {
84   GEOM::GEOM_Object_var obj = getObject( index );
85   SALOME_View* view = GEOM_Displayer::GetActiveView();
86   if ( !CORBA::is_nil( obj ) && view ) {
87     Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( entry( index ).latin1(), "GEOM", "TEMP_IO" );
88     return view->isVisible( io );
89   }
90   return false;
91 }
92
93 QString GEOMGUI_Selection::displayMode( const int index ) const
94 {
95   SALOME_View* view = GEOM_Displayer::GetActiveView();
96   QString viewType = activeViewType();
97   if ( view /*fix for 9320==>*/&& ( viewType == OCCViewer_Viewer::Type() || viewType == VTKViewer_Viewer::Type() ) ) {
98     SALOME_Prs* prs = view->CreatePrs( entry( index ) );
99     if ( prs ) {
100       if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
101         SOCC_Prs* occPrs = (SOCC_Prs*) prs;
102         AIS_ListOfInteractive lst;
103         occPrs->GetObjects( lst );
104         if ( lst.Extent() ) {
105           Handle(AIS_InteractiveObject) io = lst.First();
106           if ( !io.IsNull() ) {
107             int dm = io->DisplayMode();
108             if ( dm == AIS_WireFrame )
109               return "Wireframe";
110             else if ( dm == AIS_Shaded )
111               return "Shading";
112             else { // return default display mode of AIS_InteractiveContext
113               OCCViewer_Viewer* occViewer = (OCCViewer_Viewer*) SUIT_Session::session()->activeApplication()->desktop(
114                                             )->activeWindow()->getViewManager()->getViewModel();
115               Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
116               dm = ic->DisplayMode();
117               if ( dm == AIS_WireFrame )
118                 return "Wireframe";
119               else if ( dm == AIS_Shaded )
120                 return "Shading";
121             }
122           }
123         }
124       }
125       else if ( viewType == VTKViewer_Viewer::Type() ) { // assuming VTK
126         SVTK_Prs* vtkPrs = (SVTK_Prs*) prs;
127         vtkActorCollection* lst = vtkPrs->GetObjects();
128         if ( lst ) {
129           lst->InitTraversal();
130           vtkActor* actor = lst->GetNextActor();
131           if ( actor ) {
132             SALOME_Actor* salActor = dynamic_cast<SALOME_Actor*>( actor );
133             if ( salActor ) {
134               int dm = salActor->getDisplayMode();
135               if ( dm == 0 )
136                 return "Wireframe";
137               else if ( dm == 1 )
138                 return "Shading";
139             } // if ( salome actor )
140           } // if ( actor )
141         } // if ( lst == vtkPrs->GetObjects() )
142       } // if VTK
143     }
144   }
145   return "";
146 }
147
148 bool GEOMGUI_Selection::isComponent( const int index ) const
149 {
150   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
151     (SUIT_Session::session()->activeApplication()->activeStudy());
152
153   if ( appStudy && index >= 0 && index < count() )  {
154     _PTR(Study) study = appStudy->studyDS();
155     QString anEntry = entry( index );
156
157     if ( study && !anEntry.isNull() ) {
158       _PTR(SObject) aSO( study->FindObjectID( anEntry.latin1() ) );
159       if ( aSO && aSO->GetFatherComponent() )
160         return aSO->GetFatherComponent()->GetIOR() == aSO->GetIOR();
161     }
162   }
163   return false;
164 }
165
166 GEOM::GEOM_Object_ptr GEOMGUI_Selection::getObject( const int index ) const
167 {
168   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
169     (SUIT_Session::session()->activeApplication()->activeStudy());
170
171   if (appStudy && index >= 0 && index < count()) {
172     _PTR(Study) study = appStudy->studyDS();
173     QString anEntry = entry(index);
174
175     if (study && !anEntry.isNull()) {
176       _PTR(SObject) aSO (study->FindObjectID(anEntry.latin1()));
177       if (aSO) {
178         CORBA::Object_var anObj = GeometryGUI::ClientSObjectToObject(aSO);
179         return GEOM::GEOM_Object::_narrow(anObj);
180       }
181     }
182   }
183   return GEOM::GEOM_Object::_nil();
184 }