Salome HOME
Fix for PAL21179:
[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 == "hasHiddenChildren" )
153     v = hasHiddenChildren( idx );
154   else if ( p == "hasShownChildren" )
155     v = hasShownChildren( idx );
156   else if ( p == "compoundOfVertices" )
157     v = compoundOfVertices( idx );
158   else if ( p == "imported" )
159     v = isImported( idx );
160   else
161     v = LightApp_Selection::parameter( idx, p );
162
163   return v;
164 }
165
166 // the method to skip temporary objects from selection (called from LightApp)
167 bool GEOMGUI_Selection::processOwner( const LightApp_DataOwner* theOwner )
168 {
169   return !theOwner->entry().contains("_");
170 }
171
172 QString GEOMGUI_Selection::typeName( const int index ) const
173 {
174   if ( isComponent( index ) )
175     return "Component";
176
177   static QString aGroup( "Group" );
178   static QString aShape( "Shape" );
179   static QString anUnknown( "Unknown" );
180
181   GEOM::GEOM_Object_var anObj = getObject( index );
182   if ( !CORBA::is_nil( anObj ) ) {
183     const int aGeomType = anObj->GetType();
184     if ( aGeomType == GEOM_GROUP )
185       return aGroup;
186     else
187       return aShape;
188   }
189   return anUnknown;
190 }
191
192 int GEOMGUI_Selection::typeId( const int index ) const
193 {
194   int aType = -1;
195   GEOM::GEOM_Object_var anObj = getObject( index );
196   if ( !CORBA::is_nil( anObj ) )
197     aType = (int)anObj->GetShapeType();
198   return aType;
199 }
200
201 bool GEOMGUI_Selection::isVisible( const int index ) const
202 {
203   bool res = false;
204
205 #ifdef USE_VISUAL_PROP_MAP
206   bool found = false;
207   QVariant v = visibleProperty( entry( index ), VISIBILITY_PROP );
208   if ( v.canConvert( QVariant::Bool ) ) {
209     res = v.toBool();
210     found = true;
211   }
212
213   if ( !found ) {
214 #endif
215     GEOM::GEOM_Object_var obj = getObject( index );
216     SALOME_View* view = GEOM_Displayer::GetActiveView();
217     if ( !CORBA::is_nil( obj ) && view ) {
218       Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( entry( index ).toLatin1().constData(), "GEOM", "TEMP_IO" );
219       res = view->isVisible( io );
220     }
221 #ifdef USE_VISUAL_PROP_MAP
222   }
223 #endif
224
225   return res;
226 }
227
228 bool GEOMGUI_Selection::isAutoColor( const int index ) const
229 {
230   GEOM::GEOM_Object_var obj = getObject( index );
231   if ( !CORBA::is_nil( obj ) )
232     return obj->GetAutoColor();
233   return false;
234 }
235
236 bool GEOMGUI_Selection::isImported( const int index ) const
237 {
238   GEOM::GEOM_Object_var obj = getObject( index );
239   if ( !CORBA::is_nil( obj ) )
240     return obj->GetType() == GEOM_IMPORT;
241   return false;
242 }
243
244 bool GEOMGUI_Selection::hasImported() const
245 {
246   bool res = false;
247   for ( int i = 0; i < count() && !res; i++ )
248     res = isImported( i );
249   return res;
250 }
251
252 bool GEOMGUI_Selection::allImported() const
253 {
254   bool res = true;
255   for ( int i = 0; i < count() && res; i++ )
256     res = isImported( i );
257   return res;
258 }
259
260 QVariant GEOMGUI_Selection::visibleProperty( const QString& entry, const QString& propName ) const
261 {
262   QVariant v;
263   LightApp_Study* aStudy = study();
264   if ( aStudy ) {
265     LightApp_Application* anApp = ::qobject_cast<LightApp_Application*>( aStudy->application() );
266     if ( anApp && anApp->activeViewManager() ) {
267       int id = anApp->activeViewManager()->getGlobalId();
268       v = aStudy->getObjectProperty( id, entry, propName, QVariant() );
269     }
270   }
271   return v;
272 }
273
274 QString GEOMGUI_Selection::displayMode( const int index ) const
275 {
276   QString res;
277   QString viewType = activeViewType();
278 #ifdef USE_VISUAL_PROP_MAP
279   QVariant v = visibleProperty( entry( index ), DISPLAY_MODE_PROP );
280   if ( v.canConvert( QVariant::Int ) ) {
281     int dm = v.toInt();
282     if ( viewType == OCCViewer_Viewer::Type() ) {
283       OCC_DISPLAY_MODE_TO_STRING( res, dm );
284     } else if ( viewType == SVTK_Viewer::Type() ) {
285       VTK_DISPLAY_MODE_TO_STRING( res, dm );
286     }
287   }
288
289   if ( res.isEmpty() ) {
290 #endif
291     SALOME_View* view = GEOM_Displayer::GetActiveView();
292     if ( view /*fix for 9320==>*/&& ( viewType == OCCViewer_Viewer::Type() || viewType == SVTK_Viewer::Type() ) ) {
293       SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
294       if ( prs ) {
295         if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
296           SOCC_Prs* occPrs = (SOCC_Prs*) prs;
297           AIS_ListOfInteractive lst;
298           occPrs->GetObjects( lst );
299           if ( lst.Extent() ) {
300             Handle(AIS_InteractiveObject) io = lst.First();
301             if ( !io.IsNull() ) {
302               int dm = io->DisplayMode();
303               OCC_DISPLAY_MODE_TO_STRING( res, dm );
304               if ( res.isEmpty() ) { // return default display mode of AIS_InteractiveContext
305                 OCCViewer_Viewer* occViewer = (OCCViewer_Viewer*)SUIT_Session::session()->activeApplication()->
306                   desktop()->activeWindow()->getViewManager()->getViewModel();
307                 Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
308                 dm = ic->DisplayMode();
309                 OCC_DISPLAY_MODE_TO_STRING( res, dm );
310               }
311             }
312           }
313         }
314         else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
315           SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
316           vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
317           if ( lst ) {
318             lst->InitTraversal();
319             vtkActor* actor = lst->GetNextActor();
320             if ( actor ) {
321               SALOME_Actor* salActor = dynamic_cast<SALOME_Actor*>( actor );
322               if ( salActor ) {
323                 int dm = salActor->getDisplayMode();
324                 VTK_DISPLAY_MODE_TO_STRING( res, dm );
325               } // if ( salome actor )
326             } // if ( actor )
327           } // if ( lst == vtkPrs->GetObjects() )
328         } // if VTK
329       }
330     }
331
332 #ifdef USE_VISUAL_PROP_MAP
333   }
334 #endif
335
336   return res;
337 }
338
339 bool GEOMGUI_Selection::isVectorsMode( const int index ) const
340 {
341   bool res = false;
342
343 #ifdef USE_VISUAL_PROP_MAP
344   bool found = false;
345   QVariant v = visibleProperty( entry( index ), VECTOR_MODE_PROP );
346   if ( v.canConvert( QVariant::Bool ) ) {
347     res = v.toBool();
348     found = true;
349   }
350
351   if ( !found ) {
352 #endif
353     SALOME_View* view = GEOM_Displayer::GetActiveView();
354     QString viewType = activeViewType();
355     if ( view && ( viewType == OCCViewer_Viewer::Type() || viewType == SVTK_Viewer::Type() ) ) {
356       SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
357       if ( prs ) {
358         if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
359           SOCC_Prs* occPrs = (SOCC_Prs*) prs;
360           AIS_ListOfInteractive lst;
361           occPrs->GetObjects( lst );
362           if ( lst.Extent() ) {
363             Handle(AIS_InteractiveObject) io = lst.First();
364             if ( !io.IsNull() ) {
365               Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
366               if ( !aSh.IsNull() )
367                 res = aSh->isShowVectors();
368             }
369           }
370         } else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
371           SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
372           vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
373           if ( lst ) {
374             lst->InitTraversal();
375             vtkActor* actor = lst->GetNextActor();
376             if ( actor ) {
377               GEOM_Actor* aGeomActor = GEOM_Actor::SafeDownCast(actor);
378               if ( aGeomActor )
379                 res = aGeomActor->GetVectorMode();
380             }
381           }
382         }
383       }
384     }
385 #ifdef USE_VISUAL_PROP_MAP
386   }
387 #endif
388
389   return res;
390 }
391
392 bool GEOMGUI_Selection::hasChildren( const _PTR(SObject)& obj )
393 {
394   bool ok = false;
395   if ( obj ) {
396     _PTR(ChildIterator) it ( obj->GetStudy()->NewChildIterator( obj ) );
397     for ( ; it->More() && !ok; it->Next() ) {
398       _PTR(SObject) child = it->Value();
399       if ( child ) {
400         _PTR(SObject) refObj;
401         if ( child->ReferencedObject( refObj ) ) continue; // omit references
402         if ( child->GetName() != "" ) ok = true;
403       }
404     }
405   }
406   return ok;
407 }
408
409 bool GEOMGUI_Selection::expandable( const _PTR(SObject)& obj )
410 {
411   bool exp = true;
412   _PTR(GenericAttribute) anAttr;
413   if ( obj && obj->FindAttribute( anAttr, "AttributeExpandable" ) ) {
414     _PTR(AttributeExpandable) aAttrExp = anAttr;
415     exp = aAttrExp->IsExpandable();
416   }
417   return exp;
418 }
419
420 bool GEOMGUI_Selection::isCompoundOfVertices( GEOM::GEOM_Object_ptr obj )
421 {
422   bool ret = false;
423   /*
424   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
425   (SUIT_Session::session()->activeApplication()->activeStudy());*/
426   if ( /*appStudy && */!CORBA::is_nil( obj ) )
427     ret = obj->GetShapeType() == GEOM::COMPOUND && obj->GetMaxShapeType() == GEOM::VERTEX;
428   return ret;
429 }
430
431 bool GEOMGUI_Selection::hasHiddenChildren( const int index ) const
432 {
433   bool OK = false;
434   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
435
436   if ( appStudy ) {
437     QString anEntry = entry( index );
438     _PTR(Study) study = appStudy->studyDS();
439     if ( study && !anEntry.isEmpty() ) {
440       _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
441       OK = !expandable( aSO ) && hasChildren( aSO );
442     }
443   }
444   return OK;
445 }
446
447 bool GEOMGUI_Selection::hasShownChildren( const int index ) const
448 {
449   bool OK = false;
450   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
451
452   if ( appStudy )  {
453     QString anEntry = entry( index );
454     _PTR(Study) study = appStudy->studyDS();
455     if ( study && !anEntry.isEmpty() ) {
456       _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
457       OK = expandable( aSO ) && hasChildren( aSO );
458     }
459   }
460   return OK;
461 }
462
463 bool GEOMGUI_Selection::compoundOfVertices( const int index ) const
464 {
465   GEOM::GEOM_Object_var obj = getObject( index );
466   return isCompoundOfVertices( obj );
467 }
468
469 bool GEOMGUI_Selection::isComponent( const int index ) const
470 {
471   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
472
473   if ( appStudy ) {
474     QString anEntry = entry( index );
475     _PTR(Study) study = appStudy->studyDS();
476     if ( study && !anEntry.isNull() ) {
477       _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
478       if ( aSO && aSO->GetFatherComponent() )
479         return aSO->GetFatherComponent()->GetIOR() == aSO->GetIOR();
480     }
481   }
482   return false;
483 }
484
485 GEOM::GEOM_Object_ptr GEOMGUI_Selection::getObject( const int index ) const
486 {
487   GEOM::GEOM_Object_var o;
488   if ( 0 <= index && index < myObjects.size() )
489     o = GEOM::GEOM_Object::_duplicate( myObjects[index] );
490   return o._retn();
491 }
492
493 QString GEOMGUI_Selection::selectionMode() const
494 {
495   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( study()->application() );
496   if ( app ) {
497     GeometryGUI* aGeomGUI = dynamic_cast<GeometryGUI*>( app->module( "Geometry" ) );
498     if ( aGeomGUI ) {
499       switch ( aGeomGUI->getLocalSelectionMode() )
500       {
501         case GEOM_POINT      : return "VERTEX";
502         case GEOM_EDGE       : return "EDGE";
503         case GEOM_WIRE       : return "WIRE";
504         case GEOM_FACE       : return "FACE";
505         case GEOM_SHELL      : return "SHELL";
506         case GEOM_SOLID      : return "SOLID";
507         case GEOM_COMPOUND   : return "COMPOUND";
508         case GEOM_ALLOBJECTS : return "ALL";
509         default: return "";
510       }
511     }
512   }
513   return "";
514 }