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