1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
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.
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.
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
17 // See http://www.salome-platform.org/
19 #include "SALOME_Actor.h"
21 #include <qapplication.h>
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>
32 #include "QtxAction.h"
34 #include "SUIT_Session.h"
35 #include "SUIT_ToolButton.h"
36 #include "SUIT_MessageBox.h"
37 #include "SUIT_Accel.h"
39 #include "SUIT_Tools.h"
40 #include "SUIT_ResourceMgr.h"
41 #include "SUIT_Accel.h"
43 #include "VTKViewer_Utilities.h"
45 #include "SVTK_View.h"
46 #include "SVTK_MainWindow.h"
47 #include "SVTK_Selector.h"
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"
57 #include "SALOME_ListIteratorOfListIO.hxx"
59 #include "VTKViewer_Algorithm.h"
60 #include "SVTK_Functor.h"
62 //----------------------------------------------------------------------------
64 ::SVTK_ViewWindow(SUIT_Desktop* theDesktop):
65 SUIT_ViewWindow(theDesktop),
72 ::Initialize(SVTK_ViewModelBase* theModel)
74 if(SUIT_ResourceMgr* aResourceMgr = SUIT_Session::session()->resourceMgr()){
75 myMainWindow = new SVTK_MainWindow(this,"SVTK_MainWindow",aResourceMgr,this);
77 SVTK_RenderWindowInteractor* anIteractor =
78 new SVTK_RenderWindowInteractor(myMainWindow,"SVTK_RenderWindowInteractor");
80 SVTK_Selector* aSelector = SVTK_Selector::New();
82 SVTK_GenericRenderWindowInteractor* aDevice =
83 SVTK_GenericRenderWindowInteractor::New();
84 aDevice->SetRenderWidget(anIteractor);
85 aDevice->SetSelector(aSelector);
87 SVTK_Renderer* aRenderer = SVTK_Renderer::New();
88 aRenderer->Initialize(aDevice,aSelector);
90 anIteractor->Initialize(aDevice,aRenderer,aSelector);
96 myMainWindow->Initialize(anIteractor);
98 SVTK_InteractorStyle* aStyle = SVTK_InteractorStyle::New();
99 anIteractor->PushInteractorStyle(aStyle);
102 setCentralWidget(myMainWindow);
104 myView = new SVTK_View(myMainWindow);
105 Initialize(myView,theModel);
111 ::Initialize(SVTK_View* theView,
112 SVTK_ViewModelBase* theModel)
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()));
137 //----------------------------------------------------------------------------
156 return getMainWindow()->getRenderWindow();
159 vtkRenderWindowInteractor*
163 return getMainWindow()->getInteractor();
170 return myMainWindow->getRenderer();
177 return myMainWindow->GetSelector();
181 //----------------------------------------------------------------------------
186 myMainWindow->onFrontView();
189 //----------------------------------------------------------------------------
194 myMainWindow->onBackView();
197 //----------------------------------------------------------------------------
202 myMainWindow->onTopView();
205 //----------------------------------------------------------------------------
210 myMainWindow->onBottomView();
213 //----------------------------------------------------------------------------
218 myMainWindow->onLeftView();
221 //----------------------------------------------------------------------------
226 myMainWindow->onRightView();
229 //----------------------------------------------------------------------------
234 myMainWindow->onResetView();
237 //----------------------------------------------------------------------------
242 myMainWindow->onFitAll();
245 //----------------------------------------------------------------
248 ::onSelectionChanged()
250 myView->onSelectionChanged();
253 //----------------------------------------------------------------
256 ::SetSelectionMode(Selection_Mode theMode)
258 myMainWindow->SetSelectionMode( theMode );
261 //----------------------------------------------------------------
264 ::SelectionMode() const
266 return myMainWindow->SelectionMode();
269 //----------------------------------------------------------------
274 myView->unHighlightAll();
277 //----------------------------------------------------------------
280 ::highlight(const Handle(SALOME_InteractiveObject)& theIO,
284 myView->highlight( theIO, theIsHighlight, theIsUpdate );
287 //----------------------------------------------------------------
290 ::isInViewer( const Handle(SALOME_InteractiveObject)& theIO )
292 return myView->isInViewer( theIO );
295 //----------------------------------------------------------------
298 ::isVisible( const Handle(SALOME_InteractiveObject)& theIO )
300 return myView->isVisible( theIO );
303 //----------------------------------------------------------------
306 ::Display(const Handle(SALOME_InteractiveObject)& theIO,
309 myView->Display(theIO,theImmediatly);
314 ::Erase(const Handle(SALOME_InteractiveObject)& theIO,
317 myView->Erase(theIO,theImmediatly);
322 ::DisplayOnly(const Handle(SALOME_InteractiveObject)& theIO)
324 myView->DisplayOnly(theIO);
331 myView->DisplayAll();
341 //----------------------------------------------------------------------------
344 ::setBackgroundColor( const QColor& color )
346 myMainWindow->SetBackgroundColor( color );
349 //----------------------------------------------------------------------------
352 ::backgroundColor() const
354 return myMainWindow->BackgroundColor();
357 //----------------------------------------------------------------------------
360 ::Repaint(bool theUpdateTrihedron)
362 myMainWindow->Repaint( theUpdateTrihedron );
365 //----------------------------------------------------------------------------
368 ::GetScale( double theScale[3] )
370 myMainWindow->GetScale( theScale );
373 //----------------------------------------------------------------------------
376 ::SetScale( double theScale[3] )
378 myMainWindow->SetScale( theScale );
381 //----------------------------------------------------------------------------
384 ::isTrihedronDisplayed()
386 return myMainWindow->IsTrihedronDisplayed();
391 ::isCubeAxesDisplayed()
393 return myMainWindow->IsCubeAxesDisplayed();
396 //----------------------------------------------------------------------------
401 myMainWindow->onViewTrihedron();
408 myMainWindow->onViewCubeAxes();
411 //----------------------------------------------------------------------------
416 return myMainWindow->GetTrihedron();
419 SVTK_CubeAxesActor2D*
423 return myMainWindow->GetCubeAxes();
428 ::GetTrihedronSize() const
430 return myMainWindow->GetTrihedronSize();
435 ::SetTrihedronSize(const int theSize, const bool theRelative)
437 myMainWindow->SetTrihedronSize(theSize, theRelative);
440 /*! If parameter theIsForcedUpdate is true, recalculate parameters for
441 * trihedron and cube axes, even if trihedron and cube axes is invisible.
445 ::AdjustTrihedrons(const bool theIsForcedUpdate)
447 myMainWindow->AdjustActors();
450 //----------------------------------------------------------------------------
453 ::onAdjustTrihedron()
455 myMainWindow->onAdjustTrihedron();
462 myMainWindow->onAdjustCubeAxes();
465 //=======================================================================
468 ::onKeyPressed(QKeyEvent* event)
470 emit keyPressed( this, event );
473 //=======================================================================
476 ::onKeyReleased(QKeyEvent* event)
478 emit keyReleased( this, event );
481 //=======================================================================
484 ::onMousePressed(QMouseEvent* event)
486 emit mousePressed(this, event);
489 //=======================================================================
492 ::onMouseReleased(QMouseEvent* event)
494 emit mouseReleased( this, event );
497 //=======================================================================
500 ::onMouseMoving(QMouseEvent* event)
502 emit mouseMoving( this, event );
505 //=======================================================================
508 ::onMouseDoubleClicked( QMouseEvent* event )
510 emit mouseDoubleClicked( this, event );
513 //----------------------------------------------------------------------------
516 ::AddActor( VTKViewer_Actor* theActor,
519 myMainWindow->AddActor( theActor, theUpdate );
522 //----------------------------------------------------------------------------
525 ::RemoveActor( VTKViewer_Actor* theActor,
528 myMainWindow->RemoveActor( theActor, theUpdate );
531 //----------------------------------------------------------------------------
536 return myMainWindow->dumpView();
539 //----------------------------------------------------------------------------
542 ::SetSelectionProp(const double& theRed,
543 const double& theGreen,
544 const double& theBlue,
547 myView->SetSelectionProp(theRed,theGreen,theBlue,theWidth);
550 //----------------------------------------------------------------------------
553 ::SetPreselectionProp(const double& theRed,
554 const double& theGreen,
555 const double& theBlue,
558 myView->SetPreselectionProp(theRed,theGreen,theBlue,theWidth);
561 //----------------------------------------------------------------------------
564 ::SetSelectionTolerance(const double& theTolNodes,
565 const double& theTolItems)
567 myView->SetSelectionTolerance(theTolNodes,theTolItems);
570 //----------------------------------------------------------------------------
571 int convertAction( const int accelAction )
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;
588 //----------------------------------------------------------------------------
591 ::action( const int accelAction )
593 if ( accelAction == SUIT_Accel::ZoomFit )
596 int anEvent = convertAction( accelAction );
597 myMainWindow->InvokeEvent( anEvent, 0 );
601 /*! The method returns the visual parameters of this view as a formated string
605 ::getVisualParameters()
607 double pos[3], focalPnt[3], viewUp[3], parScale, scale[3];
609 vtkCamera* camera = getRenderer()->GetActiveCamera();
610 camera->GetPosition( pos );
611 camera->GetFocalPoint( focalPnt );
612 camera->GetViewUp( viewUp );
613 parScale = camera->GetParallelScale();
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] );
623 /* The method restores visual parameters of this view or postpones it untill the view is shown
627 ::setVisualParameters( const QString& parameters )
629 SVTK_RenderWindowInteractor* anInteractor = getMainWindow()->GetInteractor();
630 if ( anInteractor->isVisible() ) {
631 doSetVisualParameters( parameters );
634 myVisualParams = parameters;
635 anInteractor->installEventFilter(this);
639 /* The method restores visual parameters of this view from a formated string
643 ::doSetVisualParameters( const QString& parameters )
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();
663 // applying parameters
664 vtkCamera* camera = getRenderer()->GetActiveCamera();
665 camera->SetPosition( pos );
666 camera->SetFocalPoint( focalPnt );
667 camera->SetViewUp( viewUp );
668 camera->SetParallelScale( parScale );
673 // getRenderer()->ResetCameraClippingRange();
675 // getMainWindow()->GetRenderer()->GetTransform()->SetMatrixScale( scale[0], scale[1], scale[2] );
680 //================================================================
681 // Function : eventFilter
682 /*! Purpose : delayed setVisualParameters
684 //================================================================
685 bool SVTK_ViewWindow::eventFilter( QObject* theWatched, QEvent* theEvent )
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
694 return SUIT_ViewWindow::eventFilter( theWatched, theEvent );