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