Salome HOME
6e39d15d07cf376a35647b8da8d4c2325e0c6d89
[modules/geom.git] / src / GEOMToolsGUI / GEOMToolsGUI_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 // GEOM GEOMGUI : GUI for Geometry component
24 // File   : GEOMToolsGUI_TransparencyDlg.cxx
25 // Author : Lucien PIGNOLONI
26 //
27 #include "GEOMToolsGUI_TransparencyDlg.h"
28 #include <GEOMBase.h>
29 #include <GEOM_AISShape.hxx>
30 #include <GeometryGUI.h>
31
32 #include <SALOME_ListIO.hxx>
33 #include <SALOME_ListIteratorOfListIO.hxx>
34
35 #include <SVTK_ViewModel.h>
36 #include <SVTK_ViewWindow.h>
37 #include <SVTK_View.h>
38
39 #include <OCCViewer_ViewModel.h>
40
41 #include <SUIT_ViewManager.h>
42 #include <SUIT_Desktop.h>
43 #include <SUIT_MessageBox.h>
44 #include <SUIT_ResourceMgr.h>
45 #include <SUIT_Session.h>
46 #include <SUIT_OverrideCursor.h>
47
48 #include <SalomeApp_Application.h>
49 #include <LightApp_Application.h>
50 #include <LightApp_SelectionMgr.h>
51
52 #include <QLabel>
53 #include <QPushButton>
54 #include <QSlider>
55 #include <QGridLayout>
56 #include <QVBoxLayout>
57 #include <QHBoxLayout>
58 #include <QGroupBox>
59 #include <QKeyEvent>
60
61 #include <AIS_InteractiveContext.hxx>
62
63
64 //=================================================================================
65 // class    : GEOMBase_TransparencyDlg()
66 // purpose  : Constructs a GEOMBase_SUBSHAPE which is a child of 'parent', with the
67 //            name 'name' and widget flags set to 'f'.
68 //
69 //          : WARNING : this dialog is modal !
70 //
71 //=================================================================================
72 GEOMToolsGUI_TransparencyDlg::GEOMToolsGUI_TransparencyDlg( QWidget* parent )
73   :QDialog( parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint )
74 {
75   setModal( true );
76   
77   resize(152, 107); 
78   setWindowTitle(tr("GEOM_TRANSPARENCY_TITLE"));
79   setSizeGripEnabled(true);
80   QVBoxLayout* lay = new QVBoxLayout(this); 
81   lay->setSpacing(6);
82   lay->setMargin(11);
83   
84   /*************************************************************************/
85   QGroupBox* GroupButtons = new QGroupBox( this );
86   QHBoxLayout* GroupButtonsLayout = new QHBoxLayout( GroupButtons );
87   GroupButtonsLayout->setAlignment( Qt::AlignTop );
88   GroupButtonsLayout->setSpacing( 6 );
89   GroupButtonsLayout->setMargin( 11 );
90   
91   /*************************************************************************/
92   QGroupBox* GroupC1 = new QGroupBox( this );
93   QGridLayout* GroupC1Layout = new QGridLayout( GroupC1 );
94   GroupC1Layout->setAlignment( Qt::AlignTop );
95   GroupC1Layout->setSpacing( 6 );
96   GroupC1Layout->setMargin( 11 );
97   
98   QLabel* TextLabelTransparent = new QLabel( tr( "GEOM_TRANSPARENCY_TRANSPARENT" ), GroupC1 );
99   TextLabelTransparent->setAlignment( Qt::AlignRight );
100   GroupC1Layout->addWidget( TextLabelTransparent, 0, 0 );
101   
102   myValueLab = new QLabel( GroupC1 );
103   myValueLab->setAlignment( Qt::AlignCenter );
104   myValueLab->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
105   QFont fnt = myValueLab->font(); fnt.setBold( true ); myValueLab->setFont( fnt );
106   GroupC1Layout->addWidget( myValueLab, 0, 1 );
107
108   QLabel* TextLabelOpaque = new QLabel( tr( "GEOM_TRANSPARENCY_OPAQUE" ), GroupC1 );
109   TextLabelOpaque->setAlignment( Qt::AlignLeft );
110   GroupC1Layout->addWidget( TextLabelOpaque, 0, 2 );
111   //GroupC1Layout->addItem( new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum ), 0, 1 );
112   
113   mySlider = new QSlider( Qt::Horizontal, GroupC1 );
114   mySlider->setFocusPolicy( Qt::NoFocus );
115   mySlider->setMinimumSize( 300, 0 );
116   mySlider->setTickPosition( QSlider::TicksAbove );
117   mySlider->setTickInterval( 10 );
118   mySlider->setMinimum( 0 );
119   mySlider->setMaximum( 100 );
120   mySlider->setSingleStep( 1 );
121   mySlider->setPageStep( 10 );
122   //mySlider->setValue( 5 );
123   mySlider->setTracking(false);
124
125   mySlider->setTickPosition( QSlider::TicksLeft );
126   GroupC1Layout->addWidget( mySlider, 1, 0, 1, 3 );
127
128   /*************************************************************************/
129   QPushButton* buttonOk = new QPushButton( tr( "GEOM_BUT_OK" ), GroupButtons );
130   buttonOk->setAutoDefault( true );
131   buttonOk->setDefault( true );
132   
133   QPushButton* buttonHelp = new QPushButton( tr( "GEOM_BUT_HELP" ), GroupButtons );
134   buttonHelp->setAutoDefault( true );
135   buttonHelp->setDefault( true );
136   
137   GroupButtonsLayout->addWidget( buttonOk );
138   GroupButtonsLayout->addSpacing( 10 );
139   GroupButtonsLayout->addStretch();
140   GroupButtonsLayout->addWidget( buttonHelp );
141   
142   /*************************************************************************/
143   lay->addWidget(GroupC1);
144   lay->addWidget(GroupButtons);
145     
146   /* First call valueChanged() method for initialisation               */
147   /* The default value of transparency will change with the selection  */
148   myFirstInit = true;
149   //  mySlider->setMaxValue( 10 );
150   //  mySlider->setValue( 5 ) ;
151   SetTransparency();
152   
153   myHelpFileName = "transparency_page.html";
154
155   // signals and slots connections : after ValueHasChanged()
156   connect(buttonOk,   SIGNAL(clicked()), this, SLOT(ClickOnOk()));
157   connect(buttonHelp, SIGNAL(clicked()), this, SLOT(ClickOnHelp()));
158   connect(mySlider,   SIGNAL(valueChanged(int)), this, SLOT(SetTransparency()));
159   connect(mySlider,   SIGNAL(sliderMoved(int)),  this, SLOT(SliderHasMoved(int)));
160 }
161
162
163 //=================================================================================
164 // function : ~GEOMBase_TransparencyDlg()
165 // purpose  :
166 //=================================================================================
167 GEOMToolsGUI_TransparencyDlg::~GEOMToolsGUI_TransparencyDlg()
168 {
169     // no need to delete child widgets, Qt does it all for us
170 }
171
172
173 //=======================================================================
174 // function : ClickOnOk()
175 // purpose  :
176 //=======================================================================
177 void GEOMToolsGUI_TransparencyDlg::ClickOnOk()
178 {
179   accept();
180   return;
181 }
182
183
184 //=======================================================================
185 // function : ClickOnClose()
186 // purpose  :
187 //=======================================================================
188 void GEOMToolsGUI_TransparencyDlg::ClickOnClose()
189 {
190   accept();
191   return;
192 }
193
194 //=================================================================================
195 // function : ClickOnHelp()
196 // purpose  :
197 //=================================================================================
198 void GEOMToolsGUI_TransparencyDlg::ClickOnHelp()
199 {
200   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
201   if (app) {
202     GeometryGUI* aGeomGUI = dynamic_cast<GeometryGUI*>( app->module( "Geometry" ) );
203     app->onHelpContextModule(aGeomGUI ? app->moduleName(aGeomGUI->moduleName()) : QString(""), myHelpFileName);
204   }
205   else {
206                 QString platform;
207 #ifdef WIN32
208                 platform = "winapplication";
209 #else
210                 platform = "application";
211 #endif
212     SUIT_MessageBox::warning(0, QObject::tr("WRN_WARNING"),
213                              QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
214                              arg(app->resourceMgr()->stringValue("ExternalBrowser", platform)).arg(myHelpFileName),
215                              QObject::tr("BUT_OK"));
216   }
217 }
218
219 //=================================================================================
220 // function : ValueHasChanged()
221 // purpose  : Called when value of slider change
222 //=================================================================================
223 void GEOMToolsGUI_TransparencyDlg::ValueHasChanged()
224 {
225   myValueLab->setText( QString("%1%").arg( mySlider->value() ) );
226 }
227
228 //=================================================================================
229 // function : SliderHasMoved()
230 // purpose  : Called when value of slider change
231 //=================================================================================
232 void GEOMToolsGUI_TransparencyDlg::SliderHasMoved(int value)
233 {
234   myValueLab->setText( QString("%1%").arg( value ) );
235 }
236
237 //=================================================================================
238 // function : SetTransparency()
239 // purpose  : Called when value of slider change
240 //          : or the first time as initilisation
241 //=================================================================================
242 void GEOMToolsGUI_TransparencyDlg::SetTransparency()
243 {
244   float newValue = ( 100 - mySlider->value() ) / 100.;
245
246   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
247   if ( !app )
248     return;
249   LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
250   if ( !aSelMgr )
251     return;
252   SALOME_ListIO selected;
253   aSelMgr->selectedObjects( selected );
254   if ( selected.IsEmpty() )
255     return;
256
257   Handle(SALOME_InteractiveObject) FirstIOS =  selected.First();
258   if ( FirstIOS.IsNull() )
259     return;
260         
261   SUIT_ViewWindow* window = app->desktop()->activeWindow();
262   bool isOCC = ( window && window->getViewManager()->getType() == OCCViewer_Viewer::Type() );
263   bool isVTK = ( window && window->getViewManager()->getType() == SVTK_Viewer::Type() );
264
265   if ( isVTK ) {
266     SVTK_ViewWindow* vtkVW = dynamic_cast<SVTK_ViewWindow*>( window );
267     if ( !vtkVW )
268       return;
269     SVTK_View* aView = vtkVW->getView();
270     if ( myFirstInit ) {        
271       myFirstInit = false;
272       int transp = int (100 - ((aView->GetTransparency(FirstIOS))*100.0) + 0.5);
273       mySlider->setValue(transp);
274       ValueHasChanged();
275       return;
276     }
277
278     SUIT_OverrideCursor();
279     for ( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
280       aView->SetTransparency( It.Value(), newValue );
281     }
282     GeometryGUI::Modified();
283     aView->Repaint();
284   } // if ( isVTK )
285         
286   else if ( isOCC ) {
287     GEOMBase* gb = new GEOMBase();
288     Standard_Boolean found;
289     Handle(GEOM_AISShape) aisShape;
290     if ( myFirstInit ) {
291       myFirstInit = false;
292       aisShape = gb->ConvertIOinGEOMAISShape( FirstIOS, found );
293       if( !found )
294         return;
295       int transp = int( 100 - ( aisShape->Transparency() * 100.0 ) + 0.5);
296       mySlider->setValue(transp);
297       ValueHasChanged();
298       return;
299     }
300     
301     SUIT_OverrideCursor();
302     OCCViewer_Viewer* vm = dynamic_cast<OCCViewer_Viewer*>( window->getViewManager()->getViewModel() );
303     if ( !vm )
304       return;
305     Handle(AIS_InteractiveContext) ic = vm->getAISContext();
306     for ( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
307       aisShape = gb->ConvertIOinGEOMAISShape( It.Value(), found );
308       if ( found ) {
309         ic->SetTransparency( aisShape, newValue, false );
310         ic->Redisplay( aisShape, Standard_False, Standard_True );
311       }
312     } // for...
313     ic->UpdateCurrentViewer();
314     GeometryGUI::Modified();
315   } // if ( isOCC )
316
317   ValueHasChanged();
318 }
319
320 //=================================================================================
321 // function : keyPressEvent()
322 // purpose  :
323 //=================================================================================
324 void GEOMToolsGUI_TransparencyDlg::keyPressEvent( QKeyEvent* e )
325 {
326   QDialog::keyPressEvent( e );
327   if ( e->isAccepted() )
328     return;
329
330   if ( e->key() == Qt::Key_F1 )
331     {
332       e->accept();
333       ClickOnHelp();
334     }
335 }