Salome HOME
Join modifications from branch OCC_development_for_3_2_0a2
[modules/visu.git] / src / VVTK / VVTK_MainWindow.cxx
1 //  SALOME OBJECT : implementation of interactive object visualization for OCC and VTK viewers
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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : 
25 //  Author : 
26 //  Module : 
27 //  $Header$
28
29 #include "VVTK_MainWindow.h"
30 #include "VVTK_InteractorStyle.h"
31 #include "VVTK_Recorder.h"
32 #include "VVTK_RecorderDlg.h"
33 #include "VISU_WidgetCtrl.hxx"
34 #include "VISU_GaussPtsAct.h"
35 #include "VISU_Event.h" 
36
37 #include "SVTK_RenderWindowInteractor.h"
38 #include "VVTK_Renderer.h"
39 #include "VVTK_PickingDlg.h"
40 #include "VVTK_SegmentationCursorDlg.h"
41
42 #include "SUIT_Application.h"
43 #include "SUIT_Session.h"
44 #include "SUIT_Tools.h"
45 #include "SUIT_ViewWindow.h"
46 #include "SUIT_ResourceMgr.h"
47 #include "SUIT_ToolButton.h"
48 #include "SUIT_Accel.h"
49 #include "QtxAction.h"
50
51 #include <qimage.h>
52 #include <qtoolbar.h>
53 #include <qsplitter.h>
54 #include <qfiledialog.h>
55 #include <qapplication.h>
56
57 //----------------------------------------------------------------------------
58 VVTK_MainWindow
59 ::VVTK_MainWindow(QWidget* theParent, 
60                   const char* theName,
61                   SUIT_ResourceMgr* theResourceMgr,
62                   SUIT_ViewWindow* theViewWindow):
63   SVTK_MainWindow(theParent,theName,theResourceMgr,theViewWindow),
64   myInteractorStyle(VVTK_InteractorStyle::New()),
65   myControllerIncrement(VVTK_ControllerIncrement::New()),
66   myControllerOnKeyDown(VVTK_ControllerOnKeyDown::New())
67 {
68   myInteractorStyle->SetControllerIncrement(myControllerIncrement.GetPointer());
69   myControllerIncrement->Delete();
70
71   myInteractorStyle->SetControllerOnKeyDown(myControllerOnKeyDown.GetPointer());
72   myControllerOnKeyDown->Delete();
73
74   myInteractorStyle->Delete();
75
76   moveDockWindow(myToolBar,Qt::DockLeft);
77   myActionsMap[NonIsometric]->removeFrom(myToolBar);
78
79   // Recording
80   myRecordingToolBar = new QToolBar(this);
81   myRecordingToolBar->setCloseMode(QDockWindow::Undocked);
82   myRecordingToolBar->setLabel(tr("LBL_TOOLBAR_RECORD_LABEL"));
83   moveDockWindow(myRecordingToolBar,Qt::DockLeft);
84
85   myStartAction = new QtxAction(tr("MNU_VVTK_RECORDING_START"), 
86                                 theResourceMgr->loadPixmap( "VISU", tr( "ICON_VVTK_RECORDING_START" ) ),
87                                 tr( "MNU_VVTK_RECORDING_START" ), 0, this);
88   myStartAction->setStatusTip(tr("DSC_VVTK_RECORDING_START"));
89   myStartAction->addTo( myRecordingToolBar );
90   connect( myStartAction, SIGNAL( activated() ), this, SLOT( OnStartRecording() ) );
91
92   myPlayAction = new QtxAction(tr("MNU_VVTK_RECORDING_PLAY"), 
93                                theResourceMgr->loadPixmap( "VISU", tr( "ICON_VVTK_RECORDING_PLAY" ) ),
94                                tr( "MNU_VVTK_RECORDING_PLAY" ), 0, this);
95   myPlayAction->setStatusTip(tr("DSC_VVTK_RECORDING_PLAY"));
96   myPlayAction->setEnabled( false );
97   myPlayAction->addTo( myRecordingToolBar );
98   connect( myPlayAction, SIGNAL( activated() ), this, SLOT( OnPlayRecording() ) );
99
100   myPauseAction = new QtxAction(tr("MNU_VVTK_RECORDING_PAUSE"), 
101                                 theResourceMgr->loadPixmap( "VISU", tr( "ICON_VVTK_RECORDING_PAUSE" ) ),
102                                 tr( "MNU_VVTK_RECORDING_PAUSE" ), 0, this);
103   myPauseAction->setStatusTip(tr("DSC_VVTK_RECORDING_PAUSE"));
104   myPauseAction->setEnabled( false );
105   myPauseAction->addTo( myRecordingToolBar );
106   connect( myPauseAction, SIGNAL( activated() ), this, SLOT( OnPauseRecording() ) );
107
108   myStopAction = new QtxAction(tr("MNU_VVTK_RECORDING_STOP"), 
109                                theResourceMgr->loadPixmap( "VISU", tr( "ICON_VVTK_RECORDING_STOP" ) ),
110                                tr( "MNU_VVTK_RECORDING_STOP" ), 0, this);
111   myStopAction->setStatusTip(tr("DSC_VVTK_RECORDING_STOP"));
112   myStopAction->setEnabled( false );
113   myStopAction->addTo( myRecordingToolBar );
114   connect( myStopAction, SIGNAL( activated() ), this, SLOT( OnStopRecording() ) );
115   
116   myRecorder = VVTK_Recorder::New();
117   myRecorder->CheckExistAVIMaker();
118   if(myRecorder->ErrorStatus())
119     myRecordingToolBar->setEnabled(false);
120 }
121
122 //----------------------------------------------------------------------------
123 void
124 VVTK_MainWindow
125 ::Initialize(SVTK_RenderWindowInteractor* theInteractor)
126
127   vtkInteractorStyle* aStyle = theInteractor->GetInteractorStyle();
128   if(SVTK_InteractorStyle *anInteractorStyle = dynamic_cast<SVTK_InteractorStyle*>(aStyle)){
129     anInteractorStyle->SetControllerIncrement(myControllerIncrement.GetPointer());
130     anInteractorStyle->SetControllerOnKeyDown(myControllerOnKeyDown.GetPointer());
131   }
132
133   myRecorder->SetNbFPS(17.3);
134   myRecorder->SetQuality(100);
135   myRecorder->SetProgressiveMode(true);
136   myRecorder->SetUseSkippedFrames(true);
137   myRecorder->SetRenderWindow(theInteractor->getRenderWindow());
138
139   disconnect( myActionsMap[ DumpId ], SIGNAL( activated() ),
140               myViewWindow, SLOT( onDumpView() ) );
141
142   connect( myActionsMap[ DumpId ], SIGNAL( activated() ),
143            this, SLOT( onDumpView() ) );
144
145   SVTK_MainWindow::Initialize(theInteractor);
146 }
147
148 VVTK_MainWindow::~VVTK_MainWindow()
149 {
150   if(myRecorder)
151     myRecorder->Delete();
152 }
153
154 //----------------------------------------------------------------------------
155 void VVTK_MainWindow::onDumpView()
156 {
157   SUIT_Application* app = SUIT_Session::session()->activeApplication();
158   QString fileName = app->getFileName( false, QString::null,
159                                        tr( "TLT_IMAGE_FILES" ),
160                                        tr( "TLT_DUMP_VIEW" ), 0 );
161   if( fileName.isEmpty() )
162     return;
163
164   QImage img = dumpView();
165   if( img.isNull() )
166     return; 
167
168   QString fmt = SUIT_Tools::extension( fileName ).upper();
169   if( fmt.isEmpty() )
170     fmt = QString( "BMP" ); // default format
171   else if( fmt == "JPG" )
172     fmt = "JPEG";
173
174   QApplication::setOverrideCursor( Qt::waitCursor );
175   bool res = img.save( fileName, fmt.latin1() );
176   QApplication::restoreOverrideCursor();
177 }
178
179 //----------------------------------------------------------------------------
180 void
181 VVTK_MainWindow
182 ::OnInteractorStyleSwitch(bool theIsGaussStyleOn)
183 {
184   if ( theIsGaussStyleOn )
185     this->PushInteractorStyle(myInteractorStyle.GetPointer());
186   else
187     this->PopInteractorStyle();
188 }
189
190 //----------------------------------------------------------------------------
191 void VVTK_MainWindow::OnStartRecording()
192 {
193   VVTK_RecorderDlg* aRecorderDlg = new VVTK_RecorderDlg( this, myRecorder );
194
195   if( !aRecorderDlg->exec() )
196     return;
197
198   myStartAction->setEnabled( false );
199   myPlayAction->setEnabled( false );
200   myPauseAction->setEnabled( true );
201   myStopAction->setEnabled( true );
202
203   myRecorder->Record();
204 }
205
206 //----------------------------------------------------------------------------
207 void VVTK_MainWindow::OnPlayRecording()
208 {
209   myStartAction->setEnabled( false );
210   myPlayAction->setEnabled( false );
211   myPauseAction->setEnabled( true );
212   myStopAction->setEnabled( true );
213   //
214   myRecorder->Pause();
215 }
216
217 //----------------------------------------------------------------------------
218 void VVTK_MainWindow::OnPauseRecording()
219 {
220   myStartAction->setEnabled( false );
221   myPlayAction->setEnabled( true );
222   myPauseAction->setEnabled( false );
223   myStopAction->setEnabled( true );
224   //
225   myRecorder->Pause();
226 }
227
228 //----------------------------------------------------------------------------
229 void VVTK_MainWindow::OnStopRecording()
230 {
231   myStartAction->setEnabled( true );
232   myPlayAction->setEnabled( false );
233   myPauseAction->setEnabled( false );
234   myStopAction->setEnabled( false );
235   //
236   myRecorder->Stop();
237 }
238
239 //----------------------------------------------------------------------------
240 int 
241 convertAction( const int accelAction );
242
243 void 
244 VVTK_MainWindow
245 ::action( const int accelAction  )
246 {
247   if ( accelAction == SUIT_Accel::ZoomFit )
248     onFitAll();
249   else {
250     int anEvent = convertAction( accelAction );
251     InvokeEvent( anEvent, 0 );
252   }
253 }
254
255 //----------------------------------------------------------------------------
256 VVTK_MainWindow1
257 ::VVTK_MainWindow1(QSplitter* theParent, 
258                   const char* theName,
259                   SUIT_ResourceMgr* theResourceMgr,
260                   SUIT_ViewWindow* theViewWindow):
261   VVTK_MainWindow(theParent,theName,theResourceMgr,theViewWindow),
262   myStyleSwitchAction(NULL),
263   mySplitter(theParent),
264   myPickingDlg(NULL)
265 {
266   myPtsToolBar = new QToolBar(this);
267   myPtsToolBar->setCloseMode(QDockWindow::Undocked);
268   myPtsToolBar->setLabel(tr("LBL_TOOLBAR_GAUSS_LABEL"));
269   moveDockWindow(myPtsToolBar,Qt::DockLeft);
270
271   QPixmap aPixmap;
272   QtxAction* anAction;
273
274   aPixmap = theResourceMgr->loadPixmap( "VISU", tr( "ICON_VVTK_INTERACTOR_STYLE_SWITCH" ) );
275   anAction = new QtxAction(tr("MNU_VVTK_INTERACTOR_STYLE_SWITCH"), 
276                            aPixmap,
277                            tr( "MNU_VVTK_INTERACTOR_STYLE_SWITCH" ), 
278                            0, 
279                            this, 
280                            "VVTK/SVTK StyleSwitch", 
281                            true);
282   anAction->setToggleAction(true);
283   anAction->setStatusTip(tr("DSC_VVTK_INTERACTOR_STYLE_SWITCH"));
284
285   anAction->addTo( myPtsToolBar );
286   myStyleSwitchAction = anAction;
287
288   if( theResourceMgr->integerValue( "VISU", "mouse_behaviour", true ) == 1 )
289     myStyleSwitchAction->toggle();
290
291   aPixmap = theResourceMgr->loadPixmap("VISU",tr("ICON_VVTK_SELECTION_MODE_SWITCH"));
292   myPickingAction = new QtxAction(tr("MNU_VVTK_SELECTION_MODE_SWITCH"), 
293                                   aPixmap,
294                                   tr( "MNU_VVTK_SELECTION_MODE_SWITCH" ), 
295                                   0, 
296                                   this, 
297                                   "VVTK/SVTK SelectionSwitch", 
298                                   true);
299   myPickingAction->setToggleAction(true);
300   myPickingAction->setStatusTip(tr("DSC_VVTK_SELECTION_MODE_SWITCH"));
301   myPickingAction->addTo( myPtsToolBar );
302   connect(myPickingAction, SIGNAL(toggled(bool)), this, SLOT(OnSelectionModeSwitch(bool)));
303
304   myPickingDlg = new VVTK_PickingDlg( this, "PickingDlg" );
305   myPickingDlg->SetAction( myPickingAction );
306
307   // Plane/Sphere Segmentation
308   aPixmap = theResourceMgr->loadPixmap("VISU",tr("ICON_VVTK_PLANE_SEGMENTATION_SWITCH"));
309   myPlaneSegmentationAction = new QtxAction(tr("MNU_VVTK_PLANE_SEGMENTATION_SWITCH"), 
310                                             aPixmap,
311                                             tr( "MNU_VVTK_PLANE_SEGMENTATION_SWITCH" ), 
312                                             0, 
313                                             this, 
314                                             "VVTK/SVTK PlaneSegmentationSwitch", 
315                                             true);
316   myPlaneSegmentationAction->setToggleAction(true);
317   myPlaneSegmentationAction->setStatusTip(tr("DSC_VVTK_PLANE_SEGMENTATION_SWITCH"));
318   //myPlaneSegmentationAction->addTo( myPtsToolBar );
319   connect( myPlaneSegmentationAction, SIGNAL( activated() ), this, SLOT( OnSegmentationSwitch() ) );
320
321   aPixmap = theResourceMgr->loadPixmap("VISU",tr("ICON_VVTK_SPHERE_SEGMENTATION_SWITCH"));
322   mySphereSegmentationAction = new QtxAction(tr("MNU_VVTK_SPHERE_SEGMENTATION_SWITCH"), 
323                                              aPixmap,
324                                              tr( "MNU_VVTK_SPHERE_SEGMENTATION_SWITCH" ), 
325                                              0, 
326                                              this, 
327                                              "VVTK/SVTK SphereSegmentationSwitch", 
328                                              true);
329   mySphereSegmentationAction->setToggleAction(true);
330   mySphereSegmentationAction->setStatusTip(tr("DSC_VVTK_SPHERE_SEGMENTATION_SWITCH"));
331   //mySphereSegmentationAction->addTo( myPtsToolBar );
332   connect( mySphereSegmentationAction, SIGNAL( activated() ), this, SLOT( OnSegmentationSwitch() ) );
333
334   mySegmentationCursorDlg = new VVTK_SegmentationCursorDlg( this, "SegmentationCursorDlg" );
335   mySegmentationCursorDlg->SetPlaneAction( myPlaneSegmentationAction );
336   mySegmentationCursorDlg->SetSphereAction( mySphereSegmentationAction );
337   connect( mySegmentationCursorDlg, SIGNAL( scgClose() ), this, SLOT( OnSegmentationSwitch() ) );
338
339   SUIT_ToolButton* aSegmentationButton = new SUIT_ToolButton( myPtsToolBar );
340   aSegmentationButton->AddAction( myPlaneSegmentationAction );
341   aSegmentationButton->AddAction( mySphereSegmentationAction );
342 }
343
344 void
345 VVTK_MainWindow1
346 ::Initialize(SVTK_RenderWindowInteractor* theInteractor,
347              VVTK_Renderer1* theRenderer)
348 {
349   myRenderer = theRenderer;
350   VVTK_MainWindow::Initialize(theInteractor);
351
352   if( myStyleSwitchAction->isOn() )
353     PushInteractorStyle(myInteractorStyle.GetPointer());
354   connect(myStyleSwitchAction, SIGNAL(toggled(bool)), this, SLOT(OnInteractorStyleSwitch(bool)));
355
356   mySegmentationCursorDlg->SetWidgetCtrl( theRenderer->GetWidgetCtrl() );
357   mySegmentationCursorDlg->SetInteractor( theInteractor );
358
359   connect( theInteractor, SIGNAL( selectionChanged() ), SLOT( OnSelectionChanged() ) );
360   myPickingDlg->SetInteractor( theInteractor );
361 }
362
363 VVTK_MainWindow1
364 ::~VVTK_MainWindow1()
365 {}
366
367 //----------------------------------------------------------------------------
368 VVTK_MainWindow2*
369 VVTK_MainWindow1
370 ::CreateMainWindow2(QWidget* theParent, 
371                     const char* theName,
372                     SUIT_ResourceMgr* theResourceMgr,
373                     SUIT_ViewWindow* theViewWindow)
374 {
375   myMainWindow2 = new VVTK_MainWindow2(theParent,
376                                        theName,
377                                        theResourceMgr,
378                                        theViewWindow,
379                                        myStyleSwitchAction);
380   return myMainWindow2;
381 }
382
383
384 //----------------------------------------------------------------------------
385 void
386 VVTK_MainWindow1
387 ::OnSelectionModeSwitch(bool theIsSelectionOn)
388 {
389   Selection_Mode aSelectionMode = SelectionMode();
390   if(theIsSelectionOn && aSelectionMode != GaussPointSelection)
391     SetSelectionMode(GaussPointSelection);
392   else if(!theIsSelectionOn && aSelectionMode == GaussPointSelection)
393     SetSelectionMode(ActorSelection);
394
395   if( theIsSelectionOn )
396   {
397     myPickingDlg->Update();
398     myPickingDlg->show();
399   }
400   else
401     myPickingDlg->hide();
402 }
403
404 void
405 VVTK_MainWindow1
406 ::OnSelectionChanged()
407 {
408   Selection_Mode aSelectionMode = SelectionMode();
409   if(myPickingAction->isOn() && aSelectionMode != GaussPointSelection)
410     myPickingAction->setOn(false);
411   else if(!myPickingAction->isOn() && aSelectionMode == GaussPointSelection)
412     myPickingAction->setOn(true);
413 }
414
415
416 //----------------------------------------------------------------------------
417 VISU_WidgetCtrl* 
418 VVTK_MainWindow1
419 ::GetWidgetCtrl()
420 {
421   return myRenderer->GetWidgetCtrl();
422 }
423
424 VISU_InsideCursorSettings* 
425 VVTK_MainWindow1
426 ::GetInsideCursorSettings()
427 {
428   return mySegmentationCursorDlg->GetInsideCursorSettings();
429 }
430
431 VISU_OutsideCursorSettings* 
432 VVTK_MainWindow1
433 ::GetOutsideCursorSettings()
434 {
435   return mySegmentationCursorDlg->GetOutsideCursorSettings();
436 }
437
438 VISU_PickingSettings* 
439 VVTK_MainWindow1
440 ::GetPickingSettings()
441 {
442   return myPickingDlg->GetPickingSettings();
443 }
444
445 //----------------------------------------------------------------------------
446 void
447 VVTK_MainWindow1
448 ::SetPlanesSegementation(bool theIsOn)
449 {
450   myPlaneSegmentationAction->setOn( theIsOn );
451   OnSegmentationSwitch(myPlaneSegmentationAction);
452 }
453
454 void
455 VVTK_MainWindow1
456 ::SetSphereSegementation(bool theIsOn)
457 {
458   mySphereSegmentationAction->setOn( theIsOn );
459   OnSegmentationSwitch(mySphereSegmentationAction);
460 }
461
462 void
463 VVTK_MainWindow1
464 ::OnSegmentationSwitch(QtxAction* theAction)
465 {
466   bool anIsSegmentationOn = myPlaneSegmentationAction->isOn() ||
467     mySphereSegmentationAction->isOn();
468
469   if( !theAction )
470     return;
471   
472   VISU_WidgetCtrl *aWidgetCtrl = myRenderer->GetWidgetCtrl();
473   
474   if (anIsSegmentationOn) {
475     int anIndex = (theAction == myPlaneSegmentationAction) ? 0 : 1;
476     aWidgetCtrl->SetActiveIndex(anIndex);
477   }
478   aWidgetCtrl->SetEnabled(anIsSegmentationOn);
479
480   if( theAction == myPlaneSegmentationAction && anIsSegmentationOn )
481     mySphereSegmentationAction->setOn( false );
482   else if( theAction == mySphereSegmentationAction && anIsSegmentationOn )
483     myPlaneSegmentationAction->setOn( false );
484
485   if( anIsSegmentationOn )
486   {
487     myMainWindow2->show();
488     mySegmentationCursorDlg->SetIsPlaneSegmentation( theAction == myPlaneSegmentationAction );
489     mySegmentationCursorDlg->UpdateSegmentation();
490     mySegmentationCursorDlg->UpdateInsideGaussPoints();
491     mySegmentationCursorDlg->UpdateOutsideGaussPoints();
492     mySegmentationCursorDlg->show();
493   }
494   else
495   {
496     myMainWindow2->hide();
497     mySegmentationCursorDlg->hide();
498   }
499 }
500
501 void
502 VVTK_MainWindow1
503 ::OnSegmentationSwitch()
504 {
505   QtxAction* anAction = ( QtxAction* )sender();
506   OnSegmentationSwitch(anAction);
507 }
508
509
510 //----------------------------------------------------------------------------
511 VVTK_MainWindow2
512 ::VVTK_MainWindow2(QWidget* theParent, 
513                    const char* theName,
514                    SUIT_ResourceMgr* theResourceMgr,
515                    SUIT_ViewWindow* theViewWindow,
516                    QtxAction* theStyleSwitchAction):
517   VVTK_MainWindow(theParent,theName,theResourceMgr,theViewWindow),
518   myStyleSwitchAction(theStyleSwitchAction)
519 {}
520
521 VVTK_MainWindow2
522 ::~VVTK_MainWindow2()
523 {}
524
525
526 //----------------------------------------------------------------------------
527 void
528 VVTK_MainWindow2
529 ::Initialize(SVTK_RenderWindowInteractor* theInteractor)
530 {
531   VVTK_MainWindow::Initialize(theInteractor);
532
533   if( myStyleSwitchAction->isOn() )
534     PushInteractorStyle(myInteractorStyle.GetPointer());
535   connect(myStyleSwitchAction, SIGNAL(toggled(bool)), this, SLOT(OnInteractorStyleSwitch(bool)));
536 }
537