]> SALOME platform Git repositories - modules/visu.git/blob - src/VISUGUI/VisuGUI_TransparencyDlg.cxx
Salome HOME
0227625a267af9cda64ffa94f75fae9ade683ce3
[modules/visu.git] / src / VISUGUI / VisuGUI_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 //  VISU VISUGUI : GUI of VISU component
23 //  File   : VisuGUI_TransparencyDlg.cxx
24 //  Author : Nicolas REJNERI
25 //  Module : VISU
26 //  $Header$
27 //
28 #include "VisuGUI_TransparencyDlg.h"
29
30 #include "VisuGUI.h"
31 #include "VisuGUI_ViewTools.h"
32 #include "VisuGUI_Tools.h"
33 #include "VISU_Actor.h"
34
35 #include "SUIT_Desktop.h"
36 #include "SUIT_OverrideCursor.h"
37 #include "SUIT_Session.h"
38 #include "SUIT_MessageBox.h"
39 #include "SUIT_ResourceMgr.h"
40
41 #include "SALOME_ListIO.hxx"
42 #include "SALOME_ListIteratorOfListIO.hxx"
43 #include "SALOME_InteractiveObject.hxx"
44
45 #include "LightApp_Study.h"
46 #include "LightApp_SelectionMgr.h"
47 #include "LightApp_Application.h"
48
49 #include "SVTK_ViewWindow.h"
50
51 // QT Includes
52 #include <QLabel>
53 #include <QPushButton>
54 #include <QSlider>
55 #include <QLayout>
56 #include <QGroupBox>
57 #include <QKeyEvent>
58
59 using namespace std;
60
61 //=================================================================================
62 // class    : VisuGUI_TransparencyDlg()
63 // purpose  :
64 //
65 //=================================================================================
66 VisuGUI_TransparencyDlg::VisuGUI_TransparencyDlg( VisuGUI* theModule,
67                                                   bool modal )
68      : QDialog( VISU::GetDesktop( theModule ), Qt::WindowTitleHint | Qt::WindowSystemMenuHint ),
69      myModule(theModule),
70      mySelectionMgr( VISU::GetSelectionMgr( theModule ) ),
71      myViewWindow( VISU::GetActiveViewWindow<SVTK_ViewWindow>(theModule) )
72 {
73   setModal( modal );
74   setAttribute( Qt::WA_DeleteOnClose );
75     
76   setWindowTitle(tr("TRANSPARENCY_TITLE" ));
77   setSizeGripEnabled(TRUE);
78   QGridLayout* VisuGUI_TransparencyDlgLayout = new QGridLayout(this);
79   VisuGUI_TransparencyDlgLayout->setSpacing(6);
80   VisuGUI_TransparencyDlgLayout->setMargin(11);
81
82   /*************************************************************************/
83   QGroupBox* GroupC1 = new QGroupBox(this);
84   //GroupC1->setColumnLayout(0, Qt::Vertical);
85   //GroupC1->layout()->setSpacing(0);
86   //GroupC1->layout()->setMargin(0);
87   QGridLayout* GroupC1Layout = new QGridLayout(GroupC1);
88   GroupC1Layout->setAlignment(Qt::AlignTop);
89   GroupC1Layout->setSpacing(6);
90   GroupC1Layout->setMargin(11);
91
92   TextLabelTransparent = new QLabel(tr("TRANSPARENCY_TRANSPARENT" ), GroupC1);
93   TextLabelTransparent->setAlignment(Qt::AlignLeft);
94   GroupC1Layout->addWidget(TextLabelTransparent, 0, 0);
95
96   ValueLab = new QLabel(GroupC1);
97   ValueLab->setAlignment(Qt::AlignCenter);
98   ValueLab->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
99   QFont fnt = ValueLab->font(); fnt.setBold(true); ValueLab->setFont(fnt);
100   GroupC1Layout->addWidget(ValueLab, 0, 1);
101
102   TextLabelOpaque = new QLabel(tr("TRANSPARENCY_OPAQUE" ), GroupC1);
103   TextLabelOpaque->setAlignment(Qt::AlignRight);
104   GroupC1Layout->addWidget(TextLabelOpaque, 0, 2);
105
106   Slider1 = new QSlider( Qt::Horizontal, GroupC1 );
107   Slider1->setMinimum( 0 );
108   Slider1->setMaximum( 100 );
109   Slider1->setPageStep( 10 );
110   Slider1->setSingleStep( 1 );  
111   Slider1->setFocusPolicy(Qt::NoFocus);
112   Slider1->setMinimumSize(300, 0);
113   Slider1->setTickPosition(QSlider::TicksAbove);
114   Slider1->setTickInterval(10);
115   Slider1->setTracking(true);
116   GroupC1Layout->addWidget(Slider1, 1, 0, 1, 3);
117
118   /*************************************************************************/
119   QGroupBox* GroupButtons = new QGroupBox(this);
120   //GroupButtons->setColumnLayout(0, Qt::Vertical);
121   //GroupButtons->layout()->setSpacing(0);
122   //GroupButtons->layout()->setMargin(0);
123   QGridLayout* GroupButtonsLayout = new QGridLayout(GroupButtons);
124   GroupButtonsLayout->setAlignment(Qt::AlignTop);
125   GroupButtonsLayout->setSpacing(6);
126   GroupButtonsLayout->setMargin(11);
127
128   buttonOk = new QPushButton(GroupButtons);
129   buttonOk->setText(tr("BUT_CLOSE"));
130   buttonOk->setAutoDefault(TRUE);
131   buttonOk->setDefault(TRUE);
132
133   buttonHelp = new QPushButton(GroupButtons);
134   buttonHelp->setText(tr("BUT_HELP"));
135   buttonHelp->setAutoDefault(TRUE);
136
137   GroupButtonsLayout->addWidget(buttonOk, 0, 0);
138   GroupButtonsLayout->addItem(new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum), 0, 1);
139   GroupButtonsLayout->addWidget(buttonHelp, 0, 2);
140
141   VisuGUI_TransparencyDlgLayout->addWidget(GroupC1,      0, 0);
142   VisuGUI_TransparencyDlgLayout->addWidget(GroupButtons, 1, 0);
143
144   // Initial state
145   this->onSelectionChanged();
146
147   // signals and slots connections : after ValueHasChanged()
148   connect(buttonOk, SIGNAL(clicked()),         this, SLOT(ClickOnOk()));
149   connect(buttonHelp, SIGNAL(clicked()),       this, SLOT(ClickOnHelp()));
150   connect(Slider1,  SIGNAL(valueChanged(int)), this, SLOT(SetTransparency()));
151   connect(Slider1,  SIGNAL(sliderMoved(int)),  this, SLOT(ValueHasChanged()));
152   connect(mySelectionMgr,  SIGNAL(currentSelectionChanged()), this, SLOT(onSelectionChanged()));
153 }
154
155 //=================================================================================
156 // function : ~VisuGUI_TransparencyDlg()
157 // purpose  :
158 //=================================================================================
159 VisuGUI_TransparencyDlg::~VisuGUI_TransparencyDlg()
160 {
161   // no need to delete child widgets, Qt does it all for us
162 }
163
164 //=======================================================================
165 // function : ClickOnOk()
166 // purpose  :
167 //=======================================================================
168 void VisuGUI_TransparencyDlg::ClickOnOk()
169 {
170   close();
171 }
172
173 //=======================================================================
174 // function : ClickOnHelp()
175 // purpose  :
176 //=======================================================================
177 void VisuGUI_TransparencyDlg::ClickOnHelp()
178 {
179   QString aHelpFileName = "viewing_3d_presentations_page.html#width_opacity_anchor";
180   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
181   if (app) {
182     VisuGUI* aVisuGUI = dynamic_cast<VisuGUI*>( app->activeModule() );
183     app->onHelpContextModule(aVisuGUI ? app->moduleName(aVisuGUI->moduleName()) : QString(""), aHelpFileName);
184   }
185   else {
186                 QString platform;
187 #ifdef WIN32
188                 platform = "winapplication";
189 #else
190                 platform = "application";
191 #endif
192     SUIT_MessageBox::warning(0, tr("WRN_WARNING"),
193                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
194                              arg(app->resourceMgr()->stringValue("ExternalBrowser", platform)).arg(aHelpFileName),
195                              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 VisuGUI_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 anIter(aList);
214     for (; anIter.More(); anIter.Next()) {
215       Handle(SALOME_InteractiveObject) anIO = anIter.Value();
216       VISU_Actor* anActor = VISU::FindActor(VISU::GetAppStudy(myModule), myViewWindow, anIO->getEntry());
217       if (anActor)
218         anActor->SetOpacity(opacity);
219       else {
220         VISU_ActorBase* anActor = VISU::FindActorBase(VISU::GetAppStudy(myModule), myViewWindow, anIO->getEntry());
221       if (anActor)
222         anActor->SetOpacity(opacity);
223       }
224     }
225     myViewWindow->Repaint();
226   }
227   ValueHasChanged();
228 }
229
230 //=================================================================================
231 // function : ValueHasChanged()
232 // purpose  : Called when user moves a slider
233 //=================================================================================
234 void VisuGUI_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 VisuGUI_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         VISU_Actor* anActor = VISU::FindActor(VISU::GetAppStudy(myModule), myViewWindow, 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           VISU_Actor* anActor = VISU::FindActor(VISU::GetAppStudy(myModule), myViewWindow, 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 VisuGUI_TransparencyDlg::keyPressEvent( QKeyEvent* e )
290 {
291   QDialog::keyPressEvent( e );
292   if ( e->isAccepted() )
293     return;
294
295   if ( e->key() == Qt::Key_F1 )
296     {
297       e->accept();
298       ClickOnHelp();
299     }
300 }