Salome HOME
8925aeb1ac4fa624cf47198891c8a11c9141725c
[modules/geom.git] / src / GEOMGUI / GeometryGUI_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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : GeometryGUI_TransparencyDlg.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27
28 using namespace std;
29 #include "GeometryGUI_TransparencyDlg.h"
30 #include "GeometryGUI.h"
31
32 #include "QAD_RightFrame.h"
33 #include "SALOME_ListIteratorOfListIO.hxx"
34
35 #include <qframe.h>
36 #include <qlabel.h>
37 #include <qpushbutton.h>
38 #include <qslider.h>
39 #include <qlayout.h>
40 #include <qvariant.h>
41 #include <qtooltip.h>
42 #include <qwhatsthis.h>
43 #include <qapplication.h>
44 #include <qgroupbox.h>
45
46 #include "VTKViewer_ViewFrame.h"
47 #include "VTKViewer_RenderWindowInteractor.h"
48
49 //=================================================================================
50 // class    : GeometryGUI_TransparencyDlg()
51 // purpose  : Constructs a GeometryGUI_SUBSHAPE which is a child of 'parent', with the
52 //            name 'name' and widget flags set to 'f'.
53 //
54 //          : WARNING : this dialog is modal !
55 //
56 //=================================================================================
57 GeometryGUI_TransparencyDlg::GeometryGUI_TransparencyDlg( QWidget* parent,
58                                                           const char* name,
59                                                           SALOME_Selection* Sel,
60                                                           const Handle(AIS_InteractiveContext)& ic,
61                                                           bool modal,
62                                                           WFlags fl )
63   : QDialog( parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu )
64 {
65   if ( !name )
66     setName( "GeometryGUI_TransparencyDlg" );
67   resize( 152, 107 ); 
68   setCaption( tr( "GEOM_TRANSPARENCY_TITLE"  ) );
69   setSizeGripEnabled( TRUE );
70   GeometryGUI_TransparencyDlgLayout = new QGridLayout( this ); 
71   GeometryGUI_TransparencyDlgLayout->setSpacing( 6 );
72   GeometryGUI_TransparencyDlgLayout->setMargin( 11 );
73
74   /*************************************************************************/
75   QGroupBox* GroupButtons = new QGroupBox( this, "GroupButtons" );
76   GroupButtons->setColumnLayout(0, Qt::Vertical );
77   GroupButtons->layout()->setSpacing( 0 );
78   GroupButtons->layout()->setMargin( 0 );
79   QGridLayout* GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
80   GroupButtonsLayout->setAlignment( Qt::AlignTop );
81   GroupButtonsLayout->setSpacing( 6 );
82   GroupButtonsLayout->setMargin( 11 );
83   
84   buttonOk = new QPushButton( GroupButtons, "buttonOk" );
85   buttonOk->setText( tr( "GEOM_BUT_OK" ) );
86   buttonOk->setAutoDefault( TRUE );
87   buttonOk->setDefault( TRUE );
88   GroupButtonsLayout->addItem( new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum), 0, 0 );
89   GroupButtonsLayout->addWidget( buttonOk, 0, 1 );
90   GroupButtonsLayout->addItem( new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum), 0, 2 );
91
92   /*************************************************************************/
93   QGroupBox* GroupC1 = new QGroupBox( this, "GroupC1" );
94   GroupC1->setColumnLayout(0, Qt::Vertical );
95   GroupC1->layout()->setSpacing( 0 );
96   GroupC1->layout()->setMargin( 0 );
97   QGridLayout* GroupC1Layout = new QGridLayout( GroupC1->layout() );
98   GroupC1Layout->setAlignment( Qt::AlignTop );
99   GroupC1Layout->setSpacing( 6 );
100   GroupC1Layout->setMargin( 11 );
101   
102   TextLabelOpaque = new QLabel( GroupC1, "TextLabelOpaque" );
103   TextLabelOpaque->setText( tr( "GEOM_TRANSPARENCY_OPAQUE"  ) );
104   TextLabelOpaque->setAlignment( int( QLabel::AlignLeft ) );
105   GroupC1Layout->addWidget( TextLabelOpaque, 0, 0 );
106   GroupC1Layout->addItem( new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum ), 0, 1 );
107   
108   TextLabelTransparent = new QLabel( GroupC1, "TextLabelTransparent" );
109   TextLabelTransparent->setText( tr( "GEOM_TRANSPARENCY_TRANSPARENT"  ) );
110   TextLabelTransparent->setAlignment( int( QLabel::AlignRight ) );
111   GroupC1Layout->addWidget( TextLabelTransparent, 0, 2 );
112   
113   Slider1 = new QSlider( 0, 10, 1, 5, Horizontal, GroupC1, "Slider1" );
114   Slider1->setMinimumSize( 300, 0 );
115   Slider1->setTickmarks( QSlider::Left );
116   GroupC1Layout->addMultiCellWidget( Slider1, 1, 1, 0, 2 );
117   /*************************************************************************/
118   
119   GeometryGUI_TransparencyDlgLayout->addWidget( GroupC1,      0,  0 );
120   GeometryGUI_TransparencyDlgLayout->addWidget( GroupButtons, 1, 0 );
121   
122   /* Initialisations */
123   this->myGeomGUI = GeometryGUI::GetGeometryGUI() ;
124   this->myIc  = ic ;
125   this->mySel = Sel ;
126   
127   /* First call valueChanged() method for initialisation               */
128   /* The default value of transparency will change with the selection  */
129   this->myFirstInit = true ;
130 //  Slider1->setMaxValue( 10 );
131 //  Slider1->setValue( 5 ) ;
132   this->ValueHasChanged( Slider1->value() ) ;
133   
134   // signals and slots connections : after ValueHasChanged()
135   connect( buttonOk, SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
136   connect( Slider1, SIGNAL( valueChanged(int) ), this, SLOT( ValueHasChanged(int) ) );
137   
138   /* Move widget on the botton right corner of main widget */
139   int x, y ;
140   myGeomGUI->DefineDlgPosition( this, x, y ) ;
141   this->move( x, y ) ;
142   this->show() ; /* Displays this Dialog */
143 }
144
145
146
147
148 //=================================================================================
149 // function : ~GeometryGUI_TransparencyDlg()
150 // purpose  :
151 //=================================================================================
152 GeometryGUI_TransparencyDlg::~GeometryGUI_TransparencyDlg()
153 {
154     // no need to delete child widgets, Qt does it all for us
155 }
156
157
158 //=======================================================================
159 // function : ClickOnOk()
160 // purpose  :
161 //=======================================================================
162 void GeometryGUI_TransparencyDlg::ClickOnOk()
163 {
164   accept() ;
165   return ;
166 }
167
168
169 //=======================================================================
170 // function : ClickOnClose()
171 // purpose  :
172 //=======================================================================
173 void GeometryGUI_TransparencyDlg::ClickOnClose()
174 {
175   accept() ;
176   return ;
177 }
178
179
180 //=================================================================================
181 // function : ValueHasChanged()
182 // purpose  : Called when value of slider change
183 //          : or the first time as initilisation
184 //=================================================================================
185 void GeometryGUI_TransparencyDlg::ValueHasChanged( int newValue )
186 {
187
188   if ( myGeomGUI->GetActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_VTK ) {
189     // VTK
190     //    vtkQGLRenderWindowInteractor* myRenderInter= myGeomGUI->GetActiveStudy()->getActiveStudyFrame()->getRightFrame()->getVTKView()->getRWInteractor();
191     VTKViewer_RenderWindowInteractor* myRenderInter= ((VTKViewer_ViewFrame*)myGeomGUI->GetActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getRWInteractor();
192     SALOME_ListIteratorOfListIO It( this->mySel->StoredIObjects() );
193
194     Handle(SALOME_InteractiveObject) FirstIOS =  mySel->firstIObject();
195     if( !FirstIOS.IsNull() ) {
196       
197       /* The first time as initialisation */
198       if( this->myFirstInit ) { 
199         this->myFirstInit = false ;
200         float transp = ( myRenderInter->GetTransparency(FirstIOS))*10.0 ;
201         this->Slider1->setValue( int(transp) ) ;
202       }
203     }
204     
205     QApplication::setOverrideCursor( Qt::waitCursor );
206     for( ;It.More(); It.Next() ) {
207       Handle(SALOME_InteractiveObject) IOS = It.Value();
208       myRenderInter->SetTransparency( IOS, newValue/10.0 );
209     }
210     QApplication::restoreOverrideCursor();
211   }
212
213   else if ( myGeomGUI->GetActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC ) {
214     // OCC  
215     SALOME_ListIteratorOfListIO It( this->mySel->StoredIObjects() );
216     Handle(SALOME_InteractiveObject) FirstIOS = mySel->firstIObject() ;
217     if( !FirstIOS.IsNull() ) {
218       
219       /* The first time as initialisation */
220       if( this->myFirstInit ) {
221         this->myFirstInit = false ;
222         Standard_Boolean found;
223         Handle(GEOM_AISShape) Shape = myGeomGUI->ConvertIOinGEOMAISShape(FirstIOS, found);
224         if( !found ) {
225           return ;
226         }
227         float transp = ( int( Shape->Transparency() * 10.0 + 0.001) );
228         this->Slider1->setValue(int(transp) ) ;
229         return ;
230       }
231     }
232
233     QApplication::setOverrideCursor( Qt::waitCursor );
234     for( ;It.More(); It.Next() ) {
235       Handle(SALOME_InteractiveObject) IObject = It.Value();
236       Standard_Boolean found;
237       Handle(GEOM_AISShape) Shape = myGeomGUI->ConvertIOinGEOMAISShape(IObject, found);      
238       if( !found ) {
239         QApplication::restoreOverrideCursor();
240         return ;
241       }
242       this->myIc->SetTransparency( Shape, newValue / 10.0, false );
243       myIc->Redisplay( Shape, Standard_False, Standard_True );
244     }
245     myIc->UpdateCurrentViewer();
246   }
247   QApplication::restoreOverrideCursor();
248   return ;
249 }