Salome HOME
Update mail address
[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.htm";
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     SUIT_MessageBox::warn1(0, QObject::tr("WRN_WARNING"),
202                            QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
203                            arg(app->resourceMgr()->stringValue("ExternalBrowser", "application")).arg(myHelpFileName),
204                            QObject::tr("BUT_OK"));
205   }
206 }
207
208 //=================================================================================
209 // function : ValueHasChanged()
210 // purpose  : Called when value of slider change
211 //          : or the first time as initilisation
212 //=================================================================================
213 void GEOMToolsGUI_TransparencyDlg::ValueHasChanged( int newValue )
214 {
215   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
216   if ( !app )
217     return;
218   LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
219   if ( !aSelMgr )
220     return;
221   SALOME_ListIO selected;
222   aSelMgr->selectedObjects( selected );
223   if ( selected.IsEmpty() )
224     return;
225
226   Handle(SALOME_InteractiveObject) FirstIOS =  selected.First();
227   if ( FirstIOS.IsNull() )
228     return;
229         
230   SUIT_ViewWindow* window = app->desktop()->activeWindow();
231   bool isOCC = ( window && window->getViewManager()->getType() == OCCViewer_Viewer::Type() );
232   bool isVTK = ( window && window->getViewManager()->getType() == SVTK_Viewer::Type() );
233
234   if ( isVTK ) {
235     SVTK_ViewWindow* vtkVW = dynamic_cast<SVTK_ViewWindow*>( window );
236     if ( !vtkVW )
237       return;
238     SVTK_View* aView = vtkVW->getView();
239     if ( myFirstInit ) {        
240       myFirstInit = false;
241       float transp = (aView->GetTransparency(FirstIOS))*10.0;
242       mySlider->setValue(int(transp));
243       return;
244     }
245
246     SUIT_OverrideCursor();
247     for ( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
248       aView->SetTransparency( It.Value(), newValue/10.0 );
249     }
250     aView->Repaint();
251   } // if ( isVTK )
252         
253   else if ( isOCC ) {
254     GEOMBase* gb = new GEOMBase();
255     Standard_Boolean found;
256     Handle(GEOM_AISShape) aisShape;
257     if ( myFirstInit ) {
258       myFirstInit = false;
259       aisShape = gb->ConvertIOinGEOMAISShape( FirstIOS, found );
260       if( !found )
261         return;
262       float transp = (int(aisShape->Transparency() * 10.0 + 0.001));
263       mySlider->setValue(int(transp));
264       return;
265     }
266     
267     SUIT_OverrideCursor();
268     OCCViewer_Viewer* vm = dynamic_cast<OCCViewer_Viewer*>( window->getViewManager()->getViewModel() );
269     if ( !vm )
270       return;
271     Handle(AIS_InteractiveContext) ic = vm->getAISContext();
272     for ( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
273       aisShape = gb->ConvertIOinGEOMAISShape( It.Value(), found );
274       if ( found ) {
275         ic->SetTransparency( aisShape, newValue / 10.0, false );
276         ic->Redisplay( aisShape, Standard_False, Standard_True );
277       }
278     } // for...
279     ic->UpdateCurrentViewer();
280   } // if ( isOCC )
281 }