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