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