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