]> SALOME platform Git repositories - modules/visu.git/blob - src/VISUGUI/VisuGUI_Slider.cxx
Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/visu.git] / src / VISUGUI / VisuGUI_Slider.cxx
1 //  VISU VISUGUI : GUI of VISU 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   : VisuGUI_Slider.cxx
25 //  Author : Oleg UVAROV
26 //  Module : VISU
27
28 #include "VisuGUI_Slider.h"
29
30 #include "SUIT_ResourceMgr.h"
31 #include "SUIT_Session.h"
32
33 #include "SalomeApp_Application.h"
34 #include "SalomeApp_Study.h"
35
36 #include "LightApp_SelectionMgr.h"
37
38 #include "SALOME_ListIteratorOfListIO.hxx"
39
40 #include "VISU_PipeLine.hxx"
41
42 #include "VISU_Actor.h"
43
44 #include "VVTK_MainWindow.h"
45 #include "VVTK_ViewWindow.h"
46
47 #include "VISU_Gen_i.hh"
48 #include "VisuGUI_Module.h"
49 #include "VisuGUI_Tools.h"
50 #include "VisuGUI_Prs3dTools.h"
51
52 #include "VTKViewer_Algorithm.h"
53 #include "SVTK_Functor.h"
54
55 #include <vtkActorCollection.h>
56 #include <vtkRenderer.h>
57
58 #include <qcombobox.h>
59 #include <qfont.h>
60 #include <qlabel.h>
61 #include <qlayout.h>
62 #include <qlineedit.h>
63 #include <qtoolbutton.h>
64 #include <qtooltip.h>
65 #include <qslider.h>
66 #include <qspinbox.h>
67 #include <qtimer.h>
68
69 /*!
70   Constructor
71 */
72 VisuGUI_Slider::VisuGUI_Slider( VisuGUI_Module* theModule, 
73                                 VVTK_ViewWindow* theViewWindow,
74                                 LightApp_SelectionMgr* theSelectionMgr )
75   : QtxToolBar( InDock, theViewWindow->getMainWindow1() ),
76     myModule( theModule ),
77     myMainWindow( theViewWindow->getMainWindow1() ),
78     mySelectionMgr( theSelectionMgr )
79 {
80   setLabel(tr("TOOLBAR_TITLE"));
81   setCloseMode(QDockWindow::Undocked);
82
83   VISU::ViewManager_var aViewManager = VISU::GetVisuGen( myModule )->GetViewManager();
84   VISU::View_var aView = aViewManager->GetCurrentView();
85   if(!CORBA::is_nil(aView.in()))
86     myView3D = VISU::View3D::_narrow(aView);
87
88   SUIT_ResourceMgr* aResourceMgr = VISU::GetResourceMgr();
89
90   setHorizontallyStretchable( true );
91
92   QWidget* aWidget = new QWidget( this );
93   aWidget->setFocusPolicy(StrongFocus);
94   setWidget( aWidget );
95
96   QGridLayout* aLayout = new QGridLayout( aWidget, 2, 13, 11, 6 );
97
98   // Slider and time stamps
99   myFirstTimeStamp = new QLabel( aWidget );
100   myFirstTimeStamp->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
101   aLayout->addWidget( myFirstTimeStamp, 0, 0, Qt::AlignHCenter );
102
103   mySlider = new QSlider( aWidget );
104   mySlider->setMinValue( 0 );
105   mySlider->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
106   mySlider->setOrientation( QSlider::Horizontal );
107   mySlider->setTracking( false );
108   mySlider->setFocusPolicy(StrongFocus);
109   aLayout->addMultiCellWidget( mySlider, 0, 0, 1, 8 );
110
111   myLastTimeStamp = new QLabel( aWidget );
112   myLastTimeStamp->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
113   aLayout->addWidget( myLastTimeStamp, 0, 9, Qt::AlignHCenter );
114
115   myTimeStampsNumber = new QLabel( aWidget );
116   myTimeStampsNumber->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
117   aLayout->addWidget( myTimeStampsNumber, 0, 10, Qt::AlignHCenter );
118
119   QLabel* aCacheMemoryLabel = new QLabel( tr( "CACHE_MEMORY" ), aWidget );
120   aCacheMemoryLabel->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
121   aLayout->addWidget( aCacheMemoryLabel, 0, 11 );
122
123   myCacheMemory = new QLabel( aWidget );
124   myCacheMemory->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
125   QFontMetrics fm( myCacheMemory->font() );
126   myCacheMemory->setFixedWidth( fm.width( "9.99E+99 Mb" ) );
127
128   aLayout->addWidget( myCacheMemory, 0, 12 );
129
130   // Buttons
131 #ifdef ENABLE_AVI_AND_MORE_BUTTONS
132   myMoreButton = new QToolButton( aWidget );
133   myMoreButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
134   myMoreButton->setEnabled( false );
135   myMoreButton->setToggleButton( true );
136   myMoreButton->setIconSet( aResourceMgr->loadPixmap( "VISU", tr( "ICON_SLIDER_MORE" ) ) );
137   QToolTip::add( myMoreButton, tr( "MORE" ) );
138   aLayout->addWidget( myMoreButton, 1, 0 );
139
140   myAVIButton = new QToolButton( aWidget );
141   myAVIButton->setEnabled( false );
142   myAVIButton->setToggleButton( true );
143   myAVIButton->setIconSet( aResourceMgr->loadPixmap( "VISU", tr( "ICON_SLIDER_AVI" ) ) );
144   QToolTip::add( myAVIButton, tr( "AVI" ) );
145   aLayout->addWidget( myAVIButton, 1, 1 );
146 #endif
147
148   aLayout->addItem( new QSpacerItem( 24, 24, QSizePolicy::Expanding, QSizePolicy::Minimum ), 1, 2 );
149
150   myFirstButton = new QToolButton( aWidget );
151   myFirstButton->setIconSet( aResourceMgr->loadPixmap( "VISU", tr( "ICON_SLIDER_FIRST" ) ) );
152   aLayout->addWidget( myFirstButton, 1, 3 );
153
154   myPreviousButton = new QToolButton( aWidget );
155   myPreviousButton->setIconSet( aResourceMgr->loadPixmap( "VISU", tr( "ICON_SLIDER_PREVIOUS" ) ) );
156   aLayout->addWidget( myPreviousButton, 1, 4 );
157
158   myPlayButton = new QToolButton( aWidget );
159   myPlayButton->setIconSet( aResourceMgr->loadPixmap( "VISU", tr( "ICON_SLIDER_PLAY" ) ) );
160   myPlayButton->setToggleButton( true );
161   aLayout->addWidget( myPlayButton, 1, 5 );
162
163   myNextButton = new QToolButton( aWidget );
164   myNextButton->setIconSet( aResourceMgr->loadPixmap( "VISU", tr( "ICON_SLIDER_NEXT" ) ) );
165   aLayout->addWidget( myNextButton, 1, 6 );
166
167   myLastButton = new QToolButton( aWidget );
168   myLastButton->setIconSet( aResourceMgr->loadPixmap( "VISU", tr( "ICON_SLIDER_LAST" ) ) );
169   aLayout->addWidget( myLastButton, 1, 7 );
170
171   aLayout->addItem( new QSpacerItem( 24, 24, QSizePolicy::Expanding, QSizePolicy::Minimum ), 1, 8 );
172
173   myTimeStampStrings = new QComboBox( aWidget );
174   myTimeStampStrings->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
175   myTimeStampStrings->setFocusPolicy(StrongFocus);
176   aLayout->addWidget( myTimeStampStrings, 1, 9 );
177
178   myTimeStampIndices = new QComboBox( aWidget );
179   myTimeStampIndices->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
180   myTimeStampIndices->setFocusPolicy(StrongFocus);
181   aLayout->addWidget( myTimeStampIndices, 1, 10 );
182
183   // Speed
184   QLabel* aSpeedLabel = new QLabel( tr( "SPEED" ), aWidget );
185   aSpeedLabel->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
186   aLayout->addWidget( aSpeedLabel, 1, 11 );
187
188   mySpeedBox = new QSpinBox( 1, 100, 1, aWidget );
189   mySpeedBox->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
190   mySpeedBox->setFocusPolicy(StrongFocus);
191   mySpeedBox->setValue( 50 );
192   aLayout->addWidget( mySpeedBox, 1, 12 );
193
194   myTimer = new QTimer( this );
195
196   // Common
197   connect( mySelectionMgr, SIGNAL( currentSelectionChanged() ), SLOT( onSelectionChanged() ) );
198
199   connect( myTimeStampStrings, SIGNAL( activated( int ) ),    SLOT( onTimeStampActivated( int ) ) );
200   connect( myTimeStampIndices, SIGNAL( activated( int ) ),    SLOT( onTimeStampActivated( int ) ) );
201
202   connect( myFirstButton,      SIGNAL( clicked() ),           SLOT( onFirst() ) );
203   connect( myPreviousButton,   SIGNAL( clicked() ),           SLOT( onPrevious() ) );
204   connect( myPlayButton,       SIGNAL( toggled( bool ) ),     SLOT( onPlay( bool ) ) );
205   connect( myNextButton,       SIGNAL( clicked() ),           SLOT( onNext() ) );
206   connect( myLastButton,       SIGNAL( clicked() ),           SLOT( onLast() ) );
207
208   connect( mySlider,           SIGNAL( valueChanged( int ) ), SLOT( onValueChanged( int ) ) );
209
210   connect( mySpeedBox,         SIGNAL( valueChanged( int ) ), SLOT( onSpeedChanged( int ) ) );
211
212   connect( myTimer,            SIGNAL( timeout() ),           SLOT( onTimeout() ) );
213
214   enableControls( false );
215
216   myMainWindow->addDockWindow( this, Qt::DockBottom );
217 }
218
219 /*!
220   Destructor
221 */
222 VisuGUI_Slider::~VisuGUI_Slider()
223 {
224 }
225
226 bool VisuGUI_Slider::checkHolderList()
227 {
228   THolderList aHolderList;
229   THolderList::const_iterator anIter = myHolderList.begin();
230   THolderList::const_iterator anIterEnd = myHolderList.end();
231   for(; anIter != anIterEnd; anIter++){
232     VISU::ColoredPrs3dHolder_var aHolder = *anIter;
233     if(!aHolder->_non_existent())
234       aHolderList.push_back(aHolder);
235   }
236   myHolderList.swap(aHolderList);
237   return myHolderList.empty();
238 }
239
240 void VisuGUI_Slider::enableControls( bool on )
241 {
242   widget()->setEnabled( on );
243   if( on )
244   {
245     if( checkHolderList() )
246       return;
247
248     myTimeStampStrings->clear();
249     myTimeStampIndices->clear();
250
251     VISU::ColoredPrs3dHolder_var aHolder = myHolderList.front();
252
253     VISU::ColoredPrs3dHolder::TimeStampsRange_var aTimeStampsRange = aHolder->GetTimeStampsRange();
254     CORBA::Long aLength = aTimeStampsRange->length();
255
256     VISU::ColoredPrs3dHolder::BasicInput_var anInput = aHolder->GetBasicInput();
257     CORBA::Long aTimeStampNumber = anInput->myTimeStampNumber;
258
259     myFirstTimeStamp->setText( aTimeStampsRange[0].myTime.in() );
260     myLastTimeStamp->setText( aTimeStampsRange[aLength-1].myTime.in() );
261     myTimeStampsNumber->setText( QString("(") + QString::number( aLength ) + ")" );
262
263     int aPageStep = aLength / 10;
264     aPageStep = std::max(aPageStep, 1);
265     mySlider->setPageStep(aPageStep);
266
267     CORBA::Long a_current_index = 0;
268     for( CORBA::Long an_index = 0; an_index < aLength; an_index++ )
269     {
270       VISU::ColoredPrs3dHolder::TimeStampInfo anInfo = aTimeStampsRange[ an_index ];
271       CORBA::Long aNumber = anInfo.myNumber;
272       QString aTime = anInfo.myTime.in();
273
274       myTimeStampStrings->insertItem( aTime );
275       myTimeStampIndices->insertItem( QString::number( aNumber ) );
276
277       if( aNumber == aTimeStampNumber )
278         a_current_index = an_index;
279     }
280     // work around - to update controls' sizes
281     myTimeStampStrings->setFont(myTimeStampStrings->font());
282     myTimeStampStrings->updateGeometry();
283     
284     myTimeStampIndices->setFont(myTimeStampStrings->font());
285     myTimeStampIndices->updateGeometry();
286
287     if( a_current_index > mySlider->maxValue() )
288       mySlider->setMaxValue( aLength-1 );
289
290     myTimeStampStrings->setCurrentItem( a_current_index );
291     myTimeStampIndices->setCurrentItem( a_current_index );
292     mySlider->setValue( a_current_index );
293
294     if( a_current_index <= mySlider->maxValue() )
295       mySlider->setMaxValue( aLength-1 );
296   }
297   else
298   {
299     myPlayButton->setOn( false );
300   }
301
302 }
303
304 void VisuGUI_Slider::updateMemoryState()
305 {
306   if( checkHolderList() )
307     return;
308
309   VISU::ColoredPrs3dHolder_var aHolder = myHolderList.front();
310
311   VISU::ColoredPrs3dCache_var aCache = aHolder->GetCache();
312   CORBA::Float aCacheMemory = aCache->GetMemorySize();
313   myCacheMemory->setText( QString::number( double( aCacheMemory ), 'E', 2 ) + " Mb" );
314 }
315
316 void VisuGUI_Slider::onSelectionChanged()
317 {
318   //cout << "VisuGUI_Slider::onSelectionChanged()" << endl;
319   myHolderList.clear();
320
321   _PTR(SObject) aSObject;
322
323   SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>
324     (SUIT_Session::session()->activeApplication());
325
326   SALOME_ListIO aListIO;
327   mySelectionMgr->selectedObjects(aListIO);
328   SALOME_ListIteratorOfListIO anIter(aListIO);
329   for(; anIter.More(); anIter.Next() ){
330     Handle(SALOME_InteractiveObject) anIO = anIter.Value();
331     if (anIO->hasEntry()) {
332       SalomeApp_Study* theStudy = dynamic_cast<SalomeApp_Study*>(anApp->activeStudy());
333       _PTR(Study) aStudy = theStudy->studyDS();
334       aSObject = aStudy->FindObjectID(anIO->getEntry());
335
336       if (aSObject) {
337         CORBA::Object_var anObject = VISU::ClientSObjectToObject(aSObject);
338         if (!CORBA::is_nil(anObject)) {
339           VISU::ColoredPrs3dHolder_var aHolder = VISU::ColoredPrs3dHolder::_narrow(anObject);
340
341           if(!CORBA::is_nil(aHolder))
342           {
343             //cout << "ColoredPrs3dHolder" << endl;
344             myHolderList.push_back(aHolder);
345           }
346         }
347       }
348     }
349   }
350
351   enableControls( !checkHolderList() );
352   updateMemoryState();
353 }
354
355 void VisuGUI_Slider::onTimeStampActivated( int value )
356 {
357   mySlider->setValue( value );
358   onValueChanged( value );
359 }
360
361 void VisuGUI_Slider::onFirst()
362 {
363   int value = mySlider->minValue();
364   mySlider->setValue( value );
365 }
366
367 void VisuGUI_Slider::onPrevious()
368 {
369   int value = mySlider->value() - 1;
370   if( value >= mySlider->minValue() )
371     mySlider->setValue( value );
372 }
373
374 void VisuGUI_Slider::onPlay( bool on )
375 {
376   SUIT_ResourceMgr* aResourceMgr = VISU::GetResourceMgr();
377   if( on )
378   {
379     myPlayButton->setIconSet( aResourceMgr->loadPixmap( "VISU", tr( "ICON_SLIDER_PAUSE" ) ) );
380
381     int delay = int(5000.0 / double(mySpeedBox->value()));
382     myTimer->start( delay );
383   }
384   else
385   {
386     myTimer->stop();
387     myPlayButton->setIconSet( aResourceMgr->loadPixmap( "VISU", tr( "ICON_SLIDER_PLAY" ) ) );
388   }
389 }
390
391 void VisuGUI_Slider::onNext()
392 {
393   int value = mySlider->value() + 1;
394   if( value <= mySlider->maxValue() )
395     mySlider->setValue( value );
396 }
397
398 void VisuGUI_Slider::onLast()
399 {
400   int value = mySlider->maxValue();
401   mySlider->setValue( value );
402 }
403
404 void VisuGUI_Slider::onValueChanged( int value )
405 {
406   if( checkHolderList() ){
407     enableControls( false );
408     return;
409   }
410
411   myTimeStampStrings->setCurrentItem( value );
412   myTimeStampIndices->setCurrentItem( value );
413
414   VISU::ColoredPrs3dHolder_var aHolder = myHolderList.front();
415
416   VISU::ColoredPrs3dHolder::TimeStampsRange_var aTimeStampsRange = aHolder->GetTimeStampsRange();
417   CORBA::Long aLength = aTimeStampsRange->length();
418   if(value < 0 || aLength <= value)
419     return;
420
421   VISU::ColoredPrs3dHolder::BasicInput_var anInput = aHolder->GetBasicInput();
422   CORBA::Long aTimeStampNumber = anInput->myTimeStampNumber;
423   CORBA::Long aNumber = aTimeStampsRange[ value ].myNumber;
424   if(aNumber == aTimeStampNumber)
425     return;
426
427   THolderList::const_iterator anIter = myHolderList.begin();
428   THolderList::const_iterator anIterEnd = myHolderList.end();
429   for( ; anIter != anIterEnd; anIter++ )
430   {
431     VISU::ColoredPrs3dHolder_var aHolder = *anIter;
432     if( CORBA::is_nil( aHolder.in() ) )
433       continue;
434
435     VISU::ColoredPrs3d_var aPrs3d = aHolder->GetDevice();
436     VISU::ColoredPrs3dHolder::BasicInput_var anInput = aHolder->GetBasicInput();
437     anInput->myTimeStampNumber = aNumber;
438
439     QApplication::setOverrideCursor(Qt::waitCursor);
440     
441     aHolder->Apply( aPrs3d, anInput, myView3D );
442     
443     QApplication::restoreOverrideCursor();
444   }
445
446   updateMemoryState();
447 }
448
449 void VisuGUI_Slider::onSpeedChanged( int value )
450 {
451   if(myPlayButton->isOn()){
452     int delay = int(5000.0 / double(mySpeedBox->value()));
453     myTimer->start( delay );
454   }
455 }
456
457 void VisuGUI_Slider::onTimeout()
458 {
459   int value = mySlider->value();
460   if( value < mySlider->maxValue() )
461     onNext();
462   else
463     myPlayButton->setOn( false );
464 }