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