Salome HOME
Bug IPAL19361 - Qt4 porting. It is impossible showing ID of elements in 3D Viewer...
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_Selection.cxx
1 // SMESH SMESHGUI_Selection
2 //
3 // Copyright (C) 2003  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 // File   : SMESHGUI_Selection.cxx
23 // Author : Alexander SOLOVYOV, Open CASCADE S.A.S.
24 //
25
26 // SMESH includes
27 #include "SMESHGUI_Selection.h"
28
29 #include "SMESHGUI_Utils.h"
30 #include "SMESHGUI_VTKUtils.h"
31 #include "SMESHGUI_GEOMGenUtils.h"
32
33 #include <SMESH_Type.h>
34 #include <SMESH_Actor.h>
35
36 // SALOME GUI includes
37 #include <SalomeApp_Study.h>
38 #include <LightApp_VTKSelector.h>
39 #include <SVTK_ViewWindow.h>
40
41 // IDL includes
42 #include <SALOMEconfig.h>
43 #include CORBA_CLIENT_HEADER(SMESH_Gen)
44 #include CORBA_CLIENT_HEADER(SMESH_Mesh)
45 #include CORBA_CLIENT_HEADER(SMESH_Group)
46
47 //=======================================================================
48 //function : SMESHGUI_Selection
49 //purpose  : 
50 //=======================================================================
51 SMESHGUI_Selection::SMESHGUI_Selection()
52 : LightApp_Selection()
53 {
54 }
55
56 //=======================================================================
57 //function : ~SMESHGUI_Selection
58 //purpose  : 
59 //=======================================================================
60 SMESHGUI_Selection::~SMESHGUI_Selection()
61 {
62 }
63
64 //=======================================================================
65 //function : init
66 //purpose  : 
67 //=======================================================================
68 void SMESHGUI_Selection::init( const QString& client, LightApp_SelectionMgr* mgr )
69 {
70   LightApp_Selection::init( client, mgr );
71
72   if( mgr && study() )
73   {
74     SalomeApp_Study* aSStudy = dynamic_cast<SalomeApp_Study*>(study());
75     if (!aSStudy)
76       return;
77     _PTR(Study) aStudy = aSStudy->studyDS();
78
79     for( int i=0, n=count(); i<n; i++ )
80       myTypes.append( typeName( type( entry( i ), aStudy ) ) );
81   }
82 }
83
84 //=======================================================================
85 //function : processOwner
86 //purpose  : 
87 //=======================================================================
88 void SMESHGUI_Selection::processOwner( const LightApp_DataOwner* ow )
89 {
90   const LightApp_SVTKDataOwner* owner =
91     dynamic_cast<const LightApp_SVTKDataOwner*> ( ow );
92   if( owner )
93     myActors.append( dynamic_cast<SMESH_Actor*>( owner->GetActor() ) );
94   else
95     myActors.append( 0 );
96 }
97
98 //=======================================================================
99 //function : parameter
100 //purpose  : 
101 //=======================================================================
102 QVariant SMESHGUI_Selection::parameter( const int ind, const QString& p ) const
103 {
104   QVariant val;
105   if      ( p=="client" )        val = QVariant( LightApp_Selection::parameter( p ) );
106   else if ( p=="type" )          val = QVariant( myTypes[ind] );
107   else if ( p=="elemTypes" )     val = QVariant( elemTypes( ind ) );
108   else if ( p=="isAutoColor" )   val = QVariant( isAutoColor( ind ) );
109   else if ( p=="numberOfNodes" ) val = QVariant( numberOfNodes( ind ) );
110   else if ( p=="labeledTypes" )  val = QVariant( labeledTypes( ind ) );
111   else if ( p=="shrinkMode" )    val = QVariant( shrinkMode( ind ) );
112   else if ( p=="entityMode" )    val = QVariant( entityMode( ind ) );
113   else if ( p=="controlMode" )   val = QVariant( controlMode( ind ) );
114   else if ( p=="displayMode" )   val = QVariant( displayMode( ind ) );
115   else if ( p=="isComputable" )  val = QVariant( isComputable( ind ) );
116   else if ( p=="hasReference" )  val = QVariant( hasReference( ind ) );
117
118   if( val.isValid() )
119     return val;
120   else
121     return LightApp_Selection::parameter( ind, p );
122 }
123
124 //=======================================================================
125 //function : getVtkOwner
126 //purpose  : 
127 //=======================================================================
128
129 SMESH_Actor* SMESHGUI_Selection::getActor( int ind ) const
130 {
131   if( ind >= 0 && ind < count() )
132     return myActors.isEmpty() ? 0 : myActors.at( ind );
133   else
134     return 0;
135 }
136
137 //=======================================================================
138 //function : elemTypes
139 //purpose  : may return {'Edge' 'Face' 'Volume'} at most
140 //=======================================================================
141
142 QList<QVariant> SMESHGUI_Selection::elemTypes( int ind ) const
143 {
144   QList<QVariant> types;
145   SMESH_Actor* actor = getActor( ind );
146   if ( actor ) {
147     TVisualObjPtr object = actor->GetObject();
148     if ( object ) {
149       if ( object->GetNbEntities( SMDSAbs_Edge )) types.append( "Edge" );
150       if ( object->GetNbEntities( SMDSAbs_Face )) types.append( "Face" );
151       if ( object->GetNbEntities( SMDSAbs_Volume )) types.append( "Volume" );
152     }
153   }
154   return types;
155 }
156
157 //=======================================================================
158 //function : labeledTypes
159 //purpose  : may return {'Point' 'Cell'} at most
160 //=======================================================================
161
162 QList<QVariant> SMESHGUI_Selection::labeledTypes( int ind ) const
163 {
164   QList<QVariant> types;
165   SMESH_Actor* actor = getActor( ind );
166   if ( actor ) {
167     if ( actor->GetPointsLabeled()) types.append( "Point" );
168     if ( actor->GetCellsLabeled()) types.append( "Cell" );
169   }
170   return types;
171 }
172
173 //=======================================================================
174 //function : displayMode
175 //purpose  : return SMESH_Actor::EReperesent
176 //=======================================================================
177
178 QString SMESHGUI_Selection::displayMode( int ind ) const
179 {
180   SMESH_Actor* actor = getActor( ind );
181   if ( actor ) {
182     switch( actor->GetRepresentation() ) {
183     case SMESH_Actor::eEdge:    return "eEdge";
184     case SMESH_Actor::eSurface: return "eSurface";
185     case SMESH_Actor::ePoint:   return "ePoint";
186     default: break;
187     }
188   }
189   return "Unknown";
190 }
191
192 //=======================================================================
193 //function : shrinkMode
194 //purpose  : return either 'IsSrunk', 'IsNotShrunk' or 'IsNotShrinkable'
195 //=======================================================================
196
197 QString SMESHGUI_Selection::shrinkMode( int ind ) const
198 {
199   SMESH_Actor* actor = getActor( ind );
200   if ( actor && actor->IsShrunkable() ) {
201     if ( actor->IsShrunk() )
202       return "IsShrunk";
203     return "IsNotShrunk";
204   }
205   return "IsNotShrinkable";
206 }
207
208 //=======================================================================
209 //function : entityMode
210 //purpose  : may return {'Edge' 'Face' 'Volume'} at most
211 //=======================================================================
212
213 QList<QVariant> SMESHGUI_Selection::entityMode( int ind ) const
214 {
215   QList<QVariant> types;
216   SMESH_Actor* actor = getActor( ind );
217   if ( actor ) {
218     unsigned int aMode = actor->GetEntityMode();
219     if ( aMode & SMESH_Actor::eVolumes) types.append( "Volume");
220     if ( aMode & SMESH_Actor::eFaces  ) types.append( "Face"  );
221     if ( aMode & SMESH_Actor::eEdges  ) types.append( "Edge"  );
222   }
223   return types;
224 }
225
226 //=======================================================================
227 //function : controlMode
228 //purpose  : return SMESH_Actor::eControl
229 //=======================================================================
230
231 QString SMESHGUI_Selection::controlMode( int ind ) const
232 {
233   SMESH_Actor* actor = getActor( ind );
234   if ( actor ) {
235     switch( actor->GetControlMode() ) {
236     case SMESH_Actor::eLength:            return "eLength";
237     case SMESH_Actor::eLength2D:          return "eLength2D";
238     case SMESH_Actor::eFreeEdges:         return "eFreeEdges";
239     case SMESH_Actor::eFreeBorders:       return "eFreeBorders";
240     case SMESH_Actor::eMultiConnection:   return "eMultiConnection";
241     case SMESH_Actor::eMultiConnection2D: return "eMultiConnection2D";
242     case SMESH_Actor::eArea:              return "eArea";
243     case SMESH_Actor::eVolume3D:          return "eVolume3D";
244     case SMESH_Actor::eTaper:             return "eTaper";
245     case SMESH_Actor::eAspectRatio:       return "eAspectRatio";
246     case SMESH_Actor::eAspectRatio3D:     return "eAspectRatio3D";
247     case SMESH_Actor::eMinimumAngle:      return "eMinimumAngle";
248     case SMESH_Actor::eWarping:           return "eWarping";
249     case SMESH_Actor::eSkew:              return "eSkew";
250     default:;
251     }
252   }
253   return "eNone";
254 }
255
256 //=======================================================================
257 //function : isAutoColor
258 //purpose  : 
259 //=======================================================================
260
261 bool SMESHGUI_Selection::isAutoColor( int ind ) const
262 {
263   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
264   {
265     _PTR(SObject) sobj = SMESH::GetActiveStudyDocument()->FindObjectID( entry( ind ).toLatin1().data() );
266     CORBA::Object_var obj = SMESH::SObjectToObject( sobj, SMESH::GetActiveStudyDocument() );
267
268     if ( ! CORBA::is_nil( obj )) {
269       SMESH::SMESH_Mesh_var mesh = SMESH::SMESH_Mesh::_narrow( obj );
270       if ( ! mesh->_is_nil() )
271         return mesh->GetAutoColor();
272     }
273   }
274   return false;
275 }
276
277 //=======================================================================
278 //function : numberOfNodes
279 //purpose  : 
280 //=======================================================================
281
282 int SMESHGUI_Selection::numberOfNodes( int ind ) const
283 {
284   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
285   {
286     _PTR(SObject) sobj = SMESH::GetActiveStudyDocument()->FindObjectID( entry( ind ).toLatin1().data() );
287     CORBA::Object_var obj = SMESH::SObjectToObject( sobj, SMESH::GetActiveStudyDocument() );
288
289     if ( ! CORBA::is_nil( obj )) {
290       SMESH::SMESH_Mesh_var mesh = SMESH::SMESH_Mesh::_narrow( obj );
291       if ( ! mesh->_is_nil() )
292         return mesh->NbNodes();
293       SMESH::SMESH_subMesh_var aSubMeshObj = SMESH::SMESH_subMesh::_narrow( obj );
294       if ( !aSubMeshObj->_is_nil() )
295         return aSubMeshObj->GetNumberOfNodes(true);
296       SMESH::SMESH_GroupBase_var aGroupObj = SMESH::SMESH_GroupBase::_narrow( obj );
297       if ( !aGroupObj->_is_nil() )
298         return aGroupObj->Size();
299     }
300   }
301   return 0;
302 }
303
304 //=======================================================================
305 //function : isComputable
306 //purpose  : 
307 //=======================================================================
308
309 QVariant SMESHGUI_Selection::isComputable( int ind ) const
310 {
311   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
312   {
313 /*    Handle(SALOME_InteractiveObject) io =
314       static_cast<LightApp_DataOwner*>( myDataOwners[ ind ].get() )->IO();
315     if ( !io.IsNull() ) {
316       SMESH::SMESH_Mesh_var mesh = SMESH::GetMeshByIO(io); // m,sm,gr->m
317       if ( !mesh->_is_nil() ) {*/
318         _PTR(SObject) so = SMESH::GetActiveStudyDocument()->FindObjectID( entry( ind ).toLatin1().data() );
319         //FindSObject( mesh );
320         if ( so ) {
321           CORBA::Object_var obj = SMESH::SObjectToObject(so, SMESH::GetActiveStudyDocument());
322           if(!CORBA::is_nil(obj)){
323             SMESH::SMESH_Mesh_var mesh = SMESH::SMESH_Mesh::_narrow( obj );
324             if (!mesh->_is_nil()){
325               if(mesh->HasShapeToMesh()) {
326                 GEOM::GEOM_Object_var shape = SMESH::GetShapeOnMeshOrSubMesh( so );
327                 return QVariant( !shape->_is_nil() );
328               }
329               else
330               {
331                 return QVariant(!mesh->NbFaces()==0);
332               }
333             }
334             else
335             {
336               GEOM::GEOM_Object_var shape = SMESH::GetShapeOnMeshOrSubMesh( so );
337               return QVariant( !shape->_is_nil() );
338             }
339           }
340         }
341 //      }
342 //    }
343   }
344   return QVariant( false );
345 }
346
347 //=======================================================================
348 //function : hasReference
349 //purpose  : 
350 //=======================================================================
351
352 QVariant SMESHGUI_Selection::hasReference( int ind ) const
353 {
354   return QVariant( isReference( ind ) );
355 }
356
357 //=======================================================================
358 //function : isVisible
359 //purpose  : 
360 //=======================================================================
361
362 QVariant SMESHGUI_Selection::isVisible( int ind ) const
363 {
364   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
365   {
366     QString ent = entry( ind );
367     SMESH_Actor* actor = SMESH::FindActorByEntry( ent.toLatin1().data() );
368     if ( actor && actor->hasIO() ) {
369       if(SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView())
370         return QVariant( aViewWindow->isVisible( actor->getIO() ) );
371     }
372   }
373   return QVariant( false );
374 }
375
376 //=======================================================================
377 //function : type
378 //purpose  : 
379 //=======================================================================
380
381 int SMESHGUI_Selection::type( const QString& entry, _PTR(Study) study )
382 {
383   _PTR(SObject) obj (study->FindObjectID(entry.toLatin1().data()));
384   if( !obj )
385     return -1;
386
387   _PTR(SObject) ref;
388   if( obj->ReferencedObject( ref ) )
389     obj = ref;
390
391   _PTR(SObject) objFather = obj->GetFather();
392   _PTR(SComponent) objComponent = obj->GetFatherComponent();
393
394   if( objComponent->ComponentDataType()!="SMESH" )
395     return -1;
396
397   if( objComponent->GetIOR()==obj->GetIOR() )
398     return COMPONENT;
399
400   int aLevel = obj->Depth() - objComponent->Depth(),
401       aFTag = objFather->Tag(),
402       anOTag = obj->Tag(),
403       res = -1;
404
405   switch (aLevel)
406   {
407   case 1:
408     if (anOTag >= SMESH::Tag_FirstMeshRoot)
409       res = MESH;
410     break;
411   case 2:
412     switch (aFTag)
413     {
414     case SMESH::Tag_HypothesisRoot:
415       res = HYPOTHESIS;
416       break;
417     case SMESH::Tag_AlgorithmsRoot:
418       res = ALGORITHM;
419       break;
420     }
421     break;
422   case 3:
423     switch (aFTag)
424     {
425     case SMESH::Tag_SubMeshOnVertex:
426       res = SUBMESH_VERTEX;
427       break;
428     case SMESH::Tag_SubMeshOnEdge:
429       res = SUBMESH_EDGE;
430       break;
431     case SMESH::Tag_SubMeshOnFace:
432       res = SUBMESH_FACE;
433       break;
434     case SMESH::Tag_SubMeshOnSolid:
435       res = SUBMESH_SOLID;
436       break;
437     case SMESH::Tag_SubMeshOnCompound:
438       res = SUBMESH_COMPOUND;
439       break;
440     default:
441       if (aFTag >= SMESH::Tag_FirstGroup)
442         res = GROUP;
443       else
444         res = SUBMESH;
445     }
446     break;
447   }
448
449   return res;
450 }
451
452 //=======================================================================
453 //function : typeName
454 //purpose  : 
455 //=======================================================================
456
457 QString SMESHGUI_Selection::typeName( const int t )
458 {
459   switch( t )
460   {
461   case HYPOTHESIS:
462     return "Hypothesis";
463   case ALGORITHM:
464     return "Algorithm";
465   case MESH:
466     return "Mesh";
467   case SUBMESH:
468     return "SubMesh";
469   case MESHorSUBMESH:
470     return "Mesh or submesh";
471   case SUBMESH_VERTEX:
472     return "Mesh vertex";
473   case SUBMESH_EDGE:
474     return "Mesh edge";
475   case SUBMESH_FACE:
476     return "Mesh face";
477   case SUBMESH_SOLID:
478     return "Mesh solid";
479   case SUBMESH_COMPOUND:
480     return "Mesh compound";
481   case GROUP:
482     return "Group";
483   case COMPONENT:
484     return "Component";
485   default:
486     return "Unknown";
487   }
488 }