Salome HOME
23368: [CEA 1865] Possibility to define faces to mesh as a single one: transpatch...
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_TransparencyDlg.cxx
1 // Copyright (C) 2007-2016  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_page.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 initilisation
195 //=================================================================================
196 void SMESHGUI_TransparencyDlg::SetTransparency()
197 {
198   if ( myViewWindow ) {
199     SUIT_OverrideCursor wc;
200     float opacity = ( 100 - Slider1->value() ) / 100.;
201
202     SALOME_ListIO aList;
203     mySelectionMgr->selectedObjects( aList );
204
205     SALOME_ListIteratorOfListIO It( aList );
206     for ( ; It.More(); It.Next() ) {
207       Handle(SALOME_InteractiveObject) IOS = It.Value();
208       SMESH_Actor* anActor = SMESH::FindActorByEntry( IOS->getEntry() );
209       if ( anActor )
210         anActor->SetOpacity( opacity );
211     }
212     myViewWindow->Repaint();
213   }
214   ValueHasChanged();
215 }
216
217 //=================================================================================
218 // function : ValueHasChanged()
219 // purpose  : Called when user moves a slider
220 //=================================================================================
221 void SMESHGUI_TransparencyDlg::ValueHasChanged()
222 {
223   ValueLab->setText( QString::number( Slider1->value() ) + "%") ;
224 }
225
226 //=================================================================================
227 // function : onSelectionChanged()
228 // purpose  : Called when selection is changed
229 //=================================================================================
230 void SMESHGUI_TransparencyDlg::onSelectionChanged()
231 {
232   if ( myViewWindow ) {
233     int opacity = 100;
234
235     SALOME_ListIO aList;
236     mySelectionMgr->selectedObjects( aList );
237
238     if ( aList.Extent() == 1 ) {
239       Handle(SALOME_InteractiveObject) FirstIOS = aList.First();
240       if ( !FirstIOS.IsNull() ) {
241         SMESH_Actor* anActor = SMESH::FindActorByEntry( FirstIOS->getEntry() );
242         if ( anActor )
243           opacity = int( anActor->GetOpacity() * 100. + 0.5 );
244       }
245     } 
246     else if ( aList.Extent() > 1 ) {
247       SALOME_ListIteratorOfListIO It( aList );
248       int setOp = -1;
249       for ( ; It.More(); It.Next() ) {
250         Handle(SALOME_InteractiveObject) IO = It.Value();
251         if ( !IO.IsNull() ) {
252           SMESH_Actor* anActor = SMESH::FindActorByEntry( IO->getEntry() );
253           if ( anActor ) {
254             int op = int( anActor->GetOpacity() * 100. + 0.5 );
255             if ( setOp < 0 )
256               setOp = op;
257             else if ( setOp != op ) {
258               setOp = 100;
259               break;
260             }
261           }
262         }
263       }
264       if ( setOp >= 0 )
265         opacity = setOp;
266     } 
267     else {
268     }
269     Slider1->setValue( 100 - opacity );
270   }
271   ValueHasChanged();
272 }
273
274 //=================================================================================
275 // function : keyPressEvent()
276 // purpose  :
277 //=================================================================================
278 void SMESHGUI_TransparencyDlg::keyPressEvent( QKeyEvent* e )
279 {
280   QDialog::keyPressEvent( e );
281   if ( e->isAccepted() )
282     return;
283
284   if ( e->key() == Qt::Key_F1 ) {
285     e->accept();
286     ClickOnHelp();
287   }
288 }