Salome HOME
Merge remote branch 'origin/V8_5_asterstudy'
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_ClippingDlg.cxx
index e9bf24bc05584deec7bcd6abc85ec8d85ba75c56..18aede561ad2457ed2965a9348177aa63d491b1c 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
@@ -231,34 +231,37 @@ SMESH::OrientedPlane::~OrientedPlane()
   myPlaneSource->Delete();
 }
 
-/*!
-  Definition of class ActorItem
- */
-class ActorItem : public QListWidgetItem
+namespace
 {
-public:
-  ActorItem( SMESH_Actor* theActor, const QString& theName, QListWidget* theListWidget ) :
-    QListWidgetItem( theName, theListWidget ),
-    myActor( theActor ) {}
+  /*!
+    Definition of class ActorItem
+  */
+  class ActorItem : public QListWidgetItem
+  {
+  public:
+    ActorItem( SMESH_Actor* theActor, const QString& theName, QListWidget* theListWidget ) :
+      QListWidgetItem( theName, theListWidget ),
+      myActor( theActor ) {}
 
-  SMESH_Actor* getActor() const { return myActor; }
+    SMESH_Actor* getActor() const { return myActor; }
 
-private:
-  SMESH_Actor* myActor;
-};
+  private:
+    SMESH_Actor* myActor;
+  };
 
-/*!
-  Definition of class TSetVisibility
- */
-struct TSetVisibility {
-  // Set visibility of cutting plane
-  TSetVisibility(int theIsVisible): myIsVisible(theIsVisible){}
-  void operator()(SMESH::TPlaneData& thePlaneData){
-    bool anIsEmpty = thePlaneData.ActorList.empty();
-    thePlaneData.Plane.GetPointer()->myActor->SetVisibility(myIsVisible && !anIsEmpty);
-  }
-  int myIsVisible;
-};
+  /*!
+    Definition of class TSetVisibility
+  */
+  struct TSetVisibility {
+    // Set visibility of cutting plane
+    TSetVisibility(int theIsVisible): myIsVisible(theIsVisible){}
+    void operator()(SMESH::TPlaneData& thePlaneData){
+      bool anIsEmpty = thePlaneData.ActorList.empty();
+      thePlaneData.Plane.GetPointer()->myActor->SetVisibility(myIsVisible && !anIsEmpty);
+    }
+    int myIsVisible;
+  };
+}
 
 /*********************************************************************************
  *********************      class SMESHGUI_ClippingDlg      *********************
@@ -476,7 +479,7 @@ SMESHGUI_ClippingDlg::SMESHGUI_ClippingDlg( SMESHGUI* theModule, SVTK_ViewWindow
   QFont fnt = SpinSliderDistance->font(); fnt.setBold( true ); SpinSliderDistance->setFont( fnt );
   GroupParametersLayout->addWidget( SpinSliderDistance, 1, 1 );
   
-  QString aUnitRot = "\xB0";
+  QString aUnitRot = QString(QChar(0xB0));
 
   TextLabelRotation1 = new QLabel( tr("ROTATION_AROUND_X_Y2Z"), GroupParameters );
   TextLabelRotation1->setObjectName( "TextLabelRotation1" );
@@ -549,7 +552,7 @@ SMESHGUI_ClippingDlg::SMESHGUI_ClippingDlg( SMESHGUI* theModule, SVTK_ViewWindow
 
   myIsSelectPlane = false;
 
-  myHelpFileName = "clipping_page.html";
+  myHelpFileName = "clipping.html";
 
   // signals and slots connections :
   connect( ComboBoxPlanes, SIGNAL( activated( int ) ), this, SLOT( onSelectPlane( int ) ) );
@@ -601,12 +604,12 @@ SMESHGUI_ClippingDlg::~SMESHGUI_ClippingDlg()
   if (myViewWindow)
     SMESH::RenderViewWindow(myViewWindow);
 
-  for( int i=0; i< myPlanes.size(); i++ ) {
+  for ( size_t i = 0; i < myPlanes.size(); i++ ) {
     SMESH::TPlaneData aPlaneData = myPlanes[i];
     aPlaneData.Plane->Delete();
   }
 
-  if (myPreviewWidget) {
+  if ( myPreviewWidget ) {
     myPreviewWidget->Off();
     myPreviewWidget->Delete();
   }
@@ -629,7 +632,7 @@ double SMESHGUI_ClippingDlg::getDistance() const
 */
 void SMESHGUI_ClippingDlg::setDistance( const double theDistance )
 {
-  SpinSliderDistance->setValue( theDistance*100 );
+  SpinSliderDistance->setValue( theDistance );
 }
 
 /*!
@@ -713,46 +716,66 @@ vtkImplicitPlaneWidget* SMESHGUI_ClippingDlg::createPreviewWidget()
   return aPlaneWgt;
 }
 
-/*!
-  Translate two angles of plane to normal
-*/
-void rotationToNormal ( double theRotation[2],
-                        int theOrientation,
-                        double theNormal[3],
-                        double theDir[2][3] )
+namespace
 {
-  static double aCoeff = M_PI/180.0;
-
-  double anU[2] = { cos( aCoeff * theRotation[0] ), cos( aCoeff * theRotation[1] ) };
-  double aV[2] = { sqrt( 1.0 - anU[0]*anU[0] ), sqrt( 1.0 - anU[1] * anU[1] ) };
-  aV[0] = theRotation[0] > 0? aV[0]: -aV[0];
-  aV[1] = theRotation[1] > 0? aV[1]: -aV[1];
+  /*!
+    Translate two angles of plane to normal
+  */
+  void rotationToNormal ( double theRotation[2],
+                          int theOrientation,
+                          double theNormal[3],
+                          double theDir[2][3] )
+  {
+    static double aCoeff = M_PI/180.0;
+
+    double anU[2] = { cos( aCoeff * theRotation[0] ), cos( aCoeff * theRotation[1] ) };
+    double aV[2] = { sqrt( 1.0 - anU[0]*anU[0] ), sqrt( 1.0 - anU[1] * anU[1] ) };
+    aV[0] = theRotation[0] > 0? aV[0]: -aV[0];
+    aV[1] = theRotation[1] > 0? aV[1]: -aV[1];
+
+    switch ( theOrientation ) {
+    case 0:
+    case 1:
+      theDir[0][1] = anU[0];
+      theDir[0][2] = aV[0];
+      theDir[1][0] = anU[1];
+      theDir[1][2] = aV[1];
+      break;
+    case 2:
+      theDir[0][2] = anU[0];
+      theDir[0][0] = aV[0];
+      theDir[1][1] = anU[1];
+      theDir[1][0] = aV[1];
+      break;
+    case 3:
+      theDir[0][0] = anU[0];
+      theDir[0][1] = aV[0];
+      theDir[1][2] = anU[1];
+      theDir[1][1] = aV[1];
+      break;
+    }
 
-  switch ( theOrientation ) {
-  case 0:
-  case 1:
-    theDir[0][1] = anU[0];
-    theDir[0][2] = aV[0];
-    theDir[1][0] = anU[1];
-    theDir[1][2] = aV[1];
-    break;
-  case 2:
-    theDir[0][2] = anU[0];
-    theDir[0][0] = aV[0];
-    theDir[1][1] = anU[1];
-    theDir[1][0] = aV[1];
-    break;
-  case 3:
-    theDir[0][0] = anU[0];
-    theDir[0][1] = aV[0];
-    theDir[1][2] = anU[1];
-    theDir[1][1] = aV[1];
-    break;
+    vtkMath::Cross( theDir[1], theDir[0], theNormal );
+    vtkMath::Normalize( theNormal );
+    vtkMath::Cross( theNormal, theDir[1], theDir[0] );
   }
 
-  vtkMath::Cross( theDir[1], theDir[0], theNormal );
-  vtkMath::Normalize( theNormal );
-  vtkMath::Cross( theNormal, theDir[1], theDir[0] );
+  /*!
+   * \brief Return a name of a father mesh if any
+   */
+  QString getFatherName( _PTR(SObject)& theSObj )
+  {
+    _PTR(SComponent) objComponent = theSObj->GetFatherComponent();
+    const int theMeshDepth = 1 + objComponent->Depth();
+    if ( theSObj->Depth() <= theMeshDepth )
+      return QString(); // theSObj is a mesh
+
+    _PTR(SObject) sobj = theSObj->GetFather();
+    while ( sobj && sobj->Depth() > theMeshDepth )
+      sobj = sobj->GetFather();
+
+    return sobj ? sobj->GetName().c_str() : "";
+  }
 }
 
 /*!
@@ -766,7 +789,7 @@ bool SMESHGUI_ClippingDlg::AddPlane ( SMESH::TActorList       theActorList,
   double aDir[2][3] = {{0, 0, 0}, {0, 0, 0}};
   static double aCoeff = vtkMath::Pi()/180.0;
 
-  int anOrientation;
+  int anOrientation = 0;
   if ( thePlane->PlaneMode == SMESH::Absolute )
     anOrientation = thePlane->myAbsoluteOrientation;
   else if ( thePlane->PlaneMode == SMESH::Relative )
@@ -1045,11 +1068,7 @@ void SMESHGUI_ClippingDlg::updateActorList()
 {
   ActorList->clear();
 
-  SalomeApp_Study* anAppStudy = SMESHGUI::activeStudy();
-  if( !anAppStudy )
-    return;
-
-  _PTR(Study) aStudy = anAppStudy->studyDS();
+  _PTR(Study) aStudy = SMESH::getStudy();
   if( !aStudy )
     return;
 
@@ -1063,6 +1082,8 @@ void SMESHGUI_ClippingDlg::updateActorList()
   std::for_each( myPlanes.begin(),myPlanes.end(), TSetVisibility( PreviewCheckBox->isChecked() ) );
   aPlaneData.Plane.GetPointer()->myActor->SetVisibility( false );
 
+  std::map< std::string, QListWidgetItem* > itemMap; // used to sort items by entry
+
   VTK::ActorCollectionCopy aCopy( myViewWindow->getRenderer()->GetActors() );
   vtkActorCollection* anAllActors = aCopy.GetActors();
   anAllActors->InitTraversal();
@@ -1084,13 +1105,24 @@ void SMESHGUI_ClippingDlg::updateActorList()
             }
           }
           QString aName = QString( aSObj->GetName().c_str() );
-          QListWidgetItem* anItem = new ActorItem( anActor, aName, ActorList );
+          QString aFatherName = getFatherName( aSObj );
+          if ( !aFatherName.isEmpty() )
+            aName = aFatherName + " / " + aName;
+          aName += QString(" (%1)").arg( aSObj->GetID().c_str() );
+          QListWidgetItem* anItem = new ActorItem( anActor, aName, 0 );
           anItem->setCheckState( anIsChecked ? Qt::Checked : Qt::Unchecked );
-          updateActorItem( anItem, true, false );
+          itemMap.insert( std::make_pair( aSObj->GetID(), anItem ));
         }
       }
     }
   }
+  std::map< std::string, QListWidgetItem* >::iterator s2i = itemMap.begin();
+  for ( ; s2i != itemMap.end(); ++s2i )
+  {
+    QListWidgetItem* anItem = s2i->second;
+    ActorList->addItem( anItem );
+  }
+  updateActorItem( 0, true, false );
 }
 
 /*!
@@ -1133,9 +1165,14 @@ void SMESHGUI_ClippingDlg::updateActorItem( QListWidgetItem* theItem,
         else if( theItem->checkState() == Qt::Unchecked && anIsPushed )
           anActorList.remove( anActor );
 
-        SMESH::ComputeBounds( anActorList, myBounds );
-        myPreviewWidget->PlaceWidget( myBounds[0], myBounds[1], myBounds[2],
-                                      myBounds[3], myBounds[4], myBounds[5] );
+        if( SMESH::ComputeBounds( anActorList, myBounds ) ) {
+          myPreviewWidget->PlaceWidget( myBounds[0], myBounds[1], myBounds[2],
+                                        myBounds[3], myBounds[4], myBounds[5] );
+          if( PreviewCheckBox->isChecked() )
+            myPreviewWidget->On();
+        }
+        else
+          myPreviewWidget->Off();
       }
     }
   }
@@ -1226,16 +1263,19 @@ void SMESHGUI_ClippingDlg::ClickOnNew()
     SMESH::OrientedPlane* aPlane = SMESH::OrientedPlane::New(myViewWindow);
     SMESH::TPlane aTPlane(aPlane);
     aPlane->PlaneMode = CurrentMode;
-    SMESH::TActorList anActorList;
+    SMESH::TActorList anActorList, aVisibleActorList;
     VTK::ActorCollectionCopy aCopy( myViewWindow->getRenderer()->GetActors() );
     vtkActorCollection* anAllActors = aCopy.GetActors();
     anAllActors->InitTraversal();
     while( vtkActor* aVTKActor = anAllActors->GetNextActor() )
       if( SMESH_Actor* anActor = SMESH_Actor::SafeDownCast( aVTKActor ) )
+      {
         anActorList.push_back( anActor );
-
-    SMESH::TPlaneData aPlaneData(aTPlane, anActorList);
-
+        if ( anActor->GetVisibility() )
+          aVisibleActorList.push_back( anActor );
+      }
+    SMESH::TPlaneData aPlaneData(aTPlane,
+                                 aVisibleActorList.empty() ? anActorList : aVisibleActorList);
     myPlanes.push_back(aPlaneData);
 
 
@@ -1244,9 +1284,15 @@ void SMESHGUI_ClippingDlg::ClickOnNew()
 
     bool anIsBlocked = ActorList->blockSignals( true );
 
-    SMESH::ComputeBounds( anActorList, myBounds );
-    myPreviewWidget->PlaceWidget( myBounds[0],myBounds[1],myBounds[2],
-                                  myBounds[3],myBounds[4],myBounds[5] );
+    if( SMESH::ComputeBounds( anActorList, myBounds ) ) {
+      myPreviewWidget->PlaceWidget( myBounds[0], myBounds[1], myBounds[2],
+                                    myBounds[3], myBounds[4], myBounds[5] );
+      if( PreviewCheckBox->isChecked() )
+        myPreviewWidget->On();
+    }
+    else
+      myPreviewWidget->Off();
+
     synchronize();
     SetCurrentPlaneParam();
 
@@ -1331,9 +1377,16 @@ void SMESHGUI_ClippingDlg::onSelectPlane ( int theIndex )
     }
   }
   myIsSelectPlane = false;
-  SMESH::ComputeBounds( aPlaneData.ActorList, myBounds );
-  myPreviewWidget->PlaceWidget( myBounds[0], myBounds[1], myBounds[2],
-                                myBounds[3], myBounds[4], myBounds[5] );
+
+  if( SMESH::ComputeBounds( aPlaneData.ActorList, myBounds ) ) {
+    myPreviewWidget->PlaceWidget( myBounds[0], myBounds[1], myBounds[2],
+                                  myBounds[3], myBounds[4], myBounds[5] );
+    if( PreviewCheckBox->isChecked() )
+      myPreviewWidget->On();
+  }
+  else
+    myPreviewWidget->Off();
+
   SetCurrentPlaneParam();
 
   // Actors
@@ -1418,7 +1471,7 @@ void SMESHGUI_ClippingDlg::SetCurrentPlaneParam()
   static double aCoeff = vtkMath::Pi()/180.0;
 
   double aRot[2] = { getRotation1(), getRotation2() };
-  int anOrient;
+  int anOrient = 0;
   if ( aPlane->PlaneMode == SMESH::Absolute )
     anOrient = CBAbsoluteOrientation->currentIndex();
   else if ( aPlane->PlaneMode == SMESH::Relative )
@@ -1605,8 +1658,8 @@ void SMESHGUI_ClippingDlg::absolutePlaneToRelative ( double theOrigin[3], double
   double eps = 0.0001;
 
   int anOrientation = CBRelativeOrientation->currentIndex();
-  double aDirection[3];
-  double aRotation1, aRotation2;
+  double aDirection[3] = { 0.,0.,0. };
+  double aRotation1 = 0, aRotation2 = 0;
   switch( anOrientation ) {
   case 0:
     aDirection[0] = theDir[0] + eps;
@@ -1779,11 +1832,9 @@ void SMESHGUI_ClippingDlg::ClickOnApply()
       aClippingPlaneInfoList.push_back( aClippingPlaneInfo );
     }
 
-    SMESH_Actor* anSMESHActor;
     anAllActors->InitTraversal();
     while( vtkActor* aVTKActor = anAllActors->GetNextActor() )
       if( SMESH_Actor* anActor = SMESH_Actor::SafeDownCast( aVTKActor ) ) {
-        anSMESHActor = anActor;
         anActor->SetOpenGLClippingPlane();
       }