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>
22 //****************************************************************
23 //////////////////////////////////////////////////////////////////////
24 // Construction/Destruction
25 //////////////////////////////////////////////////////////////////////
26 VTKViewer_ViewWindow::VTKViewer_ViewWindow( SUIT_Desktop* theDesktop,
27 VTKViewer_Viewer* theModel,
28 VTKViewer_InteractorStyle* iStyle,
29 VTKViewer_RenderWindowInteractor* rw )
30 : SUIT_ViewWindow(theDesktop)
34 myTrihedron = VTKViewer_Trihedron::New();
35 myTransform = VTKViewer_Transform::New();
36 myRenderer = vtkRenderer::New() ;
38 myTrihedron->AddToRender( myRenderer );
40 myRenderWindow = new VTKViewer_RenderWindow( this, "RenderWindow" );
41 setCentralWidget(myRenderWindow);
42 myRenderWindow->setFocusPolicy( StrongFocus );
43 myRenderWindow->setFocus();
45 myRenderWindow->getRenderWindow()->AddRenderer( myRenderer );
47 myRenderer->GetActiveCamera()->ParallelProjectionOn();
48 myRenderer->LightFollowCameraOn();
49 myRenderer->TwoSidedLightingOn();
51 // Set BackgroundColor
52 QString BgrColorRed = "0";//SUIT_CONFIG->getSetting("VTKViewer:BackgroundColorRed");
53 QString BgrColorGreen = "0";//SUIT_CONFIG->getSetting("VTKViewer:BackgroundColorGreen");
54 QString BgrColorBlue = "0";//SUIT_CONFIG->getSetting("VTKViewer:BackgroundColorBlue");
56 if( !BgrColorRed.isEmpty() && !BgrColorGreen.isEmpty() && !BgrColorBlue.isEmpty() )
57 myRenderer->SetBackground( BgrColorRed.toInt()/255., BgrColorGreen.toInt()/255., BgrColorBlue.toInt()/255. );
59 myRenderer->SetBackground( 0, 0, 0 );
61 // Create an interactor.
62 myRWInteractor = rw ? rw : VTKViewer_RenderWindowInteractor::New();
63 myRWInteractor->SetRenderWindow( myRenderWindow->getRenderWindow() );
65 VTKViewer_InteractorStyle* RWS = iStyle ? iStyle : VTKViewer_InteractorStyle::New();
66 RWS->setGUIWindow( myRenderWindow );
67 myRWInteractor->SetInteractorStyle( RWS );
69 myRWInteractor->Initialize();
70 RWS->setTriedron( myTrihedron );
71 RWS->FindPokedRenderer( 0, 0 );
73 setCentralWidget( myRenderWindow );
75 myToolBar = new QToolBar(this);
76 myToolBar->setCloseMode(QDockWindow::Undocked);
77 myToolBar->setLabel(tr("LBL_TOOLBAR_LABEL"));
82 connect( myRenderWindow, SIGNAL(KeyPressed( QKeyEvent* )),
83 this, SLOT(onKeyPressed( QKeyEvent* )) );
84 connect( myRenderWindow, SIGNAL(KeyReleased( QKeyEvent* )),
85 this, SLOT(onKeyReleased( QKeyEvent* )) );
86 connect( myRenderWindow, SIGNAL(MouseButtonPressed( QMouseEvent* )),
87 this, SLOT(onMousePressed( QMouseEvent* )) );
88 connect( myRenderWindow, SIGNAL(MouseButtonReleased( QMouseEvent* )),
89 this, SLOT(onMouseReleased( QMouseEvent* )) );
90 connect( myRenderWindow, SIGNAL(MouseDoubleClicked( QMouseEvent* )),
91 this, SLOT(onMouseDoubleClicked( QMouseEvent* )) );
92 connect( myRenderWindow, SIGNAL(MouseMove( QMouseEvent* )),
93 this, SLOT(onMouseMoving( QMouseEvent* )) );
94 connect( myRWInteractor, SIGNAL(RenderWindowModified()),
95 myRenderWindow, SLOT(update()) );
97 connect( myRenderWindow, SIGNAL(contextMenuRequested( QContextMenuEvent * )),
98 this, SIGNAL(contextMenuRequested( QContextMenuEvent * )) );
100 connect( myRWInteractor, SIGNAL(contextMenuRequested( QContextMenuEvent * )),
101 this, SIGNAL(contextMenuRequested( QContextMenuEvent * )) );
107 //****************************************************************
108 VTKViewer_ViewWindow::~VTKViewer_ViewWindow()
110 myTransform->Delete();
111 // In order to ensure that the interactor unregisters
112 // this RenderWindow, we assign a NULL RenderWindow to
113 // it before deleting it.
114 myRWInteractor->SetRenderWindow( NULL );
115 myRWInteractor->Delete();
118 myRenderer->RemoveAllProps();
119 //m_Renderer->Delete() ;
120 myTrihedron->Delete();
123 //****************************************************************
124 bool VTKViewer_ViewWindow::isTrihedronDisplayed(){
125 return myTrihedron->GetVisibility() == VTKViewer_Trihedron::eOn;
128 //****************************************************************
130 Activates 'zooming' transformation
132 void VTKViewer_ViewWindow::activateZoom()
134 myRWInteractor->GetInteractorStyle()->startZoom();
137 //****************************************************************
139 Activates 'panning' transformation
141 void VTKViewer_ViewWindow::activatePanning()
143 myRWInteractor->GetInteractorStyle()->startPan();
146 //****************************************************************
148 Activates 'rotation' transformation
150 void VTKViewer_ViewWindow::activateRotation()
152 myRWInteractor->GetInteractorStyle()->startRotate();
155 //****************************************************************
156 void VTKViewer_ViewWindow::activateGlobalPanning()
158 //if(myTrihedron->GetVisibleActorCount(myRenderer))
159 myRWInteractor->GetInteractorStyle()->startGlobalPan();
162 //****************************************************************
164 Activates 'fit area' transformation
166 void VTKViewer_ViewWindow::activateWindowFit()
168 myRWInteractor->GetInteractorStyle()->startFitArea();
171 //****************************************************************
172 void VTKViewer_ViewWindow::createActions()
174 if (!myActionsMap.isEmpty()) return;
176 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
181 aAction = new QtxAction(tr("MNU_DUMP_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_DUMP" ) ),
182 tr( "MNU_DUMP_VIEW" ), 0, this);
183 aAction->setStatusTip(tr("DSC_DUMP_VIEW"));
184 connect(aAction, SIGNAL(activated()), this, SLOT(onDumpView()));
185 myActionsMap[ DumpId ] = aAction;
188 aAction = new QtxAction(tr("MNU_FITALL"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_FITALL" ) ),
189 tr( "MNU_FITALL" ), 0, this);
190 aAction->setStatusTip(tr("DSC_FITALL"));
191 connect(aAction, SIGNAL(activated()), this, SLOT(onFitAll()));
192 myActionsMap[ FitAllId ] = aAction;
195 aAction = new QtxAction(tr("MNU_FITRECT"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_FITAREA" ) ),
196 tr( "MNU_FITRECT" ), 0, this);
197 aAction->setStatusTip(tr("DSC_FITRECT"));
198 connect(aAction, SIGNAL(activated()), this, SLOT(activateWindowFit()));
199 myActionsMap[ FitRectId ] = aAction;
202 aAction = new QtxAction(tr("MNU_ZOOM_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_ZOOM" ) ),
203 tr( "MNU_ZOOM_VIEW" ), 0, this);
204 aAction->setStatusTip(tr("DSC_ZOOM_VIEW"));
205 connect(aAction, SIGNAL(activated()), this, SLOT(activateZoom()));
206 myActionsMap[ ZoomId ] = aAction;
209 aAction = new QtxAction(tr("MNU_PAN_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_PAN" ) ),
210 tr( "MNU_PAN_VIEW" ), 0, this);
211 aAction->setStatusTip(tr("DSC_PAN_VIEW"));
212 connect(aAction, SIGNAL(activated()), this, SLOT(activatePanning()));
213 myActionsMap[ PanId ] = aAction;
216 aAction = new QtxAction(tr("MNU_GLOBALPAN_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_GLOBALPAN" ) ),
217 tr( "MNU_GLOBALPAN_VIEW" ), 0, this);
218 aAction->setStatusTip(tr("DSC_GLOBALPAN_VIEW"));
219 connect(aAction, SIGNAL(activated()), this, SLOT(activateGlobalPanning()));
220 myActionsMap[ GlobalPanId ] = aAction;
223 aAction = new QtxAction(tr("MNU_ROTATE_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_ROTATE" ) ),
224 tr( "MNU_ROTATE_VIEW" ), 0, this);
225 aAction->setStatusTip(tr("DSC_ROTATE_VIEW"));
226 connect(aAction, SIGNAL(activated()), this, SLOT(activateRotation()));
227 myActionsMap[ RotationId ] = aAction;
230 aAction = new QtxAction(tr("MNU_FRONT_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_FRONT" ) ),
231 tr( "MNU_FRONT_VIEW" ), 0, this);
232 aAction->setStatusTip(tr("DSC_FRONT_VIEW"));
233 connect(aAction, SIGNAL(activated()), this, SLOT(onFrontView()));
234 myActionsMap[ FrontId ] = aAction;
236 aAction = new QtxAction(tr("MNU_BACK_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_BACK" ) ),
237 tr( "MNU_BACK_VIEW" ), 0, this);
238 aAction->setStatusTip(tr("DSC_BACK_VIEW"));
239 connect(aAction, SIGNAL(activated()), this, SLOT(onBackView()));
240 myActionsMap[ BackId ] = aAction;
242 aAction = new QtxAction(tr("MNU_TOP_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_TOP" ) ),
243 tr( "MNU_TOP_VIEW" ), 0, this);
244 aAction->setStatusTip(tr("DSC_TOP_VIEW"));
245 connect(aAction, SIGNAL(activated()), this, SLOT(onTopView()));
246 myActionsMap[ TopId ] = aAction;
248 aAction = new QtxAction(tr("MNU_BOTTOM_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_BOTTOM" ) ),
249 tr( "MNU_BOTTOM_VIEW" ), 0, this);
250 aAction->setStatusTip(tr("DSC_BOTTOM_VIEW"));
251 connect(aAction, SIGNAL(activated()), this, SLOT(onBottomView()));
252 myActionsMap[ BottomId ] = aAction;
254 aAction = new QtxAction(tr("MNU_LEFT_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_LEFT" ) ),
255 tr( "MNU_LEFT_VIEW" ), 0, this);
256 aAction->setStatusTip(tr("DSC_LEFT_VIEW"));
257 connect(aAction, SIGNAL(activated()), this, SLOT(onLeftView()));
258 myActionsMap[ LeftId ] = aAction;
260 aAction = new QtxAction(tr("MNU_RIGHT_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_RIGHT" ) ),
261 tr( "MNU_RIGHT_VIEW" ), 0, this);
262 aAction->setStatusTip(tr("DSC_RIGHT_VIEW"));
263 connect(aAction, SIGNAL(activated()), this, SLOT(onRightView()));
264 myActionsMap[ RightId ] = aAction;
267 aAction = new QtxAction(tr("MNU_RESET_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_RESET" ) ),
268 tr( "MNU_RESET_VIEW" ), 0, this);
269 aAction->setStatusTip(tr("DSC_RESET_VIEW"));
270 connect(aAction, SIGNAL(activated()), this, SLOT(onResetView()));
271 myActionsMap[ ResetId ] = aAction;
273 aAction = new QtxAction(tr("MNU_SHOW_TRIHEDRON"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_TRIHEDRON" ) ),
274 tr( "MNU_SHOW_TRIHEDRON" ), 0, this);
275 aAction->setStatusTip(tr("DSC_SHOW_TRIHEDRON"));
276 connect(aAction, SIGNAL(activated()), this, SLOT(onTrihedronShow()));
277 myActionsMap[ TrihedronShowId ] = aAction;
280 //****************************************************************
281 void VTKViewer_ViewWindow::createToolBar()
283 myActionsMap[DumpId]->addTo(myToolBar);
284 myActionsMap[TrihedronShowId]->addTo(myToolBar);
286 SUIT_ToolButton* aScaleBtn = new SUIT_ToolButton(myToolBar);
287 aScaleBtn->AddAction(myActionsMap[FitAllId]);
288 aScaleBtn->AddAction(myActionsMap[FitRectId]);
289 aScaleBtn->AddAction(myActionsMap[ZoomId]);
291 SUIT_ToolButton* aPanningBtn = new SUIT_ToolButton(myToolBar);
292 aPanningBtn->AddAction(myActionsMap[PanId]);
293 aPanningBtn->AddAction(myActionsMap[GlobalPanId]);
295 myActionsMap[RotationId]->addTo(myToolBar);
297 SUIT_ToolButton* aViewsBtn = new SUIT_ToolButton(myToolBar);
298 aViewsBtn->AddAction(myActionsMap[FrontId]);
299 aViewsBtn->AddAction(myActionsMap[BackId]);
300 aViewsBtn->AddAction(myActionsMap[TopId]);
301 aViewsBtn->AddAction(myActionsMap[BottomId]);
302 aViewsBtn->AddAction(myActionsMap[LeftId]);
303 aViewsBtn->AddAction(myActionsMap[RightId]);
305 myActionsMap[ResetId]->addTo(myToolBar);
308 //****************************************************************
309 void VTKViewer_ViewWindow::onFrontView()
311 vtkCamera* camera = myRenderer->GetActiveCamera();
312 camera->SetPosition(1,0,0);
313 camera->SetViewUp(0,0,1);
314 camera->SetFocalPoint(0,0,0);
318 //****************************************************************
319 void VTKViewer_ViewWindow::onBackView()
321 vtkCamera* camera = myRenderer->GetActiveCamera();
322 camera->SetPosition(-1,0,0);
323 camera->SetViewUp(0,0,1);
324 camera->SetFocalPoint(0,0,0);
328 //****************************************************************
329 void VTKViewer_ViewWindow::onTopView()
331 vtkCamera* camera = myRenderer->GetActiveCamera();
332 camera->SetPosition(0,0,1);
333 camera->SetViewUp(0,1,0);
334 camera->SetFocalPoint(0,0,0);
338 //****************************************************************
339 void VTKViewer_ViewWindow::onBottomView()
341 vtkCamera* camera = myRenderer->GetActiveCamera();
342 camera->SetPosition(0,0,-1);
343 camera->SetViewUp(0,1,0);
344 camera->SetFocalPoint(0,0,0);
348 //****************************************************************
349 void VTKViewer_ViewWindow::onLeftView()
351 vtkCamera* camera = myRenderer->GetActiveCamera();
352 camera->SetPosition(0,-1,0);
353 camera->SetViewUp(0,0,1);
354 camera->SetFocalPoint(0,0,0);
358 //****************************************************************
359 void VTKViewer_ViewWindow::onRightView()
361 vtkCamera* camera = myRenderer->GetActiveCamera();
362 camera->SetPosition(0,1,0);
363 camera->SetViewUp(0,0,1);
364 camera->SetFocalPoint(0,0,0);
368 //****************************************************************
369 void VTKViewer_ViewWindow::onResetView()
371 int aTriedronIsVisible = isTrihedronDisplayed();
372 myTrihedron->SetVisibility( VTKViewer_Trihedron::eOnlyLineOn );
373 ::ResetCamera(myRenderer,true);
374 vtkCamera* aCamera = myRenderer->GetActiveCamera();
375 aCamera->SetPosition(1,-1,1);
376 aCamera->SetViewUp(0,0,1);
377 ::ResetCamera(myRenderer,true);
378 if(aTriedronIsVisible) myTrihedron->VisibilityOn();
379 else myTrihedron->VisibilityOff();
380 static float aCoeff = 3.0;
381 aCamera->SetParallelScale(aCoeff*aCamera->GetParallelScale());
385 //****************************************************************
386 void VTKViewer_ViewWindow::onFitAll()
388 myRWInteractor->GetInteractorStyle()->ViewFitAll();
392 //****************************************************************
394 Dumps 3d-Viewer contents into image file
395 File format is defined by file's extension; supported formats : PNG, BMP, GIF, JPG
397 void VTKViewer_ViewWindow::onDumpView()
399 QApplication::setOverrideCursor( Qt::waitCursor );
400 QPixmap px = QPixmap::grabWindow(myRenderWindow->winId());
401 QApplication::restoreOverrideCursor();
403 SUIT_Application* app = getViewManager()->study()->application();
405 QString aFileName = app->getFileName( false, QString::null, tr("VTK_IMAGE_FILES"), tr("INF_APP_DUMP_VIEW"), 0 );
407 if ( !aFileName.isNull() ) {
408 QApplication::setOverrideCursor( Qt::waitCursor );
409 QString fmt = SUIT_Tools::extension( aFileName ).upper();
411 fmt = QString("BMP"); // default format
414 bool bOk = px.save(aFileName, fmt.latin1());
415 QApplication::restoreOverrideCursor();
417 SUIT_MessageBox::error1(this, tr("ERROR"), tr("ERR_DOC_CANT_SAVE_FILE"), tr("BUT_OK"));
422 //****************************************************************
424 Set background of the viewport
426 void VTKViewer_ViewWindow::setBackgroundColor( const QColor& color )
429 myRenderer->SetBackground( color.red()/255., color.green()/255., color.blue()/255. );
432 //****************************************************************
434 Returns background of the viewport
436 QColor VTKViewer_ViewWindow::backgroundColor() const
440 myRenderer->GetBackground( backint );
441 return QColor(int(backint[0]*255), int(backint[1]*255), int(backint[2]*255));
443 return SUIT_ViewWindow::backgroundColor();
446 //****************************************************************
447 void VTKViewer_ViewWindow::Repaint(bool theUpdateTrihedron)
449 if (theUpdateTrihedron) onAdjustTrihedron();
450 myRenderWindow->update();
453 //****************************************************************
454 void VTKViewer_ViewWindow::GetScale( double theScale[3] ) {
455 myTransform->GetScale( theScale );
458 //****************************************************************
459 void VTKViewer_ViewWindow::SetScale( double theScale[3] ) {
460 myTransform->SetScale( theScale[0], theScale[1], theScale[2] );
461 myRWInteractor->Render();
465 //****************************************************************
466 void VTKViewer_ViewWindow::onAdjustTrihedron(){
467 if( !isTrihedronDisplayed() )
469 int aVisibleNum = myTrihedron->GetVisibleActorCount(myRenderer);
471 // calculating diagonal of visible props of the renderer
473 myTrihedron->VisibilityOff();
474 ::ComputeVisiblePropBounds(myRenderer,bnd);
475 myTrihedron->VisibilityOn();
477 static bool CalcByDiag = false;
479 aLength = sqrt((bnd[1]-bnd[0])*(bnd[1]-bnd[0])+
480 (bnd[3]-bnd[2])*(bnd[3]-bnd[2])+
481 (bnd[5]-bnd[4])*(bnd[5]-bnd[4]));
483 aLength = bnd[1]-bnd[0];
484 aLength = max((bnd[3]-bnd[2]),aLength);
485 aLength = max((bnd[5]-bnd[4]),aLength);
488 static float aSizeInPercents = 105;
489 QString aSetting;// = SUIT_CONFIG->getSetting("Viewer:TrihedronSize");
490 if(!aSetting.isEmpty()) aSizeInPercents = aSetting.toFloat();
492 static float EPS_SIZE = 5.0E-3;
493 float aSize = myTrihedron->GetSize();
494 float aNewSize = aLength*aSizeInPercents/100.0;
495 // if the new trihedron size have sufficient difference, then apply the value
496 if(fabs(aNewSize-aSize) > aSize*EPS_SIZE || fabs(aNewSize-aSize) > aNewSize*EPS_SIZE){
497 myTrihedron->SetSize(aNewSize);
500 ::ResetCameraClippingRange(myRenderer);
503 //****************************************************************
504 void VTKViewer_ViewWindow::onKeyPressed(QKeyEvent* event)
506 emit keyPressed( this, event );
509 //****************************************************************
510 void VTKViewer_ViewWindow::onKeyReleased(QKeyEvent* event)
512 emit keyReleased( this, event );
515 //****************************************************************
516 void VTKViewer_ViewWindow::onMousePressed(QMouseEvent* event)
518 emit mousePressed(this, event);
521 //****************************************************************
522 void VTKViewer_ViewWindow::onMouseReleased(QMouseEvent* event)
524 emit mouseReleased( this, event );
527 //****************************************************************
528 void VTKViewer_ViewWindow::onMouseMoving(QMouseEvent* event)
530 emit mouseMoving( this, event );
533 //****************************************************************
534 void VTKViewer_ViewWindow::onMouseDoubleClicked( QMouseEvent* event )
536 emit mouseDoubleClicked( this, event );
539 void VTKViewer_ViewWindow::InsertActor( VTKViewer_Actor* theActor, bool theMoveInternalActors ){
540 theActor->AddToRender(myRenderer);
541 theActor->SetTransform(myTransform);
542 if(theMoveInternalActors)
543 myRWInteractor->MoveInternalActors();
546 void VTKViewer_ViewWindow::AddActor( VTKViewer_Actor* theActor, bool theUpdate /*=false*/ ){
547 InsertActor(theActor);
552 void VTKViewer_ViewWindow::RemoveActor( VTKViewer_Actor* theActor, bool theUpdate /*=false*/ ){
553 theActor->RemoveFromRender(myRenderer);
558 void VTKViewer_ViewWindow::MoveActor( VTKViewer_Actor* theActor)
560 RemoveActor(theActor);
561 InsertActor(theActor,true);
564 //****************************************************************
565 void VTKViewer_ViewWindow::onTrihedronShow()
567 if (isTrihedronDisplayed())
568 myTrihedron->VisibilityOff();
570 myTrihedron->VisibilityOn();
571 myRenderWindow->update();