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