Salome HOME
f4aeb8ccfec76545ed58681fcbc389bf00cfee68
[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 if ( p == "compoundOfVertices" )
102     return QVariant( compoundOfVertices( ind ) );
103   else
104     return LightApp_Selection::parameter( ind, p );
105 }
106
107 // the method to skip temporary objects from selection (called from LightApp)
108
109 bool GEOMGUI_Selection::processOwner( const LightApp_DataOwner* theOwner)
110 {
111   return !theOwner->entry().contains("_");
112 }
113
114 QString GEOMGUI_Selection::typeName( const int index ) const
115 {
116   if ( isComponent( index ) )
117     return "Component";
118   GEOM::GEOM_Object_var anObj = getObject( index );
119   if ( !CORBA::is_nil( anObj ) ) {
120     const int aGeomType = anObj->GetType();
121     if ( aGeomType == GEOM_GROUP )
122       return "Group";
123     else
124       return "Shape";
125   }
126   return "Unknown";
127 }
128
129 int GEOMGUI_Selection::typeId( const int index ) const
130 {
131   int aType = -1;
132   GEOM::GEOM_Object_var anObj = getObject( index );
133   if ( !CORBA::is_nil( anObj ) )
134     //aType = anObj->GetType();
135     aType = (int)anObj->GetShapeType();
136   return aType;
137 }
138
139 bool GEOMGUI_Selection::isVisible( const int index ) const
140 {
141   GEOM::GEOM_Object_var obj = getObject( index );
142   SALOME_View* view = GEOM_Displayer::GetActiveView();
143   if ( !CORBA::is_nil( obj ) && view ) {
144     Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( entry( index ).toLatin1().constData(), "GEOM", "TEMP_IO" );
145     return view->isVisible( io );
146   }
147   return false;
148 }
149
150 bool GEOMGUI_Selection::isAutoColor( const int index ) const
151 {
152   GEOM::GEOM_Object_var obj = getObject( index );
153   if ( !CORBA::is_nil( obj ) )
154     return obj->GetAutoColor();
155   return false;
156 }
157
158 QString GEOMGUI_Selection::displayMode( const int index ) const
159 {
160   SALOME_View* view = GEOM_Displayer::GetActiveView();
161   QString viewType = activeViewType();
162   if ( view /*fix for 9320==>*/&& ( viewType == OCCViewer_Viewer::Type() || viewType == SVTK_Viewer::Type() ) ) {
163     SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
164     if ( prs ) {
165       if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
166         SOCC_Prs* occPrs = (SOCC_Prs*) prs;
167         AIS_ListOfInteractive lst;
168         occPrs->GetObjects( lst );
169         if ( lst.Extent() ) {
170           Handle(AIS_InteractiveObject) io = lst.First();
171           if ( !io.IsNull() ) {
172             int dm = io->DisplayMode();
173             if ( dm == AIS_WireFrame )
174               return "Wireframe";
175             else if ( dm == AIS_Shaded )
176               return "Shading";
177             else { // return default display mode of AIS_InteractiveContext
178               OCCViewer_Viewer* occViewer = (OCCViewer_Viewer*) SUIT_Session::session()->activeApplication()->desktop(
179                                             )->activeWindow()->getViewManager()->getViewModel();
180               Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
181               dm = ic->DisplayMode();
182               if ( dm == AIS_WireFrame )
183                 return "Wireframe";
184               else if ( dm == AIS_Shaded )
185                 return "Shading";
186             }
187           }
188         }
189       }
190       else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
191         SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
192         vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
193         if ( lst ) {
194           lst->InitTraversal();
195           vtkActor* actor = lst->GetNextActor();
196           if ( actor ) {
197             SALOME_Actor* salActor = dynamic_cast<SALOME_Actor*>( actor );
198             if ( salActor ) {
199               int dm = salActor->getDisplayMode();
200               if ( dm == 0 )
201                 return "Wireframe";
202               else if ( dm == 1 )
203                 return "Shading";
204             } // if ( salome actor )
205           } // if ( actor )
206         } // if ( lst == vtkPrs->GetObjects() )
207       } // if VTK
208     }
209   }
210   return "";
211 }
212
213 bool GEOMGUI_Selection::isVectorsMode( const int index ) const
214 {
215   bool ret = false;
216   SALOME_View* view = GEOM_Displayer::GetActiveView();
217   QString viewType = activeViewType();
218   if ( view && ( viewType == OCCViewer_Viewer::Type() || viewType == SVTK_Viewer::Type() ) ) {
219     SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
220     if ( prs ) {
221       if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
222         SOCC_Prs* occPrs = (SOCC_Prs*) prs;
223         AIS_ListOfInteractive lst;
224         occPrs->GetObjects( lst );
225         if ( lst.Extent() ) {
226           Handle(AIS_InteractiveObject) io = lst.First();
227           if ( !io.IsNull() ) {
228             Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
229             if ( !aSh.IsNull() )
230               ret = aSh->isShowVectors();
231           }
232         }
233       } else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
234         SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
235         vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
236         if ( lst ) {
237           lst->InitTraversal();
238           vtkActor* actor = lst->GetNextActor();
239           if ( actor ) {
240             GEOM_Actor* aGeomActor = GEOM_Actor::SafeDownCast(actor);
241             if ( aGeomActor )
242               ret = aGeomActor->GetVectorMode();
243           }
244         }
245       }
246     }
247   }
248   return ret;
249 }
250
251 bool GEOMGUI_Selection::hasChildren( const _PTR(SObject)& obj )
252 {
253   bool ok = false;
254   if ( obj ) {
255     _PTR(ChildIterator) it ( obj->GetStudy()->NewChildIterator( obj ) );
256     for ( ; it->More() && !ok; it->Next() ) {
257       _PTR(SObject) child = it->Value();
258       if ( child ) {
259         _PTR(SObject) refObj;
260         if ( child->ReferencedObject( refObj ) ) continue; // omit references
261         if ( child->GetName() != "" ) ok = true;
262       }
263     }
264   }
265   return ok;
266 }
267
268 bool GEOMGUI_Selection::expandable( const _PTR(SObject)& obj )
269 {
270   bool exp = true;
271   _PTR(GenericAttribute) anAttr;
272   if ( obj && obj->FindAttribute( anAttr, "AttributeExpandable" ) ) {
273     _PTR(AttributeExpandable) aAttrExp = anAttr;
274     exp = aAttrExp->IsExpandable();
275   }
276   return exp;
277 }
278
279 bool GEOMGUI_Selection::isCompoundOfVertices( GEOM::GEOM_Object_ptr obj )
280 {
281   bool ret = false;
282   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
283     (SUIT_Session::session()->activeApplication()->activeStudy());
284   if ( appStudy && !CORBA::is_nil( obj ) && obj->GetShapeType() == GEOM::COMPOUND ) {
285     GEOM::GEOM_IMeasureOperations_var anOper = GeometryGUI::GetGeomGen()->GetIMeasureOperations( appStudy->id() );
286     QString whatIs = anOper->WhatIs( obj );
287     QStringList data = whatIs.split( "\n", QString::SkipEmptyParts );
288     int nbVertices = 0, nbCompounds = 0, nbOther = 0;
289     foreach ( QString s, data ) {
290       QString type = s.section( ":", 0, 0 ).trimmed().toLower();
291       int cnt = s.section( ":", 1, 1 ).trimmed().toInt();
292       if ( type == "vertex" ) nbVertices += cnt;
293       else if ( type == "compound" ) nbCompounds += cnt;
294       else if ( type != "shape" ) nbOther += cnt;
295     }
296     ret = nbVertices > 0 && nbCompounds == 1 && nbOther == 0;
297     anOper->Destroy();
298   }
299   return ret;
300 }
301
302 bool GEOMGUI_Selection::hasHiddenChildren( const int index ) const
303 {
304   bool OK = false;
305   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
306     (SUIT_Session::session()->activeApplication()->activeStudy());
307
308   if ( appStudy && index >= 0 && index < count() )  {
309     _PTR(Study) study = appStudy->studyDS();
310     QString anEntry = entry( index );
311
312     if ( study && !anEntry.isEmpty() ) {
313       _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
314       OK = !expandable( aSO ) && hasChildren( aSO );
315     }
316   }
317   return OK;
318 }
319
320 bool GEOMGUI_Selection::hasShownChildren( const int index ) const
321 {
322   bool OK = false;
323   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
324     (SUIT_Session::session()->activeApplication()->activeStudy());
325
326   if ( appStudy && index >= 0 && index < count() )  {
327     _PTR(Study) study = appStudy->studyDS();
328     QString anEntry = entry( index );
329
330     if ( study && !anEntry.isEmpty() ) {
331       _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
332       OK = expandable( aSO ) && hasChildren( aSO );
333     }
334   }
335   return OK;
336 }
337
338 bool GEOMGUI_Selection::compoundOfVertices( const int index ) const
339 {
340   return isCompoundOfVertices( getObject( index ) );
341 }
342
343 bool GEOMGUI_Selection::isComponent( const int index ) const
344 {
345   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
346     (SUIT_Session::session()->activeApplication()->activeStudy());
347
348   if ( appStudy && index >= 0 && index < count() )  {
349     _PTR(Study) study = appStudy->studyDS();
350     QString anEntry = entry( index );
351
352     if ( study && !anEntry.isNull() ) {
353       _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
354       if ( aSO && aSO->GetFatherComponent() )
355         return aSO->GetFatherComponent()->GetIOR() == aSO->GetIOR();
356     }
357   }
358   return false;
359 }
360
361 GEOM::GEOM_Object_ptr GEOMGUI_Selection::getObject( const int index ) const
362 {
363   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
364     (SUIT_Session::session()->activeApplication()->activeStudy());
365
366   if (appStudy && index >= 0 && index < count()) {
367     _PTR(Study) study = appStudy->studyDS();
368     QString anEntry = entry(index);
369
370     if (study && !anEntry.isNull()) {
371       _PTR(SObject) aSO (study->FindObjectID(anEntry.toStdString()));
372       if (aSO) {
373         CORBA::Object_var anObj = GeometryGUI::ClientSObjectToObject(aSO);
374         return GEOM::GEOM_Object::_narrow(anObj);
375       }
376     }
377   }
378   return GEOM::GEOM_Object::_nil();
379 }
380
381 QString GEOMGUI_Selection::selectionMode() const
382 {
383   SalomeApp_Application* app = (SalomeApp_Application*)(SUIT_Session::session()->activeApplication());
384   if (app) {
385     GeometryGUI* aGeomGUI = dynamic_cast<GeometryGUI*>( app->module( "Geometry" ) );
386     if (aGeomGUI) {
387       switch (aGeomGUI->getLocalSelectionMode())
388       {
389         case GEOM_POINT      : return "VERTEX";
390         case GEOM_EDGE       : return "EDGE";
391         case GEOM_WIRE       : return "WIRE";
392         case GEOM_FACE       : return "FACE";
393         case GEOM_SHELL      : return "SHELL";
394         case GEOM_SOLID      : return "SOLID";
395         case GEOM_COMPOUND   : return "COMPOUND";
396         case GEOM_ALLOBJECTS : return "ALL";
397         default: return "";
398       }
399     }
400   }
401   return "";
402 }