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