Salome HOME
Join modifications from branch OCC_development_for_3_2_0a2
[modules/gui.git] / src / SVTK / SVTK_ViewWindow.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either 
6 // version 2.1 of the License.
7 // 
8 // This library is distributed in the hope that it will be useful 
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public  
14 // License along with this library; if not, write to the Free Software 
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/
18 //
19 #include "SALOME_Actor.h"
20
21 #include <qapplication.h>
22 #include <qimage.h>
23
24 #include <vtkTextProperty.h>
25 #include <vtkActorCollection.h>
26 #include <vtkRenderWindow.h>
27 #include <vtkRenderer.h>
28 #include <vtkCamera.h>
29 #include <vtkPointPicker.h>
30 #include <vtkCellPicker.h>
31
32 #include "QtxAction.h"
33
34 #include "SUIT_Session.h"
35 #include "SUIT_ToolButton.h"
36 #include "SUIT_MessageBox.h"
37 #include "SUIT_Accel.h"
38
39 #include "SUIT_Tools.h"
40 #include "SUIT_ResourceMgr.h"
41 #include "SUIT_Accel.h"
42
43 #include "VTKViewer_Utilities.h"
44
45 #include "SVTK_View.h"
46 #include "SVTK_MainWindow.h"
47 #include "SVTK_Selector.h"
48
49 #include "SVTK_Event.h"
50 #include "SVTK_Renderer.h"
51 #include "SVTK_ViewWindow.h"
52 #include "SVTK_ViewModelBase.h"
53 #include "SVTK_InteractorStyle.h"
54 #include "SVTK_RenderWindowInteractor.h"
55 #include "SVTK_GenericRenderWindowInteractor.h"
56
57 #include "SALOME_ListIteratorOfListIO.hxx"
58
59 #include "VTKViewer_Algorithm.h"
60 #include "SVTK_Functor.h"
61
62 //----------------------------------------------------------------------------
63 SVTK_ViewWindow
64 ::SVTK_ViewWindow(SUIT_Desktop* theDesktop):
65   SUIT_ViewWindow(theDesktop),
66   myMainWindow(NULL),
67   myView(NULL)
68 {}
69
70 void
71 SVTK_ViewWindow
72 ::Initialize(SVTK_ViewModelBase* theModel)
73 {
74   if(SUIT_ResourceMgr* aResourceMgr = SUIT_Session::session()->resourceMgr()){
75     myMainWindow = new SVTK_MainWindow(this,"SVTK_MainWindow",aResourceMgr,this);
76
77     SVTK_RenderWindowInteractor* anIteractor = 
78       new SVTK_RenderWindowInteractor(myMainWindow,"SVTK_RenderWindowInteractor");
79
80     SVTK_Selector* aSelector = SVTK_Selector::New();
81
82     SVTK_GenericRenderWindowInteractor* aDevice = 
83       SVTK_GenericRenderWindowInteractor::New();
84     aDevice->SetRenderWidget(anIteractor);
85     aDevice->SetSelector(aSelector);
86
87     SVTK_Renderer* aRenderer = SVTK_Renderer::New();
88     aRenderer->Initialize(aDevice,aSelector);
89
90     anIteractor->Initialize(aDevice,aRenderer,aSelector);
91
92     aDevice->Delete();
93     aRenderer->Delete();
94     aSelector->Delete();
95
96     myMainWindow->Initialize(anIteractor);
97
98     SVTK_InteractorStyle* aStyle = SVTK_InteractorStyle::New();
99     anIteractor->PushInteractorStyle(aStyle);
100     aStyle->Delete();
101
102     setCentralWidget(myMainWindow);
103     
104     myView = new SVTK_View(myMainWindow);
105     Initialize(myView,theModel);
106   }
107 }
108
109 void
110 SVTK_ViewWindow
111 ::Initialize(SVTK_View* theView,
112              SVTK_ViewModelBase* theModel)
113 {
114   connect(theView,SIGNAL(KeyPressed(QKeyEvent*)),
115           this,SLOT(onKeyPressed(QKeyEvent*)) );
116   connect(theView,SIGNAL(KeyReleased(QKeyEvent*)),
117           this,SLOT(onKeyReleased(QKeyEvent*)));
118   connect(theView,SIGNAL(MouseButtonPressed(QMouseEvent*)),
119           this,SLOT(onMousePressed(QMouseEvent*)));
120   connect(theView,SIGNAL(MouseButtonReleased(QMouseEvent*)),
121           this,SLOT(onMouseReleased(QMouseEvent*)));
122   connect(theView,SIGNAL(MouseDoubleClicked(QMouseEvent*)),
123           this,SLOT(onMouseDoubleClicked(QMouseEvent*)));
124   connect(theView,SIGNAL(MouseMove(QMouseEvent*)),
125           this,SLOT(onMouseMoving(QMouseEvent*)));
126   connect(theView,SIGNAL(contextMenuRequested(QContextMenuEvent*)),
127           this,SIGNAL(contextMenuRequested(QContextMenuEvent *)));
128   connect(theView,SIGNAL(selectionChanged()),
129           theModel,SLOT(onSelectionChanged()));
130 }
131
132 SVTK_ViewWindow
133 ::~SVTK_ViewWindow()
134 {}
135
136
137 //----------------------------------------------------------------------------
138 SVTK_View* 
139 SVTK_ViewWindow
140 ::getView() 
141
142   return myView; 
143 }
144
145 SVTK_MainWindow* 
146 SVTK_ViewWindow
147 ::getMainWindow() 
148
149   return myMainWindow; 
150 }
151
152 vtkRenderWindow*
153 SVTK_ViewWindow
154 ::getRenderWindow()
155 {
156   return getMainWindow()->getRenderWindow();
157 }
158
159 vtkRenderWindowInteractor*
160 SVTK_ViewWindow
161 ::getInteractor()
162 {
163   return getMainWindow()->getInteractor();
164 }
165
166 vtkRenderer*
167 SVTK_ViewWindow
168 ::getRenderer()
169 {
170   return myMainWindow->getRenderer();
171 }
172
173 SVTK_Selector* 
174 SVTK_ViewWindow
175 ::GetSelector() 
176
177   return myMainWindow->GetSelector(); 
178 }
179
180
181 //----------------------------------------------------------------------------
182 void
183 SVTK_ViewWindow
184 ::onFrontView()
185 {
186   myMainWindow->onFrontView();
187 }
188
189 //----------------------------------------------------------------------------
190 void
191 SVTK_ViewWindow
192 ::onBackView()
193 {
194   myMainWindow->onBackView();
195 }
196
197 //----------------------------------------------------------------------------
198 void
199 SVTK_ViewWindow
200 ::onTopView()
201 {
202   myMainWindow->onTopView();
203 }
204
205 //----------------------------------------------------------------------------
206 void
207 SVTK_ViewWindow
208 ::onBottomView()
209 {
210   myMainWindow->onBottomView();
211 }
212
213 //----------------------------------------------------------------------------
214 void
215 SVTK_ViewWindow
216 ::onLeftView()
217 {
218   myMainWindow->onLeftView();
219 }
220
221 //----------------------------------------------------------------------------
222 void
223 SVTK_ViewWindow
224 ::onRightView()
225 {
226   myMainWindow->onRightView();
227 }
228
229 //----------------------------------------------------------------------------
230 void
231 SVTK_ViewWindow
232 ::onResetView()
233 {
234   myMainWindow->onResetView();
235 }
236
237 //----------------------------------------------------------------------------
238 void
239 SVTK_ViewWindow
240 ::onFitAll()
241 {
242   myMainWindow->onFitAll();
243 }
244
245 //----------------------------------------------------------------
246 void
247 SVTK_ViewWindow
248 ::onSelectionChanged()
249 {
250   myView->onSelectionChanged();
251 }
252
253 //----------------------------------------------------------------
254 void
255 SVTK_ViewWindow
256 ::SetSelectionMode(Selection_Mode theMode)
257 {
258   myMainWindow->SetSelectionMode( theMode );
259 }
260
261 //----------------------------------------------------------------
262 Selection_Mode
263 SVTK_ViewWindow
264 ::SelectionMode() const
265 {
266   return myMainWindow->SelectionMode();
267 }
268
269 //----------------------------------------------------------------
270 void 
271 SVTK_ViewWindow
272 ::unHighlightAll() 
273 {
274   myView->unHighlightAll();
275 }
276
277 //----------------------------------------------------------------
278 void
279 SVTK_ViewWindow
280 ::highlight(const Handle(SALOME_InteractiveObject)& theIO, 
281             bool theIsHighlight, 
282             bool theIsUpdate ) 
283 {
284   myView->highlight( theIO, theIsHighlight, theIsUpdate );
285 }
286
287 //----------------------------------------------------------------
288 bool
289 SVTK_ViewWindow
290 ::isInViewer( const Handle(SALOME_InteractiveObject)& theIO ) 
291 {
292   return myView->isInViewer( theIO );
293 }
294
295 //----------------------------------------------------------------
296 bool
297 SVTK_ViewWindow
298 ::isVisible( const Handle(SALOME_InteractiveObject)& theIO ) 
299 {
300   return myView->isVisible( theIO );
301 }
302
303 //----------------------------------------------------------------
304 void
305 SVTK_ViewWindow
306 ::Display(const Handle(SALOME_InteractiveObject)& theIO,
307           bool theImmediatly) 
308 {
309   myView->Display(theIO,theImmediatly);
310 }
311
312 void
313 SVTK_ViewWindow
314 ::Erase(const Handle(SALOME_InteractiveObject)& theIO,
315           bool theImmediatly) 
316 {
317   myView->Erase(theIO,theImmediatly);
318 }
319
320 void
321 SVTK_ViewWindow
322 ::DisplayOnly(const Handle(SALOME_InteractiveObject)& theIO) 
323 {
324   myView->DisplayOnly(theIO);
325 }
326
327 void 
328 SVTK_ViewWindow
329 ::DisplayAll() 
330 {
331   myView->DisplayAll();
332 }
333
334 void 
335 SVTK_ViewWindow
336 ::EraseAll() 
337 {
338   myView->EraseAll();
339 }
340
341 //----------------------------------------------------------------------------
342 void
343 SVTK_ViewWindow
344 ::setBackgroundColor( const QColor& color )
345 {
346   myMainWindow->SetBackgroundColor( color );
347 }
348
349 //----------------------------------------------------------------------------
350 QColor
351 SVTK_ViewWindow
352 ::backgroundColor() const
353 {
354   return myMainWindow->BackgroundColor();
355 }
356
357 //----------------------------------------------------------------------------
358 void
359 SVTK_ViewWindow
360 ::Repaint(bool theUpdateTrihedron)
361 {
362   myMainWindow->Repaint( theUpdateTrihedron );
363 }
364
365 //----------------------------------------------------------------------------
366 void
367 SVTK_ViewWindow
368 ::GetScale( double theScale[3] ) 
369 {
370   myMainWindow->GetScale( theScale );
371 }
372
373 //----------------------------------------------------------------------------
374 void
375 SVTK_ViewWindow
376 ::SetScale( double theScale[3] ) 
377 {
378   myMainWindow->SetScale( theScale );
379 }
380
381 //----------------------------------------------------------------------------
382 bool
383 SVTK_ViewWindow
384 ::isTrihedronDisplayed()
385 {
386   return myMainWindow->IsTrihedronDisplayed();
387 }
388
389 bool
390 SVTK_ViewWindow
391 ::isCubeAxesDisplayed()
392 {
393   return myMainWindow->IsCubeAxesDisplayed();
394 }
395
396 //----------------------------------------------------------------------------
397 void 
398 SVTK_ViewWindow
399 ::onViewTrihedron()
400 {
401   myMainWindow->onViewTrihedron();
402 }
403
404 void
405 SVTK_ViewWindow
406 ::onViewCubeAxes()
407 {
408   myMainWindow->onViewCubeAxes();
409 }
410
411 //----------------------------------------------------------------------------
412 VTKViewer_Trihedron* 
413 SVTK_ViewWindow::
414 GetTrihedron()
415 {
416   return myMainWindow->GetTrihedron();
417 }
418
419 SVTK_CubeAxesActor2D* 
420 SVTK_ViewWindow
421 ::GetCubeAxes()
422 {
423   return myMainWindow->GetCubeAxes();
424 }
425
426 int
427 SVTK_ViewWindow
428 ::GetTrihedronSize() const
429 {
430   return myMainWindow->GetTrihedronSize();
431 }
432
433 void
434 SVTK_ViewWindow
435 ::SetTrihedronSize(const int theSize, const bool theRelative)
436 {
437   myMainWindow->SetTrihedronSize(theSize, theRelative);
438 }
439
440 /*! If parameter theIsForcedUpdate is true, recalculate parameters for
441  *  trihedron and cube axes, even if trihedron and cube axes is invisible.
442  */
443 void
444 SVTK_ViewWindow
445 ::AdjustTrihedrons(const bool theIsForcedUpdate)
446 {
447   myMainWindow->AdjustActors();
448 }
449
450 //----------------------------------------------------------------------------
451 void
452 SVTK_ViewWindow
453 ::onAdjustTrihedron()
454 {   
455   myMainWindow->onAdjustTrihedron();
456 }
457
458 void
459 SVTK_ViewWindow
460 ::onAdjustCubeAxes()
461 {   
462   myMainWindow->onAdjustCubeAxes();
463 }
464
465 //=======================================================================
466 void
467 SVTK_ViewWindow
468 ::onKeyPressed(QKeyEvent* event)
469 {
470   emit keyPressed( this, event );
471 }
472
473 //=======================================================================
474 void
475 SVTK_ViewWindow
476 ::onKeyReleased(QKeyEvent* event)
477 {
478   emit keyReleased( this, event );
479 }
480
481 //=======================================================================
482 void
483 SVTK_ViewWindow
484 ::onMousePressed(QMouseEvent* event)
485 {
486   emit mousePressed(this, event);
487 }
488
489 //=======================================================================
490 void
491 SVTK_ViewWindow
492 ::onMouseReleased(QMouseEvent* event)
493 {
494   emit mouseReleased( this, event );
495 }
496
497 //=======================================================================
498 void
499 SVTK_ViewWindow
500 ::onMouseMoving(QMouseEvent* event)
501 {
502   emit mouseMoving( this, event );
503 }
504
505 //=======================================================================
506 void
507 SVTK_ViewWindow
508 ::onMouseDoubleClicked( QMouseEvent* event )
509 {
510   emit mouseDoubleClicked( this, event );
511 }
512
513 //----------------------------------------------------------------------------
514 void
515 SVTK_ViewWindow
516 ::AddActor( VTKViewer_Actor* theActor, 
517             bool theUpdate )
518 {
519   myMainWindow->AddActor( theActor, theUpdate );
520 }
521
522 //----------------------------------------------------------------------------
523 void
524 SVTK_ViewWindow
525 ::RemoveActor( VTKViewer_Actor* theActor, 
526                bool theUpdate )
527 {
528   myMainWindow->RemoveActor( theActor, theUpdate );
529 }
530
531 //----------------------------------------------------------------------------
532 QImage
533 SVTK_ViewWindow
534 ::dumpView()
535 {
536   return myMainWindow->dumpView();
537 }
538
539 //----------------------------------------------------------------------------
540 void
541 SVTK_ViewWindow
542 ::SetSelectionProp(const double& theRed, 
543                    const double& theGreen, 
544                    const double& theBlue, 
545                    const int& theWidth) 
546 {
547   myView->SetSelectionProp(theRed,theGreen,theBlue,theWidth);
548 }
549
550 //----------------------------------------------------------------------------
551 void
552 SVTK_ViewWindow
553 ::SetPreselectionProp(const double& theRed, 
554                       const double& theGreen, 
555                       const double& theBlue, 
556                       const int& theWidth) 
557 {
558   myView->SetPreselectionProp(theRed,theGreen,theBlue,theWidth);
559 }
560
561 //----------------------------------------------------------------------------
562 void
563 SVTK_ViewWindow
564 ::SetSelectionTolerance(const double& theTolNodes, 
565                         const double& theTolItems)
566 {
567   myView->SetSelectionTolerance(theTolNodes,theTolItems);
568 }
569
570 //----------------------------------------------------------------------------
571 int convertAction( const int accelAction )
572 {
573   switch ( accelAction ) {
574   case SUIT_Accel::PanLeft     : return SVTK::PanLeftEvent;
575   case SUIT_Accel::PanRight    : return SVTK::PanRightEvent;
576   case SUIT_Accel::PanUp       : return SVTK::PanUpEvent;
577   case SUIT_Accel::PanDown     : return SVTK::PanDownEvent;
578   case SUIT_Accel::ZoomIn      : return SVTK::ZoomInEvent;
579   case SUIT_Accel::ZoomOut     : return SVTK::ZoomOutEvent;
580   case SUIT_Accel::RotateLeft  : return SVTK::RotateLeftEvent;
581   case SUIT_Accel::RotateRight : return SVTK::RotateRightEvent;
582   case SUIT_Accel::RotateUp    : return SVTK::RotateUpEvent;
583   case SUIT_Accel::RotateDown  : return SVTK::RotateDownEvent;  
584   }
585   return accelAction;
586 }
587
588 //----------------------------------------------------------------------------
589 void 
590 SVTK_ViewWindow
591 ::action( const int accelAction  )
592 {
593   if ( accelAction == SUIT_Accel::ZoomFit )
594     onFitAll();
595   else {
596     int anEvent = convertAction( accelAction );
597     myMainWindow->InvokeEvent( anEvent, 0 );
598   }
599 }
600
601 /*! The method returns the visual parameters of this view as a formated string
602  */
603 QString
604 SVTK_ViewWindow
605 ::getVisualParameters()
606 {
607   double pos[3], focalPnt[3], viewUp[3], parScale, scale[3];
608
609   vtkCamera* camera = getRenderer()->GetActiveCamera();
610   camera->GetPosition( pos );
611   camera->GetFocalPoint( focalPnt );
612   camera->GetViewUp( viewUp );
613   parScale = camera->GetParallelScale();
614   GetScale( scale );
615
616   QString retStr;
617   retStr.sprintf( "%.12e*%.12e*%.12e*%.12e*%.12e*%.12e*%.12e*%.12e*%.12e*%.12e*%.12e*%.12e*%.12e", 
618                   pos[0], pos[1], pos[2], focalPnt[0], focalPnt[1], focalPnt[2], viewUp[0], viewUp[1], 
619                   viewUp[2], parScale, scale[0], scale[1], scale[2] );
620   return retStr;
621 }
622
623 /* The method restores visual parameters of this view or postpones it untill the view is shown
624  */
625 void
626 SVTK_ViewWindow
627 ::setVisualParameters( const QString& parameters )
628 {
629   SVTK_RenderWindowInteractor* anInteractor = getMainWindow()->GetInteractor();
630   if ( anInteractor->isVisible() ) {
631     doSetVisualParameters( parameters ); 
632   }
633   else {
634     myVisualParams = parameters;
635     anInteractor->installEventFilter(this);
636   }
637 }
638
639 /* The method restores visual parameters of this view from a formated string
640  */
641 void
642 SVTK_ViewWindow
643 ::doSetVisualParameters( const QString& parameters )
644 {
645   QStringList paramsLst = QStringList::split( '*', parameters, true );
646   if ( paramsLst.size() == 13 ) {
647     // 'reading' list of parameters
648     double pos[3], focalPnt[3], viewUp[3], parScale, scale[3];
649     pos[0] = paramsLst[0].toDouble();
650     pos[1] = paramsLst[1].toDouble();
651     pos[2] = paramsLst[2].toDouble();
652     focalPnt[0] = paramsLst[3].toDouble();
653     focalPnt[1] = paramsLst[4].toDouble();
654     focalPnt[2] = paramsLst[5].toDouble();
655     viewUp[0] = paramsLst[6].toDouble();
656     viewUp[1] = paramsLst[7].toDouble();
657     viewUp[2] = paramsLst[8].toDouble();
658     parScale = paramsLst[9].toDouble();
659     scale[0] = paramsLst[10].toDouble();
660     scale[1] = paramsLst[11].toDouble();
661     scale[2] = paramsLst[12].toDouble();
662     
663     // applying parameters
664     vtkCamera* camera = getRenderer()->GetActiveCamera();
665     camera->SetPosition( pos );
666     camera->SetFocalPoint( focalPnt );
667     camera->SetViewUp( viewUp );
668     camera->SetParallelScale( parScale );
669     SetScale( scale );
670
671     //    resize( size() );
672
673     //    getRenderer()->ResetCameraClippingRange();
674     //    Repaint();
675     //    getMainWindow()->GetRenderer()->GetTransform()->SetMatrixScale( scale[0], scale[1], scale[2] );
676   }
677 }
678
679
680 //================================================================
681 // Function : eventFilter
682 /*! Purpose : delayed setVisualParameters
683 */
684 //================================================================
685 bool SVTK_ViewWindow::eventFilter( QObject* theWatched, QEvent* theEvent )
686 {
687   if ( theEvent->type() == QEvent::Show && theWatched->inherits( "SVTK_RenderWindowInteractor" ) ) {
688     SVTK_RenderWindowInteractor* anInteractor = (SVTK_RenderWindowInteractor*)theWatched;
689     if ( anInteractor->isVisible() ) {
690       doSetVisualParameters( myVisualParams );
691       anInteractor->removeEventFilter( this ); // theWatched = RenderWindowInteractor
692     }
693   }
694   return SUIT_ViewWindow::eventFilter( theWatched, theEvent );
695 }