Salome HOME
Update copyright
[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_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 ) )
285     ret = obj->GetShapeType() == GEOM::COMPOUND && obj->GetMaxShapeType() == GEOM::VERTEX;
286   return ret;
287 }
288
289 bool GEOMGUI_Selection::hasHiddenChildren( const int index ) const
290 {
291   bool OK = false;
292   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
293     (SUIT_Session::session()->activeApplication()->activeStudy());
294
295   if ( appStudy && index >= 0 && index < count() )  {
296     _PTR(Study) study = appStudy->studyDS();
297     QString anEntry = entry( index );
298
299     if ( study && !anEntry.isEmpty() ) {
300       _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
301       OK = !expandable( aSO ) && hasChildren( aSO );
302     }
303   }
304   return OK;
305 }
306
307 bool GEOMGUI_Selection::hasShownChildren( const int index ) const
308 {
309   bool OK = false;
310   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
311     (SUIT_Session::session()->activeApplication()->activeStudy());
312
313   if ( appStudy && index >= 0 && index < count() )  {
314     _PTR(Study) study = appStudy->studyDS();
315     QString anEntry = entry( index );
316
317     if ( study && !anEntry.isEmpty() ) {
318       _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
319       OK = expandable( aSO ) && hasChildren( aSO );
320     }
321   }
322   return OK;
323 }
324
325 bool GEOMGUI_Selection::compoundOfVertices( const int index ) const
326 {
327   return isCompoundOfVertices( getObject( index ) );
328 }
329
330 bool GEOMGUI_Selection::isComponent( const int index ) const
331 {
332   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
333     (SUIT_Session::session()->activeApplication()->activeStudy());
334
335   if ( appStudy && index >= 0 && index < count() )  {
336     _PTR(Study) study = appStudy->studyDS();
337     QString anEntry = entry( index );
338
339     if ( study && !anEntry.isNull() ) {
340       _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
341       if ( aSO && aSO->GetFatherComponent() )
342         return aSO->GetFatherComponent()->GetIOR() == aSO->GetIOR();
343     }
344   }
345   return false;
346 }
347
348 GEOM::GEOM_Object_ptr GEOMGUI_Selection::getObject( const int index ) const
349 {
350   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
351     (SUIT_Session::session()->activeApplication()->activeStudy());
352
353   if (appStudy && index >= 0 && index < count()) {
354     _PTR(Study) study = appStudy->studyDS();
355     QString anEntry = entry(index);
356
357     if (study && !anEntry.isNull()) {
358       _PTR(SObject) aSO (study->FindObjectID(anEntry.toStdString()));
359       if (aSO) {
360         CORBA::Object_var anObj = GeometryGUI::ClientSObjectToObject(aSO);
361         return GEOM::GEOM_Object::_narrow(anObj);
362       }
363     }
364   }
365   return GEOM::GEOM_Object::_nil();
366 }
367
368 QString GEOMGUI_Selection::selectionMode() const
369 {
370   SalomeApp_Application* app = (SalomeApp_Application*)(SUIT_Session::session()->activeApplication());
371   if (app) {
372     GeometryGUI* aGeomGUI = dynamic_cast<GeometryGUI*>( app->module( "Geometry" ) );
373     if (aGeomGUI) {
374       switch (aGeomGUI->getLocalSelectionMode())
375       {
376         case GEOM_POINT      : return "VERTEX";
377         case GEOM_EDGE       : return "EDGE";
378         case GEOM_WIRE       : return "WIRE";
379         case GEOM_FACE       : return "FACE";
380         case GEOM_SHELL      : return "SHELL";
381         case GEOM_SOLID      : return "SOLID";
382         case GEOM_COMPOUND   : return "COMPOUND";
383         case GEOM_ALLOBJECTS : return "ALL";
384         default: return "";
385       }
386     }
387   }
388   return "";
389 }