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