Salome HOME
Fix for bug NPAL19028 ( Default way browser ).
[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 #include <OCCViewer_ViewWindow.h>
43
44 #include <SUIT_ViewManager.h>
45 #include <SUIT_Application.h>
46 #include <SUIT_Desktop.h>
47 #include <SUIT_MessageBox.h>
48 #include <SUIT_ResourceMgr.h>
49 #include <SUIT_Session.h>
50 #include <SUIT_OverrideCursor.h>
51
52 #include <SalomeApp_Application.h>
53 #include <LightApp_Application.h>
54 #include <LightApp_SelectionMgr.h>
55
56 #include <qframe.h>
57 #include <qlabel.h>
58 #include <qpushbutton.h>
59 #include <qslider.h>
60 #include <qlayout.h>
61 #include <qvariant.h>
62 #include <qtooltip.h>
63 #include <qwhatsthis.h>
64 #include <qapplication.h>
65 #include <qgroupbox.h>
66
67 #include <AIS_InteractiveContext.hxx>
68 using namespace std;
69
70 //=================================================================================
71 // class    : GEOMBase_TransparencyDlg()
72 // purpose  : Constructs a GEOMBase_SUBSHAPE which is a child of 'parent', with the
73 //            name 'name' and widget flags set to 'f'.
74 //
75 //          : WARNING : this dialog is modal !
76 //
77 //=================================================================================
78 GEOMToolsGUI_TransparencyDlg::GEOMToolsGUI_TransparencyDlg( QWidget* parent )
79   :QDialog( parent, "GEOMBase_TransparencyDlg", true, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu )
80 {
81   resize(152, 107); 
82   setCaption(tr("GEOM_TRANSPARENCY_TITLE"));
83   setSizeGripEnabled(TRUE);
84   QGridLayout* lay = new QGridLayout(this); 
85   lay->setSpacing(6);
86   lay->setMargin(11);
87
88   /*************************************************************************/
89   QGroupBox* GroupButtons = new QGroupBox( this, "GroupButtons" );
90   GroupButtons->setColumnLayout(0, Qt::Vertical );
91   GroupButtons->layout()->setSpacing( 0 );
92   GroupButtons->layout()->setMargin( 0 );
93   QGridLayout* GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
94   GroupButtonsLayout->setAlignment( Qt::AlignTop );
95   GroupButtonsLayout->setSpacing( 6 );
96   GroupButtonsLayout->setMargin( 11 );
97   
98   QPushButton* buttonOk = new QPushButton( GroupButtons, "buttonOk" );
99   buttonOk->setText( tr( "GEOM_BUT_OK" ) );
100   buttonOk->setAutoDefault( TRUE );
101   buttonOk->setDefault( TRUE );
102
103   QPushButton* buttonHelp = new QPushButton( GroupButtons, "buttonHelp" );
104   buttonHelp->setText( tr( "GEOM_BUT_HELP" ) );
105   buttonHelp->setAutoDefault( TRUE );
106   buttonHelp->setDefault( TRUE );
107   
108   GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
109   GroupButtonsLayout->addItem( new QSpacerItem( 0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum), 0, 1 );
110   GroupButtonsLayout->addWidget( buttonHelp, 0, 2 );
111
112   /*************************************************************************/
113   QGroupBox* GroupC1 = new QGroupBox( this, "GroupC1" );
114   GroupC1->setColumnLayout(0, Qt::Vertical );
115   GroupC1->layout()->setSpacing( 0 );
116   GroupC1->layout()->setMargin( 0 );
117   QGridLayout* GroupC1Layout = new QGridLayout( GroupC1->layout() );
118   GroupC1Layout->setAlignment( Qt::AlignTop );
119   GroupC1Layout->setSpacing( 6 );
120   GroupC1Layout->setMargin( 11 );
121   
122   QLabel* TextLabelOpaque = new QLabel( GroupC1, "TextLabelOpaque" );
123   TextLabelOpaque->setText( tr( "GEOM_TRANSPARENCY_OPAQUE"  ) );
124   TextLabelOpaque->setAlignment( int( QLabel::AlignLeft ) );
125   GroupC1Layout->addWidget( TextLabelOpaque, 0, 0 );
126   GroupC1Layout->addItem( new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum ), 0, 1 );
127   
128   QLabel* TextLabelTransparent = new QLabel( GroupC1, "TextLabelTransparent" );
129   TextLabelTransparent->setText( tr( "GEOM_TRANSPARENCY_TRANSPARENT"  ) );
130   TextLabelTransparent->setAlignment( int( QLabel::AlignRight ) );
131   GroupC1Layout->addWidget( TextLabelTransparent, 0, 2 );
132   
133   mySlider = new QSlider( 0, 10, 1, 5, Horizontal, GroupC1, "mySlider" );
134   mySlider->setMinimumSize( 300, 0 );
135   mySlider->setTickmarks( QSlider::Left );
136   GroupC1Layout->addMultiCellWidget( mySlider, 1, 1, 0, 2 );
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::warn1(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() == Key_F1 )
300     {
301       e->accept();
302       ClickOnHelp();
303     }
304 }