Salome HOME
Update copyright information
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_TransparencyDlg.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  SMESH SMESHGUI : GUI for SMESH component
23 //  File   : SMESHGUI_TransparencyDlg.cxx
24 //  Author : Nicolas REJNERI
25 //  Module : SMESH
26 //  $Header$
27 //
28 #include "SMESHGUI_TransparencyDlg.h"
29
30 #include "SMESHGUI.h"
31 #include "SMESHGUI_VTKUtils.h"
32 #include "SMESHGUI_Utils.h"
33 #include "SMESH_Actor.h"
34
35 #include "SUIT_Desktop.h"
36 #include "SUIT_OverrideCursor.h"
37 #include "SUIT_Session.h"
38 #include "SUIT_MessageBox.h"
39
40 #include "SALOME_ListIO.hxx"
41 #include "SALOME_ListIteratorOfListIO.hxx"
42 #include "SALOME_InteractiveObject.hxx"
43
44 #include "SalomeApp_Study.h"
45 #include "LightApp_Application.h"
46 #include "LightApp_SelectionMgr.h"
47
48 #include "SVTK_ViewWindow.h"
49
50 // QT Includes
51 #include <qlabel.h>
52 #include <qpushbutton.h>
53 #include <qslider.h>
54 #include <qlayout.h>
55 #include <qgroupbox.h>
56
57 using namespace std;
58
59 //=================================================================================
60 // class    : SMESHGUI_TransparencyDlg()
61 // purpose  :
62 //
63 //=================================================================================
64 SMESHGUI_TransparencyDlg::SMESHGUI_TransparencyDlg( SMESHGUI* theModule,
65                                                     const char* name,
66                                                     bool modal,
67                                                     WFlags fl)
68      : QDialog( SMESH::GetDesktop( theModule ), name, modal, WStyle_Customize | WStyle_NormalBorder |
69                 WStyle_Title | WStyle_SysMenu | WDestructiveClose ),
70      mySMESHGUI( theModule ),
71      mySelectionMgr( SMESH::GetSelectionMgr( theModule ) ),
72      myViewWindow( SMESH::GetViewWindow( theModule ) )
73 {
74   if (!name)
75     setName("SMESHGUI_TransparencyDlg");
76   setCaption(tr("SMESH_TRANSPARENCY_TITLE" ));
77   setSizeGripEnabled(TRUE);
78   QGridLayout* SMESHGUI_TransparencyDlgLayout = new QGridLayout(this);
79   SMESHGUI_TransparencyDlgLayout->setSpacing(6);
80   SMESHGUI_TransparencyDlgLayout->setMargin(11);
81
82   /*************************************************************************/
83   QGroupBox* GroupC1 = new QGroupBox(this, "GroupC1");
84   GroupC1->setColumnLayout(0, Qt::Vertical);
85   GroupC1->layout()->setSpacing(0);
86   GroupC1->layout()->setMargin(0);
87   QGridLayout* GroupC1Layout = new QGridLayout(GroupC1->layout());
88   GroupC1Layout->setAlignment(Qt::AlignTop);
89   GroupC1Layout->setSpacing(6);
90   GroupC1Layout->setMargin(11);
91
92   TextLabelTransparent = new QLabel(GroupC1, "TextLabelTransparent");
93   TextLabelTransparent->setText(tr("SMESH_TRANSPARENCY_TRANSPARENT" ));
94   TextLabelTransparent->setAlignment(AlignLeft);
95   GroupC1Layout->addWidget(TextLabelTransparent, 0, 0);
96
97   ValueLab = new QLabel(GroupC1, "ValueLab");
98   ValueLab->setAlignment(AlignCenter);
99   ValueLab->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
100   QFont fnt = ValueLab->font(); fnt.setBold(true); ValueLab->setFont(fnt);
101   GroupC1Layout->addWidget(ValueLab, 0, 1);
102
103   TextLabelOpaque = new QLabel(GroupC1, "TextLabelOpaque");
104   TextLabelOpaque->setText(tr("SMESH_TRANSPARENCY_OPAQUE" ));
105   TextLabelOpaque->setAlignment(AlignRight);
106   GroupC1Layout->addWidget(TextLabelOpaque, 0, 2);
107
108   Slider1 = new QSlider(0, 10, 1, 5, Horizontal, GroupC1, "Slider1");
109   Slider1->setFocusPolicy(QWidget::NoFocus);
110   Slider1->setMinimumSize(300, 0);
111   Slider1->setTickmarks(QSlider::Above);
112   Slider1->setTickInterval(10);
113   Slider1->setTracking(true);
114   Slider1->setMinValue(0);
115   Slider1->setMaxValue(100);
116   Slider1->setLineStep(1);
117   Slider1->setPageStep(10);
118   GroupC1Layout->addMultiCellWidget(Slider1, 1, 1, 0, 2);
119
120   /*************************************************************************/
121   QGroupBox* GroupButtons = new QGroupBox(this, "GroupButtons");
122   GroupButtons->setColumnLayout(0, Qt::Vertical);
123   GroupButtons->layout()->setSpacing(0);
124   GroupButtons->layout()->setMargin(0);
125   QGridLayout* GroupButtonsLayout = new QGridLayout(GroupButtons->layout());
126   GroupButtonsLayout->setAlignment(Qt::AlignTop);
127   GroupButtonsLayout->setSpacing(6);
128   GroupButtonsLayout->setMargin(11);
129
130   buttonOk = new QPushButton(GroupButtons, "buttonOk");
131   buttonOk->setText(tr("SMESH_BUT_CLOSE"));
132   buttonOk->setAutoDefault(TRUE);
133   buttonOk->setDefault(TRUE);
134   buttonHelp = new QPushButton(GroupButtons, "buttonHelp");
135   buttonHelp->setText(tr("SMESH_BUT_HELP"));
136   buttonHelp->setAutoDefault(TRUE);
137
138   //GroupButtonsLayout->addItem(new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum), 0, 0);
139   GroupButtonsLayout->addWidget(buttonOk, 0, 0);
140   GroupButtonsLayout->addItem(new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum), 0, 1);
141   GroupButtonsLayout->addWidget(buttonHelp, 0, 2);  
142   //GroupButtonsLayout->addItem(new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum), 0, 2);
143
144   SMESHGUI_TransparencyDlgLayout->addWidget(GroupC1,      0, 0);
145   SMESHGUI_TransparencyDlgLayout->addWidget(GroupButtons, 1, 0);
146
147   // Initial state
148   this->onSelectionChanged();
149
150   // signals and slots connections : after ValueHasChanged()
151   connect(buttonOk, SIGNAL(clicked()),         this, SLOT(ClickOnOk()));
152   connect(buttonHelp, SIGNAL(clicked()),       this, SLOT(ClickOnHelp()));
153   connect(Slider1,  SIGNAL(valueChanged(int)), this, SLOT(SetTransparency()));
154   connect(Slider1,  SIGNAL(sliderMoved(int)),  this, SLOT(ValueHasChanged()));
155   connect(mySMESHGUI, SIGNAL (SignalCloseAllDialogs()), this, SLOT(ClickOnOk()));
156   connect(mySelectionMgr,  SIGNAL(currentSelectionChanged()), this, SLOT(onSelectionChanged()));
157
158   myHelpFileName = "transparency_page.html";
159
160   this->show();
161 }
162
163 //=================================================================================
164 // function : ~SMESHGUI_TransparencyDlg()
165 // purpose  :
166 //=================================================================================
167 SMESHGUI_TransparencyDlg::~SMESHGUI_TransparencyDlg()
168 {
169   // no need to delete child widgets, Qt does it all for us
170 }
171
172 //=======================================================================
173 // function : ClickOnOk()
174 // purpose  :
175 //=======================================================================
176 void SMESHGUI_TransparencyDlg::ClickOnOk()
177 {
178   close();
179 }
180
181 //=================================================================================
182 // function : ClickOnHelp()
183 // purpose  :
184 //=================================================================================
185 void SMESHGUI_TransparencyDlg::ClickOnHelp()
186 {
187   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
188   if (app) 
189     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
190   else {
191                 QString platform;
192 #ifdef WIN32
193                 platform = "winapplication";
194 #else
195                 platform = "application";
196 #endif
197     SUIT_MessageBox::warn1(0, QObject::tr("WRN_WARNING"),
198                            QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
199                            arg(app->resourceMgr()->stringValue("ExternalBrowser", platform)).arg(myHelpFileName),
200                            QObject::tr("BUT_OK"));
201   }
202 }
203
204 //=================================================================================
205 // function : SetTransparency()
206 // purpose  : Called when value of slider change
207 //          : or the first time as initilisation
208 //=================================================================================
209 void SMESHGUI_TransparencyDlg::SetTransparency()
210 {
211   if( myViewWindow ) {
212     SUIT_OverrideCursor wc;
213     float opacity = this->Slider1->value() / 100.;
214
215     SALOME_ListIO aList;
216     mySelectionMgr->selectedObjects(aList);
217
218     SALOME_ListIteratorOfListIO It (aList);
219     for (;It.More(); It.Next()) {
220       Handle(SALOME_InteractiveObject) IOS = It.Value();
221       SMESH_Actor* anActor = SMESH::FindActorByEntry(IOS->getEntry());
222       if (anActor)
223         anActor->SetOpacity(opacity);
224     }
225     myViewWindow->Repaint();
226   }
227   ValueHasChanged();
228 }
229
230 //=================================================================================
231 // function : ValueHasChanged()
232 // purpose  : Called when user moves a slider
233 //=================================================================================
234 void SMESHGUI_TransparencyDlg::ValueHasChanged()
235 {
236   ValueLab->setText(QString::number(this->Slider1->value()) + "%");
237 }
238
239 //=================================================================================
240 // function : onSelectionChanged()
241 // purpose  : Called when selection is changed
242 //=================================================================================
243 void SMESHGUI_TransparencyDlg::onSelectionChanged()
244 {
245   if( myViewWindow ) {
246     int opacity = 100;
247
248     SALOME_ListIO aList;
249     mySelectionMgr->selectedObjects(aList);
250
251     if (aList.Extent() == 1) {
252       Handle(SALOME_InteractiveObject) FirstIOS = aList.First();
253       if (!FirstIOS.IsNull()) {
254         SMESH_Actor* anActor = SMESH::FindActorByEntry(FirstIOS->getEntry());
255         if (anActor)
256           opacity = int(anActor->GetOpacity() * 100. + 0.5);
257       }
258     } else if (aList.Extent() > 1) {
259       SALOME_ListIteratorOfListIO It (aList);
260       int setOp = -1;
261       for (; It.More(); It.Next()) {
262         Handle(SALOME_InteractiveObject) IO = It.Value();
263         if (!IO.IsNull()) {
264           SMESH_Actor* anActor = SMESH::FindActorByEntry(IO->getEntry());
265           if (anActor) {
266             int op = int(anActor->GetOpacity() * 100. + 0.5);
267             if (setOp < 0)
268               setOp = op;
269             else if (setOp != op) {
270               setOp = 100;
271               break;
272             }
273           }
274         }
275       }
276       if (setOp >= 0)
277         opacity = setOp;
278     } else {
279     }
280     Slider1->setValue(opacity);
281   }
282   ValueHasChanged();
283 }
284
285 //=================================================================================
286 // function : keyPressEvent()
287 // purpose  :
288 //=================================================================================
289 void SMESHGUI_TransparencyDlg::keyPressEvent( QKeyEvent* e )
290 {
291   QDialog::keyPressEvent( e );
292   if ( e->isAccepted() )
293     return;
294
295   if ( e->key() == Key_F1 )
296     {
297       e->accept();
298       ClickOnHelp();
299     }
300 }