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