Salome HOME
Update of CheckDone
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_TransparencyDlg.cxx
1 // Copyright (C) 2007-2021  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, or (at your option) any later version.
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
44 #include <LightApp_Application.h>
45 #include <LightApp_SelectionMgr.h>
46
47 #include <SVTK_ViewWindow.h>
48
49 // Qt includes
50 #include <QLabel>
51 #include <QPushButton>
52 #include <QSlider>
53 #include <QVBoxLayout>
54 #include <QHBoxLayout>
55 #include <QGridLayout>
56 #include <QGroupBox>
57 #include <QKeyEvent>
58
59 #define SPACING 6
60 #define MARGIN  11
61
62 //=================================================================================
63 // class    : SMESHGUI_TransparencyDlg()
64 // purpose  :
65 //
66 //=================================================================================
67 SMESHGUI_TransparencyDlg::SMESHGUI_TransparencyDlg( SMESHGUI* theModule )
68   : QDialog( SMESH::GetDesktop( theModule ) ),
69     mySMESHGUI( theModule ),
70     mySelectionMgr( SMESH::GetSelectionMgr( theModule ) ),
71     myViewWindow( SMESH::GetViewWindow( theModule ) )
72 {
73   setModal( false );
74   setAttribute( Qt::WA_DeleteOnClose, true );
75   setWindowTitle( tr( "SMESH_TRANSPARENCY_TITLE" ) );
76   setSizeGripEnabled( true );
77
78   QVBoxLayout* SMESHGUI_TransparencyDlgLayout = new QVBoxLayout( this );
79   SMESHGUI_TransparencyDlgLayout->setSpacing( SPACING );
80   SMESHGUI_TransparencyDlgLayout->setMargin( MARGIN );
81
82   /*************************************************************************/
83   QGroupBox* GroupC1 = new QGroupBox( this );
84   QGridLayout* GroupC1Layout = new QGridLayout( GroupC1 );
85   GroupC1Layout->setSpacing( SPACING );
86   GroupC1Layout->setMargin( MARGIN );
87
88   TextLabelTransparent = new QLabel( tr( "SMESH_TRANSPARENCY_TRANSPARENT" ), GroupC1 );
89   TextLabelTransparent->setAlignment( Qt::AlignRight );
90
91   ValueLab = new QLabel( GroupC1 );
92   ValueLab->setAlignment( Qt::AlignCenter );
93   ValueLab->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
94   QFont fnt = ValueLab->font(); fnt.setBold( true ); ValueLab->setFont( fnt );
95
96   TextLabelOpaque = new QLabel( tr( "SMESH_TRANSPARENCY_OPAQUE" ), GroupC1 );
97   TextLabelOpaque->setAlignment( Qt::AlignLeft );
98
99   Slider1 = new QSlider( Qt::Horizontal, GroupC1 );
100   Slider1->setRange( 0, 100 );
101   Slider1->setSingleStep( 1 );
102   Slider1->setPageStep( 10 );
103   Slider1->setTickPosition( QSlider::TicksAbove );
104   Slider1->setTickInterval( 10 );
105   Slider1->setTracking( true );
106   Slider1->setFocusPolicy( Qt::NoFocus );
107   Slider1->setMinimumWidth( 300 );
108
109   GroupC1Layout->addWidget( TextLabelOpaque, 0, 0 );
110   GroupC1Layout->addWidget( ValueLab, 0, 1 );
111   GroupC1Layout->addWidget( TextLabelTransparent, 0, 2 );
112   GroupC1Layout->addWidget( Slider1, 1, 0, 1, 3 );
113
114   /*************************************************************************/
115   QGroupBox* GroupButtons = new QGroupBox( this );
116   QHBoxLayout* GroupButtonsLayout = new QHBoxLayout( GroupButtons );
117   GroupButtonsLayout->setSpacing( SPACING );
118   GroupButtonsLayout->setMargin( MARGIN );
119
120   buttonOk = new QPushButton( tr( "SMESH_BUT_CLOSE" ), GroupButtons );
121   buttonOk->setAutoDefault( true );
122   buttonOk->setDefault( true );
123   buttonHelp = new QPushButton( tr( "SMESH_BUT_HELP" ), GroupButtons );
124   buttonHelp->setAutoDefault( true );
125
126   GroupButtonsLayout->addWidget( buttonOk );
127   GroupButtonsLayout->addSpacing( 10 );
128   GroupButtonsLayout->addStretch();
129   GroupButtonsLayout->addWidget( buttonHelp );  
130
131   /*************************************************************************/
132   SMESHGUI_TransparencyDlgLayout->addWidget( GroupC1 );
133   SMESHGUI_TransparencyDlgLayout->addWidget( GroupButtons );
134
135   // Initial state
136   onSelectionChanged();
137
138   // signals and slots connections : after ValueHasChanged()
139   connect( buttonOk,       SIGNAL( clicked() ),                 this, SLOT( ClickOnOk() ) );
140   connect( buttonHelp,     SIGNAL( clicked() ),                 this, SLOT( ClickOnHelp() ) );
141   connect( Slider1,        SIGNAL( valueChanged( int ) ),       this, SLOT( SetTransparency() ) );
142   connect( Slider1,        SIGNAL( sliderMoved( int ) ),        this, SLOT( ValueHasChanged() ) );
143   connect( mySMESHGUI,     SIGNAL( SignalCloseAllDialogs() ),   this, SLOT( ClickOnOk() ) );
144   connect( mySelectionMgr, SIGNAL( currentSelectionChanged() ), this, SLOT( onSelectionChanged() ) );
145
146   myHelpFileName = "transparency.html";
147 }
148
149 //=================================================================================
150 // function : ~SMESHGUI_TransparencyDlg()
151 // purpose  :
152 //=================================================================================
153 SMESHGUI_TransparencyDlg::~SMESHGUI_TransparencyDlg()
154 {
155 }
156
157 //=======================================================================
158 // function : ClickOnOk()
159 // purpose  :
160 //=======================================================================
161 void SMESHGUI_TransparencyDlg::ClickOnOk()
162 {
163   close();
164 }
165
166 //=================================================================================
167 // function : ClickOnHelp()
168 // purpose  :
169 //=================================================================================
170 void SMESHGUI_TransparencyDlg::ClickOnHelp()
171 {
172   LightApp_Application* app = (LightApp_Application*)( SUIT_Session::session()->activeApplication() );
173   if ( app )
174     app->onHelpContextModule( mySMESHGUI ? app->moduleName( mySMESHGUI->moduleName() ) : 
175                               QString( "" ), myHelpFileName );
176   else {
177     QString platform;
178 #ifdef WIN32
179     platform = "winapplication";
180 #else
181     platform = "application";
182 #endif
183     SUIT_MessageBox::warning( this, tr( "WRN_WARNING" ),
184                               tr( "EXTERNAL_BROWSER_CANNOT_SHOW_PAGE" ).
185                               arg( app->resourceMgr()->stringValue( "ExternalBrowser", 
186                                                                     platform ) ).
187                               arg( myHelpFileName ) );
188   }
189 }
190
191 //=================================================================================
192 // function : SetTransparency()
193 // purpose  : Called when value of slider change
194 //          : or the first time as initialisation
195 //=================================================================================
196 void SMESHGUI_TransparencyDlg::SetTransparency()
197 {
198   if ( myViewWindow ) {
199     SUIT_OverrideCursor wc;
200     //float opacity = ( 100.f - Slider1->value() ) / 100.;
201     float opacity = float( 100 - 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( 100 - 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 }