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