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