]> SALOME platform Git repositories - modules/visu.git/blob - src/VISUGUI/VisuGUI_TransparencyDlg.cxx
Salome HOME
Merge from V5_1_main branch 24/11/2010
[modules/visu.git] / src / VISUGUI / VisuGUI_TransparencyDlg.cxx
1 //  Copyright (C) 2007-2010  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
23 //  VISU VISUGUI : GUI of VISU component
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 #include "SUIT_Session.h"
39 #include "SUIT_MessageBox.h"
40 #include "SUIT_ResourceMgr.h"
41
42 #include "SALOME_ListIO.hxx"
43 #include "SALOME_ListIteratorOfListIO.hxx"
44 #include "SALOME_InteractiveObject.hxx"
45
46 #include "LightApp_Study.h"
47 #include "LightApp_SelectionMgr.h"
48 #include "LightApp_Application.h"
49
50 #include "SVTK_ViewWindow.h"
51
52 // QT Includes
53 #include <QLabel>
54 #include <QPushButton>
55 #include <QSlider>
56 #include <QLayout>
57 #include <QGroupBox>
58 #include <QKeyEvent>
59
60 const int DEFAULT_OPACITY = 100;
61 const int UNDEF_OPACITY = -1;
62
63 //=================================================================================
64 // class    : VisuGUI_TransparencyDlg()
65 // purpose  :
66 //
67 //=================================================================================
68 VisuGUI_TransparencyDlg::VisuGUI_TransparencyDlg( VisuGUI* theModule,
69                                                   bool modal )
70   : QDialog( VISU::GetDesktop( theModule ), Qt::WindowTitleHint | Qt::WindowSystemMenuHint ),
71     myModule(theModule),
72     mySelectionMgr( VISU::GetSelectionMgr( theModule ) ),
73     myViewWindow( VISU::GetActiveViewWindow<SVTK_ViewWindow>(theModule) )
74 {
75   setModal( modal );
76   setAttribute( Qt::WA_DeleteOnClose );
77     
78   setWindowTitle(tr("TRANSPARENCY_TITLE" ));
79   setSizeGripEnabled(TRUE);
80   QGridLayout* VisuGUI_TransparencyDlgLayout = new QGridLayout(this);
81   VisuGUI_TransparencyDlgLayout->setSpacing(6);
82   VisuGUI_TransparencyDlgLayout->setMargin(11);
83
84   /*************************************************************************/
85   QGroupBox* GroupC1 = new QGroupBox(this);
86   //GroupC1->setColumnLayout(0, Qt::Vertical);
87   //GroupC1->layout()->setSpacing(0);
88   //GroupC1->layout()->setMargin(0);
89   QGridLayout* GroupC1Layout = new QGridLayout(GroupC1);
90   GroupC1Layout->setAlignment(Qt::AlignTop);
91   GroupC1Layout->setSpacing(6);
92   GroupC1Layout->setMargin(11);
93
94   TextLabelTransparent = new QLabel(tr("TRANSPARENCY_TRANSPARENT" ), GroupC1);
95   TextLabelTransparent->setAlignment(Qt::AlignLeft);
96   GroupC1Layout->addWidget(TextLabelTransparent, 0, 0);
97
98   ValueLab = new QLabel(GroupC1);
99   ValueLab->setAlignment(Qt::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(tr("TRANSPARENCY_OPAQUE" ), GroupC1);
105   TextLabelOpaque->setAlignment(Qt::AlignRight);
106   GroupC1Layout->addWidget(TextLabelOpaque, 0, 2);
107
108   Slider1 = new QSlider( Qt::Horizontal, GroupC1 );
109   Slider1->setMinimum( 0 );
110   Slider1->setMaximum( 100 );
111   Slider1->setPageStep( 10 );
112   Slider1->setSingleStep( 1 );  
113   Slider1->setFocusPolicy(Qt::NoFocus);
114   Slider1->setMinimumSize(300, 0);
115   Slider1->setTickPosition(QSlider::TicksAbove);
116   Slider1->setTickInterval(10);
117   Slider1->setTracking(true);
118   GroupC1Layout->addWidget(Slider1, 1, 0, 1, 3);
119
120   /*************************************************************************/
121   QGroupBox* GroupButtons = new QGroupBox(this);
122   //GroupButtons->setColumnLayout(0, Qt::Vertical);
123   //GroupButtons->layout()->setSpacing(0);
124   //GroupButtons->layout()->setMargin(0);
125   QGridLayout* GroupButtonsLayout = new QGridLayout(GroupButtons);
126   GroupButtonsLayout->setAlignment(Qt::AlignTop);
127   GroupButtonsLayout->setSpacing(6);
128   GroupButtonsLayout->setMargin(11);
129
130   buttonOk = new QPushButton(GroupButtons);
131   buttonOk->setText(tr("BUT_CLOSE"));
132   buttonOk->setAutoDefault(TRUE);
133   buttonOk->setDefault(TRUE);
134
135   buttonHelp = new QPushButton(GroupButtons);
136   buttonHelp->setText(tr("BUT_HELP"));
137   buttonHelp->setAutoDefault(TRUE);
138
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
143   VisuGUI_TransparencyDlgLayout->addWidget(GroupC1,      0, 0);
144   VisuGUI_TransparencyDlgLayout->addWidget(GroupButtons, 1, 0);
145
146   // Initial state
147   this->onSelectionChanged();
148
149   // signals and slots connections : after ValueHasChanged()
150   connect(buttonOk, SIGNAL(clicked()),         this, SLOT(ClickOnOk()));
151   connect(buttonHelp, SIGNAL(clicked()),       this, SLOT(ClickOnHelp()));
152   connect(Slider1,  SIGNAL(valueChanged(int)), this, SLOT(SetTransparency()));
153   connect(Slider1,  SIGNAL(sliderMoved(int)),  this, SLOT(ValueHasChanged()));
154   connect(mySelectionMgr,  SIGNAL(currentSelectionChanged()), this, SLOT(onSelectionChanged()));
155 }
156
157 //=================================================================================
158 // function : ~VisuGUI_TransparencyDlg()
159 // purpose  :
160 //=================================================================================
161 VisuGUI_TransparencyDlg::~VisuGUI_TransparencyDlg()
162 {
163   // no need to delete child widgets, Qt does it all for us
164 }
165
166 //=======================================================================
167 // function : ClickOnOk()
168 // purpose  :
169 //=======================================================================
170 void VisuGUI_TransparencyDlg::ClickOnOk()
171 {
172   close();
173 }
174
175 //=======================================================================
176 // function : ClickOnHelp()
177 // purpose  :
178 //=======================================================================
179 void VisuGUI_TransparencyDlg::ClickOnHelp()
180 {
181   QString aHelpFileName = "viewing_3d_presentations_page.html#width_opacity_anchor";
182   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
183   if (app) {
184     VisuGUI* aVisuGUI = dynamic_cast<VisuGUI*>( app->activeModule() );
185     app->onHelpContextModule(aVisuGUI ? app->moduleName(aVisuGUI->moduleName()) : QString(""), aHelpFileName);
186   }
187   else {
188                 QString platform;
189 #ifdef WIN32
190                 platform = "winapplication";
191 #else
192                 platform = "application";
193 #endif
194     SUIT_MessageBox::warning(0, tr("WRN_WARNING"),
195                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
196                              arg(app->resourceMgr()->stringValue("ExternalBrowser", platform)).arg(aHelpFileName),
197                              tr("BUT_OK"));
198   }
199 }
200
201 //=================================================================================
202 // function : SetTransparency()
203 // purpose  : Called when value of slider change
204 //          : or the first time as initilisation
205 //=================================================================================
206 void VisuGUI_TransparencyDlg::SetTransparency()
207 {
208   if( myViewWindow ) {
209     SUIT_OverrideCursor wc;
210     float opacity = this->Slider1->value() / 100.;
211
212     SALOME_ListIO aList;
213     mySelectionMgr->selectedObjects(aList);
214
215     SALOME_ListIteratorOfListIO anIter(aList);
216     for (; anIter.More(); anIter.Next()) {
217       Handle(SALOME_InteractiveObject) anIO = anIter.Value();
218       VISU_Actor* anActor = VISU::FindActor(VISU::GetAppStudy(myModule), myViewWindow, anIO->getEntry());
219       if (anActor)
220         anActor->SetOpacity(opacity);
221       else {
222         VISU_ActorBase* anActor = VISU::FindActorBase(VISU::GetAppStudy(myModule), myViewWindow, anIO->getEntry());
223         if (anActor)
224           anActor->SetOpacity(opacity);
225       }
226     }
227     myViewWindow->Repaint();
228   }
229   ValueHasChanged();
230 }
231
232 //=================================================================================
233 // function : ValueHasChanged()
234 // purpose  : Called when user moves a slider
235 //=================================================================================
236 void VisuGUI_TransparencyDlg::ValueHasChanged()
237 {
238   ValueLab->setText(QString::number(this->Slider1->value()) + "%");
239 }
240
241 //=================================================================================
242 // function : onSelectionChanged()
243 // purpose  : Called when selection is changed
244 //=================================================================================
245 void VisuGUI_TransparencyDlg::onSelectionChanged()
246 {
247   if ( myViewWindow ) {
248     int opacity = UNDEF_OPACITY;
249
250     SALOME_ListIO aList;
251     mySelectionMgr->selectedObjects(aList);
252
253     SALOME_ListIteratorOfListIO It (aList);
254     for (; It.More(); It.Next()) {
255       Handle(SALOME_InteractiveObject) IO = It.Value();
256       if (!IO.IsNull()) {
257         int op = UNDEF_OPACITY;
258         VISU_Actor* anActor = VISU::FindActor(VISU::GetAppStudy(myModule), myViewWindow, IO->getEntry());
259         if (anActor) {
260           op = int(anActor->GetOpacity() * 100. + 0.5);
261         }
262         else {
263           VISU_ActorBase* anActor = VISU::FindActorBase(VISU::GetAppStudy(myModule), myViewWindow, IO->getEntry());
264           if (anActor)
265             op = int(anActor->GetOpacity() * 100. + 0.5);
266         }
267         if ( op != UNDEF_OPACITY ) {
268           if ( opacity == UNDEF_OPACITY )
269             opacity = op;
270           else if ( opacity != op ) {
271             opacity = DEFAULT_OPACITY;
272             break;
273           }
274         }
275       }
276     }
277     Slider1->setValue( opacity == UNDEF_OPACITY ? DEFAULT_OPACITY : opacity );
278   }
279   ValueHasChanged();
280 }
281
282 //=================================================================================
283 // function : keyPressEvent()
284 // purpose  :
285 //=================================================================================
286 void VisuGUI_TransparencyDlg::keyPressEvent( QKeyEvent* e )
287 {
288   QDialog::keyPressEvent( e );
289   if ( e->isAccepted() )
290     return;
291
292   if ( e->key() == Qt::Key_F1 )
293     {
294       e->accept();
295       ClickOnHelp();
296     }
297 }