Salome HOME
Fix for Bug IPAL9161(3.0.0 (09 June): It is impossible to perform Fillet and Chamfer...
[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::param( const int ind, const QString& p ) const
41 {
42   QtxValue val( SalomeApp_Selection::param( ind, p ) );
43   if ( !val.isValid() ) {
44     if      ( p == "isVisible"   )    val = QtxValue( isVisible( ind ) );
45     else if ( p == "isOCC"       )    val = QtxValue( isOCC() );
46     else if ( p == "type"        )    val = QtxValue( typeName( ind ) );
47     else if ( p == "displaymode" )    val = QtxValue( displayMode( ind ) );
48     else if ( p == "isActiveViewer" ) val = QtxValue( isActiveViewer() );
49   }
50
51   printf( "--> param() : [%s] = %s\n", p.latin1(), val.toString ().latin1() );
52
53   return val;
54 }
55
56 QString GEOMGUI_Selection::typeName( const int index ) const
57 {
58   if ( isComponent( index ) )
59     return "Component";
60   GEOM::GEOM_Object_var anObj = getObject( index );
61   if ( !CORBA::is_nil( anObj ) ) {
62     const int aGeomType = anObj->GetType();
63     if ( aGeomType == GEOM_GROUP )
64       return "Group";
65     else
66       return "Shape";
67   }
68   return "Unknown";
69 }
70
71 bool GEOMGUI_Selection::isVisible( const int index ) const
72 {
73   GEOM::GEOM_Object_var obj = getObject( index );
74   SALOME_View* view = GEOM_Displayer::GetActiveView();
75   if ( !CORBA::is_nil( obj ) && view ) {
76     Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( entry( index ).latin1(), "GEOM", "TEMP_IO" );
77     return view->isVisible( io );
78   }
79   return false;
80 }
81
82 bool GEOMGUI_Selection::isOCC() const
83 {
84   SUIT_ViewWindow* window = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
85   return ( window && window->getViewManager()->getType() == OCCViewer_Viewer::Type() );
86 }
87
88 QString GEOMGUI_Selection::displayMode( const int index ) const
89 {
90   SALOME_View* view = GEOM_Displayer::GetActiveView();
91   if ( view ) {
92     SALOME_Prs* prs = view->CreatePrs( entry( index ) );
93     if ( prs ) {
94       if ( isOCC() ) { // assuming OCC
95         SOCC_Prs* occPrs = (SOCC_Prs*) prs;
96         AIS_ListOfInteractive lst;
97         occPrs->GetObjects( lst );
98         if ( lst.Extent() ) {
99           Handle(AIS_InteractiveObject) io = lst.First();
100           if ( !io.IsNull() ) {
101             int dm = io->DisplayMode();
102             if ( dm == AIS_WireFrame )
103               return "Wireframe";
104             else if ( dm == AIS_Shaded )
105               return "Shading";
106             else { // return default display mode of AIS_InteractiveContext
107               OCCViewer_Viewer* occViewer = (OCCViewer_Viewer*) SUIT_Session::session()->activeApplication()->desktop(
108                                             )->activeWindow()->getViewManager()->getViewModel();
109               Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
110               dm = ic->DisplayMode();
111               if ( dm == AIS_WireFrame )
112                 return "Wireframe";
113               else if ( dm == AIS_Shaded )
114                 return "Shading";
115             }
116           }
117         }
118       } 
119       else { // assuming VTK
120         SVTK_Prs* vtkPrs = (SVTK_Prs*) prs;
121         vtkActorCollection* lst = vtkPrs->GetObjects();
122         if ( lst ) {
123           lst->InitTraversal();
124           vtkActor* actor = lst->GetNextActor();
125           if ( actor ) {
126             SALOME_Actor* salActor = dynamic_cast<SALOME_Actor*>( actor );
127             if ( salActor ) {
128               int dm = salActor->getDisplayMode();
129               if ( dm == 0 )
130                 return "Wireframe";
131               else if ( dm == 1 ) 
132                 return "Shading";
133             } // if ( salome actor )
134           } // if ( actor )
135         } // if ( lst == vtkPrs->GetObjects() )
136       } // if VTK
137     }
138   }
139   return "";
140 }
141
142 bool GEOMGUI_Selection::isComponent( const int index ) const
143 {
144   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
145     (SUIT_Session::session()->activeApplication()->activeStudy());
146
147   if ( appStudy && index >= 0 && index < count() )  {
148     _PTR(Study) study = appStudy->studyDS();
149     QString anEntry = entry( index );
150
151     if ( study && !anEntry.isNull() ) { 
152       _PTR(SObject) aSO( study->FindObjectID( anEntry.latin1() ) );
153       if ( aSO && aSO->GetFatherComponent() ) 
154         return aSO->GetFatherComponent()->GetIOR() == aSO->GetIOR();
155     }
156   }
157   return false;
158 }
159
160 GEOM::GEOM_Object_ptr GEOMGUI_Selection::getObject( const int index ) const
161 {
162   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
163     (SUIT_Session::session()->activeApplication()->activeStudy());
164
165   if ( appStudy && index >= 0 && index < count() )  {
166     _PTR(Study) study = appStudy->studyDS();
167     QString anEntry = entry( index );
168
169     if ( study && !anEntry.isNull() ) { 
170       _PTR(SObject) aSO( study->FindObjectID( anEntry.latin1() ) );
171       if ( aSO ) {
172         SALOMEDS_SObject* aDSObj = dynamic_cast<SALOMEDS_SObject*>( aSO.get() );
173         return GEOM::GEOM_Object::_narrow( aDSObj->GetObject() );
174       }
175     }
176   }
177   return GEOM::GEOM_Object::_nil();
178 }
179
180 bool GEOMGUI_Selection::isActiveViewer() const
181 {
182   return ( SUIT_Session::session()->activeApplication()->desktop()->activeWindow() != 0 );
183 }