Salome HOME
1610f9149b9602d83a5653aadaed3b7459b64815
[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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
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
33 #include <SALOME_ListIO.hxx>
34 #include <SALOME_ListIteratorOfListIO.hxx>
35
36 #include <VTKViewer_ViewModel.h>
37 #include <OCCViewer_ViewModel.h>
38 #include <OCCViewer_ViewWindow.h>
39
40 #include <SVTK_ViewWindow.h>
41 #include <SVTK_RenderWindowInteractor.h>
42
43 #include <SUIT_ViewManager.h>
44 #include <SUIT_Application.h>
45 #include <SUIT_Desktop.h>
46 #include <SUIT_ResourceMgr.h>
47 #include <SUIT_Session.h>
48 #include <SUIT_OverrideCursor.h>
49
50 #include <SalomeApp_Application.h>
51 #include <SalomeApp_SelectionMgr.h>
52
53 #include <qframe.h>
54 #include <qlabel.h>
55 #include <qpushbutton.h>
56 #include <qslider.h>
57 #include <qlayout.h>
58 #include <qvariant.h>
59 #include <qtooltip.h>
60 #include <qwhatsthis.h>
61 #include <qapplication.h>
62 #include <qgroupbox.h>
63
64 #include <AIS_InteractiveContext.hxx>
65 using namespace std;
66
67 //=================================================================================
68 // class    : GEOMBase_TransparencyDlg()
69 // purpose  : Constructs a GEOMBase_SUBSHAPE which is a child of 'parent', with the
70 //            name 'name' and widget flags set to 'f'.
71 //
72 //          : WARNING : this dialog is modal !
73 //
74 //=================================================================================
75 GEOMToolsGUI_TransparencyDlg::GEOMToolsGUI_TransparencyDlg( QWidget* parent )
76   :QDialog( parent, "GEOMBase_TransparencyDlg", true, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu )
77 {
78   resize(152, 107); 
79   setCaption(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, "GroupButtons" );
87   GroupButtons->setColumnLayout(0, Qt::Vertical );
88   GroupButtons->layout()->setSpacing( 0 );
89   GroupButtons->layout()->setMargin( 0 );
90   QGridLayout* GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
91   GroupButtonsLayout->setAlignment( Qt::AlignTop );
92   GroupButtonsLayout->setSpacing( 6 );
93   GroupButtonsLayout->setMargin( 11 );
94   
95   QPushButton* buttonOk = new QPushButton( GroupButtons, "buttonOk" );
96   buttonOk->setText( tr( "GEOM_BUT_OK" ) );
97   buttonOk->setAutoDefault( TRUE );
98   buttonOk->setDefault( TRUE );
99   GroupButtonsLayout->addItem( new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum), 0, 0 );
100   GroupButtonsLayout->addWidget( buttonOk, 0, 1 );
101   GroupButtonsLayout->addItem( new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum), 0, 2 );
102
103   /*************************************************************************/
104   QGroupBox* GroupC1 = new QGroupBox( this, "GroupC1" );
105   GroupC1->setColumnLayout(0, Qt::Vertical );
106   GroupC1->layout()->setSpacing( 0 );
107   GroupC1->layout()->setMargin( 0 );
108   QGridLayout* GroupC1Layout = new QGridLayout( GroupC1->layout() );
109   GroupC1Layout->setAlignment( Qt::AlignTop );
110   GroupC1Layout->setSpacing( 6 );
111   GroupC1Layout->setMargin( 11 );
112   
113   QLabel* TextLabelOpaque = new QLabel( GroupC1, "TextLabelOpaque" );
114   TextLabelOpaque->setText( tr( "GEOM_TRANSPARENCY_OPAQUE"  ) );
115   TextLabelOpaque->setAlignment( int( QLabel::AlignLeft ) );
116   GroupC1Layout->addWidget( TextLabelOpaque, 0, 0 );
117   GroupC1Layout->addItem( new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum ), 0, 1 );
118   
119   QLabel* TextLabelTransparent = new QLabel( GroupC1, "TextLabelTransparent" );
120   TextLabelTransparent->setText( tr( "GEOM_TRANSPARENCY_TRANSPARENT"  ) );
121   TextLabelTransparent->setAlignment( int( QLabel::AlignRight ) );
122   GroupC1Layout->addWidget( TextLabelTransparent, 0, 2 );
123   
124   mySlider = new QSlider( 0, 10, 1, 5, Horizontal, GroupC1, "mySlider" );
125   mySlider->setMinimumSize( 300, 0 );
126   mySlider->setTickmarks( QSlider::Left );
127   GroupC1Layout->addMultiCellWidget( mySlider, 1, 1, 0, 2 );
128   /*************************************************************************/
129   
130   lay->addWidget(GroupC1, 0,  0);
131   lay->addWidget(GroupButtons, 1, 0);
132     
133   /* First call valueChanged() method for initialisation               */
134   /* The default value of transparency will change with the selection  */
135   myFirstInit = true;
136   //  mySlider->setMaxValue( 10 );
137   //  mySlider->setValue( 5 ) ;
138   ValueHasChanged(mySlider->value());
139   
140   // signals and slots connections : after ValueHasChanged()
141   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
142   connect(mySlider, SIGNAL(valueChanged(int)), this, SLOT(ValueHasChanged(int)));
143 }
144
145
146 //=================================================================================
147 // function : ~GEOMBase_TransparencyDlg()
148 // purpose  :
149 //=================================================================================
150 GEOMToolsGUI_TransparencyDlg::~GEOMToolsGUI_TransparencyDlg()
151 {
152     // no need to delete child widgets, Qt does it all for us
153 }
154
155
156 //=======================================================================
157 // function : ClickOnOk()
158 // purpose  :
159 //=======================================================================
160 void GEOMToolsGUI_TransparencyDlg::ClickOnOk()
161 {
162   accept();
163   return;
164 }
165
166
167 //=======================================================================
168 // function : ClickOnClose()
169 // purpose  :
170 //=======================================================================
171 void GEOMToolsGUI_TransparencyDlg::ClickOnClose()
172 {
173   accept();
174   return;
175 }
176
177
178 //=================================================================================
179 // function : ValueHasChanged()
180 // purpose  : Called when value of slider change
181 //          : or the first time as initilisation
182 //=================================================================================
183 void GEOMToolsGUI_TransparencyDlg::ValueHasChanged( int newValue )
184 {
185   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
186   if ( !app )
187     return;
188   SalomeApp_SelectionMgr* aSelMgr = app->selectionMgr();
189   if ( !aSelMgr )
190     return;
191   SALOME_ListIO selected;
192   aSelMgr->selectedObjects( selected );
193   if ( selected.IsEmpty() )
194     return;
195
196   Handle(SALOME_InteractiveObject) FirstIOS =  selected.First();
197   if ( FirstIOS.IsNull() )
198     return;
199         
200   SUIT_ViewWindow* window = app->desktop()->activeWindow();
201   bool isOCC = ( window && window->getViewManager()->getType() == OCCViewer_Viewer::Type() );
202   bool isVTK = ( window && window->getViewManager()->getType() == VTKViewer_Viewer::Type() );
203
204   if ( isVTK ) {
205     SVTK_ViewWindow* vtkVW = dynamic_cast<SVTK_ViewWindow*>( window );
206     if ( !vtkVW )
207       return;
208     SVTK_RenderWindowInteractor* rwi = vtkVW->getRWInteractor();
209     if ( myFirstInit ) {        
210       myFirstInit = false;
211       float transp = (rwi->GetTransparency(FirstIOS))*10.0;
212       mySlider->setValue(int(transp));
213       return;
214     }
215
216     SUIT_OverrideCursor();
217     for ( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
218       rwi->SetTransparency( It.Value(), newValue/10.0 );
219     }
220     rwi->Render();
221   } // if ( isVTK )
222         
223   else if ( isOCC ) {
224     GEOMBase* gb = new GEOMBase();
225     Standard_Boolean found;
226     Handle(GEOM_AISShape) aisShape;
227     if ( myFirstInit ) {
228       myFirstInit = false;
229       aisShape = gb->ConvertIOinGEOMAISShape( FirstIOS, found );
230       if( !found )
231         return;
232       float transp = (int(aisShape->Transparency() * 10.0 + 0.001));
233       mySlider->setValue(int(transp));
234       return;
235     }
236     
237     SUIT_OverrideCursor();
238     OCCViewer_Viewer* vm = dynamic_cast<OCCViewer_Viewer*>( window->getViewManager()->getViewModel() );
239     if ( !vm )
240       return;
241     Handle(AIS_InteractiveContext) ic = vm->getAISContext();
242     for ( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
243       aisShape = gb->ConvertIOinGEOMAISShape( It.Value(), found );
244       if ( found ) {
245         ic->SetTransparency( aisShape, newValue / 10.0, false );
246         ic->Redisplay( aisShape, Standard_False, Standard_True );
247       }
248     } // for...
249     ic->UpdateCurrentViewer();
250   } // if ( isOCC )
251 }