Salome HOME
Merge from BR_V5_DEV 16Feb09
[modules/visu.git] / src / VISUGUI / VisuGUI_Sweep.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_Slider.cxx
24 //  Author : Oleg UVAROV
25 //  Module : VISU
26 //
27 #include "VisuGUI_Sweep.h"
28
29 #include "SUIT_ResourceMgr.h"
30
31 #include "LightApp_SelectionMgr.h"
32
33 #include "VISU_Actor.h"
34
35 #include "VisuGUI.h"
36 #include "VisuGUI_Tools.h"
37 #include "VisuGUI_ViewTools.h"
38 #include "VISU_ColoredPrs3d_i.hh"
39 #include "VISU_Actor.h"
40
41 #include "QtxDockWidget.h"
42 #include "QtxIntSpinBox.h"
43 #include "QtxDoubleSpinBox.h"
44
45 #include <QMainWindow>
46 #include <QComboBox>
47 #include <QFont>
48 #include <QLabel>
49 #include <QLayout>
50 #include <QLineEdit>
51 #include <QToolButton>
52 #include <QToolTip>
53 #include <QSlider>
54 #include <QGroupBox>
55 #include <QCheckBox>
56 #include <QRadioButton>
57 #include <QTimer>
58 #include <QAction>
59
60 #include <vtkMath.h>
61
62 /*!
63   Constructor
64 */
65 VisuGUI_Sweep::VisuGUI_Sweep( VisuGUI* theModule, 
66                               QMainWindow* theParent,
67                               LightApp_SelectionMgr* theSelectionMgr )
68   : QWidget( theParent )
69   , myModule( theModule )
70   , myViewWindow( NULL )
71 {
72   setWindowTitle( tr("TITLE") );
73   setObjectName( tr("TITLE") );
74
75   SUIT_ResourceMgr* aResourceMgr = VISU::GetResourceMgr();
76
77   //----------------------------------------------------------------------------
78   QVBoxLayout* aVBoxLayout = new QVBoxLayout( this );
79
80   QTabWidget* aTabWidget = new QTabWidget( this );
81   aTabWidget->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Fixed );
82   aVBoxLayout->addWidget( aTabWidget );
83
84   {
85     QWidget* aNavigationTab = new QWidget();
86     {
87       QVBoxLayout* aVBoxLayout = new QVBoxLayout( aNavigationTab );
88       {
89         QHBoxLayout* aHBoxLayout = new QHBoxLayout();
90         
91         QLabel* aStartSweepPosition = new QLabel( aNavigationTab );
92         aStartSweepPosition->setText( tr( "START_SWEEP_POSITION" ) );
93         aHBoxLayout->addWidget( aStartSweepPosition );
94       
95         mySweepSlider = new QSlider( aNavigationTab );
96         mySweepSlider->setOrientation( Qt::Horizontal );
97         mySweepSlider->setMinimum( 0 );
98         aHBoxLayout->addWidget( mySweepSlider );
99         
100         QLabel* anEndSweepPosition = new QLabel( aNavigationTab );
101         anEndSweepPosition->setText( tr( "END_SWEEP_POSITION" ) );
102         aHBoxLayout->addWidget( anEndSweepPosition );
103         
104         aVBoxLayout->addLayout( aHBoxLayout );
105       }
106       {
107         QHBoxLayout* aHBoxLayout = new QHBoxLayout();
108       
109         myFirstButton = new QToolButton( aNavigationTab );
110         myFirstButton->setIcon( aResourceMgr->loadPixmap( "VISU", tr( "ICON_SLIDER_FIRST" ) ) );
111         myFirstButton->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
112         connect( myFirstButton, SIGNAL( clicked() ), SLOT( onFirst() ) );
113         aHBoxLayout->addWidget( myFirstButton );
114         
115         myPreviousButton = new QToolButton( aNavigationTab );
116         myPreviousButton->setIcon( aResourceMgr->loadPixmap( "VISU", tr( "ICON_SLIDER_PREVIOUS" ) ) );
117         myPreviousButton->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
118         connect( myPreviousButton, SIGNAL( clicked() ), SLOT( onPrevious() ) );
119         aHBoxLayout->addWidget( myPreviousButton );
120         
121         myPlayButton = new QToolButton( aNavigationTab );
122         myPlayButton->setIcon( aResourceMgr->loadPixmap( "VISU", tr( "ICON_SLIDER_PLAY" ) ) );
123         myPlayButton->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
124         myPlayButton->setCheckable( true );
125         aHBoxLayout->addWidget( myPlayButton );
126         
127         myNextButton = new QToolButton( aNavigationTab );
128         myNextButton->setIcon( aResourceMgr->loadPixmap( "VISU", tr( "ICON_SLIDER_NEXT" ) ) );
129         myNextButton->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
130         connect( myNextButton, SIGNAL( clicked() ), SLOT( onNext() ) );
131         aHBoxLayout->addWidget( myNextButton );
132         
133         myLastButton = new QToolButton( aNavigationTab );
134         myLastButton->setIcon( aResourceMgr->loadPixmap( "VISU", tr( "ICON_SLIDER_LAST" ) ) );
135         myLastButton->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
136         connect( myLastButton, SIGNAL( clicked() ), SLOT( onLast() ) );
137         aHBoxLayout->addWidget( myLastButton );
138         
139         aVBoxLayout->addLayout( aHBoxLayout );
140       }
141       {
142         QHBoxLayout* aHBoxLayout = new QHBoxLayout();
143       
144         QToolButton* aStopButton = new QToolButton( aNavigationTab );
145         aStopButton->setIcon( aResourceMgr->loadPixmap( "VISU", tr( "ICON_SWEEP_STOP" ) ) );
146         aStopButton->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
147         aStopButton->setCheckable( false );
148         connect( aStopButton, SIGNAL( clicked( ) ), SLOT( onStop( ) ) );
149         aHBoxLayout->addWidget( aStopButton );
150         
151         myIsCycled = new QCheckBox( aNavigationTab );
152         myIsCycled->setText( tr( "IS_CYCLED" ) );
153         myIsCycled->setChecked( false );
154         aHBoxLayout->addWidget( myIsCycled );
155         
156         aVBoxLayout->addLayout( aHBoxLayout );
157       }
158
159       aTabWidget->addTab( aNavigationTab, tr( "NAVIGATION_TAB" ) );
160     }
161   }
162   {
163     QWidget* aPropertiesTab = new QWidget();
164     {
165       QGridLayout* aGridLayout = new QGridLayout( aPropertiesTab );     
166       {
167         QHBoxLayout* aHBoxLayout = new QHBoxLayout();
168         
169         QLabel* aModeAnnotation = new QLabel( aPropertiesTab );
170         aModeAnnotation->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Preferred );
171         aModeAnnotation->setText( tr( "MODE" ) );
172         aHBoxLayout->addWidget( aModeAnnotation );
173         
174         mySweepMode = new QComboBox( aPropertiesTab );
175         mySweepMode->insertItems( 0, QStringList() 
176                                   << tr( "LINEAR" ) 
177                                   << tr( "COSINUSOIDAL" ) 
178                                   << tr( "SINUSOIDAL" ) );
179         mySweepMode->setCurrentIndex( aResourceMgr->integerValue( "VISU", "sweeping_modes", 0 ) );
180         aHBoxLayout->addWidget( mySweepMode );
181         
182         myIntervals = new QComboBox( aPropertiesTab );
183         myIntervals->insertItems( 0, QStringList() 
184                                   << tr( "[ 0, +PI ]" ) 
185                                   << tr( "[ -PI, +PI ]" ) );
186         myIntervals->setCurrentIndex( aResourceMgr->integerValue( "VISU", "sweeping_is2PI", 0 ) );
187         aHBoxLayout->addWidget( myIntervals );
188         
189         aGridLayout->addLayout( aHBoxLayout, 0, 0, 1, 2 );
190       }
191       
192       QLabel* aNumberOfStepsAnnotation = new QLabel( aPropertiesTab );
193       aNumberOfStepsAnnotation->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Preferred );
194       aNumberOfStepsAnnotation->setText( tr( "NUMBER_OF_STEPS" ) );
195       aGridLayout->addWidget( aNumberOfStepsAnnotation, 1, 0, 1, 1 );
196       
197       myNumberOfSteps = new QtxIntSpinBox( aPropertiesTab );
198       connect( myNumberOfSteps, SIGNAL( valueChanged( int ) ), SLOT( onNumberOfStepsChanged( int ) ) );
199       myNumberOfSteps->setValue( aResourceMgr->integerValue( "VISU", "sweeping_number_steps", 40 ) );
200       aGridLayout->addWidget( myNumberOfSteps, 1, 1, 1, 1 );
201       
202       QLabel* aStepDelayAnnotation = new QLabel( aPropertiesTab );
203       aStepDelayAnnotation->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Preferred );
204       aStepDelayAnnotation->setText( tr( "DELAY_BETWEEN_STEPS" ) );
205       aGridLayout->addWidget( aStepDelayAnnotation, 2, 0, 1, 1 );
206       
207       myStepDelay = new QtxDoubleSpinBox( aPropertiesTab );
208       myStepDelay->setValue( aResourceMgr->doubleValue("VISU", "sweeping_time_step", 0.1) );
209       aGridLayout->addWidget( myStepDelay, 2, 1, 1, 1 );
210     }
211
212     aTabWidget->addTab( aPropertiesTab, tr( "PROPERTIES_TAB" ) );
213   }
214
215   {
216     QSpacerItem* aSpacerItem = new QSpacerItem( 16, 20, QSizePolicy::Minimum, QSizePolicy::Expanding );
217     aVBoxLayout->addItem( aSpacerItem );
218   }
219
220
221   //----------------------------------------------------------------------------
222   QtxDockWidget* aQtxDockWidget = new QtxDockWidget( true, theParent );
223   theParent->addDockWidget( Qt::BottomDockWidgetArea , aQtxDockWidget );
224   aQtxDockWidget->setObjectName( objectName() );
225   aQtxDockWidget->setWidget( this );
226
227   myToggleViewAction = aQtxDockWidget->toggleViewAction();
228   myToggleViewAction->setIcon( QIcon( aResourceMgr->loadPixmap( "VISU", tr( "ICON_SWEEP_PANEL" ) ) ) );
229   myToggleViewAction->setToolTip( tr( "MEN_SWEEP_PANE" ) );
230   myToggleViewAction->setText( tr( "MEN_SWEEP_PANE" ) );
231   myToggleViewAction->setCheckable( true );
232   aQtxDockWidget->setVisible( false );
233
234   connect( myToggleViewAction, SIGNAL( toggled( bool ) ), this, SLOT( onToggleView( bool ) ) );
235
236   //----------------------------------------------------------------------------
237   myTimer = new QTimer( this );
238
239   myPlayPixmap = aResourceMgr->loadPixmap( "VISU", tr( "ICON_SLIDER_PLAY" ) );
240   myPausePixmap = aResourceMgr->loadPixmap( "VISU", tr( "ICON_SLIDER_PAUSE" ) );
241
242   connect( myTimer, SIGNAL( timeout() ), SLOT( onTimeout() ) );
243
244   connect( myPlayButton, SIGNAL( toggled( bool ) ), SLOT( onPlay( bool ) ) );
245
246   connect( mySweepSlider, SIGNAL( valueChanged( int ) ), SLOT( onValueChanged( int ) ) );
247
248   connect( myStepDelay, SIGNAL( valueChanged( double ) ), SLOT( onDelayChanged( double ) ) );
249
250   connect( mySweepMode, SIGNAL( currentIndexChanged( int ) ), SLOT( onModeChanged( int ) ) );
251
252   connect( theModule, SIGNAL( moduleDeactivated() ), SLOT( onModuleDeactivated() ) );
253
254   connect( theModule, SIGNAL( moduleActivated() ), SLOT( onModuleActivated() ) );
255
256   connect( theSelectionMgr, SIGNAL( currentSelectionChanged() ), SLOT( onSelectionChanged() ) );
257
258   onSelectionChanged();
259   
260   onModeChanged( mySweepMode->currentIndex() );
261 }
262
263
264 //----------------------------------------------------------------------------
265 VisuGUI_Sweep::~VisuGUI_Sweep()
266 {}
267
268
269 //----------------------------------------------------------------------------
270 QAction* VisuGUI_Sweep::toggleViewAction()
271 {
272   return myToggleViewAction;
273 }
274
275
276 //----------------------------------------------------------------------------
277 void VisuGUI_Sweep::onModuleDeactivated()
278 {
279   setHidden( true );
280
281   onEnable( true );
282 }
283
284
285 //----------------------------------------------------------------------------
286 void VisuGUI_Sweep::onModuleActivated()
287 {
288   setHidden( false );
289 }
290
291
292 //----------------------------------------------------------------------------
293 namespace
294 {
295   struct TEnabler
296   {
297     VisuGUI_Sweep* myWidget;
298     bool& myIsValidSelection;
299
300     TEnabler( VisuGUI_Sweep* theWidget, bool& theIsValidSelection )
301       : myWidget( theWidget )
302       , myIsValidSelection( theIsValidSelection )
303     {}
304     
305     ~TEnabler()
306     {
307       myWidget->onEnable( !myIsValidSelection );
308     }
309   };
310 }
311
312
313 //----------------------------------------------------------------------------
314 void VisuGUI_Sweep::onSelectionChanged()
315 {
316   VISU_Actor* anActor = NULL;
317   VISU::Prs3d_i* aPrs3d = NULL;
318   SVTK_ViewWindow* aViewWindow = NULL;
319
320   bool anIsValidSelection = VISU::GetPrs3dSelectionInfo( myModule, aPrs3d, aViewWindow, anActor );
321
322   // To instantiate special helper class which will analyze validity of selection 
323   // and make appropritate actions
324   TEnabler anEnabler( this, anIsValidSelection );
325
326   anIsValidSelection &= bool( aViewWindow );
327
328   // To keep the pointer on the view window properly it is necessary to get known when
329   // it will be destroyed
330   if ( myViewWindow != aViewWindow ) {
331     if ( myViewWindow )
332       disconnect( myViewWindow, SIGNAL( destroyed( QObject * ) ), this, SLOT( onSelectionChanged() ) );
333
334     myViewWindow = aViewWindow;
335
336     if ( myViewWindow )
337       connect( myViewWindow, SIGNAL( destroyed( QObject * ) ), this, SLOT( onSelectionChanged() ) );
338   }
339
340   if(!VISU::GetActiveViewWindow<SVTK_ViewWindow>(myModule)) {
341     myActor = NULL;
342     return;
343   }
344
345   VISU::ColoredPrs3d_i* aColoredPrs3d = dynamic_cast< VISU::ColoredPrs3d_i* >( aPrs3d );
346   anIsValidSelection &= ( aColoredPrs3d && aColoredPrs3d->IsTimeStampFixed() );
347
348   if ( myColoredPrs3d ) {
349     myColoredPrs3d->SetMapScale( 1.0 );
350     myColoredPrs3d->UpdateActors();
351   }
352
353   myColoredPrs3d = aColoredPrs3d;
354
355   anIsValidSelection &= ( anActor && anActor->GetVisibility() );
356
357   myActor = anActor;
358 }
359
360
361 //----------------------------------------------------------------------------
362 void VisuGUI_Sweep::onValueChanged( int value )
363 {
364   bool anIsValidSelection = ( myColoredPrs3d != NULL )
365                           && (myActor.GetPointer() != NULL );
366
367   if ( !anIsValidSelection )
368     return;
369
370   QApplication::setOverrideCursor(Qt::WaitCursor);
371
372   int aSweepMode = mySweepMode->currentIndex();
373   int aNbSteps = myNumberOfSteps->value();
374   
375   double anArgument = double( value ) / aNbSteps;
376
377   int anIntervalMode = myIntervals->currentIndex();
378   // To correct the input value according to the given interval mode
379   if ( aSweepMode != 0 ) {
380     switch ( anIntervalMode ) {
381     case 0 :
382       anArgument = vtkMath::Pi() * anArgument;
383       break;
384     case 1 :
385       anArgument = -vtkMath::Pi() + 2.0 * vtkMath::Pi() * anArgument;
386       break;
387     default :
388       break;
389     }
390   }
391   
392   // To correct the input value according to the given sweep mode
393   double aValue = anArgument;
394   switch ( aSweepMode ) {
395   case 1 :
396     aValue = ( 1.0 - cos( anArgument ) ) / 2.0;
397     break;
398   case 2 :
399     aValue = sin( anArgument - vtkMath::Pi() / 2.0 );
400     break;
401   default :
402     break;
403   }
404
405   try {
406     myColoredPrs3d->SetMapScale( aValue );
407     myColoredPrs3d->UpdateActor( myActor );
408     if(myViewWindow)
409       myViewWindow->Repaint();
410   } catch (std::exception& exc) {
411     INFOS( "Follow exception was occured :\n" << exc.what() );
412   } catch (...) {
413     INFOS( "Unknown exception was occured!" );
414   }
415
416   QApplication::restoreOverrideCursor();
417 }
418
419
420 //----------------------------------------------------------------------------
421 void VisuGUI_Sweep::onDelayChanged( double value )
422 {
423   myTimer->start( int( value * 1000 ) );
424 }
425
426
427 //----------------------------------------------------------------------------
428 void VisuGUI_Sweep::onNumberOfStepsChanged( int value )
429 {
430   int anOldNumberOfSteps = mySweepSlider->maximum();
431   mySweepSlider->setMaximum( value );
432
433   double aValue = double( value );
434
435   double aNewSweepValue = aValue / anOldNumberOfSteps * mySweepSlider->value();
436   mySweepSlider->setValue( int( aNewSweepValue + 1 ) );
437
438   double aNewPageStep = aValue / 10;
439   mySweepSlider->setPageStep( int( aNewPageStep + 1 ) );
440 }
441
442
443 //----------------------------------------------------------------------------
444 void VisuGUI_Sweep::onModeChanged( int )
445 {
446   myIntervals->setEnabled( mySweepMode->currentIndex() != 0 );
447 }
448
449
450 //----------------------------------------------------------------------------
451 void VisuGUI_Sweep::onFirst()
452 {
453   mySweepSlider->setValue( mySweepSlider->minimum() );
454 }
455
456
457 //----------------------------------------------------------------------------
458 void VisuGUI_Sweep::onPrevious()
459 {
460   mySweepSlider->setValue( mySweepSlider->value() - 1 );
461 }
462
463 void VisuGUI_Sweep::onNext()
464 {
465   mySweepSlider->setValue( mySweepSlider->value() + 1 );
466 }
467
468 void VisuGUI_Sweep::onLast()
469 {
470   mySweepSlider->setValue( mySweepSlider->maximum() );
471 }
472
473 //----------------------------------------------------------------------------
474 void VisuGUI_Sweep::onPlay( bool on )
475 {
476   if ( on ) {
477     myPlayButton->setIcon( myPausePixmap );
478     myTimer->start( int( myStepDelay->value() * 1000 ) );
479     myPlayButton->setChecked( true );
480   } else {
481     myTimer->stop();
482     myPlayButton->setChecked( false );
483     myPlayButton->setIcon( myPlayPixmap );
484   }
485 }
486
487
488 //----------------------------------------------------------------------------
489 void VisuGUI_Sweep::onToggleView( bool on )
490 {
491   if ( !on ) {
492     onPlay( on );
493   }
494 }
495
496
497 //----------------------------------------------------------------------------
498 void VisuGUI_Sweep::onStop()
499 {
500   onPlay( false );
501   mySweepSlider->setValue( mySweepSlider->maximum() );
502
503   if ( myViewWindow )
504     myViewWindow->Repaint();
505 }
506
507
508 //----------------------------------------------------------------------------
509 void VisuGUI_Sweep::onEnable( bool on )
510 {
511   onStop();
512
513   mySweepSlider->setEnabled( !on );
514   
515   myFirstButton->setEnabled( !on );
516   myPreviousButton->setEnabled( !on );
517   
518   myPlayButton->setEnabled( !on );
519   
520   myNextButton->setEnabled( !on );
521   myLastButton->setEnabled( !on );
522 }
523
524
525 //----------------------------------------------------------------------------
526 void VisuGUI_Sweep::onTimeout()
527 {
528   int value = mySweepSlider->value();
529   if ( value < mySweepSlider->maximum() ) {
530     mySweepSlider->setValue( value + 1 );
531   } else {
532     if ( myIsCycled->isChecked() )
533       mySweepSlider->setValue( 0 );
534     else
535       myPlayButton->setChecked( false );
536     //myStopButton->setEnabled( false );
537   }
538 }
539
540
541 //----------------------------------------------------------------------------