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