Salome HOME
DCQ : Merge with Ecole_Ete_a6.
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_TransparencyDlg.cxx
1 //  SMESH SMESHGUI : GUI for SMESH component
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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SMESHGUI_TransparencyDlg.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SMESH
27 //  $Header$
28
29 using namespace std;
30 #include "SMESHGUI_TransparencyDlg.h"
31 #include "SMESHGUI.h"
32
33 // QT Includes
34 #include <qlabel.h>
35 #include <qpushbutton.h>
36 #include <qslider.h>
37 #include <qlayout.h>
38 #include <qgroupbox.h>
39
40 #include "VTKViewer_ViewFrame.h"
41 #include "VTKViewer_RenderWindowInteractor.h"
42 #include "QAD_RightFrame.h"
43 #include "QAD_WaitCursor.h"
44 #include "SALOME_ListIteratorOfListIO.hxx"
45 #include "SMESH_Actor.h"
46 #include "SALOME_Selection.h"
47 #include "SALOME_InteractiveObject.hxx"
48
49 static SMESH_Actor* FindActorByEntry(const char* theEntry)
50 {
51   QAD_Study* aStudy = SMESHGUI::GetSMESHGUI()->GetActiveStudy();
52   QAD_StudyFrame *aStudyFrame = aStudy->getActiveStudyFrame();
53   VTKViewer_ViewFrame* aViewFrame = dynamic_cast<VTKViewer_ViewFrame*>( aStudyFrame->getRightFrame()->getViewFrame() );
54
55   if(aViewFrame){
56     vtkRenderer *aRenderer = aViewFrame->getRenderer();
57     vtkActorCollection *aCollection = aRenderer->GetActors();
58     aCollection->InitTraversal();
59     while(vtkActor *anAct = aCollection->GetNextActor()){
60       if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
61         if(anActor->hasIO()){
62           Handle(SALOME_InteractiveObject) anIO = anActor->getIO();
63           if(anIO->hasEntry() && strcmp(anIO->getEntry(),theEntry) == 0){
64             return anActor;
65           }
66         }
67       }
68     }
69   }
70   return NULL;
71 }
72
73 //=================================================================================
74 // class    : SMESHGUI_TransparencyDlg()
75 // purpose  : 
76 //
77 //=================================================================================
78 SMESHGUI_TransparencyDlg::SMESHGUI_TransparencyDlg( QWidget* parent,
79                                                     const char* name,
80                                                     bool modal,
81                                                     WFlags fl )
82   : QDialog( parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | WDestructiveClose )
83 {
84   if ( !name )
85     setName( "SMESHGUI_TransparencyDlg" );
86   setCaption( tr( "SMESH_TRANSPARENCY_TITLE"  ) );
87   setSizeGripEnabled( TRUE );
88   QGridLayout* SMESHGUI_TransparencyDlgLayout = new QGridLayout( this ); 
89   SMESHGUI_TransparencyDlgLayout->setSpacing( 6 );
90   SMESHGUI_TransparencyDlgLayout->setMargin( 11 );
91
92   /*************************************************************************/
93   QGroupBox* GroupC1 = new QGroupBox( this, "GroupC1" );
94   GroupC1->setColumnLayout(0, Qt::Vertical );
95   GroupC1->layout()->setSpacing( 0 );
96   GroupC1->layout()->setMargin( 0 );
97   QGridLayout* GroupC1Layout = new QGridLayout( GroupC1->layout() );
98   GroupC1Layout->setAlignment( Qt::AlignTop );
99   GroupC1Layout->setSpacing( 6 );
100   GroupC1Layout->setMargin( 11 );
101   
102   TextLabelTransparent = new QLabel( GroupC1, "TextLabelTransparent" );
103   TextLabelTransparent->setText( tr( "SMESH_TRANSPARENCY_TRANSPARENT"  ) );
104   TextLabelTransparent->setAlignment( AlignLeft );
105   GroupC1Layout->addWidget( TextLabelTransparent, 0, 0 );
106   
107   ValueLab = new QLabel( GroupC1, "ValueLab" );
108   ValueLab->setAlignment( AlignCenter );
109   ValueLab->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
110   QFont fnt = ValueLab->font(); fnt.setBold( true ); ValueLab->setFont( fnt );
111   GroupC1Layout->addWidget( ValueLab, 0, 1 );
112
113   TextLabelOpaque = new QLabel( GroupC1, "TextLabelOpaque" );
114   TextLabelOpaque->setText( tr( "SMESH_TRANSPARENCY_OPAQUE"  ) );
115   TextLabelOpaque->setAlignment( AlignRight );
116   GroupC1Layout->addWidget( TextLabelOpaque, 0, 2 );
117   
118   Slider1 = new QSlider( 0, 10, 1, 5, Horizontal, GroupC1, "Slider1" );
119   Slider1->setFocusPolicy( QWidget::NoFocus );
120   Slider1->setMinimumSize( 300, 0 );
121   Slider1->setTickmarks( QSlider::Above );
122   Slider1->setTickInterval( 10 );
123   Slider1->setTracking( true );
124   Slider1->setMinValue( 0 ) ;
125   Slider1->setMaxValue( 100 );
126   Slider1->setLineStep( 1 );
127   Slider1->setPageStep( 10 );
128   GroupC1Layout->addMultiCellWidget( Slider1, 1, 1, 0, 2 );
129
130   /*************************************************************************/
131   QGroupBox* GroupButtons = new QGroupBox( this, "GroupButtons" );
132   GroupButtons->setColumnLayout(0, Qt::Vertical );
133   GroupButtons->layout()->setSpacing( 0 );
134   GroupButtons->layout()->setMargin( 0 );
135   QGridLayout* GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
136   GroupButtonsLayout->setAlignment( Qt::AlignTop );
137   GroupButtonsLayout->setSpacing( 6 );
138   GroupButtonsLayout->setMargin( 11 );
139
140   buttonOk = new QPushButton( GroupButtons, "buttonOk" );
141   buttonOk->setText( tr( "SMESH_BUT_CLOSE" ) );
142   buttonOk->setAutoDefault( TRUE );
143   buttonOk->setDefault( TRUE );
144   GroupButtonsLayout->addItem( new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum), 0, 0 );
145   GroupButtonsLayout->addWidget( buttonOk, 0, 1 );
146   GroupButtonsLayout->addItem( new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum), 0, 2 );
147
148   SMESHGUI_TransparencyDlgLayout->addWidget( GroupC1,      0, 0 );
149   SMESHGUI_TransparencyDlgLayout->addWidget( GroupButtons, 1, 0 );
150   
151   mySelection = SALOME_Selection::Selection( SMESHGUI::GetSMESHGUI()->GetActiveStudy()->getSelection());
152
153   // Initial state
154   this->onSelectionChanged() ;
155   
156   // signals and slots connections : after ValueHasChanged()
157   connect( buttonOk, SIGNAL( clicked() ),         this, SLOT( ClickOnOk() ) );
158   connect( Slider1,  SIGNAL( valueChanged(int) ), this, SLOT( SetTransparency() ) );
159   connect( Slider1,  SIGNAL( sliderMoved(int) ),  this, SLOT( ValueHasChanged() ) );
160   connect( SMESHGUI::GetSMESHGUI(), SIGNAL ( SignalCloseAllDialogs() ), this, SLOT( ClickOnOk() ) ) ;
161   connect( mySelection,  SIGNAL( currentSelectionChanged() ), this, SLOT( onSelectionChanged() ) );
162   
163   /* Move widget on the botton right corner of main widget */
164   int x, y ;
165   SMESHGUI::GetSMESHGUI()->DefineDlgPosition( this, x, y ) ;
166   this->move( x, y ) ;
167   this->show();
168 }
169
170
171 //=================================================================================
172 // function : ~SMESHGUI_TransparencyDlg()
173 // purpose  :
174 //=================================================================================
175 SMESHGUI_TransparencyDlg::~SMESHGUI_TransparencyDlg()
176 {
177   // no need to delete child widgets, Qt does it all for us
178 }
179
180
181 //=======================================================================
182 // function : ClickOnOk()
183 // purpose  :
184 //=======================================================================
185 void SMESHGUI_TransparencyDlg::ClickOnOk()
186 {
187   close();
188 }
189
190
191 //=================================================================================
192 // function : SetTransparency()
193 // purpose  : Called when value of slider change
194 //          : or the first time as initilisation
195 //=================================================================================
196 void SMESHGUI_TransparencyDlg::SetTransparency()
197 {
198   if ( SMESHGUI::GetSMESHGUI()->GetActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_VTK ) {
199     QAD_WaitCursor wc;
200     float opacity = this->Slider1->value() / 100. ;
201     SALOME_ListIteratorOfListIO It( mySelection->StoredIObjects() );
202     for( ;It.More(); It.Next() ) {
203       Handle(SALOME_InteractiveObject) IOS = It.Value();
204       SMESH_Actor* anActor = FindActorByEntry(IOS->getEntry());
205       if ( anActor )
206         anActor->SetOpacity( opacity );
207     }
208     SMESHGUI::GetSMESHGUI()->GetActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame()->Repaint();
209   }
210   ValueHasChanged();
211 }
212
213 //=================================================================================
214 // function : ValueHasChanged()
215 // purpose  : Called when user moves a slider
216 //=================================================================================
217 void SMESHGUI_TransparencyDlg::ValueHasChanged()
218 {
219   ValueLab->setText( QString::number( this->Slider1->value() ) + "%" );
220 }
221
222 //=================================================================================
223 // function : onSelectionChanged()
224 // purpose  : Called when selection is changed
225 //=================================================================================
226 void SMESHGUI_TransparencyDlg::onSelectionChanged()
227 {
228   if ( SMESHGUI::GetSMESHGUI()->GetActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_VTK ) {
229     int opacity = 100;
230     if ( mySelection->IObjectCount() == 1 ) {
231       Handle(SALOME_InteractiveObject) FirstIOS = mySelection->firstIObject();
232       if( !FirstIOS.IsNull() ) {
233         SMESH_Actor* anActor = FindActorByEntry( FirstIOS->getEntry() );
234         if ( anActor )
235           opacity = int( anActor->GetOpacity() * 100. + 0.5 );
236       }
237     }
238     else if ( mySelection->IObjectCount() > 1 ) {
239       SALOME_ListIteratorOfListIO It( mySelection->StoredIObjects() );
240       int setOp = -1;
241       for ( ; It.More(); It.Next() ) {
242         Handle(SALOME_InteractiveObject) IO = It.Value();
243         if( !IO.IsNull() ) {
244           SMESH_Actor* anActor = FindActorByEntry( IO->getEntry() );
245           if ( anActor ) {
246             int op = int( anActor->GetOpacity() * 100. + 0.5 );
247             if ( setOp < 0 )
248               setOp = op;
249             else if ( setOp != op ) {
250               setOp = 100;
251               break;
252             }
253           }
254         }
255       }
256       if ( setOp >= 0 )
257         opacity = setOp;
258     }
259     Slider1->setValue( opacity ) ;
260   }
261   ValueHasChanged();
262 }