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