]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMToolsGUI/GEOMToolsGUI_TransparencyDlg.cxx
Salome HOME
Bug 0020413: Dump file has many GetMainShape instructions.
[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   : GEOMBase_TransparencyDlg.cxx
24 //  Author : Lucien PIGNOLONI
25 //  Module : GEOM
26 //
27 #include "GEOMToolsGUI_TransparencyDlg.h"
28 #include "GEOMBase.h"
29 #include "GEOM_AISShape.hxx"
30 #include "GeometryGUI.h"
31
32 #include "SALOME_ListIO.hxx"
33 #include "SALOME_ListIteratorOfListIO.hxx"
34
35 #include <SVTK_ViewModel.h>
36 #include <SVTK_ViewWindow.h>
37 #include <SVTK_View.h>
38
39 #include <OCCViewer_ViewModel.h>
40 #include <OCCViewer_ViewWindow.h>
41
42 #include <SUIT_ViewManager.h>
43 #include <SUIT_Application.h>
44 #include <SUIT_Desktop.h>
45 #include <SUIT_MessageBox.h>
46 #include <SUIT_ResourceMgr.h>
47 #include <SUIT_Session.h>
48 #include <SUIT_OverrideCursor.h>
49
50 #include <SalomeApp_Application.h>
51 #include <LightApp_Application.h>
52 #include <LightApp_SelectionMgr.h>
53
54 #include <qframe.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
100   QPushButton* buttonHelp = new QPushButton( GroupButtons, "buttonHelp" );
101   buttonHelp->setText( tr( "GEOM_BUT_HELP" ) );
102   buttonHelp->setAutoDefault( TRUE );
103   buttonHelp->setDefault( TRUE );
104   
105   GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
106   GroupButtonsLayout->addItem( new QSpacerItem( 0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum), 0, 1 );
107   GroupButtonsLayout->addWidget( buttonHelp, 0, 2 );
108
109   /*************************************************************************/
110   QGroupBox* GroupC1 = new QGroupBox( this, "GroupC1" );
111   GroupC1->setColumnLayout(0, Qt::Vertical );
112   GroupC1->layout()->setSpacing( 0 );
113   GroupC1->layout()->setMargin( 0 );
114   QGridLayout* GroupC1Layout = new QGridLayout( GroupC1->layout() );
115   GroupC1Layout->setAlignment( Qt::AlignTop );
116   GroupC1Layout->setSpacing( 6 );
117   GroupC1Layout->setMargin( 11 );
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, 0 );
123
124   myValueLab = new QLabel(GroupC1, "myValueLab");
125   myValueLab->setAlignment(AlignCenter);
126   myValueLab->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
127   QFont fnt = myValueLab->font(); fnt.setBold(true); myValueLab->setFont(fnt);
128   GroupC1Layout->addWidget(myValueLab, 0, 1);
129   
130   QLabel* TextLabelOpaque = new QLabel( GroupC1, "TextLabelOpaque" );
131   TextLabelOpaque->setText( tr( "GEOM_TRANSPARENCY_OPAQUE"  ) );
132   TextLabelOpaque->setAlignment( int( QLabel::AlignLeft ) );
133   GroupC1Layout->addWidget( TextLabelOpaque, 0, 2 );
134
135   //  GroupC1Layout->addItem( new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum ), 0, 1 );
136   
137   mySlider = new QSlider( 0, 10, 1, 5, Horizontal, GroupC1, "mySlider" );
138   mySlider->setFocusPolicy(QWidget::NoFocus);
139   mySlider->setMinimumSize( 300, 0 );
140   mySlider->setTickmarks( QSlider::Above );
141   mySlider->setTickInterval( 10 );
142   mySlider->setMinValue( 0 );
143   mySlider->setMaxValue( 100 );
144   mySlider->setLineStep( 1 );
145   mySlider->setPageStep( 10 );
146   GroupC1Layout->addMultiCellWidget( mySlider, 1, 1, 0, 2 );
147   /*************************************************************************/
148   
149   lay->addWidget(GroupC1, 0,  0);
150   lay->addWidget(GroupButtons, 1, 0);
151     
152   /* First call valueChanged() method for initialisation               */
153   /* The default value of transparency will change with the selection  */
154   myFirstInit = true;
155   //  mySlider->setMaxValue( 10 );
156   //  mySlider->setValue( 5 ) ;
157   SetTransparency();
158   
159   myHelpFileName = "transparency_page.html";
160
161   // signals and slots connections : after ValueHasChanged()
162   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
163   connect(buttonHelp, SIGNAL(clicked()), this, SLOT(ClickOnHelp()));
164   connect(mySlider,  SIGNAL(valueChanged(int)), this, SLOT(SetTransparency()));
165   connect(mySlider,  SIGNAL(sliderMoved(int)),  this, SLOT(ValueHasChanged()));
166 }
167
168
169 //=================================================================================
170 // function : ~GEOMBase_TransparencyDlg()
171 // purpose  :
172 //=================================================================================
173 GEOMToolsGUI_TransparencyDlg::~GEOMToolsGUI_TransparencyDlg()
174 {
175     // no need to delete child widgets, Qt does it all for us
176 }
177
178
179 //=======================================================================
180 // function : ClickOnOk()
181 // purpose  :
182 //=======================================================================
183 void GEOMToolsGUI_TransparencyDlg::ClickOnOk()
184 {
185   accept();
186   return;
187 }
188
189
190 //=======================================================================
191 // function : ClickOnClose()
192 // purpose  :
193 //=======================================================================
194 void GEOMToolsGUI_TransparencyDlg::ClickOnClose()
195 {
196   accept();
197   return;
198 }
199
200 //=================================================================================
201 // function : ClickOnHelp()
202 // purpose  :
203 //=================================================================================
204 void GEOMToolsGUI_TransparencyDlg::ClickOnHelp()
205 {
206   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
207   if (app) {
208     GeometryGUI* aGeomGUI = dynamic_cast<GeometryGUI*>( app->module( "Geometry" ) );
209     app->onHelpContextModule(aGeomGUI ? app->moduleName(aGeomGUI->moduleName()) : QString(""), myHelpFileName);
210   }
211   else {
212                 QString platform;
213 #ifdef WIN32
214                 platform = "winapplication";
215 #else
216                 platform = "application";
217 #endif
218     SUIT_MessageBox::warn1(0, QObject::tr("WRN_WARNING"),
219                            QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
220                            arg(app->resourceMgr()->stringValue("ExternalBrowser", platform)).arg(myHelpFileName),
221                            QObject::tr("BUT_OK"));
222   }
223 }
224
225 //=================================================================================
226 // function : ValueHasChanged()
227 // purpose  : Called when value of slider change
228 //=================================================================================
229 void GEOMToolsGUI_TransparencyDlg::ValueHasChanged()
230 {
231   myValueLab->setText(QString::number(this->mySlider->value()) + "%");
232 }
233
234 //=================================================================================
235 // function : SetTransparency()
236 // purpose  : Called when value of slider change
237 //          : or the first time as initilisation
238 //=================================================================================
239 void GEOMToolsGUI_TransparencyDlg::SetTransparency()
240 {
241   float newValue = ( 100 - mySlider->value() ) / 100.;
242
243   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
244   if ( !app )
245     return;
246   LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
247   if ( !aSelMgr )
248     return;
249   SALOME_ListIO selected;
250   aSelMgr->selectedObjects( selected );
251   if ( selected.IsEmpty() )
252     return;
253
254   Handle(SALOME_InteractiveObject) FirstIOS =  selected.First();
255   if ( FirstIOS.IsNull() )
256     return;
257         
258   SUIT_ViewWindow* window = app->desktop()->activeWindow();
259   bool isOCC = ( window && window->getViewManager()->getType() == OCCViewer_Viewer::Type() );
260   bool isVTK = ( window && window->getViewManager()->getType() == SVTK_Viewer::Type() );
261
262   if ( isVTK ) {
263     SVTK_ViewWindow* vtkVW = dynamic_cast<SVTK_ViewWindow*>( window );
264     if ( !vtkVW )
265       return;
266     SVTK_View* aView = vtkVW->getView();
267     if ( myFirstInit ) {        
268       myFirstInit = false;
269       int transp = int (100 - ((aView->GetTransparency(FirstIOS))*100.0) + 0.5);
270       mySlider->setValue(transp);
271       ValueHasChanged();
272       return;
273     }
274
275     SUIT_OverrideCursor();
276     for ( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
277       aView->SetTransparency( It.Value(), newValue );
278     }
279     aView->Repaint();
280   } // if ( isVTK )
281         
282   else if ( isOCC ) {
283     GEOMBase* gb = new GEOMBase();
284     Standard_Boolean found;
285     Handle(GEOM_AISShape) aisShape;
286     if ( myFirstInit ) {
287       myFirstInit = false;
288       aisShape = gb->ConvertIOinGEOMAISShape( FirstIOS, found );
289       if( !found )
290         return;
291       int transp = int( 100 - ( aisShape->Transparency() * 100.0 ) + 0.5);
292       mySlider->setValue(transp);
293       ValueHasChanged();
294       return;
295     }
296     
297     SUIT_OverrideCursor();
298     OCCViewer_Viewer* vm = dynamic_cast<OCCViewer_Viewer*>( window->getViewManager()->getViewModel() );
299     if ( !vm )
300       return;
301     Handle(AIS_InteractiveContext) ic = vm->getAISContext();
302     for ( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
303       aisShape = gb->ConvertIOinGEOMAISShape( It.Value(), found );
304       if ( found ) {
305         ic->SetTransparency( aisShape, newValue, false );
306         ic->Redisplay( aisShape, Standard_False, Standard_True );
307       }
308     } // for...
309     ic->UpdateCurrentViewer();
310   } // if ( isOCC )
311
312   ValueHasChanged();
313 }
314
315 //=================================================================================
316 // function : keyPressEvent()
317 // purpose  :
318 //=================================================================================
319 void GEOMToolsGUI_TransparencyDlg::keyPressEvent( QKeyEvent* e )
320 {
321   QDialog::keyPressEvent( e );
322   if ( e->isAccepted() )
323     return;
324
325   if ( e->key() == Key_F1 )
326     {
327       e->accept();
328       ClickOnHelp();
329     }
330 }