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