Salome HOME
PR: merged from V5_1_4rc1
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_TransparencyDlg.cxx
1 //  Copyright (C) 2007-2010  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.
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 : GUI for SMESH component
24 // File   : SMESHGUI_TransparencyDlg.cxx
25 // Author : Nicolas REJNERI, Open CASCADE S.A.S.
26 // SMESH includes
27 //
28 #include "SMESHGUI_TransparencyDlg.h"
29
30 #include "SMESHGUI.h"
31 #include "SMESHGUI_VTKUtils.h"
32 #include "SMESHGUI_Utils.h"
33 #include "SMESH_Actor.h"
34
35 // SALOME GUI includes
36 #include <SUIT_Desktop.h>
37 #include <SUIT_OverrideCursor.h>
38 #include <SUIT_Session.h>
39 #include <SUIT_MessageBox.h>
40 #include <SUIT_ResourceMgr.h>
41
42 #include <SALOME_ListIO.hxx>
43 #include <SALOME_ListIteratorOfListIO.hxx>
44
45 #include <LightApp_Application.h>
46 #include <LightApp_SelectionMgr.h>
47
48 #include <SVTK_ViewWindow.h>
49
50 // Qt includes
51 #include <QLabel>
52 #include <QPushButton>
53 #include <QSlider>
54 #include <QVBoxLayout>
55 #include <QHBoxLayout>
56 #include <QGridLayout>
57 #include <QGroupBox>
58 #include <QKeyEvent>
59
60 #define SPACING 6
61 #define MARGIN  11
62
63 //=================================================================================
64 // class    : SMESHGUI_TransparencyDlg()
65 // purpose  :
66 //
67 //=================================================================================
68 SMESHGUI_TransparencyDlg::SMESHGUI_TransparencyDlg( SMESHGUI* theModule )
69   : QDialog( SMESH::GetDesktop( theModule ) ),
70     mySMESHGUI( theModule ),
71     mySelectionMgr( SMESH::GetSelectionMgr( theModule ) ),
72     myViewWindow( SMESH::GetViewWindow( theModule ) )
73 {
74   setModal( false );
75   setAttribute( Qt::WA_DeleteOnClose, true );
76   setWindowTitle( tr( "SMESH_TRANSPARENCY_TITLE" ) );
77   setSizeGripEnabled( true );
78
79   QVBoxLayout* SMESHGUI_TransparencyDlgLayout = new QVBoxLayout( this );
80   SMESHGUI_TransparencyDlgLayout->setSpacing( SPACING );
81   SMESHGUI_TransparencyDlgLayout->setMargin( MARGIN );
82
83   /*************************************************************************/
84   QGroupBox* GroupC1 = new QGroupBox( this );
85   QGridLayout* GroupC1Layout = new QGridLayout( GroupC1 );
86   GroupC1Layout->setSpacing( SPACING );
87   GroupC1Layout->setMargin( MARGIN );
88
89   TextLabelTransparent = new QLabel( tr( "SMESH_TRANSPARENCY_TRANSPARENT" ), GroupC1 );
90   TextLabelTransparent->setAlignment( Qt::AlignLeft );
91
92   ValueLab = new QLabel( GroupC1 );
93   ValueLab->setAlignment( Qt::AlignCenter );
94   ValueLab->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
95   QFont fnt = ValueLab->font(); fnt.setBold( true ); ValueLab->setFont( fnt );
96
97   TextLabelOpaque = new QLabel( tr( "SMESH_TRANSPARENCY_OPAQUE" ), GroupC1 );
98   TextLabelOpaque->setAlignment( Qt::AlignRight );
99
100   Slider1 = new QSlider( Qt::Horizontal, GroupC1 );
101   Slider1->setRange( 0, 100 );
102   Slider1->setSingleStep( 1 );
103   Slider1->setPageStep( 10 );
104   Slider1->setTickPosition( QSlider::TicksAbove );
105   Slider1->setTickInterval( 10 );
106   Slider1->setTracking( true );
107   Slider1->setFocusPolicy( Qt::NoFocus );
108   Slider1->setMinimumWidth( 300 );
109
110   GroupC1Layout->addWidget( TextLabelTransparent, 0, 0 );
111   GroupC1Layout->addWidget( ValueLab, 0, 1 );
112   GroupC1Layout->addWidget( TextLabelOpaque, 0, 2 );
113   GroupC1Layout->addWidget( Slider1, 1, 0, 1, 3 );
114
115   /*************************************************************************/
116   QGroupBox* GroupButtons = new QGroupBox( this );
117   QHBoxLayout* GroupButtonsLayout = new QHBoxLayout( GroupButtons );
118   GroupButtonsLayout->setSpacing( SPACING );
119   GroupButtonsLayout->setMargin( MARGIN );
120
121   buttonOk = new QPushButton( tr( "SMESH_BUT_CLOSE" ), GroupButtons );
122   buttonOk->setAutoDefault( true );
123   buttonOk->setDefault( true );
124   buttonHelp = new QPushButton( tr( "SMESH_BUT_HELP" ), GroupButtons );
125   buttonHelp->setAutoDefault( true );
126
127   GroupButtonsLayout->addWidget( buttonOk );
128   GroupButtonsLayout->addSpacing( 10 );
129   GroupButtonsLayout->addStretch();
130   GroupButtonsLayout->addWidget( buttonHelp );  
131
132   /*************************************************************************/
133   SMESHGUI_TransparencyDlgLayout->addWidget( GroupC1 );
134   SMESHGUI_TransparencyDlgLayout->addWidget( GroupButtons );
135
136   // Initial state
137   onSelectionChanged();
138
139   // signals and slots connections : after ValueHasChanged()
140   connect( buttonOk,       SIGNAL( clicked() ),                 this, SLOT( ClickOnOk() ) );
141   connect( buttonHelp,     SIGNAL( clicked() ),                 this, SLOT( ClickOnHelp() ) );
142   connect( Slider1,        SIGNAL( valueChanged( int ) ),       this, SLOT( SetTransparency() ) );
143   connect( Slider1,        SIGNAL( sliderMoved( int ) ),        this, SLOT( ValueHasChanged() ) );
144   connect( mySMESHGUI,     SIGNAL( SignalCloseAllDialogs() ),   this, SLOT( ClickOnOk() ) );
145   connect( mySelectionMgr, SIGNAL( currentSelectionChanged() ), this, SLOT( onSelectionChanged() ) );
146
147   myHelpFileName = "transparency_page.html";
148 }
149
150 //=================================================================================
151 // function : ~SMESHGUI_TransparencyDlg()
152 // purpose  :
153 //=================================================================================
154 SMESHGUI_TransparencyDlg::~SMESHGUI_TransparencyDlg()
155 {
156 }
157
158 //=======================================================================
159 // function : ClickOnOk()
160 // purpose  :
161 //=======================================================================
162 void SMESHGUI_TransparencyDlg::ClickOnOk()
163 {
164   close();
165 }
166
167 //=================================================================================
168 // function : ClickOnHelp()
169 // purpose  :
170 //=================================================================================
171 void SMESHGUI_TransparencyDlg::ClickOnHelp()
172 {
173   LightApp_Application* app = (LightApp_Application*)( SUIT_Session::session()->activeApplication() );
174   if ( app )
175     app->onHelpContextModule( mySMESHGUI ? app->moduleName( mySMESHGUI->moduleName() ) : 
176                               QString( "" ), myHelpFileName );
177   else {
178     QString platform;
179 #ifdef WIN32
180     platform = "winapplication";
181 #else
182     platform = "application";
183 #endif
184     SUIT_MessageBox::warning( this, tr( "WRN_WARNING" ),
185                               tr( "EXTERNAL_BROWSER_CANNOT_SHOW_PAGE" ).
186                               arg( app->resourceMgr()->stringValue( "ExternalBrowser", 
187                                                                     platform ) ).
188                               arg( myHelpFileName ) );
189   }
190 }
191
192 //=================================================================================
193 // function : SetTransparency()
194 // purpose  : Called when value of slider change
195 //          : or the first time as initilisation
196 //=================================================================================
197 void SMESHGUI_TransparencyDlg::SetTransparency()
198 {
199   if ( myViewWindow ) {
200     SUIT_OverrideCursor wc;
201     float opacity = Slider1->value() / 100.;
202
203     SALOME_ListIO aList;
204     mySelectionMgr->selectedObjects( aList );
205
206     SALOME_ListIteratorOfListIO It( aList );
207     for ( ; It.More(); It.Next() ) {
208       Handle(SALOME_InteractiveObject) IOS = It.Value();
209       SMESH_Actor* anActor = SMESH::FindActorByEntry( IOS->getEntry() );
210       if ( anActor )
211         anActor->SetOpacity( opacity );
212     }
213     myViewWindow->Repaint();
214   }
215   ValueHasChanged();
216 }
217
218 //=================================================================================
219 // function : ValueHasChanged()
220 // purpose  : Called when user moves a slider
221 //=================================================================================
222 void SMESHGUI_TransparencyDlg::ValueHasChanged()
223 {
224   ValueLab->setText( QString::number( Slider1->value() ) + "%") ;
225 }
226
227 //=================================================================================
228 // function : onSelectionChanged()
229 // purpose  : Called when selection is changed
230 //=================================================================================
231 void SMESHGUI_TransparencyDlg::onSelectionChanged()
232 {
233   if ( myViewWindow ) {
234     int opacity = 100;
235
236     SALOME_ListIO aList;
237     mySelectionMgr->selectedObjects( aList );
238
239     if ( aList.Extent() == 1 ) {
240       Handle(SALOME_InteractiveObject) FirstIOS = aList.First();
241       if ( !FirstIOS.IsNull() ) {
242         SMESH_Actor* anActor = SMESH::FindActorByEntry( FirstIOS->getEntry() );
243         if ( anActor )
244           opacity = int( anActor->GetOpacity() * 100. + 0.5 );
245       }
246     } 
247     else if ( aList.Extent() > 1 ) {
248       SALOME_ListIteratorOfListIO It( aList );
249       int setOp = -1;
250       for ( ; It.More(); It.Next() ) {
251         Handle(SALOME_InteractiveObject) IO = It.Value();
252         if ( !IO.IsNull() ) {
253           SMESH_Actor* anActor = SMESH::FindActorByEntry( IO->getEntry() );
254           if ( anActor ) {
255             int op = int( anActor->GetOpacity() * 100. + 0.5 );
256             if ( setOp < 0 )
257               setOp = op;
258             else if ( setOp != op ) {
259               setOp = 100;
260               break;
261             }
262           }
263         }
264       }
265       if ( setOp >= 0 )
266         opacity = setOp;
267     } 
268     else {
269     }
270     Slider1->setValue( opacity );
271   }
272   ValueHasChanged();
273 }
274
275 //=================================================================================
276 // function : keyPressEvent()
277 // purpose  :
278 //=================================================================================
279 void SMESHGUI_TransparencyDlg::keyPressEvent( QKeyEvent* e )
280 {
281   QDialog::keyPressEvent( e );
282   if ( e->isAccepted() )
283     return;
284
285   if ( e->key() == Qt::Key_F1 ) {
286     e->accept();
287     ClickOnHelp();
288   }
289 }