Salome HOME
DCQ : Merge with Ecole_Ete_a6.
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_TransparencyDlg.cxx
index 0ab482433cb853bab12709872ec6a7e9104417f5..f42405abccb624b0f5331847469ab6757782e19a 100644 (file)
@@ -31,21 +31,44 @@ using namespace std;
 #include "SMESHGUI.h"
 
 // QT Includes
-#include <qframe.h>
 #include <qlabel.h>
 #include <qpushbutton.h>
 #include <qslider.h>
 #include <qlayout.h>
-#include <qvariant.h>
-#include <qtooltip.h>
-#include <qwhatsthis.h>
-#include <qapplication.h>
 #include <qgroupbox.h>
 
 #include "VTKViewer_ViewFrame.h"
 #include "VTKViewer_RenderWindowInteractor.h"
 #include "QAD_RightFrame.h"
+#include "QAD_WaitCursor.h"
 #include "SALOME_ListIteratorOfListIO.hxx"
+#include "SMESH_Actor.h"
+#include "SALOME_Selection.h"
+#include "SALOME_InteractiveObject.hxx"
+
+static SMESH_Actor* FindActorByEntry(const char* theEntry)
+{
+  QAD_Study* aStudy = SMESHGUI::GetSMESHGUI()->GetActiveStudy();
+  QAD_StudyFrame *aStudyFrame = aStudy->getActiveStudyFrame();
+  VTKViewer_ViewFrame* aViewFrame = dynamic_cast<VTKViewer_ViewFrame*>( aStudyFrame->getRightFrame()->getViewFrame() );
+
+  if(aViewFrame){
+    vtkRenderer *aRenderer = aViewFrame->getRenderer();
+    vtkActorCollection *aCollection = aRenderer->GetActors();
+    aCollection->InitTraversal();
+    while(vtkActor *anAct = aCollection->GetNextActor()){
+      if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
+       if(anActor->hasIO()){
+         Handle(SALOME_InteractiveObject) anIO = anActor->getIO();
+         if(anIO->hasEntry() && strcmp(anIO->getEntry(),theEntry) == 0){
+           return anActor;
+         }
+       }
+      }
+    }
+  }
+  return NULL;
+}
 
 //=================================================================================
 // class    : SMESHGUI_TransparencyDlg()
@@ -54,38 +77,18 @@ using namespace std;
 //=================================================================================
 SMESHGUI_TransparencyDlg::SMESHGUI_TransparencyDlg( QWidget* parent,
                                                    const char* name,
-                                                   SALOME_Selection* Sel,
                                                    bool modal,
                                                    WFlags fl )
-  : QDialog( parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu )
+  : QDialog( parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | WDestructiveClose )
 {
   if ( !name )
     setName( "SMESHGUI_TransparencyDlg" );
-  resize( 152, 107 ); 
   setCaption( tr( "SMESH_TRANSPARENCY_TITLE"  ) );
   setSizeGripEnabled( TRUE );
-  SMESHGUI_TransparencyDlgLayout = new QGridLayout( this ); 
+  QGridLayout* SMESHGUI_TransparencyDlgLayout = new QGridLayout( this ); 
   SMESHGUI_TransparencyDlgLayout->setSpacing( 6 );
   SMESHGUI_TransparencyDlgLayout->setMargin( 11 );
 
-  /*************************************************************************/
-  QGroupBox* GroupButtons = new QGroupBox( this, "GroupButtons" );
-  GroupButtons->setColumnLayout(0, Qt::Vertical );
-  GroupButtons->layout()->setSpacing( 0 );
-  GroupButtons->layout()->setMargin( 0 );
-  QGridLayout* GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
-  GroupButtonsLayout->setAlignment( Qt::AlignTop );
-  GroupButtonsLayout->setSpacing( 6 );
-  GroupButtonsLayout->setMargin( 11 );
-
-  buttonOk = new QPushButton( GroupButtons, "buttonOk" );
-  buttonOk->setText( tr( "GEOM_BUT_OK" ) );
-  buttonOk->setAutoDefault( TRUE );
-  buttonOk->setDefault( TRUE );
-  GroupButtonsLayout->addItem( new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum), 0, 0 );
-  GroupButtonsLayout->addWidget( buttonOk, 0, 1 );
-  GroupButtonsLayout->addItem( new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum), 0, 2 );
-
   /*************************************************************************/
   QGroupBox* GroupC1 = new QGroupBox( this, "GroupC1" );
   GroupC1->setColumnLayout(0, Qt::Vertical );
@@ -96,59 +99,82 @@ SMESHGUI_TransparencyDlg::SMESHGUI_TransparencyDlg( QWidget* parent,
   GroupC1Layout->setSpacing( 6 );
   GroupC1Layout->setMargin( 11 );
   
-  TextLabelOpaque = new QLabel( GroupC1, "TextLabelOpaque" );
-  TextLabelOpaque->setText( tr( "SMESH_TRANSPARENCY_OPAQUE"  ) );
-  TextLabelOpaque->setAlignment( int( QLabel::AlignLeft ) );
-  GroupC1Layout->addWidget( TextLabelOpaque, 0, 0 );
-  GroupC1Layout->addItem( new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum ), 0, 1 );
-  
   TextLabelTransparent = new QLabel( GroupC1, "TextLabelTransparent" );
   TextLabelTransparent->setText( tr( "SMESH_TRANSPARENCY_TRANSPARENT"  ) );
-  TextLabelTransparent->setAlignment( int( QLabel::AlignRight ) );
-  GroupC1Layout->addWidget( TextLabelTransparent, 0, 2 );
+  TextLabelTransparent->setAlignment( AlignLeft );
+  GroupC1Layout->addWidget( TextLabelTransparent, 0, 0 );
+  
+  ValueLab = new QLabel( GroupC1, "ValueLab" );
+  ValueLab->setAlignment( AlignCenter );
+  ValueLab->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
+  QFont fnt = ValueLab->font(); fnt.setBold( true ); ValueLab->setFont( fnt );
+  GroupC1Layout->addWidget( ValueLab, 0, 1 );
+
+  TextLabelOpaque = new QLabel( GroupC1, "TextLabelOpaque" );
+  TextLabelOpaque->setText( tr( "SMESH_TRANSPARENCY_OPAQUE"  ) );
+  TextLabelOpaque->setAlignment( AlignRight );
+  GroupC1Layout->addWidget( TextLabelOpaque, 0, 2 );
   
   Slider1 = new QSlider( 0, 10, 1, 5, Horizontal, GroupC1, "Slider1" );
+  Slider1->setFocusPolicy( QWidget::NoFocus );
   Slider1->setMinimumSize( 300, 0 );
-  Slider1->setTickmarks( QSlider::Left );
+  Slider1->setTickmarks( QSlider::Above );
+  Slider1->setTickInterval( 10 );
+  Slider1->setTracking( true );
+  Slider1->setMinValue( 0 ) ;
+  Slider1->setMaxValue( 100 );
+  Slider1->setLineStep( 1 );
+  Slider1->setPageStep( 10 );
   GroupC1Layout->addMultiCellWidget( Slider1, 1, 1, 0, 2 );
 
+  /*************************************************************************/
+  QGroupBox* GroupButtons = new QGroupBox( this, "GroupButtons" );
+  GroupButtons->setColumnLayout(0, Qt::Vertical );
+  GroupButtons->layout()->setSpacing( 0 );
+  GroupButtons->layout()->setMargin( 0 );
+  QGridLayout* GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
+  GroupButtonsLayout->setAlignment( Qt::AlignTop );
+  GroupButtonsLayout->setSpacing( 6 );
+  GroupButtonsLayout->setMargin( 11 );
+
+  buttonOk = new QPushButton( GroupButtons, "buttonOk" );
+  buttonOk->setText( tr( "SMESH_BUT_CLOSE" ) );
+  buttonOk->setAutoDefault( TRUE );
+  buttonOk->setDefault( TRUE );
+  GroupButtonsLayout->addItem( new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum), 0, 0 );
+  GroupButtonsLayout->addWidget( buttonOk, 0, 1 );
+  GroupButtonsLayout->addItem( new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum), 0, 2 );
+
   SMESHGUI_TransparencyDlgLayout->addWidget( GroupC1,      0, 0 );
   SMESHGUI_TransparencyDlgLayout->addWidget( GroupButtons, 1, 0 );
   
-  /* Initialisations */
-  this->mySMESHGUI = SMESHGUI::GetSMESHGUI() ;
-  this->mySel = Sel ;
-  
-  /* First call valueChanged() method for initialisation               */
-  /* The default value of transparency will change with the selection  */
-  this->myFirstInit = true ;
-//  Slider1->setMaxValue( 10 );
-//  Slider1->setValue( 5 ) ;
+  mySelection = SALOME_Selection::Selection( SMESHGUI::GetSMESHGUI()->GetActiveStudy()->getSelection());
 
-
-  this->ValueHasChanged( Slider1->value() ) ;
+  // Initial state
+  this->onSelectionChanged() ;
   
   // signals and slots connections : after ValueHasChanged()
   connect( buttonOk, SIGNAL( clicked() ),         this, SLOT( ClickOnOk() ) );
-  connect( Slider1,  SIGNAL( valueChanged(int) ), this, SLOT( ValueHasChanged(int) ) );
+  connect( Slider1,  SIGNAL( valueChanged(int) ), this, SLOT( SetTransparency() ) );
+  connect( Slider1,  SIGNAL( sliderMoved(int) ),  this, SLOT( ValueHasChanged() ) );
+  connect( SMESHGUI::GetSMESHGUI(), SIGNAL ( SignalCloseAllDialogs() ), this, SLOT( ClickOnOk() ) ) ;
+  connect( mySelection,  SIGNAL( currentSelectionChanged() ), this, SLOT( onSelectionChanged() ) );
   
   /* Move widget on the botton right corner of main widget */
   int x, y ;
-  mySMESHGUI->DefineDlgPosition( this, x, y ) ;
+  SMESHGUI::GetSMESHGUI()->DefineDlgPosition( this, x, y ) ;
   this->move( x, y ) ;
-  this->show() ; /* Displays this Dialog */
+  this->show();
 }
 
 
-
-
 //=================================================================================
 // function : ~SMESHGUI_TransparencyDlg()
 // purpose  :
 //=================================================================================
 SMESHGUI_TransparencyDlg::~SMESHGUI_TransparencyDlg()
 {
-    // no need to delete child widgets, Qt does it all for us
+  // no need to delete child widgets, Qt does it all for us
 }
 
 
@@ -158,51 +184,79 @@ SMESHGUI_TransparencyDlg::~SMESHGUI_TransparencyDlg()
 //=======================================================================
 void SMESHGUI_TransparencyDlg::ClickOnOk()
 {
-  accept() ;
-  return ;
+  close();
 }
 
 
-//=======================================================================
-// function : ClickOnClose()
-// purpose  :
-//=======================================================================
-void SMESHGUI_TransparencyDlg::ClickOnClose()
+//=================================================================================
+// function : SetTransparency()
+// purpose  : Called when value of slider change
+//          : or the first time as initilisation
+//=================================================================================
+void SMESHGUI_TransparencyDlg::SetTransparency()
 {
-  accept() ;
-  return ;
+  if ( SMESHGUI::GetSMESHGUI()->GetActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_VTK ) {
+    QAD_WaitCursor wc;
+    float opacity = this->Slider1->value() / 100. ;
+    SALOME_ListIteratorOfListIO It( mySelection->StoredIObjects() );
+    for( ;It.More(); It.Next() ) {
+      Handle(SALOME_InteractiveObject) IOS = It.Value();
+      SMESH_Actor* anActor = FindActorByEntry(IOS->getEntry());
+      if ( anActor )
+       anActor->SetOpacity( opacity );
+    }
+    SMESHGUI::GetSMESHGUI()->GetActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame()->Repaint();
+  }
+  ValueHasChanged();
 }
 
-
 //=================================================================================
 // function : ValueHasChanged()
-// purpose  : Called when value of slider change
-//          : or the first time as initilisation
+// purpose  : Called when user moves a slider
 //=================================================================================
-void SMESHGUI_TransparencyDlg::ValueHasChanged( int newValue )
+void SMESHGUI_TransparencyDlg::ValueHasChanged()
 {
+  ValueLab->setText( QString::number( this->Slider1->value() ) + "%" );
+}
 
-  if ( mySMESHGUI->GetActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_VTK ) {
-    VTKViewer_RenderWindowInteractor* myRenderInter= ((VTKViewer_ViewFrame*)mySMESHGUI->GetActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getRWInteractor();
-    SALOME_ListIteratorOfListIO It( this->mySel->StoredIObjects() );
-    Handle(SALOME_InteractiveObject) FirstIOS =  mySel->firstIObject();
-    if( !FirstIOS.IsNull() ) {
-      /* The first time as initialisation */
-      if( this->myFirstInit ) {        
-       this->myFirstInit = false ;
-       float transp = ( myRenderInter->GetTransparency(FirstIOS))*10.0 ;
-        this->Slider1->setValue( int(transp) ) ;
-       return;
+//=================================================================================
+// function : onSelectionChanged()
+// purpose  : Called when selection is changed
+//=================================================================================
+void SMESHGUI_TransparencyDlg::onSelectionChanged()
+{
+  if ( SMESHGUI::GetSMESHGUI()->GetActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_VTK ) {
+    int opacity = 100;
+    if ( mySelection->IObjectCount() == 1 ) {
+      Handle(SALOME_InteractiveObject) FirstIOS = mySelection->firstIObject();
+      if( !FirstIOS.IsNull() ) {
+       SMESH_Actor* anActor = FindActorByEntry( FirstIOS->getEntry() );
+       if ( anActor )
+         opacity = int( anActor->GetOpacity() * 100. + 0.5 );
       }
     }
-    
-    QApplication::setOverrideCursor( Qt::waitCursor );
-    for( ;It.More(); It.Next() ) {
-      Handle(SALOME_InteractiveObject) IOS = It.Value();
-      myRenderInter->SetTransparency( IOS, newValue/10.0 );
+    else if ( mySelection->IObjectCount() > 1 ) {
+      SALOME_ListIteratorOfListIO It( mySelection->StoredIObjects() );
+      int setOp = -1;
+      for ( ; It.More(); It.Next() ) {
+       Handle(SALOME_InteractiveObject) IO = It.Value();
+       if( !IO.IsNull() ) {
+         SMESH_Actor* anActor = FindActorByEntry( IO->getEntry() );
+         if ( anActor ) {
+           int op = int( anActor->GetOpacity() * 100. + 0.5 );
+           if ( setOp < 0 )
+             setOp = op;
+           else if ( setOp != op ) {
+             setOp = 100;
+             break;
+           }
+         }
+       }
+      }
+      if ( setOp >= 0 )
+       opacity = setOp;
     }
-    QApplication::restoreOverrideCursor();
+    Slider1->setValue( opacity ) ;
   }
-  QApplication::restoreOverrideCursor();
-  return ;
+  ValueHasChanged();
 }