]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMGUI/GEOMGUI_Selection.cxx
Salome HOME
Implementation of the "0021239: EDF 1829 OCC: Bring to front selected objects" issue.
[modules/geom.git] / src / GEOMGUI / GEOMGUI_Selection.cxx
1 // Copyright (C) 2007-2011  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 <GEOM_Constants.h>
32
33 #include <SalomeApp_Application.h>
34 #include <SalomeApp_Study.h>
35
36 #include "LightApp_DataOwner.h"
37
38 #include <SUIT_Session.h>
39 #include <SUIT_Desktop.h>
40 #include <SUIT_ViewWindow.h>
41 #include <SUIT_ViewManager.h>
42
43 #include <SALOME_Prs.h>
44 #include <SALOME_InteractiveObject.hxx>
45
46 #include <SOCC_Prs.h>
47 #include <SVTK_Prs.h>
48 #include <SALOME_Actor.h>
49 #include <GEOM_Actor.h>
50
51 #include <OCCViewer_ViewModel.h>
52 #include <SVTK_ViewModel.h>
53
54 #include <GEOMImpl_Types.hxx>
55
56 #include <GEOM_AISShape.hxx>
57
58 // OCCT Includes
59 #include <AIS.hxx>
60 #include <AIS_InteractiveObject.hxx>
61 #include <AIS_ListOfInteractive.hxx>
62
63 // VTK Includes
64 #include <vtkActorCollection.h>
65
66 #define OCC_DISPLAY_MODE_TO_STRING( str, dm ) { \
67     if ( dm == AIS_WireFrame ) \
68       str = QString( "Wireframe" ); \
69     else if ( dm == AIS_Shaded )    \
70       str = QString( "Shading" ); \
71     else if ( dm == GEOM_AISShape::ShadingWithEdges )    \
72       str = QString( "ShadingWithEdges" ); \
73     else if ( dm == GEOM_AISShape::TexturedShape )    \
74       str = QString( "Texture" ); \
75     else \
76       str = QString(); }
77
78 #define VTK_DISPLAY_MODE_TO_STRING( str, dm ) { \
79     if ( dm == 0 ) \
80       str = QString( "Wireframe" ); \
81     else if ( dm == 1 )    \
82       str = QString( "Shading" ); \
83     else if ( dm == 3 )    \
84       str = QString( "ShadingWithEdges" ); \
85     else \
86       str = QString(); }
87
88 #define USE_VISUAL_PROP_MAP
89
90 GEOMGUI_Selection::GEOMGUI_Selection()
91 : LightApp_Selection()
92 {
93 }
94
95 GEOMGUI_Selection::~GEOMGUI_Selection()
96 {
97 }
98
99 void GEOMGUI_Selection::init( const QString& context, LightApp_SelectionMgr* selMgr )
100 {
101   LightApp_Selection::init( context, selMgr );
102
103   myObjects.resize( count() );
104
105   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
106   if ( appStudy ) {
107     _PTR(Study) study = appStudy->studyDS();
108     for ( int idx = 0; idx < count(); idx++ ) {
109       QString anEntry = entry( idx );
110       if ( study && !anEntry.isEmpty() ) {
111         _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
112         if ( aSO ) {
113           CORBA::Object_var varObj = GeometryGUI::ClientSObjectToObject( aSO );
114           myObjects[idx] = GEOM::GEOM_Object::_narrow( varObj );
115         }
116       }
117     }
118   }
119 }
120
121 //QVariant GEOMGUI_Selection::contextParameter( const QString& p ) const
122 QVariant GEOMGUI_Selection::parameter( const QString& p ) const
123 {
124   QVariant v;
125   if ( p == "isOCC" )
126     v = activeViewType() == OCCViewer_Viewer::Type();
127   else if ( p == "selectionmode" )
128     v = selectionMode();
129   else if ( p == "hasImported" )
130     v = hasImported();
131   else if ( p == "allImported" )
132     v = allImported();
133   else
134     v = LightApp_Selection::parameter( p );
135   return v;
136 }
137
138 //QVariant GEOMGUI_Selection::objectParameter( const int idx, const QString& p ) const
139 QVariant GEOMGUI_Selection::parameter( const int idx, const QString& p ) const
140 {
141   QVariant v;
142   if ( p == "type" )
143     v = typeName( idx );
144   else if ( p == "typeid" )
145     v = typeId( idx );
146   else if ( p == "displaymode" )
147     v = displayMode( idx );
148   else if ( p == "isAutoColor" )
149     v = isAutoColor( idx );
150   else if ( p == "isVectorsMode" )
151     v = isVectorsMode( idx );
152   else if ( p == "topLevel" )
153     v = topLevel( idx );
154   else if ( p == "hasHiddenChildren" )
155     v = hasHiddenChildren( idx );
156   else if ( p == "hasShownChildren" )
157     v = hasShownChildren( idx );
158   else if ( p == "compoundOfVertices" )
159     v = compoundOfVertices( idx );
160   else if ( p == "imported" )
161     v = isImported( idx );
162   else
163     v = LightApp_Selection::parameter( idx, p );
164
165   return v;
166 }
167
168 // the method to skip temporary objects from selection (called from LightApp)
169 bool GEOMGUI_Selection::processOwner( const LightApp_DataOwner* theOwner )
170 {
171   return !theOwner->entry().contains("_");
172 }
173
174 QString GEOMGUI_Selection::typeName( const int index ) const
175 {
176   if ( isComponent( index ) )
177     return "Component";
178
179   static QString aGroup( "Group" );
180   static QString aShape( "Shape" );
181   static QString anUnknown( "Unknown" );
182
183   GEOM::GEOM_Object_var anObj = getObject( index );
184   if ( !CORBA::is_nil( anObj ) ) {
185     const int aGeomType = anObj->GetType();
186     if ( aGeomType == GEOM_GROUP )
187       return aGroup;
188     else
189       return aShape;
190   }
191   return anUnknown;
192 }
193
194 int GEOMGUI_Selection::typeId( const int index ) const
195 {
196   int aType = -1;
197   GEOM::GEOM_Object_var anObj = getObject( index );
198   if ( !CORBA::is_nil( anObj ) )
199     aType = (int)anObj->GetShapeType();
200   return aType;
201 }
202
203 bool GEOMGUI_Selection::isVisible( const int index ) const
204 {
205   bool res = false;
206
207 #ifdef USE_VISUAL_PROP_MAP
208   bool found = false;
209   QVariant v = visibleProperty( entry( index ), VISIBILITY_PROP );
210   if ( v.canConvert( QVariant::Bool ) ) {
211     res = v.toBool();
212     found = true;
213   }
214
215   if ( !found ) {
216 #endif
217     GEOM::GEOM_Object_var obj = getObject( index );
218     SALOME_View* view = GEOM_Displayer::GetActiveView();
219     if ( !CORBA::is_nil( obj ) && view ) {
220       Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( entry( index ).toLatin1().constData(), "GEOM", "TEMP_IO" );
221       res = view->isVisible( io );
222     }
223 #ifdef USE_VISUAL_PROP_MAP
224   }
225 #endif
226
227   return res;
228 }
229
230 bool GEOMGUI_Selection::isAutoColor( const int index ) const
231 {
232   GEOM::GEOM_Object_var obj = getObject( index );
233   if ( !CORBA::is_nil( obj ) )
234     return obj->GetAutoColor();
235   return false;
236 }
237
238 bool GEOMGUI_Selection::isImported( const int index ) const
239 {
240   GEOM::GEOM_Object_var obj = getObject( index );
241   if ( !CORBA::is_nil( obj ) )
242     return obj->GetType() == GEOM_IMPORT;
243   return false;
244 }
245
246 bool GEOMGUI_Selection::hasImported() const
247 {
248   bool res = false;
249   for ( int i = 0; i < count() && !res; i++ )
250     res = isImported( i );
251   return res;
252 }
253
254 bool GEOMGUI_Selection::allImported() const
255 {
256   bool res = true;
257   for ( int i = 0; i < count() && res; i++ )
258     res = isImported( i );
259   return res;
260 }
261
262 QVariant GEOMGUI_Selection::visibleProperty( const QString& entry, const QString& propName ) const
263 {
264   QVariant v;
265   LightApp_Study* aStudy = study();
266   if ( aStudy ) {
267     LightApp_Application* anApp = ::qobject_cast<LightApp_Application*>( aStudy->application() );
268     if ( anApp && anApp->activeViewManager() ) {
269       int id = anApp->activeViewManager()->getGlobalId();
270       v = aStudy->getObjectProperty( id, entry, propName, QVariant() );
271     }
272   }
273   return v;
274 }
275
276 QString GEOMGUI_Selection::displayMode( const int index ) const
277 {
278   QString res;
279   QString viewType = activeViewType();
280 #ifdef USE_VISUAL_PROP_MAP
281   QVariant v = visibleProperty( entry( index ), DISPLAY_MODE_PROP );
282   if ( v.canConvert( QVariant::Int ) ) {
283     int dm = v.toInt();
284     if ( viewType == OCCViewer_Viewer::Type() ) {
285       OCC_DISPLAY_MODE_TO_STRING( res, dm );
286     } else if ( viewType == SVTK_Viewer::Type() ) {
287       VTK_DISPLAY_MODE_TO_STRING( res, dm );
288     }
289   }
290
291   if ( res.isEmpty() ) {
292 #endif
293     SALOME_View* view = GEOM_Displayer::GetActiveView();
294     if ( view /*fix for 9320==>*/&& ( viewType == OCCViewer_Viewer::Type() || viewType == SVTK_Viewer::Type() ) ) {
295       SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
296       if ( prs ) {
297         if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
298           SOCC_Prs* occPrs = (SOCC_Prs*) prs;
299           AIS_ListOfInteractive lst;
300           occPrs->GetObjects( lst );
301           if ( lst.Extent() ) {
302             Handle(AIS_InteractiveObject) io = lst.First();
303             if ( !io.IsNull() ) {
304               int dm = io->DisplayMode();
305               OCC_DISPLAY_MODE_TO_STRING( res, dm );
306               if ( res.isEmpty() ) { // return default display mode of AIS_InteractiveContext
307                 OCCViewer_Viewer* occViewer = (OCCViewer_Viewer*)SUIT_Session::session()->activeApplication()->
308                   desktop()->activeWindow()->getViewManager()->getViewModel();
309                 Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
310                 dm = ic->DisplayMode();
311                 OCC_DISPLAY_MODE_TO_STRING( res, dm );
312               }
313             }
314           }
315         }
316         else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
317           SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
318           vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
319           if ( lst ) {
320             lst->InitTraversal();
321             vtkActor* actor = lst->GetNextActor();
322             if ( actor ) {
323               SALOME_Actor* salActor = dynamic_cast<SALOME_Actor*>( actor );
324               if ( salActor ) {
325                 int dm = salActor->getDisplayMode();
326                 VTK_DISPLAY_MODE_TO_STRING( res, dm );
327               } // if ( salome actor )
328             } // if ( actor )
329           } // if ( lst == vtkPrs->GetObjects() )
330         } // if VTK
331       }
332     }
333
334 #ifdef USE_VISUAL_PROP_MAP
335   }
336 #endif
337
338   return res;
339 }
340
341 bool GEOMGUI_Selection::isVectorsMode( const int index ) const
342 {
343   bool res = false;
344
345 #ifdef USE_VISUAL_PROP_MAP
346   bool found = false;
347   QVariant v = visibleProperty( entry( index ), VECTOR_MODE_PROP );
348   if ( v.canConvert( QVariant::Bool ) ) {
349     res = v.toBool();
350     found = true;
351   }
352
353   if ( !found ) {
354 #endif
355     SALOME_View* view = GEOM_Displayer::GetActiveView();
356     QString viewType = activeViewType();
357     if ( view && ( viewType == OCCViewer_Viewer::Type() || viewType == SVTK_Viewer::Type() ) ) {
358       SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
359       if ( prs ) {
360         if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
361           SOCC_Prs* occPrs = (SOCC_Prs*) prs;
362           AIS_ListOfInteractive lst;
363           occPrs->GetObjects( lst );
364           if ( lst.Extent() ) {
365             Handle(AIS_InteractiveObject) io = lst.First();
366             if ( !io.IsNull() ) {
367               Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
368               if ( !aSh.IsNull() )
369                 res = aSh->isShowVectors();
370             }
371           }
372         } else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
373           SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
374           vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
375           if ( lst ) {
376             lst->InitTraversal();
377             vtkActor* actor = lst->GetNextActor();
378             if ( actor ) {
379               GEOM_Actor* aGeomActor = GEOM_Actor::SafeDownCast(actor);
380               if ( aGeomActor )
381                 res = aGeomActor->GetVectorMode();
382             }
383           }
384         }
385       }
386     }
387 #ifdef USE_VISUAL_PROP_MAP
388   }
389 #endif
390
391   return res;
392 }
393
394 bool GEOMGUI_Selection::hasChildren( const _PTR(SObject)& obj )
395 {
396   bool ok = false;
397   if ( obj ) {
398     _PTR(ChildIterator) it ( obj->GetStudy()->NewChildIterator( obj ) );
399     for ( ; it->More() && !ok; it->Next() ) {
400       _PTR(SObject) child = it->Value();
401       if ( child ) {
402         _PTR(SObject) refObj;
403         if ( child->ReferencedObject( refObj ) ) continue; // omit references
404         if ( child->GetName() != "" ) ok = true;
405       }
406     }
407   }
408   return ok;
409 }
410
411 bool GEOMGUI_Selection::expandable( const _PTR(SObject)& obj )
412 {
413   bool exp = true;
414   _PTR(GenericAttribute) anAttr;
415   if ( obj && obj->FindAttribute( anAttr, "AttributeExpandable" ) ) {
416     _PTR(AttributeExpandable) aAttrExp = anAttr;
417     exp = aAttrExp->IsExpandable();
418   }
419   return exp;
420 }
421
422 bool GEOMGUI_Selection::isCompoundOfVertices( GEOM::GEOM_Object_ptr obj )
423 {
424   bool ret = false;
425   /*
426   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
427   (SUIT_Session::session()->activeApplication()->activeStudy());*/
428   if ( /*appStudy && */!CORBA::is_nil( obj ) )
429     ret = obj->GetShapeType() == GEOM::COMPOUND && obj->GetMaxShapeType() == GEOM::VERTEX;
430   return ret;
431 }
432
433 bool GEOMGUI_Selection::hasHiddenChildren( const int index ) const
434 {
435   bool OK = false;
436   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
437
438   if ( appStudy ) {
439     QString anEntry = entry( index );
440     _PTR(Study) study = appStudy->studyDS();
441     if ( study && !anEntry.isEmpty() ) {
442       _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
443       OK = !expandable( aSO ) && hasChildren( aSO );
444     }
445   }
446   return OK;
447 }
448
449 bool GEOMGUI_Selection::hasShownChildren( const int index ) const
450 {
451   bool OK = false;
452   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
453
454   if ( appStudy )  {
455     QString anEntry = entry( index );
456     _PTR(Study) study = appStudy->studyDS();
457     if ( study && !anEntry.isEmpty() ) {
458       _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
459       OK = expandable( aSO ) && hasChildren( aSO );
460     }
461   }
462   return OK;
463 }
464
465 bool GEOMGUI_Selection::compoundOfVertices( const int index ) const
466 {
467   GEOM::GEOM_Object_var obj = getObject( index );
468   return isCompoundOfVertices( obj );
469 }
470
471 bool GEOMGUI_Selection::isComponent( const int index ) const
472 {
473   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
474
475   if ( appStudy ) {
476     QString anEntry = entry( index );
477     _PTR(Study) study = appStudy->studyDS();
478     if ( study && !anEntry.isNull() ) {
479       _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
480       if ( aSO && aSO->GetFatherComponent() )
481         return aSO->GetFatherComponent()->GetIOR() == aSO->GetIOR();
482     }
483   }
484   return false;
485 }
486
487 GEOM::GEOM_Object_ptr GEOMGUI_Selection::getObject( const int index ) const
488 {
489   GEOM::GEOM_Object_var o;
490   if ( 0 <= index && index < myObjects.size() )
491     o = GEOM::GEOM_Object::_duplicate( myObjects[index] );
492   return o._retn();
493 }
494
495 QString GEOMGUI_Selection::selectionMode() const
496 {
497   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( study()->application() );
498   if ( app ) {
499     GeometryGUI* aGeomGUI = dynamic_cast<GeometryGUI*>( app->module( "Geometry" ) );
500     if ( aGeomGUI ) {
501       switch ( aGeomGUI->getLocalSelectionMode() )
502       {
503         case GEOM_POINT      : return "VERTEX";
504         case GEOM_EDGE       : return "EDGE";
505         case GEOM_WIRE       : return "WIRE";
506         case GEOM_FACE       : return "FACE";
507         case GEOM_SHELL      : return "SHELL";
508         case GEOM_SOLID      : return "SOLID";
509         case GEOM_COMPOUND   : return "COMPOUND";
510         case GEOM_ALLOBJECTS : return "ALL";
511         default: return "";
512       }
513     }
514   }
515   return "";
516 }
517
518 bool GEOMGUI_Selection::topLevel( const int index ) const {
519   bool res = false;
520   
521 #ifdef USE_VISUAL_PROP_MAP
522   bool found = false;
523   QVariant v = visibleProperty( entry( index ), TOP_LEVEL_PROP );
524   if ( v.canConvert<bool>() ) {
525     res = v.toBool();
526     found = true;
527   }
528
529   if ( !found ) {
530 #endif
531     SALOME_View* view = GEOM_Displayer::GetActiveView();
532     QString viewType = activeViewType();
533     if ( view && viewType == OCCViewer_Viewer::Type() ) {
534       SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
535       if ( prs ) {
536         if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
537           SOCC_Prs* occPrs = (SOCC_Prs*) prs;
538           AIS_ListOfInteractive lst;
539           occPrs->GetObjects( lst );
540           if ( lst.Extent() ) {
541             Handle(AIS_InteractiveObject) io = lst.First();
542             if ( !io.IsNull() ) {
543               Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
544               if ( !aSh.IsNull() )
545                 res = (bool)aSh->isTopLevel();
546             }
547           }
548         }
549       }
550     }
551   }
552   return res;
553 }