]> SALOME platform Git repositories - modules/visu.git/blob - src/VVTK/VVTK_MainWindow.cxx
Salome HOME
Join modifications from branch OCC_debug_for_3_2_0b1
[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   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( myPickingAction, this, "PickingDlg" );
305
306   // Plane/Sphere Segmentation
307   aPixmap = theResourceMgr->loadPixmap("VISU",tr("ICON_VVTK_PLANE_SEGMENTATION_SWITCH"));
308   myPlaneSegmentationAction = new QtxAction(tr("MNU_VVTK_PLANE_SEGMENTATION_SWITCH"), 
309                                             aPixmap,
310                                             tr( "MNU_VVTK_PLANE_SEGMENTATION_SWITCH" ), 
311                                             0, 
312                                             this, 
313                                             "VVTK/SVTK PlaneSegmentationSwitch", 
314                                             true);
315   myPlaneSegmentationAction->setToggleAction(true);
316   myPlaneSegmentationAction->setStatusTip(tr("DSC_VVTK_PLANE_SEGMENTATION_SWITCH"));
317   //myPlaneSegmentationAction->addTo( myPtsToolBar );
318   connect( myPlaneSegmentationAction, SIGNAL( activated() ), this, SLOT( OnSegmentationSwitch() ) );
319
320   aPixmap = theResourceMgr->loadPixmap("VISU",tr("ICON_VVTK_SPHERE_SEGMENTATION_SWITCH"));
321   mySphereSegmentationAction = new QtxAction(tr("MNU_VVTK_SPHERE_SEGMENTATION_SWITCH"), 
322                                              aPixmap,
323                                              tr( "MNU_VVTK_SPHERE_SEGMENTATION_SWITCH" ), 
324                                              0, 
325                                              this, 
326                                              "VVTK/SVTK SphereSegmentationSwitch", 
327                                              true);
328   mySphereSegmentationAction->setToggleAction(true);
329   mySphereSegmentationAction->setStatusTip(tr("DSC_VVTK_SPHERE_SEGMENTATION_SWITCH"));
330   //mySphereSegmentationAction->addTo( myPtsToolBar );
331   connect( mySphereSegmentationAction, SIGNAL( activated() ), this, SLOT( OnSegmentationSwitch() ) );
332
333   mySegmentationCursorDlg = new VVTK_SegmentationCursorDlg( this, "SegmentationCursorDlg" );
334   mySegmentationCursorDlg->SetPlaneAction( myPlaneSegmentationAction );
335   mySegmentationCursorDlg->SetSphereAction( mySphereSegmentationAction );
336   connect( mySegmentationCursorDlg, SIGNAL( scgClose() ), this, SLOT( OnSegmentationSwitch() ) );
337
338   SUIT_ToolButton* aSegmentationButton = new SUIT_ToolButton( myPtsToolBar );
339   aSegmentationButton->AddAction( myPlaneSegmentationAction );
340   aSegmentationButton->AddAction( mySphereSegmentationAction );
341 }
342
343 void
344 VVTK_MainWindow1
345 ::Initialize(SVTK_RenderWindowInteractor* theInteractor,
346              VVTK_Renderer1* theRenderer)
347 {
348   myRenderer = theRenderer;
349   VVTK_MainWindow::Initialize(theInteractor);
350
351   if( myStyleSwitchAction->isOn() )
352     PushInteractorStyle(myInteractorStyle.GetPointer());
353   connect(myStyleSwitchAction, SIGNAL(toggled(bool)), this, SLOT(OnInteractorStyleSwitch(bool)));
354
355   mySegmentationCursorDlg->SetWidgetCtrl( theRenderer->GetWidgetCtrl() );
356   mySegmentationCursorDlg->SetInteractor( theInteractor );
357
358   connect( theInteractor, SIGNAL( selectionChanged() ), SLOT( OnSelectionChanged() ) );
359   myPickingDlg->SetInteractor( theInteractor );
360 }
361
362 VVTK_MainWindow1
363 ::~VVTK_MainWindow1()
364 {}
365
366 //----------------------------------------------------------------------------
367 VVTK_MainWindow2*
368 VVTK_MainWindow1
369 ::CreateMainWindow2(QWidget* theParent, 
370                     const char* theName,
371                     SUIT_ResourceMgr* theResourceMgr,
372                     SUIT_ViewWindow* theViewWindow)
373 {
374   myMainWindow2 = new VVTK_MainWindow2(theParent,
375                                        theName,
376                                        theResourceMgr,
377                                        theViewWindow,
378                                        myStyleSwitchAction);
379   return myMainWindow2;
380 }
381
382
383 //----------------------------------------------------------------------------
384 void
385 VVTK_MainWindow1
386 ::OnSelectionModeSwitch(bool theIsSelectionOn)
387 {
388   if ( theIsSelectionOn && !isVisible() )
389     return;
390   
391   Selection_Mode aSelectionMode = SelectionMode();
392   if(theIsSelectionOn && aSelectionMode != GaussPointSelection)
393     SetSelectionMode(GaussPointSelection);
394   else if(!theIsSelectionOn && aSelectionMode == GaussPointSelection)
395     SetSelectionMode(ActorSelection);
396
397   if( theIsSelectionOn )
398   {
399     myPickingDlg->Update();
400     myPickingDlg->show();
401   }
402   else
403     myPickingDlg->hide();
404 }
405
406 void
407 VVTK_MainWindow1
408 ::OnSelectionChanged()
409 {
410   Selection_Mode aSelectionMode = SelectionMode();
411   if(myPickingAction->isOn() && aSelectionMode != GaussPointSelection)
412     myPickingAction->setOn(false);
413   else if(!myPickingAction->isOn() && aSelectionMode == GaussPointSelection)
414     myPickingAction->setOn(true);
415 }
416
417
418 //----------------------------------------------------------------------------
419 VISU_WidgetCtrl* 
420 VVTK_MainWindow1
421 ::GetWidgetCtrl()
422 {
423   return myRenderer->GetWidgetCtrl();
424 }
425
426 VISU_InsideCursorSettings* 
427 VVTK_MainWindow1
428 ::GetInsideCursorSettings()
429 {
430   return mySegmentationCursorDlg->GetInsideCursorSettings();
431 }
432
433 VISU_OutsideCursorSettings* 
434 VVTK_MainWindow1
435 ::GetOutsideCursorSettings()
436 {
437   return mySegmentationCursorDlg->GetOutsideCursorSettings();
438 }
439
440 VISU_PickingSettings* 
441 VVTK_MainWindow1
442 ::GetPickingSettings()
443 {
444   return myPickingDlg->GetPickingSettings();
445 }
446
447 //----------------------------------------------------------------------------
448 void
449 VVTK_MainWindow1
450 ::SetPlanesSegementation(bool theIsOn)
451 {
452   myPlaneSegmentationAction->setOn( theIsOn );
453   OnSegmentationSwitch(myPlaneSegmentationAction);
454 }
455
456 void
457 VVTK_MainWindow1
458 ::SetSphereSegementation(bool theIsOn)
459 {
460   mySphereSegmentationAction->setOn( theIsOn );
461   OnSegmentationSwitch(mySphereSegmentationAction);
462 }
463
464 void
465 VVTK_MainWindow1
466 ::OnSegmentationSwitch(QtxAction* theAction)
467 {
468   bool anIsSegmentationOn = myPlaneSegmentationAction->isOn() ||
469     mySphereSegmentationAction->isOn();
470
471   if( !theAction )
472     return;
473   
474   VISU_WidgetCtrl *aWidgetCtrl = myRenderer->GetWidgetCtrl();
475   
476   if (anIsSegmentationOn) {
477     int anIndex = (theAction == myPlaneSegmentationAction) ? 0 : 1;
478     aWidgetCtrl->SetActiveIndex(anIndex);
479   }
480   aWidgetCtrl->SetEnabled(anIsSegmentationOn);
481
482   if( theAction == myPlaneSegmentationAction && anIsSegmentationOn )
483     mySphereSegmentationAction->setOn( false );
484   else if( theAction == mySphereSegmentationAction && anIsSegmentationOn )
485     myPlaneSegmentationAction->setOn( false );
486
487   if( anIsSegmentationOn )
488   {
489     myMainWindow2->show();
490     mySegmentationCursorDlg->SetIsPlaneSegmentation( theAction == myPlaneSegmentationAction );
491     mySegmentationCursorDlg->UpdateSegmentation();
492     mySegmentationCursorDlg->UpdateInsideGaussPoints();
493     mySegmentationCursorDlg->UpdateOutsideGaussPoints();
494     mySegmentationCursorDlg->show();
495   }
496   else
497   {
498     myMainWindow2->hide();
499     mySegmentationCursorDlg->hide();
500   }
501 }
502
503 void
504 VVTK_MainWindow1
505 ::OnSegmentationSwitch()
506 {
507   QtxAction* anAction = ( QtxAction* )sender();
508   OnSegmentationSwitch(anAction);
509 }
510
511
512 //----------------------------------------------------------------------------
513 VVTK_MainWindow2
514 ::VVTK_MainWindow2(QWidget* theParent, 
515                    const char* theName,
516                    SUIT_ResourceMgr* theResourceMgr,
517                    SUIT_ViewWindow* theViewWindow,
518                    QtxAction* theStyleSwitchAction):
519   VVTK_MainWindow(theParent,theName,theResourceMgr,theViewWindow),
520   myStyleSwitchAction(theStyleSwitchAction)
521 {}
522
523 VVTK_MainWindow2
524 ::~VVTK_MainWindow2()
525 {}
526
527
528 //----------------------------------------------------------------------------
529 void
530 VVTK_MainWindow2
531 ::Initialize(SVTK_RenderWindowInteractor* theInteractor)
532 {
533   VVTK_MainWindow::Initialize(theInteractor);
534
535   if( myStyleSwitchAction->isOn() )
536     PushInteractorStyle(myInteractorStyle.GetPointer());
537   connect(myStyleSwitchAction, SIGNAL(toggled(bool)), this, SLOT(OnInteractorStyleSwitch(bool)));
538 }
539