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