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