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