Salome HOME
Merge 'master' branch into 'V9_dev' branch.
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_Selection.cxx
1 // Copyright (C) 2007-2016  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, or (at your option) any later version.
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 // SMESH SMESHGUI_Selection
24 // File   : SMESHGUI_Selection.cxx
25 // Author : Alexander SOLOVYOV, Open CASCADE S.A.S.
26 //
27
28 // SMESH includes
29 #include "SMESHGUI_Selection.h"
30
31 #include "SMESHGUI.h"
32 #include "SMESHGUI_ComputeDlg.h"
33 #include "SMESHGUI_ConvToQuadOp.h"
34 #include "SMESHGUI_GEOMGenUtils.h"
35 #include "SMESHGUI_Utils.h"
36 #include "SMESHGUI_VTKUtils.h"
37
38 #include <SMESH_Type.h>
39 #include <SMESH_Actor.h>
40 #include <SMESH_ScalarBarActor.h>
41
42 // SALOME GUI includes
43 #include <SalomeApp_Application.h>
44 #include <LightApp_VTKSelector.h>
45 #include <SVTK_ViewWindow.h>
46
47 // IDL includes
48 #include <SALOMEconfig.h>
49 #include CORBA_CLIENT_HEADER(SMESH_Gen)
50 #include CORBA_CLIENT_HEADER(SMESH_Mesh)
51 #include CORBA_CLIENT_HEADER(SMESH_Group)
52
53 //=======================================================================
54 //function : SMESHGUI_Selection
55 //purpose  : 
56 //=======================================================================
57 SMESHGUI_Selection::SMESHGUI_Selection()
58 : LightApp_Selection()
59 {
60 }
61
62 //=======================================================================
63 //function : ~SMESHGUI_Selection
64 //purpose  : 
65 //=======================================================================
66 SMESHGUI_Selection::~SMESHGUI_Selection()
67 {
68 }
69
70 //=======================================================================
71 //function : init
72 //purpose  : 
73 //=======================================================================
74 void SMESHGUI_Selection::init( const QString& client, LightApp_SelectionMgr* mgr )
75 {
76   LightApp_Selection::init( client, mgr );
77
78   if( mgr )
79   {
80     for( int i=0, n=count(); i<n; i++ ) {
81       myTypes.append( typeName( type( entry( i ) ) ) );
82       myControls.append( controlMode( i ) );
83     }
84   }
85 }
86
87 //=======================================================================
88 //function : processOwner
89 //purpose  : 
90 //=======================================================================
91 bool SMESHGUI_Selection::processOwner( const LightApp_DataOwner* ow )
92 {
93   const LightApp_SVTKDataOwner* owner =
94     dynamic_cast<const LightApp_SVTKDataOwner*> ( ow );
95   if( owner ) {
96     myActors.append( dynamic_cast<SMESH_Actor*>( owner->GetActor() ) );
97   }
98   else if ( ow ) { // SVTK selection disabled
99     QString entry = ow->entry();
100     myActors.append( SMESH::FindActorByEntry( entry.toStdString().c_str() ));
101   }
102   else {
103     myActors.append( 0 );
104   }
105   return true;
106 }
107
108 //=======================================================================
109 //function : parameter
110 //purpose  : 
111 //=======================================================================
112 QVariant SMESHGUI_Selection::parameter( const int ind, const QString& p ) const
113 {
114   QVariant val;
115   if      ( p=="client" )               val = QVariant( LightApp_Selection::parameter( p ) );
116   else if ( p=="type" )                 val = QVariant( myTypes[ind] );
117   else if ( p=="hasActor" )             val = QVariant( getActor( ind ) != 0 );
118   else if ( p=="elemTypes" )            val = QVariant( elemTypes( ind, false ) );
119   else if ( p=="objElemTypes" )         val = QVariant( elemTypes( ind, true ) );
120   else if ( p=="isAutoColor" )          val = QVariant( isAutoColor( ind ) );
121   else if ( p=="numberOfNodes" )        val = QVariant( numberOfNodes( ind ) );
122   else if ( p=="dim" )                  val = QVariant( dim( ind ) );
123   else if ( p=="labeledTypes" )         val = QVariant( labeledTypes( ind ) );
124   else if ( p=="shrinkMode" )           val = QVariant( shrinkMode( ind ) );
125   else if ( p=="entityMode" )           val = QVariant( entityMode( ind ) );
126   else if ( p=="isNumFunctor" )         val = QVariant( isNumFunctor( ind ) );
127   else if ( p=="displayMode" )          val = QVariant( displayMode( ind ) );
128   else if ( p=="isComputable" )         val = QVariant( isComputable( ind ) );
129   else if ( p=="isPreComputable" )      val = QVariant( isPreComputable( ind ) );
130   else if ( p=="hasGeomReference" )     val = QVariant( hasGeomReference( ind ) );
131   else if ( p=="isEditableHyp" )        val = QVariant( isEditableHyp( ind ) );
132   else if ( p=="isImported" )           val = QVariant( isImported( ind ) );
133   else if ( p=="facesOrientationMode" ) val = QVariant( facesOrientationMode( ind ) );
134   else if ( p=="groupType" )            val = QVariant( groupType( ind ) );
135   else if ( p=="isQuadratic" )          val = QVariant( isQuadratic( ind ) );
136   else if ( p=="quadratic2DMode")       val = QVariant( quadratic2DMode( ind ) );
137   else if ( p=="isDistributionVisible") val = QVariant( isDistributionVisible( ind ) );
138   else if ( p=="isScalarBarVisible")    val = QVariant( isScalarBarVisible( ind ) );
139   else if ( p=="hasChildren")           val = QVariant( hasChildren( ind ) );
140   else if ( p=="nbChildren")            val = QVariant( nbChildren( ind ) );
141   else if ( p=="isContainer")           val = QVariant( isContainer( ind ) );
142   else if ( p=="guiState")              val = QVariant( guiState() );
143
144   if ( val.isValid() )
145     return val;
146   else
147     return LightApp_Selection::parameter( ind, p );
148 }
149
150 //=======================================================================
151 //function : parameter
152 //purpose  :
153 //=======================================================================
154 QVariant SMESHGUI_Selection::parameter( const QString& p ) const
155 {
156   QVariant val;
157   if ( p=="controlMode" ) val = QVariant( controlMode() );
158
159   if ( val.isValid() )
160     return val;
161   else
162     return LightApp_Selection::parameter( p );
163 }
164
165 //=======================================================================
166 //function : getVtkOwner
167 //purpose  :
168 //=======================================================================
169
170 SMESH_Actor* SMESHGUI_Selection::getActor( int ind ) const
171 {
172   if( ind >= 0 && ind < count() )
173     return myActors.isEmpty() ? 0 : myActors.at( ind );
174   else
175     return 0;
176 }
177
178 //=======================================================================
179 //function : elemTypes
180 //purpose  : may return {'Elem0d' 'Edge' 'Face' 'Volume' 'BallElem'} at most
181 //=======================================================================
182
183 QList<QVariant> SMESHGUI_Selection::elemTypes( int ind, bool fromObj ) const
184 {
185   QList<QVariant> types;
186   SMESH_Actor* actor = getActor( ind );
187   if ( actor ) {
188     TVisualObjPtr object = actor->GetObject();
189     if ( object ) {
190       if ( object->GetNbEntities( SMDSAbs_Edge ))      types.append( "Edge" );
191       if ( object->GetNbEntities( SMDSAbs_Face ))      types.append( "Face" );
192       if ( object->GetNbEntities( SMDSAbs_Volume ))    types.append( "Volume" );
193       if ( object->GetNbEntities( SMDSAbs_0DElement )) types.append( "Elem0d" );
194       if ( object->GetNbEntities( SMDSAbs_Ball ))      types.append( "BallElem" );
195     }
196   }
197   else if ( fromObj )
198   {
199     if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
200     {
201       _PTR(SObject) sobj = SMESH::getStudy()->FindObjectID( entry( ind ).toLatin1().data() );
202       CORBA::Object_var obj = SMESH::SObjectToObject( sobj );
203       SMESH::SMESH_IDSource_var idSrc = SMESH::SMESH_IDSource::_narrow( obj );
204       if ( !CORBA::is_nil( idSrc ) )
205       {
206         SMESH::array_of_ElementType_var typeVar = idSrc->GetTypes();
207         for ( CORBA::ULong i = 0; i < typeVar->length(); ++i )
208           switch ( typeVar[i] ) {
209           case SMESH::EDGE:   types.append( "Edge" );     break;
210           case SMESH::FACE:   types.append( "Face" );     break;
211           case SMESH::VOLUME: types.append( "Volume" );   break;
212           case SMESH::ELEM0D: types.append( "Elem0d" );   break;
213           case SMESH::BALL:   types.append( "BallElem" ); break;
214           case SMESH::ALL:
215           case SMESH::NODE:
216           case SMESH::NB_ELEMENT_TYPES: break;
217           }
218       }
219     }
220   }
221   return types;
222 }
223
224 //=======================================================================
225 //function : labeledTypes
226 //purpose  : may return {'Point' 'Cell'} at most
227 //=======================================================================
228
229 QList<QVariant> SMESHGUI_Selection::labeledTypes( int ind ) const
230 {
231   QList<QVariant> types;
232   SMESH_Actor* actor = getActor( ind );
233   if ( actor ) {
234     if ( actor->GetPointsLabeled()) types.append( "Point" );
235     if ( actor->GetCellsLabeled())  types.append( "Cell" );
236   }
237   return types;
238 }
239
240 //=======================================================================
241 //function : displayMode
242 //purpose  : return SMESH_Actor::EReperesent
243 //=======================================================================
244
245 QString SMESHGUI_Selection::displayMode( int ind ) const
246 {
247   SMESH_Actor* actor = getActor( ind );
248   if ( actor ) {
249     switch( actor->GetRepresentation() ) {
250     case SMESH_Actor::eEdge:    return "eEdge";
251     case SMESH_Actor::eSurface: return "eSurface";
252     case SMESH_Actor::ePoint:   return "ePoint";
253     default: break;
254     }
255   }
256   return "Unknown";
257 }
258
259 //=======================================================================
260 //function : isQuadratic
261 //purpose  : return true if the mesh has quadratic/bi-quadratic type
262 //=======================================================================
263
264 bool SMESHGUI_Selection::isQuadratic( int ind ) const
265 {
266   _PTR(SObject) so = SMESH::getStudy()->FindObjectID( entry( ind ).toLatin1().data() );
267   if ( !so )
268     return false;
269   SMESH::SMESH_IDSource_var idSource =  SMESH::SObjectToInterface<SMESH::SMESH_IDSource>( so );
270   if ( idSource->_is_nil() )
271     return false;
272   SMESHGUI_ConvToQuadOp::MeshDestinationType meshTgtType = SMESHGUI_ConvToQuadOp::DestinationMesh( idSource );
273   if ( meshTgtType & SMESHGUI_ConvToQuadOp::Linear )
274     return true;
275   return false;
276 }
277
278 //=======================================================================
279 //function : quadratic2DMode
280 //purpose  : return SMESH_Actor::EQuadratic2DRepresentation
281 //=======================================================================
282 QString SMESHGUI_Selection::quadratic2DMode( int ind ) const
283 {
284   SMESH_Actor* actor = getActor( ind );
285   if ( actor ) {
286     switch( actor->GetQuadratic2DRepresentation() ) {
287     case SMESH_Actor::eLines: return "eLines";
288     case SMESH_Actor::eArcs:  return "eArcs";
289     default: break;
290     }
291   }
292   return "Unknown";
293 }
294
295 //=======================================================================
296 //function : isDistributionVisible
297 //purpose  : Visible/Invisible distribution of the ScalarBar Actor
298 //=======================================================================
299
300 bool SMESHGUI_Selection::isDistributionVisible(int ind) const {
301   SMESH_Actor* actor = getActor( ind );
302   return (actor && actor->GetScalarBarActor() && actor->GetScalarBarActor()->GetDistributionVisibility());
303
304
305 //=======================================================================
306 //function : isScalarBarVisible
307 //purpose  : Visible/Invisible Scalar Bar
308 //=======================================================================
309
310 bool SMESHGUI_Selection::isScalarBarVisible(int ind) const {
311   SMESH_Actor* actor = getActor( ind );
312   return (actor && actor->GetScalarBarActor() && actor->GetScalarBarActor()->GetVisibility());
313 }
314
315 //=======================================================================
316 //function : shrinkMode
317 //purpose  : return either 'IsSrunk', 'IsNotShrunk' or 'IsNotShrinkable'
318 //=======================================================================
319
320 QString SMESHGUI_Selection::shrinkMode( int ind ) const
321 {
322   SMESH_Actor* actor = getActor( ind );
323   if ( actor && actor->IsShrunkable() ) {
324     return actor->IsShrunk() ? "IsShrunk" : "IsNotShrunk";
325   }
326   return "IsNotShrinkable";
327 }
328
329 //=======================================================================
330 //function : entityMode
331 //purpose  : may return {'Elem0d' 'Edge' 'Face' 'Volume' 'BallElem' } at most
332 //=======================================================================
333
334 QList<QVariant> SMESHGUI_Selection::entityMode( int ind ) const
335 {
336   QList<QVariant> types;
337   SMESH_Actor* actor = getActor( ind );
338   if ( actor ) {
339     unsigned int aMode = actor->GetEntityMode();
340     if ( aMode & SMESH_Actor::eVolumes    ) types.append( "Volume" );
341     if ( aMode & SMESH_Actor::eFaces      ) types.append( "Face"   );
342     if ( aMode & SMESH_Actor::eEdges      ) types.append( "Edge"   );
343     if ( aMode & SMESH_Actor::e0DElements ) types.append( "Elem0d" );
344     if ( aMode & SMESH_Actor::eBallElem )   types.append( "BallElem" );
345   }
346   return types;
347 }
348
349 //=======================================================================
350 //function : controlMode
351 //purpose  : return SMESH_Actor::eControl
352 //=======================================================================
353
354 QString SMESHGUI_Selection::controlMode( int ind ) const
355 {
356   SMESH_Actor* actor = getActor( ind );
357   QString mode = "eNone";
358   if ( actor ) {
359     switch( actor->GetControlMode() ) {
360     case SMESH_Actor::eLength:                mode = "eLength";                break;
361     case SMESH_Actor::eLength2D:              mode = "eLength2D";              break;
362     case SMESH_Actor::eDeflection2D:          mode = "eDeflection2D";          break;
363     case SMESH_Actor::eFreeEdges:             mode = "eFreeEdges";             break;
364     case SMESH_Actor::eFreeNodes:             mode = "eFreeNodes";             break;
365     case SMESH_Actor::eFreeBorders:           mode = "eFreeBorders";           break;
366     case SMESH_Actor::eFreeFaces:             mode = "eFreeFaces";             break;
367     case SMESH_Actor::eMultiConnection:       mode = "eMultiConnection";       break;
368     case SMESH_Actor::eMultiConnection2D:     mode = "eMultiConnection2D";     break;
369     case SMESH_Actor::eArea:                  mode = "eArea";                  break;
370     case SMESH_Actor::eVolume3D:              mode = "eVolume3D";              break;
371     case SMESH_Actor::eMaxElementLength2D:    mode = "eMaxElementLength2D";    break;
372     case SMESH_Actor::eMaxElementLength3D:    mode = "eMaxElementLength3D";    break;
373     case SMESH_Actor::eTaper:                 mode = "eTaper";                 break;
374     case SMESH_Actor::eAspectRatio:           mode = "eAspectRatio";           break;
375     case SMESH_Actor::eAspectRatio3D:         mode = "eAspectRatio3D";         break;
376     case SMESH_Actor::eMinimumAngle:          mode = "eMinimumAngle";          break;
377     case SMESH_Actor::eWarping:               mode = "eWarping";               break;
378     case SMESH_Actor::eSkew:                  mode = "eSkew";                  break;
379     case SMESH_Actor::eBareBorderFace:        mode = "eBareBorderFace";        break;
380     case SMESH_Actor::eBareBorderVolume:      mode = "eBareBorderVolume";      break;
381     case SMESH_Actor::eOverConstrainedFace:   mode = "eOverConstrainedFace";   break;
382     case SMESH_Actor::eOverConstrainedVolume: mode = "eOverConstrainedVolume"; break;
383     case SMESH_Actor::eCoincidentNodes:       mode = "eCoincidentNodes";       break;
384     case SMESH_Actor::eCoincidentElems1D:     mode = "eCoincidentElems1D";     break;
385     case SMESH_Actor::eCoincidentElems2D:     mode = "eCoincidentElems2D";     break;
386     case SMESH_Actor::eCoincidentElems3D:     mode = "eCoincidentElems3D";     break;
387     default:break;
388     }
389   }
390   return mode;
391 }
392
393 //=======================================================================
394 //function : controlMode
395 //purpose  : gets global control mode; return SMESH_Actor::eControl
396 //=======================================================================
397 QString SMESHGUI_Selection::controlMode() const
398 {
399   if( myControls.count() > 0 ) {
400     QString mode = myControls[0];
401     for( int ind = 1; ind < myControls.count(); ind++ ) {
402       if( mode != myControls[ind] )
403         return "eMixed"; // different controls used for different actors
404     }
405     return mode;
406   }
407   return "eNone";
408 }
409
410 //=======================================================================
411 //function : isNumFunctor
412 //purpose  : return true if a given actor is shown using a numeric functor
413 //=======================================================================
414
415 bool SMESHGUI_Selection::isNumFunctor( int ind ) const
416 {
417   bool result = false;
418   SMESH_Actor* actor = getActor( ind );
419   if ( actor ) {
420     switch( actor->GetControlMode() ) {
421     case SMESH_Actor::eLength:
422     case SMESH_Actor::eLength2D:
423     case SMESH_Actor::eDeflection2D:
424     case SMESH_Actor::eMultiConnection:
425     case SMESH_Actor::eMultiConnection2D:
426     case SMESH_Actor::eArea:
427     case SMESH_Actor::eVolume3D:
428     case SMESH_Actor::eMaxElementLength2D:
429     case SMESH_Actor::eMaxElementLength3D:
430     case SMESH_Actor::eTaper:
431     case SMESH_Actor::eAspectRatio:
432     case SMESH_Actor::eAspectRatio3D:
433     case SMESH_Actor::eMinimumAngle:
434     case SMESH_Actor::eWarping:
435     case SMESH_Actor::eSkew:
436       result = true;
437       break;
438     default:
439       break;
440     }
441   }
442   return result;
443 }
444
445 //=======================================================================
446 //function : facesOrientationMode
447 //purpose  :
448 //=======================================================================
449
450 QString SMESHGUI_Selection::facesOrientationMode( int ind ) const
451 {
452   SMESH_Actor* actor = getActor( ind );
453   if ( actor ) {
454     return actor->GetFacesOriented() ? "IsOriented" : "IsNotOriented";
455   }
456   return "Unknown";
457 }
458
459 //=======================================================================
460 //function : isAutoColor
461 //purpose  : 
462 //=======================================================================
463
464 bool SMESHGUI_Selection::isAutoColor( int ind ) const
465 {
466   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
467   {
468     _PTR(SObject) sobj = SMESH::getStudy()->FindObjectID( entry( ind ).toLatin1().data() );
469     CORBA::Object_var obj = SMESH::SObjectToObject( sobj );
470
471     if ( !CORBA::is_nil( obj ) ) {
472       SMESH::SMESH_Mesh_var mesh = SMESH::SMESH_Mesh::_narrow( obj );
473       if ( !CORBA::is_nil( mesh ) )
474         return mesh->GetAutoColor();
475     }
476   }
477   return false;
478 }
479
480 //=======================================================================
481 //function : numberOfNodes
482 //purpose  : this method is actually used to check if an object is empty or not
483 //=======================================================================
484
485 int SMESHGUI_Selection::numberOfNodes( int ind ) const
486 {
487   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
488   {
489     _PTR(SObject) sobj = SMESH::getStudy()->FindObjectID( entry( ind ).toLatin1().data() );
490     CORBA::Object_var obj = SMESH::SObjectToObject( sobj );
491
492     if ( !CORBA::is_nil( obj ) ) {
493       SMESH::SMESH_Mesh_var mesh = SMESH::SMESH_Mesh::_narrow( obj );
494       if ( !CORBA::is_nil( mesh ) )
495         return mesh->NbNodes();
496       SMESH::SMESH_subMesh_var aSubMeshObj = SMESH::SMESH_subMesh::_narrow( obj );
497       if ( !CORBA::is_nil( aSubMeshObj ) )
498         return aSubMeshObj->GetNumberOfNodes(true);
499       SMESH::SMESH_GroupBase_var aGroupObj = SMESH::SMESH_GroupBase::_narrow( obj );
500       if ( !CORBA::is_nil( aGroupObj ) )
501         return aGroupObj->IsEmpty() ? 0 : 1; // aGroupObj->Size();
502     }
503   }
504   return 0;
505 }
506
507 //================================================================================
508 /*!
509  * \brief return dimension of elements of the selected object
510  *
511  *  \retval int - 0 for 0D elements, -1 for an empty object (the rest as usual)
512  */
513 //================================================================================
514
515 int SMESHGUI_Selection::dim( int ind ) const
516 {
517   int dim = -1;
518   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
519   {
520     _PTR(SObject) sobj = SMESH::getStudy()->FindObjectID( entry( ind ).toLatin1().data() );
521     CORBA::Object_var obj = SMESH::SObjectToObject( sobj );
522
523     if ( !CORBA::is_nil( obj ) ) {
524       SMESH::SMESH_IDSource_var idSrc = SMESH::SMESH_IDSource::_narrow( obj );
525       if ( !CORBA::is_nil( idSrc ) )
526       {
527         SMESH::array_of_ElementType_var types = idSrc->GetTypes();
528         for ( size_t i = 0; i < types->length(); ++ i) {
529           switch ( types[i] ) {
530           case SMESH::EDGE  : dim = std::max( dim, 1 ); break;
531           case SMESH::FACE  : dim = std::max( dim, 2 ); break;
532           case SMESH::VOLUME: dim = std::max( dim, 3 ); break;
533           case SMESH::ELEM0D: dim = std::max( dim, 0 ); break;
534           case SMESH::BALL  : dim = std::max( dim, 0 ); break;
535           default: break;
536           }
537         }
538       }
539     }
540   }
541   return dim;
542 }
543
544 //=======================================================================
545 //function : isComputable
546 //purpose  : return true for a ready-to-compute mesh
547 //=======================================================================
548
549 bool SMESHGUI_Selection::isComputable( int ind ) const
550 {
551   if ( ind >= 0 && ind < myTypes.count() && ( myTypes[ind] == "Mesh" ||
552                                               myTypes[ind].startsWith("Mesh " )))
553   {
554     QMap<int,int> modeMap;
555     _PTR(SObject) meshSO = SMESH::getStudy()->FindObjectID( entry( ind ).toLatin1().data() );
556
557     _PTR(SComponent) component = meshSO->GetFatherComponent();
558     if ( meshSO->Depth() - component->Depth() > 1 ) // sub-mesh, get a mesh
559       while ( meshSO->Depth() - component->Depth() > 1 )
560         meshSO = meshSO->GetFather();
561
562     SMESHGUI_PrecomputeOp::getAssignedAlgos( meshSO, modeMap );
563     return modeMap.size() > 0;
564   }
565   return false;
566 }
567
568 //=======================================================================
569 //function : isPreComputable
570 //purpose  : returns true for a mesh with algorithms
571 //=======================================================================
572
573 bool SMESHGUI_Selection::isPreComputable( int ind ) const
574 {
575   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] == "Mesh" )
576   {
577     int maxDim = dim( ind );
578     if ( maxDim < 2 ) // we can preview 1D or 2D
579     {
580       QMap<int,int> modeMap;
581       _PTR(SObject) pMesh = SMESH::getStudy()->FindObjectID( entry( ind ).toLatin1().data() );
582       SMESHGUI_PrecomputeOp::getAssignedAlgos( pMesh, modeMap );
583       if ( modeMap.size() > 1 )
584         return (( modeMap.contains( SMESH::DIM_3D )) ||
585                 ( modeMap.contains( SMESH::DIM_2D ) && maxDim < 1 ));
586     }
587   }
588   return false;
589 }
590
591 //=======================================================================
592 //function : hasGeomReference
593 //purpose  : returns true for a mesh or sub-mesh on geometry
594 //=======================================================================
595
596 bool SMESHGUI_Selection::hasGeomReference( int ind ) const
597 {
598   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
599   {
600     _PTR(SObject) so = SMESH::getStudy()->FindObjectID( entry( ind ).toLatin1().data() );
601     GEOM::GEOM_Object_var shape = SMESH::GetShapeOnMeshOrSubMesh( so );
602     return !shape->_is_nil();
603   }
604   return false;
605 }
606
607 //=======================================================================
608 //function : isEditableHyp
609 //purpose  : 
610 //=======================================================================
611
612 bool SMESHGUI_Selection::isEditableHyp( int ind ) const
613 {
614   bool isEditable = true;
615   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] == "Hypothesis" )
616   {
617     _PTR(SObject) so = SMESH::getStudy()->FindObjectID( entry( ind ).toLatin1().data() );
618     SMESH::SMESH_Hypothesis_var hyp = SMESH::SObjectToInterface<SMESH::SMESH_Hypothesis>( so );
619     if ( !hyp->_is_nil() )
620     {
621       isEditable = hyp->HasParameters();
622     }
623   }
624   return isEditable;
625 }
626
627 //=======================================================================
628 //function : isVisible
629 //purpose  : 
630 //=======================================================================
631
632 bool SMESHGUI_Selection::isVisible( int ind ) const
633 {
634   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
635   {
636     SMESH_Actor* actor = SMESH::FindActorByEntry( entry( ind ).toLatin1().data() );
637     if ( actor && actor->hasIO() ) {
638       if ( SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView() )
639         return aViewWindow->isVisible( actor->getIO() );
640     }
641   }
642   return false;
643 }
644
645 //=======================================================================
646 //function : hasChildren
647 //purpose  : 
648 //=======================================================================
649
650 bool SMESHGUI_Selection::hasChildren( int ind ) const
651 {
652   if ( ind >= 0 )
653   {
654     _PTR(SObject) sobj = SMESH::getStudy()->FindObjectID( entry( ind ).toLatin1().data() );
655     if ( sobj ) 
656       return SMESH::getStudy()->GetUseCaseBuilder()->HasChildren( sobj );
657   }
658   return false;
659 }
660
661 //=======================================================================
662 //function : hasChildren
663 //purpose  : 
664 //=======================================================================
665
666 int SMESHGUI_Selection::nbChildren( int ind ) const
667 {
668   int nb = 0;
669   if ( ind >= 0 )
670   {
671     _PTR(Study) study = SMESH::getStudy();
672     _PTR(SObject) sobj = study->FindObjectID( entry( ind ).toLatin1().data() );
673     if ( sobj && study->GetUseCaseBuilder()->IsUseCaseNode( sobj ) ) {
674       _PTR(UseCaseIterator) it = study->GetUseCaseBuilder()->GetUseCaseIterator( sobj );
675       for ( it->Init( false ); it->More(); it->Next() ) nb++;
676     }
677   }
678   return nb;
679 }
680
681 //=======================================================================
682 //function : isContainer
683 //purpose  : 
684 //=======================================================================
685
686 bool SMESHGUI_Selection::isContainer( int ind ) const
687 {
688   return ind >= 0 && ind < myTypes.count() && myTypes[ind] == "Unknown";
689 }
690
691 //=======================================================================
692 //function : type
693 //purpose  : 
694 //=======================================================================
695
696 int SMESHGUI_Selection::type( const QString& entry )
697 {
698   int res = -1;
699   _PTR(SObject) obj = SalomeApp_Application::getStudy()->FindObjectID( entry.toLatin1().data() );
700   if ( obj ) {
701     _PTR(SObject) ref;
702     if ( obj->ReferencedObject( ref ) )
703       obj = ref;
704
705     _PTR(SObject) objFather = obj->GetFather();
706     _PTR(SComponent) objComponent = obj->GetFatherComponent();
707
708     if ( objComponent->ComponentDataType() == "SMESH" ) {
709       if ( objComponent->GetIOR() == obj->GetIOR() ) {
710         res = SMESH::COMPONENT;
711       }
712       else {
713         int aLevel = obj->Depth() - objComponent->Depth(),
714           aFTag = objFather->Tag(),
715           anOTag = obj->Tag();
716
717         switch ( aLevel )
718         {
719         case 1:
720           if ( anOTag >= SMESH::Tag_FirstMeshRoot )
721             res = SMESH::MESH;
722           break;
723         case 2:
724           switch ( aFTag )
725           {
726           case SMESH::Tag_HypothesisRoot: res = SMESH::HYPOTHESIS; break;
727           case SMESH::Tag_AlgorithmsRoot: res = SMESH::ALGORITHM;  break;
728           default: break;
729           }
730           break;
731         case 3:
732           switch ( aFTag )
733           {
734           case SMESH::Tag_SubMeshOnVertex:   res = SMESH::SUBMESH_VERTEX;   break;
735           case SMESH::Tag_SubMeshOnEdge:     res = SMESH::SUBMESH_EDGE;     break;
736           case SMESH::Tag_SubMeshOnFace:     res = SMESH::SUBMESH_FACE;     break;
737           case SMESH::Tag_SubMeshOnSolid:    res = SMESH::SUBMESH_SOLID;    break;
738           case SMESH::Tag_SubMeshOnCompound: res = SMESH::SUBMESH_COMPOUND; break;
739           default:
740             if ( aFTag >= SMESH::Tag_FirstGroup) res = SMESH::GROUP;
741             else                                 res = SMESH::SUBMESH;
742             break;
743           }
744           break;
745         }
746       }
747     }
748   }
749   return res;
750 }
751
752 //=======================================================================
753 //function : typeName
754 //purpose  : 
755 //=======================================================================
756
757 QString SMESHGUI_Selection::typeName( const int t )
758 {
759   QString res = "Unknown";
760   switch( t )
761   {
762   case SMESH::HYPOTHESIS:
763     res = "Hypothesis"; break;
764   case SMESH::ALGORITHM:
765     res = "Algorithm"; break;
766   case SMESH::MESH:
767     res = "Mesh"; break;
768   case SMESH::SUBMESH:
769     res = "SubMesh"; break;
770   case SMESH::MESHorSUBMESH:
771     res = "Mesh or submesh"; break;
772   case SMESH::SUBMESH_VERTEX:
773     res = "Mesh vertex"; break;
774   case SMESH::SUBMESH_EDGE:
775     res = "Mesh edge"; break;
776   case SMESH::SUBMESH_FACE:
777     res = "Mesh face"; break;
778   case SMESH::SUBMESH_SOLID:
779     res = "Mesh solid"; break;
780   case SMESH::SUBMESH_COMPOUND:
781     res = "Mesh compound"; break;
782   case SMESH::GROUP:
783     res = "Group"; break;
784   case SMESH::COMPONENT:
785     res = "Component"; break;
786   default:
787      break;
788   }
789   return res;
790 }
791
792 bool SMESHGUI_Selection::isImported( const int ind ) const
793 {
794   bool res = false;
795   _PTR(SObject) sobj = SMESH::getStudy()->FindObjectID( entry( ind ).toLatin1().constData() );
796   if ( sobj )
797   {
798     SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow( SMESH::SObjectToObject( sobj ) );
799     if( !aMesh->_is_nil() )
800     {
801       SMESH::MedFileInfo_var inf = aMesh->GetMEDFileInfo();
802       res = strlen( (char*)inf->fileName ) > 0;
803     }
804   }
805   return res;
806 }
807
808 //=======================================================================
809 //function : guiState
810 //purpose  : 
811 //=======================================================================
812
813 int SMESHGUI_Selection::guiState()
814 {
815   return SMESHGUI::GetSMESHGUI() ? SMESHGUI::GetSMESHGUI()->GetState() : -1;
816 }
817
818 //=======================================================================
819 //function : groupType
820 //purpose  : 
821 //=======================================================================
822
823 QString SMESHGUI_Selection::groupType( int ind ) const
824 {
825   _PTR(SObject) sobj = SMESH::getStudy()->FindObjectID( entry( ind ).toLatin1().constData() );
826   if ( sobj )
827   {
828     SMESH::SMESH_Group_var g = SMESH::SObjectToInterface<SMESH::SMESH_Group>( sobj );
829     if ( !CORBA::is_nil( g ) )
830       return "Group";
831     SMESH::SMESH_GroupOnGeom_var gog = SMESH::SObjectToInterface<SMESH::SMESH_GroupOnGeom>( sobj );
832     if( !CORBA::is_nil( gog ) )
833       return "GroupOnGeom";
834     SMESH::SMESH_GroupOnFilter_var gof = SMESH::SObjectToInterface<SMESH::SMESH_GroupOnFilter>( sobj );
835     if ( !CORBA::is_nil( gof ) )
836       return "GroupOnFilter";
837   }
838   return "";
839 }