1 #include "VTKViewer_ViewWindow.h"
2 #include "VTKViewer_ViewModel.h"
3 #include "VTKViewer_RenderWindow.h"
4 #include "VTKViewer_RenderWindowInteractor.h"
5 #include "VTKViewer_InteractorStyle.h"
6 #include "VTKViewer_Trihedron.h"
7 #include "VTKViewer_Transform.h"
8 #include "VTKViewer_Utilities.h"
10 #include "SUIT_Session.h"
11 #include "SUIT_ToolButton.h"
12 #include "SUIT_MessageBox.h"
14 #include "SUIT_Tools.h"
15 #include "SUIT_ResourceMgr.h"
17 #include <qapplication.h>
19 #include <vtkRenderer.h>
20 #include <vtkCamera.h>
23 VTKViewer_ViewWindow::VTKViewer_ViewWindow( SUIT_Desktop* theDesktop,
24 VTKViewer_Viewer* theModel,
25 VTKViewer_InteractorStyle* iStyle,
26 VTKViewer_RenderWindowInteractor* rw )
27 : SUIT_ViewWindow( theDesktop )
31 myTrihedron = VTKViewer_Trihedron::New();
32 myTransform = VTKViewer_Transform::New();
33 myRenderer = vtkRenderer::New() ;
35 myTrihedron->AddToRender( myRenderer );
37 myRenderWindow = new VTKViewer_RenderWindow( this, "RenderWindow" );
38 setCentralWidget(myRenderWindow);
39 myRenderWindow->setFocusPolicy( StrongFocus );
40 myRenderWindow->setFocus();
42 myRenderWindow->getRenderWindow()->AddRenderer( myRenderer );
44 myRenderer->GetActiveCamera()->ParallelProjectionOn();
45 myRenderer->LightFollowCameraOn();
46 myRenderer->TwoSidedLightingOn();
48 // Set BackgroundColor
49 QString BgrColorRed = "0";//SUIT_CONFIG->getSetting("VTKViewer:BackgroundColorRed");
50 QString BgrColorGreen = "0";//SUIT_CONFIG->getSetting("VTKViewer:BackgroundColorGreen");
51 QString BgrColorBlue = "0";//SUIT_CONFIG->getSetting("VTKViewer:BackgroundColorBlue");
53 if( !BgrColorRed.isEmpty() && !BgrColorGreen.isEmpty() && !BgrColorBlue.isEmpty() )
54 myRenderer->SetBackground( BgrColorRed.toInt()/255., BgrColorGreen.toInt()/255., BgrColorBlue.toInt()/255. );
56 myRenderer->SetBackground( 0, 0, 0 );
58 // Create an interactor.
59 myRWInteractor = rw ? rw : VTKViewer_RenderWindowInteractor::New();
60 myRWInteractor->SetRenderWindow( myRenderWindow->getRenderWindow() );
62 VTKViewer_InteractorStyle* RWS = iStyle ? iStyle : VTKViewer_InteractorStyle::New();
63 RWS->setGUIWindow( myRenderWindow );
64 myRWInteractor->SetInteractorStyle( RWS );
66 myRWInteractor->Initialize();
67 RWS->setTriedron( myTrihedron );
68 RWS->FindPokedRenderer( 0, 0 );
70 setCentralWidget( myRenderWindow );
72 myToolBar = new QToolBar(this);
73 myToolBar->setCloseMode(QDockWindow::Undocked);
74 myToolBar->setLabel(tr("LBL_TOOLBAR_LABEL"));
79 connect( myRenderWindow, SIGNAL(KeyPressed( QKeyEvent* )),
80 this, SLOT(onKeyPressed( QKeyEvent* )) );
81 connect( myRenderWindow, SIGNAL(KeyReleased( QKeyEvent* )),
82 this, SLOT(onKeyReleased( QKeyEvent* )) );
83 connect( myRenderWindow, SIGNAL(MouseButtonPressed( QMouseEvent* )),
84 this, SLOT(onMousePressed( QMouseEvent* )) );
85 connect( myRenderWindow, SIGNAL(MouseButtonReleased( QMouseEvent* )),
86 this, SLOT(onMouseReleased( QMouseEvent* )) );
87 connect( myRenderWindow, SIGNAL(MouseDoubleClicked( QMouseEvent* )),
88 this, SLOT(onMouseDoubleClicked( QMouseEvent* )) );
89 connect( myRenderWindow, SIGNAL(MouseMove( QMouseEvent* )),
90 this, SLOT(onMouseMoving( QMouseEvent* )) );
91 connect( myRWInteractor, SIGNAL(RenderWindowModified()),
92 myRenderWindow, SLOT(update()) );
94 connect( myRenderWindow, SIGNAL(contextMenuRequested( QContextMenuEvent * )),
95 this, SIGNAL(contextMenuRequested( QContextMenuEvent * )) );
97 connect( myRWInteractor, SIGNAL(contextMenuRequested( QContextMenuEvent * )),
98 this, SIGNAL(contextMenuRequested( QContextMenuEvent * )) );
105 VTKViewer_ViewWindow::~VTKViewer_ViewWindow()
107 myTransform->Delete();
108 // In order to ensure that the interactor unregisters
109 // this RenderWindow, we assign a NULL RenderWindow to
110 // it before deleting it.
111 myRWInteractor->SetRenderWindow( NULL );
112 myRWInteractor->Delete();
115 myRenderer->RemoveAllProps();
116 //m_Renderer->Delete() ;
117 myTrihedron->Delete();
120 /*!Checks: is trihedron displayed.*/
121 bool VTKViewer_ViewWindow::isTrihedronDisplayed(){
122 return myTrihedron->GetVisibility() == VTKViewer_Trihedron::eOn;
125 /*!Activates 'zooming' transformation*/
126 void VTKViewer_ViewWindow::activateZoom()
128 myRWInteractor->GetInteractorStyle()->startZoom();
131 /*!Activates 'panning' transformation*/
132 void VTKViewer_ViewWindow::activatePanning()
134 myRWInteractor->GetInteractorStyle()->startPan();
137 /*!Activates 'rotation' transformation*/
138 void VTKViewer_ViewWindow::activateRotation()
140 myRWInteractor->GetInteractorStyle()->startRotate();
143 /*!Activate global panning.*/
144 void VTKViewer_ViewWindow::activateGlobalPanning()
146 //if(myTrihedron->GetVisibleActorCount(myRenderer))
147 myRWInteractor->GetInteractorStyle()->startGlobalPan();
150 /*!Activates 'fit area' transformation*/
151 void VTKViewer_ViewWindow::activateWindowFit()
153 myRWInteractor->GetInteractorStyle()->startFitArea();
157 void VTKViewer_ViewWindow::createActions()
159 if (!myActionsMap.isEmpty()) return;
161 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
166 aAction = new QtxAction(tr("MNU_DUMP_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_DUMP" ) ),
167 tr( "MNU_DUMP_VIEW" ), 0, this);
168 aAction->setStatusTip(tr("DSC_DUMP_VIEW"));
169 connect(aAction, SIGNAL(activated()), this, SLOT(onDumpView()));
170 myActionsMap[ DumpId ] = aAction;
173 aAction = new QtxAction(tr("MNU_FITALL"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_FITALL" ) ),
174 tr( "MNU_FITALL" ), 0, this);
175 aAction->setStatusTip(tr("DSC_FITALL"));
176 connect(aAction, SIGNAL(activated()), this, SLOT(onFitAll()));
177 myActionsMap[ FitAllId ] = aAction;
180 aAction = new QtxAction(tr("MNU_FITRECT"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_FITAREA" ) ),
181 tr( "MNU_FITRECT" ), 0, this);
182 aAction->setStatusTip(tr("DSC_FITRECT"));
183 connect(aAction, SIGNAL(activated()), this, SLOT(activateWindowFit()));
184 myActionsMap[ FitRectId ] = aAction;
187 aAction = new QtxAction(tr("MNU_ZOOM_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_ZOOM" ) ),
188 tr( "MNU_ZOOM_VIEW" ), 0, this);
189 aAction->setStatusTip(tr("DSC_ZOOM_VIEW"));
190 connect(aAction, SIGNAL(activated()), this, SLOT(activateZoom()));
191 myActionsMap[ ZoomId ] = aAction;
194 aAction = new QtxAction(tr("MNU_PAN_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_PAN" ) ),
195 tr( "MNU_PAN_VIEW" ), 0, this);
196 aAction->setStatusTip(tr("DSC_PAN_VIEW"));
197 connect(aAction, SIGNAL(activated()), this, SLOT(activatePanning()));
198 myActionsMap[ PanId ] = aAction;
200 //! \li Global Panning
201 aAction = new QtxAction(tr("MNU_GLOBALPAN_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_GLOBALPAN" ) ),
202 tr( "MNU_GLOBALPAN_VIEW" ), 0, this);
203 aAction->setStatusTip(tr("DSC_GLOBALPAN_VIEW"));
204 connect(aAction, SIGNAL(activated()), this, SLOT(activateGlobalPanning()));
205 myActionsMap[ GlobalPanId ] = aAction;
208 aAction = new QtxAction(tr("MNU_ROTATE_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_ROTATE" ) ),
209 tr( "MNU_ROTATE_VIEW" ), 0, this);
210 aAction->setStatusTip(tr("DSC_ROTATE_VIEW"));
211 connect(aAction, SIGNAL(activated()), this, SLOT(activateRotation()));
212 myActionsMap[ RotationId ] = aAction;
215 aAction = new QtxAction(tr("MNU_FRONT_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_FRONT" ) ),
216 tr( "MNU_FRONT_VIEW" ), 0, this);
217 aAction->setStatusTip(tr("DSC_FRONT_VIEW"));
218 connect(aAction, SIGNAL(activated()), this, SLOT(onFrontView()));
219 myActionsMap[ FrontId ] = aAction;
222 aAction = new QtxAction(tr("MNU_BACK_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_BACK" ) ),
223 tr( "MNU_BACK_VIEW" ), 0, this);
224 aAction->setStatusTip(tr("DSC_BACK_VIEW"));
225 connect(aAction, SIGNAL(activated()), this, SLOT(onBackView()));
226 myActionsMap[ BackId ] = aAction;
229 aAction = new QtxAction(tr("MNU_TOP_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_TOP" ) ),
230 tr( "MNU_TOP_VIEW" ), 0, this);
231 aAction->setStatusTip(tr("DSC_TOP_VIEW"));
232 connect(aAction, SIGNAL(activated()), this, SLOT(onTopView()));
233 myActionsMap[ TopId ] = aAction;
236 aAction = new QtxAction(tr("MNU_BOTTOM_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_BOTTOM" ) ),
237 tr( "MNU_BOTTOM_VIEW" ), 0, this);
238 aAction->setStatusTip(tr("DSC_BOTTOM_VIEW"));
239 connect(aAction, SIGNAL(activated()), this, SLOT(onBottomView()));
240 myActionsMap[ BottomId ] = aAction;
243 aAction = new QtxAction(tr("MNU_LEFT_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_LEFT" ) ),
244 tr( "MNU_LEFT_VIEW" ), 0, this);
245 aAction->setStatusTip(tr("DSC_LEFT_VIEW"));
246 connect(aAction, SIGNAL(activated()), this, SLOT(onLeftView()));
247 myActionsMap[ LeftId ] = aAction;
250 aAction = new QtxAction(tr("MNU_RIGHT_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_RIGHT" ) ),
251 tr( "MNU_RIGHT_VIEW" ), 0, this);
252 aAction->setStatusTip(tr("DSC_RIGHT_VIEW"));
253 connect(aAction, SIGNAL(activated()), this, SLOT(onRightView()));
254 myActionsMap[ RightId ] = aAction;
257 aAction = new QtxAction(tr("MNU_RESET_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_RESET" ) ),
258 tr( "MNU_RESET_VIEW" ), 0, this);
259 aAction->setStatusTip(tr("DSC_RESET_VIEW"));
260 connect(aAction, SIGNAL(activated()), this, SLOT(onResetView()));
261 myActionsMap[ ResetId ] = aAction;
263 //! \li Trihedron shown
264 aAction = new QtxAction(tr("MNU_SHOW_TRIHEDRON"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_TRIHEDRON" ) ),
265 tr( "MNU_SHOW_TRIHEDRON" ), 0, this);
266 aAction->setStatusTip(tr("DSC_SHOW_TRIHEDRON"));
267 connect(aAction, SIGNAL(activated()), this, SLOT(onTrihedronShow()));
268 myActionsMap[ TrihedronShowId ] = aAction;
271 /*!Create tool bar.*/
272 void VTKViewer_ViewWindow::createToolBar()
274 myActionsMap[DumpId]->addTo(myToolBar);
275 myActionsMap[TrihedronShowId]->addTo(myToolBar);
277 SUIT_ToolButton* aScaleBtn = new SUIT_ToolButton(myToolBar);
278 aScaleBtn->AddAction(myActionsMap[FitAllId]);
279 aScaleBtn->AddAction(myActionsMap[FitRectId]);
280 aScaleBtn->AddAction(myActionsMap[ZoomId]);
282 SUIT_ToolButton* aPanningBtn = new SUIT_ToolButton(myToolBar);
283 aPanningBtn->AddAction(myActionsMap[PanId]);
284 aPanningBtn->AddAction(myActionsMap[GlobalPanId]);
286 myActionsMap[RotationId]->addTo(myToolBar);
288 SUIT_ToolButton* aViewsBtn = new SUIT_ToolButton(myToolBar);
289 aViewsBtn->AddAction(myActionsMap[FrontId]);
290 aViewsBtn->AddAction(myActionsMap[BackId]);
291 aViewsBtn->AddAction(myActionsMap[TopId]);
292 aViewsBtn->AddAction(myActionsMap[BottomId]);
293 aViewsBtn->AddAction(myActionsMap[LeftId]);
294 aViewsBtn->AddAction(myActionsMap[RightId]);
296 myActionsMap[ResetId]->addTo(myToolBar);
299 /*!On front view event.*/
300 void VTKViewer_ViewWindow::onFrontView()
302 vtkCamera* camera = myRenderer->GetActiveCamera();
303 camera->SetPosition(1,0,0);
304 camera->SetViewUp(0,0,1);
305 camera->SetFocalPoint(0,0,0);
309 /*!On back view slot.*/
310 void VTKViewer_ViewWindow::onBackView()
312 vtkCamera* camera = myRenderer->GetActiveCamera();
313 camera->SetPosition(-1,0,0);
314 camera->SetViewUp(0,0,1);
315 camera->SetFocalPoint(0,0,0);
319 /*!On back view slot.*/
320 void VTKViewer_ViewWindow::onTopView()
322 vtkCamera* camera = myRenderer->GetActiveCamera();
323 camera->SetPosition(0,0,1);
324 camera->SetViewUp(0,1,0);
325 camera->SetFocalPoint(0,0,0);
329 /*!On bottom view slot.*/
330 void VTKViewer_ViewWindow::onBottomView()
332 vtkCamera* camera = myRenderer->GetActiveCamera();
333 camera->SetPosition(0,0,-1);
334 camera->SetViewUp(0,1,0);
335 camera->SetFocalPoint(0,0,0);
339 /*!On left view slot.*/
340 void VTKViewer_ViewWindow::onLeftView()
342 vtkCamera* camera = myRenderer->GetActiveCamera();
343 camera->SetPosition(0,-1,0);
344 camera->SetViewUp(0,0,1);
345 camera->SetFocalPoint(0,0,0);
349 /*!On right view slot.*/
350 void VTKViewer_ViewWindow::onRightView()
352 vtkCamera* camera = myRenderer->GetActiveCamera();
353 camera->SetPosition(0,1,0);
354 camera->SetViewUp(0,0,1);
355 camera->SetFocalPoint(0,0,0);
359 /*!On reset view slot.*/
360 void VTKViewer_ViewWindow::onResetView()
362 int aTriedronIsVisible = isTrihedronDisplayed();
363 myTrihedron->SetVisibility( VTKViewer_Trihedron::eOnlyLineOn );
364 ::ResetCamera(myRenderer,true);
365 vtkCamera* aCamera = myRenderer->GetActiveCamera();
366 aCamera->SetPosition(1,-1,1);
367 aCamera->SetViewUp(0,0,1);
368 ::ResetCamera(myRenderer,true);
369 if(aTriedronIsVisible) myTrihedron->VisibilityOn();
370 else myTrihedron->VisibilityOff();
371 static float aCoeff = 3.0;
372 aCamera->SetParallelScale(aCoeff*aCamera->GetParallelScale());
376 /*!On fit all slot.*/
377 void VTKViewer_ViewWindow::onFitAll()
379 myRWInteractor->GetInteractorStyle()->ViewFitAll();
383 /*!Set background of the viewport*/
384 void VTKViewer_ViewWindow::setBackgroundColor( const QColor& color )
387 myRenderer->SetBackground( color.red()/255., color.green()/255., color.blue()/255. );
390 /*!Returns background of the viewport*/
391 QColor VTKViewer_ViewWindow::backgroundColor() const
395 myRenderer->GetBackground( backint );
396 return QColor(int(backint[0]*255), int(backint[1]*255), int(backint[2]*255));
398 return SUIT_ViewWindow::backgroundColor();
401 /*!Repaint window. If \a theUpdateTrihedron is true - recalculate trihedron.*/
402 void VTKViewer_ViewWindow::Repaint(bool theUpdateTrihedron)
404 if (theUpdateTrihedron) onAdjustTrihedron();
405 myRenderWindow->update();
408 /*!Get scale of transformation filter.*/
409 void VTKViewer_ViewWindow::GetScale( double theScale[3] ) {
410 myTransform->GetScale( theScale );
413 /*!Set scale of transformation filter and repaint window.*/
414 void VTKViewer_ViewWindow::SetScale( double theScale[3] ) {
415 myTransform->SetMatrixScale( theScale[0], theScale[1], theScale[2] );
416 myRWInteractor->Render();
420 /*!Calculation trihedron size.*/
421 void VTKViewer_ViewWindow::onAdjustTrihedron(){
422 if( !isTrihedronDisplayed() )
424 int aVisibleNum = myTrihedron->GetVisibleActorCount(myRenderer);
426 // calculating diagonal of visible props of the renderer
428 myTrihedron->VisibilityOff();
429 ::ComputeVisiblePropBounds(myRenderer,bnd);
430 myTrihedron->VisibilityOn();
432 static bool CalcByDiag = false;
434 aLength = sqrt((bnd[1]-bnd[0])*(bnd[1]-bnd[0])+
435 (bnd[3]-bnd[2])*(bnd[3]-bnd[2])+
436 (bnd[5]-bnd[4])*(bnd[5]-bnd[4]));
438 aLength = bnd[1]-bnd[0];
439 aLength = max((bnd[3]-bnd[2]),aLength);
440 aLength = max((bnd[5]-bnd[4]),aLength);
443 static float aSizeInPercents = 105;
444 QString aSetting;// = SUIT_CONFIG->getSetting("Viewer:TrihedronSize");
445 if(!aSetting.isEmpty()) aSizeInPercents = aSetting.toFloat();
447 static float EPS_SIZE = 5.0E-3;
448 float aSize = myTrihedron->GetSize();
449 float aNewSize = aLength*aSizeInPercents/100.0;
450 // if the new trihedron size have sufficient difference, then apply the value
451 if(fabs(aNewSize-aSize) > aSize*EPS_SIZE || fabs(aNewSize-aSize) > aNewSize*EPS_SIZE){
452 myTrihedron->SetSize(aNewSize);
455 ::ResetCameraClippingRange(myRenderer);
458 /*!Emit key pressed.*/
459 void VTKViewer_ViewWindow::onKeyPressed(QKeyEvent* event)
461 emit keyPressed( this, event );
464 /*!Emit key released.*/
465 void VTKViewer_ViewWindow::onKeyReleased(QKeyEvent* event)
467 emit keyReleased( this, event );
470 /*!Emit key pressed.*/
471 void VTKViewer_ViewWindow::onMousePressed(QMouseEvent* event)
473 emit mousePressed(this, event);
476 /*!Emit mouse released.*/
477 void VTKViewer_ViewWindow::onMouseReleased(QMouseEvent* event)
479 emit mouseReleased( this, event );
482 /*!Emit mouse moving.*/
483 void VTKViewer_ViewWindow::onMouseMoving(QMouseEvent* event)
485 emit mouseMoving( this, event );
488 /*!Emit mouse double clicked.*/
489 void VTKViewer_ViewWindow::onMouseDoubleClicked( QMouseEvent* event )
491 emit mouseDoubleClicked( this, event );
494 /*!Insert actor to renderer and transformation filter.
495 *Move Internal actors, if \a theMoveInternalActors is true.
497 void VTKViewer_ViewWindow::InsertActor( VTKViewer_Actor* theActor, bool theMoveInternalActors ){
498 theActor->AddToRender(myRenderer);
499 theActor->SetTransform(myTransform);
500 if(theMoveInternalActors)
501 myRWInteractor->MoveInternalActors();
504 /*!Add actor.Repaint window if \a theUpdate is true.
505 *@see InsertActor( VTKViewer_Actor* theActor, bool theMoveInternalActors )
507 void VTKViewer_ViewWindow::AddActor( VTKViewer_Actor* theActor, bool theUpdate /*=false*/ ){
508 InsertActor(theActor);
513 /*!Remove \a theActor from renderer and pepaint, if \a theUpdate is true.*/
514 void VTKViewer_ViewWindow::RemoveActor( VTKViewer_Actor* theActor, bool theUpdate /*=false*/ ){
515 theActor->RemoveFromRender(myRenderer);
520 /*!@see RemoveActor() and InsertActor().*/
521 void VTKViewer_ViewWindow::MoveActor( VTKViewer_Actor* theActor)
523 RemoveActor(theActor);
524 InsertActor(theActor,true);
527 /*!On trihedron show slot.*/
528 void VTKViewer_ViewWindow::onTrihedronShow()
530 if (isTrihedronDisplayed())
531 myTrihedron->VisibilityOff();
533 myTrihedron->VisibilityOn();
534 myRenderWindow->update();
538 QImage VTKViewer_ViewWindow::dumpView()
540 QPixmap px = QPixmap::grabWindow( myRenderWindow->winId() );
541 return px.convertToImage();