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