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