Salome HOME
PAL9391
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_TransparencyDlg.cxx
1 //  SMESH SMESHGUI : GUI for SMESH 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   : SMESHGUI_TransparencyDlg.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SMESH
27 //  $Header$
28
29 #include "SMESHGUI_TransparencyDlg.h"
30
31 #include "SMESHGUI.h"
32 #include "SMESHGUI_VTKUtils.h"
33 #include "SMESHGUI_Utils.h"
34 #include "SMESH_Actor.h"
35
36 #include "SUIT_Desktop.h"
37 #include "SUIT_OverrideCursor.h"
38
39 #include "SALOME_ListIO.hxx"
40 #include "SALOME_ListIteratorOfListIO.hxx"
41 #include "SALOME_InteractiveObject.hxx"
42
43 #include "SalomeApp_Study.h"
44 #include "SalomeApp_SelectionMgr.h"
45
46 #include "SVTK_ViewWindow.h"
47
48 // QT Includes
49 #include <qlabel.h>
50 #include <qpushbutton.h>
51 #include <qslider.h>
52 #include <qlayout.h>
53 #include <qgroupbox.h>
54
55 using namespace std;
56
57 //=================================================================================
58 // class    : SMESHGUI_TransparencyDlg()
59 // purpose  :
60 //
61 //=================================================================================
62 SMESHGUI_TransparencyDlg::SMESHGUI_TransparencyDlg( SMESHGUI* theModule,
63                                                     const char* name,
64                                                     bool modal,
65                                                     WFlags fl)
66      : QDialog( SMESH::GetDesktop( theModule ), name, modal, WStyle_Customize | WStyle_NormalBorder |
67                 WStyle_Title | WStyle_SysMenu | WDestructiveClose ),
68      mySMESHGUI( theModule ),
69      mySelectionMgr( SMESH::GetSelectionMgr( theModule ) ),
70      myViewWindow( SMESH::GetViewWindow( theModule ) )
71 {
72   if (!name)
73     setName("SMESHGUI_TransparencyDlg");
74   setCaption(tr("SMESH_TRANSPARENCY_TITLE" ));
75   setSizeGripEnabled(TRUE);
76   QGridLayout* SMESHGUI_TransparencyDlgLayout = new QGridLayout(this);
77   SMESHGUI_TransparencyDlgLayout->setSpacing(6);
78   SMESHGUI_TransparencyDlgLayout->setMargin(11);
79
80   /*************************************************************************/
81   QGroupBox* GroupC1 = new QGroupBox(this, "GroupC1");
82   GroupC1->setColumnLayout(0, Qt::Vertical);
83   GroupC1->layout()->setSpacing(0);
84   GroupC1->layout()->setMargin(0);
85   QGridLayout* GroupC1Layout = new QGridLayout(GroupC1->layout());
86   GroupC1Layout->setAlignment(Qt::AlignTop);
87   GroupC1Layout->setSpacing(6);
88   GroupC1Layout->setMargin(11);
89
90   TextLabelTransparent = new QLabel(GroupC1, "TextLabelTransparent");
91   TextLabelTransparent->setText(tr("SMESH_TRANSPARENCY_TRANSPARENT" ));
92   TextLabelTransparent->setAlignment(AlignLeft);
93   GroupC1Layout->addWidget(TextLabelTransparent, 0, 0);
94
95   ValueLab = new QLabel(GroupC1, "ValueLab");
96   ValueLab->setAlignment(AlignCenter);
97   ValueLab->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
98   QFont fnt = ValueLab->font(); fnt.setBold(true); ValueLab->setFont(fnt);
99   GroupC1Layout->addWidget(ValueLab, 0, 1);
100
101   TextLabelOpaque = new QLabel(GroupC1, "TextLabelOpaque");
102   TextLabelOpaque->setText(tr("SMESH_TRANSPARENCY_OPAQUE" ));
103   TextLabelOpaque->setAlignment(AlignRight);
104   GroupC1Layout->addWidget(TextLabelOpaque, 0, 2);
105
106   Slider1 = new QSlider(0, 10, 1, 5, Horizontal, GroupC1, "Slider1");
107   Slider1->setFocusPolicy(QWidget::NoFocus);
108   Slider1->setMinimumSize(300, 0);
109   Slider1->setTickmarks(QSlider::Above);
110   Slider1->setTickInterval(10);
111   Slider1->setTracking(true);
112   Slider1->setMinValue(0);
113   Slider1->setMaxValue(100);
114   Slider1->setLineStep(1);
115   Slider1->setPageStep(10);
116   GroupC1Layout->addMultiCellWidget(Slider1, 1, 1, 0, 2);
117
118   /*************************************************************************/
119   QGroupBox* GroupButtons = new QGroupBox(this, "GroupButtons");
120   GroupButtons->setColumnLayout(0, Qt::Vertical);
121   GroupButtons->layout()->setSpacing(0);
122   GroupButtons->layout()->setMargin(0);
123   QGridLayout* GroupButtonsLayout = new QGridLayout(GroupButtons->layout());
124   GroupButtonsLayout->setAlignment(Qt::AlignTop);
125   GroupButtonsLayout->setSpacing(6);
126   GroupButtonsLayout->setMargin(11);
127
128   buttonOk = new QPushButton(GroupButtons, "buttonOk");
129   buttonOk->setText(tr("SMESH_BUT_CLOSE"));
130   buttonOk->setAutoDefault(TRUE);
131   buttonOk->setDefault(TRUE);
132   GroupButtonsLayout->addItem(new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum), 0, 0);
133   GroupButtonsLayout->addWidget(buttonOk, 0, 1);
134   GroupButtonsLayout->addItem(new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum), 0, 2);
135
136   SMESHGUI_TransparencyDlgLayout->addWidget(GroupC1,      0, 0);
137   SMESHGUI_TransparencyDlgLayout->addWidget(GroupButtons, 1, 0);
138
139   // Initial state
140   this->onSelectionChanged();
141
142   // signals and slots connections : after ValueHasChanged()
143   connect(buttonOk, SIGNAL(clicked()),         this, SLOT(ClickOnOk()));
144   connect(Slider1,  SIGNAL(valueChanged(int)), this, SLOT(SetTransparency()));
145   connect(Slider1,  SIGNAL(sliderMoved(int)),  this, SLOT(ValueHasChanged()));
146   connect(mySMESHGUI, SIGNAL (SignalCloseAllDialogs()), this, SLOT(ClickOnOk()));
147   connect(mySelectionMgr,  SIGNAL(currentSelectionChanged()), this, SLOT(onSelectionChanged()));
148
149   /* Move widget on the botton right corner of main widget */
150   int x, y;
151   mySMESHGUI->DefineDlgPosition(this, x, y);
152   this->move(x, y);
153   this->show();
154 }
155
156 //=================================================================================
157 // function : ~SMESHGUI_TransparencyDlg()
158 // purpose  :
159 //=================================================================================
160 SMESHGUI_TransparencyDlg::~SMESHGUI_TransparencyDlg()
161 {
162   // no need to delete child widgets, Qt does it all for us
163 }
164
165 //=======================================================================
166 // function : ClickOnOk()
167 // purpose  :
168 //=======================================================================
169 void SMESHGUI_TransparencyDlg::ClickOnOk()
170 {
171   close();
172 }
173
174 //=================================================================================
175 // function : SetTransparency()
176 // purpose  : Called when value of slider change
177 //          : or the first time as initilisation
178 //=================================================================================
179 void SMESHGUI_TransparencyDlg::SetTransparency()
180 {
181   if( myViewWindow ) {
182     SUIT_OverrideCursor wc;
183     float opacity = this->Slider1->value() / 100.;
184
185     SALOME_ListIO aList;
186     mySelectionMgr->selectedObjects(aList);
187
188     SALOME_ListIteratorOfListIO It (aList);
189     for (;It.More(); It.Next()) {
190       Handle(SALOME_InteractiveObject) IOS = It.Value();
191       SMESH_Actor* anActor = SMESH::FindActorByEntry(IOS->getEntry());
192       if (anActor)
193         anActor->SetOpacity(opacity);
194     }
195     myViewWindow->Repaint();
196   }
197   ValueHasChanged();
198 }
199
200 //=================================================================================
201 // function : ValueHasChanged()
202 // purpose  : Called when user moves a slider
203 //=================================================================================
204 void SMESHGUI_TransparencyDlg::ValueHasChanged()
205 {
206   ValueLab->setText(QString::number(this->Slider1->value()) + "%");
207 }
208
209 //=================================================================================
210 // function : onSelectionChanged()
211 // purpose  : Called when selection is changed
212 //=================================================================================
213 void SMESHGUI_TransparencyDlg::onSelectionChanged()
214 {
215   if( myViewWindow ) {
216     int opacity = 100;
217
218     SALOME_ListIO aList;
219     mySelectionMgr->selectedObjects(aList);
220
221     if (aList.Extent() == 1) {
222       Handle(SALOME_InteractiveObject) FirstIOS = aList.First();
223       if (!FirstIOS.IsNull()) {
224         SMESH_Actor* anActor = SMESH::FindActorByEntry(FirstIOS->getEntry());
225         if (anActor)
226           opacity = int(anActor->GetOpacity() * 100. + 0.5);
227       }
228     } else if (aList.Extent() > 1) {
229       SALOME_ListIteratorOfListIO It (aList);
230       int setOp = -1;
231       for (; It.More(); It.Next()) {
232         Handle(SALOME_InteractiveObject) IO = It.Value();
233         if (!IO.IsNull()) {
234           SMESH_Actor* anActor = SMESH::FindActorByEntry(IO->getEntry());
235           if (anActor) {
236             int op = int(anActor->GetOpacity() * 100. + 0.5);
237             if (setOp < 0)
238               setOp = op;
239             else if (setOp != op) {
240               setOp = 100;
241               break;
242             }
243           }
244         }
245       }
246       if (setOp >= 0)
247         opacity = setOp;
248     } else {
249     }
250     Slider1->setValue(opacity);
251   }
252   ValueHasChanged();
253 }