]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMGUI/GEOMGUI_Selection.cxx
Salome HOME
593d80550a2df004769b386ea02693748e394b04
[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 // File   : GEOMGUI_Selection.cxx
23 // Author : Alexander SOLOVYOV, Open CASCADE S.A.S. (alexander.solovyov@opencascade.com)
24 //
25 #include "GEOMGUI_Selection.h"
26
27 #include "GeometryGUI.h"
28 #include "GEOM_Displayer.h"
29
30 #include <SalomeApp_Application.h>
31 #include <SalomeApp_Study.h>
32
33 #include <SUIT_Desktop.h>
34 #include <SUIT_Session.h>
35 #include <SUIT_ViewWindow.h>
36 #include <SUIT_ViewManager.h>
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 #include <GEOM_Actor.h>
45
46 #include <OCCViewer_ViewModel.h>
47 #include <SVTK_ViewModel.h>
48
49 #include <GEOMImpl_Types.hxx>
50
51 #include <GEOM_AISShape.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 : LightApp_Selection()
63 {
64 }
65
66 GEOMGUI_Selection::~GEOMGUI_Selection()
67 {
68 }
69
70 QVariant GEOMGUI_Selection::parameter( const QString& p ) const
71 {
72   if ( p == "isOCC" ) return QVariant( activeViewType() == OCCViewer_Viewer::Type() );
73   if ( p == "selectionmode" ){ 
74     return QVariant(selectionMode()); 
75   }
76   return LightApp_Selection::parameter( p );
77 }
78
79 QVariant GEOMGUI_Selection::parameter( const int ind, const QString& p ) const
80 {
81 //  if      ( p == "isVisible"   )    return QVariant( isVisible( ind ) );
82 // parameter isVisible is calculated in base SalomeApp_Selection
83 //  else
84   if( p == "type" )
85     return QVariant( typeName( ind ) );
86   else if ( p == "displaymode" )
87     return QVariant( displayMode( ind ) );
88   else if ( p == "isAutoColor" )
89     return QVariant( isAutoColor( ind ) );
90   else if ( p == "isVectorsMode" )
91     return QVariant( isVectorsMode( ind ) );
92   else if ( p == "hasHiddenChildren" )
93     return QVariant( hasHiddenChildren( ind ) );
94   else if ( p == "hasShownChildren" )
95     return QVariant( hasShownChildren( ind ) );
96   else
97     return LightApp_Selection::parameter( ind, p );
98 }
99
100 QString GEOMGUI_Selection::typeName( const int index ) const
101 {
102   if ( isComponent( index ) )
103     return "Component";
104   GEOM::GEOM_Object_var anObj = getObject( index );
105   if ( !CORBA::is_nil( anObj ) ) {
106     const int aGeomType = anObj->GetType();
107     if ( aGeomType == GEOM_GROUP )
108       return "Group";
109     else
110       return "Shape";
111   }
112   return "Unknown";
113 }
114
115 bool GEOMGUI_Selection::isVisible( const int index ) const
116 {
117   GEOM::GEOM_Object_var obj = getObject( index );
118   SALOME_View* view = GEOM_Displayer::GetActiveView();
119   if ( !CORBA::is_nil( obj ) && view ) {
120     Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( entry( index ).toLatin1().constData(), "GEOM", "TEMP_IO" );
121     return view->isVisible( io );
122   }
123   return false;
124 }
125
126 bool GEOMGUI_Selection::isAutoColor( const int index ) const
127 {
128   GEOM::GEOM_Object_var obj = getObject( index );
129   if ( !CORBA::is_nil( obj ) )
130     return obj->GetAutoColor();
131   return false;
132 }
133
134 QString GEOMGUI_Selection::displayMode( const int index ) const
135 {
136   SALOME_View* view = GEOM_Displayer::GetActiveView();
137   QString viewType = activeViewType();
138   if ( view /*fix for 9320==>*/&& ( viewType == OCCViewer_Viewer::Type() || viewType == SVTK_Viewer::Type() ) ) {
139     SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
140     if ( prs ) {
141       if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
142         SOCC_Prs* occPrs = (SOCC_Prs*) prs;
143         AIS_ListOfInteractive lst;
144         occPrs->GetObjects( lst );
145         if ( lst.Extent() ) {
146           Handle(AIS_InteractiveObject) io = lst.First();
147           if ( !io.IsNull() ) {
148             int dm = io->DisplayMode();
149             if ( dm == AIS_WireFrame )
150               return "Wireframe";
151             else if ( dm == AIS_Shaded )
152               return "Shading";
153             else { // return default display mode of AIS_InteractiveContext
154               OCCViewer_Viewer* occViewer = (OCCViewer_Viewer*) SUIT_Session::session()->activeApplication()->desktop(
155                                             )->activeWindow()->getViewManager()->getViewModel();
156               Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
157               dm = ic->DisplayMode();
158               if ( dm == AIS_WireFrame )
159                 return "Wireframe";
160               else if ( dm == AIS_Shaded )
161                 return "Shading";
162             }
163           }
164         }
165       }
166       else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
167         SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
168         vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
169         if ( lst ) {
170           lst->InitTraversal();
171           vtkActor* actor = lst->GetNextActor();
172           if ( actor ) {
173             SALOME_Actor* salActor = dynamic_cast<SALOME_Actor*>( actor );
174             if ( salActor ) {
175               int dm = salActor->getDisplayMode();
176               if ( dm == 0 )
177                 return "Wireframe";
178               else if ( dm == 1 )
179                 return "Shading";
180             } // if ( salome actor )
181           } // if ( actor )
182         } // if ( lst == vtkPrs->GetObjects() )
183       } // if VTK
184     }
185   }
186   return "";
187 }
188
189 bool GEOMGUI_Selection::isVectorsMode( const int index ) const
190 {
191   bool ret = false;
192   SALOME_View* view = GEOM_Displayer::GetActiveView();
193   QString viewType = activeViewType();
194   if ( view && ( viewType == OCCViewer_Viewer::Type() || viewType == SVTK_Viewer::Type() ) ) {
195     SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
196     if ( prs ) {
197       if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
198         SOCC_Prs* occPrs = (SOCC_Prs*) prs;
199         AIS_ListOfInteractive lst;
200         occPrs->GetObjects( lst );
201         if ( lst.Extent() ) {
202           Handle(AIS_InteractiveObject) io = lst.First();
203           if ( !io.IsNull() ) {
204             Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
205             ret = aSh->isShowVectors();
206           }
207         }
208       } else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
209         SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
210         vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
211         if ( lst ) {
212           lst->InitTraversal();
213           vtkActor* actor = lst->GetNextActor();
214           if ( actor ) {
215             GEOM_Actor* aGeomActor = GEOM_Actor::SafeDownCast(actor);
216             if ( aGeomActor )
217               ret = aGeomActor->GetVectorMode();
218           }
219         }
220       }
221     }
222   }
223   return ret;
224 }
225
226 bool GEOMGUI_Selection::hasChildren( const _PTR(SObject)& obj )
227 {
228   bool ok = false;
229   if ( obj ) {
230     _PTR(ChildIterator) it ( obj->GetStudy()->NewChildIterator( obj ) );
231     for ( ; it->More() && !ok; it->Next() ) {
232       _PTR(SObject) child = it->Value();
233       if ( child ) {
234         _PTR(SObject) refObj;
235         if ( child->ReferencedObject( refObj ) ) continue; // omit references
236         if ( child->GetName() != "" ) ok = true;
237       }
238     }
239   }
240   return ok;
241 }
242
243 bool GEOMGUI_Selection::expandable( const _PTR(SObject)& obj )
244 {
245   bool exp = true;
246   _PTR(GenericAttribute) anAttr;
247   if ( obj && obj->FindAttribute( anAttr, "AttributeExpandable" ) ) {
248     _PTR(AttributeExpandable) aAttrExp = anAttr;
249     exp = aAttrExp->IsExpandable();
250   }
251   return exp;
252 }
253
254 bool GEOMGUI_Selection::hasHiddenChildren( const int index ) const
255 {
256   bool OK = false;
257   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
258     (SUIT_Session::session()->activeApplication()->activeStudy());
259
260   if ( appStudy && index >= 0 && index < count() )  {
261     _PTR(Study) study = appStudy->studyDS();
262     QString anEntry = entry( index );
263
264     if ( study && !anEntry.isEmpty() ) {
265       _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
266       OK = !expandable( aSO ) && hasChildren( aSO );
267     }
268   }
269   return OK;
270 }
271
272 bool GEOMGUI_Selection::hasShownChildren( const int index ) const
273 {
274   bool OK = false;
275   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
276     (SUIT_Session::session()->activeApplication()->activeStudy());
277
278   if ( appStudy && index >= 0 && index < count() )  {
279     _PTR(Study) study = appStudy->studyDS();
280     QString anEntry = entry( index );
281
282     if ( study && !anEntry.isEmpty() ) {
283       _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
284       OK = expandable( aSO ) && hasChildren( aSO );
285     }
286   }
287   return OK;
288 }
289
290 bool GEOMGUI_Selection::isComponent( const int index ) const
291 {
292   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
293     (SUIT_Session::session()->activeApplication()->activeStudy());
294
295   if ( appStudy && index >= 0 && index < count() )  {
296     _PTR(Study) study = appStudy->studyDS();
297     QString anEntry = entry( index );
298
299     if ( study && !anEntry.isNull() ) {
300       _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
301       if ( aSO && aSO->GetFatherComponent() )
302         return aSO->GetFatherComponent()->GetIOR() == aSO->GetIOR();
303     }
304   }
305   return false;
306 }
307
308 GEOM::GEOM_Object_ptr GEOMGUI_Selection::getObject( const int index ) const
309 {
310   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
311     (SUIT_Session::session()->activeApplication()->activeStudy());
312
313   if (appStudy && index >= 0 && index < count()) {
314     _PTR(Study) study = appStudy->studyDS();
315     QString anEntry = entry(index);
316
317     if (study && !anEntry.isNull()) {
318       _PTR(SObject) aSO (study->FindObjectID(anEntry.toStdString()));
319       if (aSO) {
320         CORBA::Object_var anObj = GeometryGUI::ClientSObjectToObject(aSO);
321         return GEOM::GEOM_Object::_narrow(anObj);
322       }
323     }
324   }
325   return GEOM::GEOM_Object::_nil();
326 }
327
328 QString GEOMGUI_Selection::selectionMode() const
329 {
330   SalomeApp_Application* app = (SalomeApp_Application*)(SUIT_Session::session()->activeApplication());
331   if (app) {
332     GeometryGUI* aGeomGUI = dynamic_cast<GeometryGUI*>( app->module( "Geometry" ) );
333     if (aGeomGUI) {
334       switch (aGeomGUI->getLocalSelectionMode())
335       {
336         case GEOM_POINT      : return "VERTEX";
337         case GEOM_EDGE       : return "EDGE";
338         case GEOM_WIRE       : return "WIRE";
339         case GEOM_FACE       : return "FACE";
340         case GEOM_SHELL      : return "SHELL";
341         case GEOM_SOLID      : return "SOLID";
342         case GEOM_COMPOUND   : return "COMPOUND";
343         case GEOM_ALLOBJECTS : return "ALL";
344         default: return "";
345       }
346     }
347   }
348   return "";
349 }