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