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"
16 #include "SUIT_FileDlg.h"
18 #include <qapplication.h>
20 #include <vtkRenderer.h>
21 #include <vtkCamera.h>
23 //****************************************************************
24 //////////////////////////////////////////////////////////////////////
25 // Construction/Destruction
26 //////////////////////////////////////////////////////////////////////
27 VTKViewer_ViewWindow::VTKViewer_ViewWindow( SUIT_Desktop* theDesktop,
28 VTKViewer_Viewer* theModel,
29 VTKViewer_InteractorStyle* iStyle,
30 VTKViewer_RenderWindowInteractor* rw )
31 : SUIT_ViewWindow(theDesktop)
35 myTrihedron = VTKViewer_Trihedron::New();
36 myTransform = VTKViewer_Transform::New();
37 myRenderer = vtkRenderer::New() ;
39 myTrihedron->AddToRender( myRenderer );
41 myRenderWindow = new VTKViewer_RenderWindow( this, "RenderWindow" );
42 setCentralWidget(myRenderWindow);
43 myRenderWindow->setFocusPolicy( StrongFocus );
44 myRenderWindow->setFocus();
46 myRenderWindow->getRenderWindow()->AddRenderer( myRenderer );
48 myRenderer->GetActiveCamera()->ParallelProjectionOn();
49 myRenderer->LightFollowCameraOn();
50 myRenderer->TwoSidedLightingOn();
52 // Set BackgroundColor
53 QString BgrColorRed = "0";//SUIT_CONFIG->getSetting("VTKViewer:BackgroundColorRed");
54 QString BgrColorGreen = "0";//SUIT_CONFIG->getSetting("VTKViewer:BackgroundColorGreen");
55 QString BgrColorBlue = "0";//SUIT_CONFIG->getSetting("VTKViewer:BackgroundColorBlue");
57 if( !BgrColorRed.isEmpty() && !BgrColorGreen.isEmpty() && !BgrColorBlue.isEmpty() )
58 myRenderer->SetBackground( BgrColorRed.toInt()/255., BgrColorGreen.toInt()/255., BgrColorBlue.toInt()/255. );
60 myRenderer->SetBackground( 0, 0, 0 );
62 // Create an interactor.
63 myRWInteractor = rw ? rw : VTKViewer_RenderWindowInteractor::New();
64 myRWInteractor->SetRenderWindow( myRenderWindow->getRenderWindow() );
66 VTKViewer_InteractorStyle* RWS = iStyle ? iStyle : VTKViewer_InteractorStyle::New();
67 RWS->setGUIWindow( myRenderWindow );
68 myRWInteractor->SetInteractorStyle( RWS );
70 myRWInteractor->Initialize();
71 RWS->setTriedron( myTrihedron );
72 RWS->FindPokedRenderer( 0, 0 );
74 setCentralWidget( myRenderWindow );
76 myToolBar = new QToolBar(this);
77 myToolBar->setCloseMode(QDockWindow::Undocked);
78 myToolBar->setLabel(tr("LBL_TOOLBAR_LABEL"));
83 connect( myRenderWindow, SIGNAL(KeyPressed( QKeyEvent* )),
84 this, SLOT(onKeyPressed( QKeyEvent* )) );
85 connect( myRenderWindow, SIGNAL(KeyReleased( QKeyEvent* )),
86 this, SLOT(onKeyReleased( QKeyEvent* )) );
87 connect( myRenderWindow, SIGNAL(MouseButtonPressed( QMouseEvent* )),
88 this, SLOT(onMousePressed( QMouseEvent* )) );
89 connect( myRenderWindow, SIGNAL(MouseButtonReleased( QMouseEvent* )),
90 this, SLOT(onMouseReleased( QMouseEvent* )) );
91 connect( myRenderWindow, SIGNAL(MouseDoubleClicked( QMouseEvent* )),
92 this, SLOT(onMouseDoubleClicked( QMouseEvent* )) );
93 connect( myRenderWindow, SIGNAL(MouseMove( QMouseEvent* )),
94 this, SLOT(onMouseMoving( QMouseEvent* )) );
95 connect( myRWInteractor, SIGNAL(RenderWindowModified()),
96 myRenderWindow, SLOT(update()) );
98 connect( myRenderWindow, SIGNAL(contextMenuRequested( QContextMenuEvent * )),
99 this, SIGNAL(contextMenuRequested( QContextMenuEvent * )) );
101 connect( myRWInteractor, SIGNAL(contextMenuRequested( QContextMenuEvent * )),
102 this, SIGNAL(contextMenuRequested( QContextMenuEvent * )) );
108 //****************************************************************
109 VTKViewer_ViewWindow::~VTKViewer_ViewWindow()
111 myTransform->Delete();
112 // In order to ensure that the interactor unregisters
113 // this RenderWindow, we assign a NULL RenderWindow to
114 // it before deleting it.
115 myRWInteractor->SetRenderWindow( NULL );
116 myRWInteractor->Delete();
119 myRenderer->RemoveAllProps();
120 //m_Renderer->Delete() ;
121 myTrihedron->Delete();
124 //****************************************************************
125 bool VTKViewer_ViewWindow::isTrihedronDisplayed(){
126 return myTrihedron->GetVisibility() == VTKViewer_Trihedron::eOn;
129 //****************************************************************
131 Activates 'zooming' transformation
133 void VTKViewer_ViewWindow::activateZoom()
135 myRWInteractor->GetInteractorStyle()->startZoom();
138 //****************************************************************
140 Activates 'panning' transformation
142 void VTKViewer_ViewWindow::activatePanning()
144 myRWInteractor->GetInteractorStyle()->startPan();
147 //****************************************************************
149 Activates 'rotation' transformation
151 void VTKViewer_ViewWindow::activateRotation()
153 myRWInteractor->GetInteractorStyle()->startRotate();
156 //****************************************************************
157 void VTKViewer_ViewWindow::activateGlobalPanning()
159 //if(myTrihedron->GetVisibleActorCount(myRenderer))
160 myRWInteractor->GetInteractorStyle()->startGlobalPan();
163 //****************************************************************
165 Activates 'fit area' transformation
167 void VTKViewer_ViewWindow::activateWindowFit()
169 myRWInteractor->GetInteractorStyle()->startFitArea();
172 //****************************************************************
173 void VTKViewer_ViewWindow::createActions()
175 if (!myActionsMap.isEmpty()) return;
177 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
182 aAction = new QtxAction(tr("MNU_DUMP_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_DUMP" ) ),
183 tr( "MNU_DUMP_VIEW" ), 0, this);
184 aAction->setStatusTip(tr("DSC_DUMP_VIEW"));
185 connect(aAction, SIGNAL(activated()), this, SLOT(onDumpView()));
186 myActionsMap[ DumpId ] = aAction;
189 aAction = new QtxAction(tr("MNU_FITALL"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_FITALL" ) ),
190 tr( "MNU_FITALL" ), 0, this);
191 aAction->setStatusTip(tr("DSC_FITALL"));
192 connect(aAction, SIGNAL(activated()), this, SLOT(onFitAll()));
193 myActionsMap[ FitAllId ] = aAction;
196 aAction = new QtxAction(tr("MNU_FITRECT"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_FITAREA" ) ),
197 tr( "MNU_FITRECT" ), 0, this);
198 aAction->setStatusTip(tr("DSC_FITRECT"));
199 connect(aAction, SIGNAL(activated()), this, SLOT(activateWindowFit()));
200 myActionsMap[ FitRectId ] = aAction;
203 aAction = new QtxAction(tr("MNU_ZOOM_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_ZOOM" ) ),
204 tr( "MNU_ZOOM_VIEW" ), 0, this);
205 aAction->setStatusTip(tr("DSC_ZOOM_VIEW"));
206 connect(aAction, SIGNAL(activated()), this, SLOT(activateZoom()));
207 myActionsMap[ ZoomId ] = aAction;
210 aAction = new QtxAction(tr("MNU_PAN_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_PAN" ) ),
211 tr( "MNU_PAN_VIEW" ), 0, this);
212 aAction->setStatusTip(tr("DSC_PAN_VIEW"));
213 connect(aAction, SIGNAL(activated()), this, SLOT(activatePanning()));
214 myActionsMap[ PanId ] = aAction;
217 aAction = new QtxAction(tr("MNU_GLOBALPAN_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_GLOBALPAN" ) ),
218 tr( "MNU_GLOBALPAN_VIEW" ), 0, this);
219 aAction->setStatusTip(tr("DSC_GLOBALPAN_VIEW"));
220 connect(aAction, SIGNAL(activated()), this, SLOT(activateGlobalPanning()));
221 myActionsMap[ GlobalPanId ] = aAction;
224 aAction = new QtxAction(tr("MNU_ROTATE_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_ROTATE" ) ),
225 tr( "MNU_ROTATE_VIEW" ), 0, this);
226 aAction->setStatusTip(tr("DSC_ROTATE_VIEW"));
227 connect(aAction, SIGNAL(activated()), this, SLOT(activateRotation()));
228 myActionsMap[ RotationId ] = aAction;
231 aAction = new QtxAction(tr("MNU_FRONT_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_FRONT" ) ),
232 tr( "MNU_FRONT_VIEW" ), 0, this);
233 aAction->setStatusTip(tr("DSC_FRONT_VIEW"));
234 connect(aAction, SIGNAL(activated()), this, SLOT(onFrontView()));
235 myActionsMap[ FrontId ] = aAction;
237 aAction = new QtxAction(tr("MNU_BACK_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_BACK" ) ),
238 tr( "MNU_BACK_VIEW" ), 0, this);
239 aAction->setStatusTip(tr("DSC_BACK_VIEW"));
240 connect(aAction, SIGNAL(activated()), this, SLOT(onBackView()));
241 myActionsMap[ BackId ] = aAction;
243 aAction = new QtxAction(tr("MNU_TOP_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_TOP" ) ),
244 tr( "MNU_TOP_VIEW" ), 0, this);
245 aAction->setStatusTip(tr("DSC_TOP_VIEW"));
246 connect(aAction, SIGNAL(activated()), this, SLOT(onTopView()));
247 myActionsMap[ TopId ] = aAction;
249 aAction = new QtxAction(tr("MNU_BOTTOM_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_BOTTOM" ) ),
250 tr( "MNU_BOTTOM_VIEW" ), 0, this);
251 aAction->setStatusTip(tr("DSC_BOTTOM_VIEW"));
252 connect(aAction, SIGNAL(activated()), this, SLOT(onBottomView()));
253 myActionsMap[ BottomId ] = aAction;
255 aAction = new QtxAction(tr("MNU_LEFT_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_LEFT" ) ),
256 tr( "MNU_LEFT_VIEW" ), 0, this);
257 aAction->setStatusTip(tr("DSC_LEFT_VIEW"));
258 connect(aAction, SIGNAL(activated()), this, SLOT(onLeftView()));
259 myActionsMap[ LeftId ] = aAction;
261 aAction = new QtxAction(tr("MNU_RIGHT_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_RIGHT" ) ),
262 tr( "MNU_RIGHT_VIEW" ), 0, this);
263 aAction->setStatusTip(tr("DSC_RIGHT_VIEW"));
264 connect(aAction, SIGNAL(activated()), this, SLOT(onRightView()));
265 myActionsMap[ RightId ] = aAction;
268 aAction = new QtxAction(tr("MNU_RESET_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_RESET" ) ),
269 tr( "MNU_RESET_VIEW" ), 0, this);
270 aAction->setStatusTip(tr("DSC_RESET_VIEW"));
271 connect(aAction, SIGNAL(activated()), this, SLOT(onResetView()));
272 myActionsMap[ ResetId ] = aAction;
275 //****************************************************************
276 void VTKViewer_ViewWindow::createToolBar()
278 myActionsMap[DumpId]->addTo(myToolBar);
280 SUIT_ToolButton* aScaleBtn = new SUIT_ToolButton(myToolBar);
281 aScaleBtn->AddAction(myActionsMap[FitAllId]);
282 aScaleBtn->AddAction(myActionsMap[FitRectId]);
283 aScaleBtn->AddAction(myActionsMap[ZoomId]);
285 SUIT_ToolButton* aPanningBtn = new SUIT_ToolButton(myToolBar);
286 aPanningBtn->AddAction(myActionsMap[PanId]);
287 aPanningBtn->AddAction(myActionsMap[GlobalPanId]);
289 myActionsMap[RotationId]->addTo(myToolBar);
291 SUIT_ToolButton* aViewsBtn = new SUIT_ToolButton(myToolBar);
292 aViewsBtn->AddAction(myActionsMap[FrontId]);
293 aViewsBtn->AddAction(myActionsMap[BackId]);
294 aViewsBtn->AddAction(myActionsMap[TopId]);
295 aViewsBtn->AddAction(myActionsMap[BottomId]);
296 aViewsBtn->AddAction(myActionsMap[LeftId]);
297 aViewsBtn->AddAction(myActionsMap[RightId]);
299 myActionsMap[ResetId]->addTo(myToolBar);
302 //****************************************************************
303 void VTKViewer_ViewWindow::onFrontView()
305 vtkCamera* camera = myRenderer->GetActiveCamera();
306 camera->SetPosition(1,0,0);
307 camera->SetViewUp(0,0,1);
308 camera->SetFocalPoint(0,0,0);
312 //****************************************************************
313 void VTKViewer_ViewWindow::onBackView()
315 vtkCamera* camera = myRenderer->GetActiveCamera();
316 camera->SetPosition(-1,0,0);
317 camera->SetViewUp(0,0,1);
318 camera->SetFocalPoint(0,0,0);
322 //****************************************************************
323 void VTKViewer_ViewWindow::onTopView()
325 vtkCamera* camera = myRenderer->GetActiveCamera();
326 camera->SetPosition(0,0,1);
327 camera->SetViewUp(0,1,0);
328 camera->SetFocalPoint(0,0,0);
332 //****************************************************************
333 void VTKViewer_ViewWindow::onBottomView()
335 vtkCamera* camera = myRenderer->GetActiveCamera();
336 camera->SetPosition(0,0,-1);
337 camera->SetViewUp(0,1,0);
338 camera->SetFocalPoint(0,0,0);
342 //****************************************************************
343 void VTKViewer_ViewWindow::onLeftView()
345 vtkCamera* camera = myRenderer->GetActiveCamera();
346 camera->SetPosition(0,-1,0);
347 camera->SetViewUp(0,0,1);
348 camera->SetFocalPoint(0,0,0);
352 //****************************************************************
353 void VTKViewer_ViewWindow::onRightView()
355 vtkCamera* camera = myRenderer->GetActiveCamera();
356 camera->SetPosition(0,1,0);
357 camera->SetViewUp(0,0,1);
358 camera->SetFocalPoint(0,0,0);
362 //****************************************************************
363 void VTKViewer_ViewWindow::onResetView()
365 int aTriedronIsVisible = isTrihedronDisplayed();
366 myTrihedron->SetVisibility( VTKViewer_Trihedron::eOnlyLineOn );
367 ::ResetCamera(myRenderer,true);
368 vtkCamera* aCamera = myRenderer->GetActiveCamera();
369 aCamera->SetPosition(1,-1,1);
370 aCamera->SetViewUp(0,0,1);
371 ::ResetCamera(myRenderer,true);
372 if(aTriedronIsVisible) myTrihedron->VisibilityOn();
373 else myTrihedron->VisibilityOff();
374 static float aCoeff = 3.0;
375 aCamera->SetParallelScale(aCoeff*aCamera->GetParallelScale());
379 //****************************************************************
380 void VTKViewer_ViewWindow::onFitAll()
382 myRWInteractor->GetInteractorStyle()->ViewFitAll();
386 //****************************************************************
388 Dumps 3d-Viewer contents into image file
389 File format is defined by file's extension; supported formats : PNG, BMP, GIF, JPG
391 void VTKViewer_ViewWindow::onDumpView()
393 QApplication::setOverrideCursor( Qt::waitCursor );
394 QPixmap px = QPixmap::grabWindow(myRenderWindow->winId());
395 QApplication::restoreOverrideCursor();
397 QString fileName = SUIT_FileDlg::getFileName(this,
399 tr("VTK_IMAGE_FILES"),
400 tr("INF_APP_DUMP_VIEW"),
402 if (!fileName.isNull()) {
403 QApplication::setOverrideCursor( Qt::waitCursor );
404 QString fmt = SUIT_Tools::extension( fileName ).upper();
406 fmt = QString("BMP"); // default format
409 bool bOk = px.save(fileName, fmt.latin1());
410 QApplication::restoreOverrideCursor();
412 SUIT_MessageBox::error1(this, tr("ERROR"), tr("ERR_DOC_CANT_SAVE_FILE"), tr("BUT_OK"));
417 //****************************************************************
419 Set background of the viewport
421 void VTKViewer_ViewWindow::setBackgroundColor( const QColor& color )
424 myRenderer->SetBackground( color.red()/255., color.green()/255., color.blue()/255. );
427 //****************************************************************
429 Returns background of the viewport
431 QColor VTKViewer_ViewWindow::backgroundColor() const
435 myRenderer->GetBackground( backint );
436 return QColor(int(backint[0]*255), int(backint[1]*255), int(backint[2]*255));
438 return SUIT_ViewWindow::backgroundColor();
441 //****************************************************************
442 void VTKViewer_ViewWindow::Repaint(bool theUpdateTrihedron)
444 if (theUpdateTrihedron) onAdjustTrihedron();
445 myRenderWindow->update();
448 //****************************************************************
449 void VTKViewer_ViewWindow::GetScale( double theScale[3] ) {
450 myTransform->GetScale( theScale );
453 //****************************************************************
454 void VTKViewer_ViewWindow::SetScale( double theScale[3] ) {
455 myTransform->SetScale( theScale[0], theScale[1], theScale[2] );
456 myRWInteractor->Render();
460 //****************************************************************
461 void VTKViewer_ViewWindow::onAdjustTrihedron(){
462 if( !isTrihedronDisplayed() )
464 int aVisibleNum = myTrihedron->GetVisibleActorCount(myRenderer);
466 // calculating diagonal of visible props of the renderer
468 myTrihedron->VisibilityOff();
469 ::ComputeVisiblePropBounds(myRenderer,bnd);
470 myTrihedron->VisibilityOn();
472 static bool CalcByDiag = false;
474 aLength = sqrt((bnd[1]-bnd[0])*(bnd[1]-bnd[0])+
475 (bnd[3]-bnd[2])*(bnd[3]-bnd[2])+
476 (bnd[5]-bnd[4])*(bnd[5]-bnd[4]));
478 aLength = bnd[1]-bnd[0];
479 aLength = max((bnd[3]-bnd[2]),aLength);
480 aLength = max((bnd[5]-bnd[4]),aLength);
483 static float aSizeInPercents = 105;
484 QString aSetting;// = SUIT_CONFIG->getSetting("Viewer:TrihedronSize");
485 if(!aSetting.isEmpty()) aSizeInPercents = aSetting.toFloat();
487 static float EPS_SIZE = 5.0E-3;
488 float aSize = myTrihedron->GetSize();
489 float aNewSize = aLength*aSizeInPercents/100.0;
490 // if the new trihedron size have sufficient difference, then apply the value
491 if(fabs(aNewSize-aSize) > aSize*EPS_SIZE || fabs(aNewSize-aSize) > aNewSize*EPS_SIZE){
492 myTrihedron->SetSize(aNewSize);
495 ::ResetCameraClippingRange(myRenderer);
498 //****************************************************************
499 void VTKViewer_ViewWindow::onKeyPressed(QKeyEvent* event)
501 emit keyPressed( this, event );
504 //****************************************************************
505 void VTKViewer_ViewWindow::onKeyReleased(QKeyEvent* event)
507 emit keyReleased( this, event );
510 //****************************************************************
511 void VTKViewer_ViewWindow::onMousePressed(QMouseEvent* event)
513 emit mousePressed(this, event);
516 //****************************************************************
517 void VTKViewer_ViewWindow::onMouseReleased(QMouseEvent* event)
519 emit mouseReleased( this, event );
522 //****************************************************************
523 void VTKViewer_ViewWindow::onMouseMoving(QMouseEvent* event)
525 emit mouseMoving( this, event );
528 //****************************************************************
529 void VTKViewer_ViewWindow::onMouseDoubleClicked( QMouseEvent* event )
531 emit mouseDoubleClicked( this, event );
534 void VTKViewer_ViewWindow::InsertActor( VTKViewer_Actor* theActor, bool theMoveInternalActors ){
535 theActor->AddToRender(myRenderer);
536 theActor->SetTransform(myTransform);
537 if(theMoveInternalActors)
538 myRWInteractor->MoveInternalActors();
541 void VTKViewer_ViewWindow::AddActor( VTKViewer_Actor* theActor, bool theUpdate /*=false*/ ){
542 InsertActor(theActor);
547 void VTKViewer_ViewWindow::RemoveActor( VTKViewer_Actor* theActor, bool theUpdate /*=false*/ ){
548 theActor->RemoveFromRender(myRenderer);
553 void VTKViewer_ViewWindow::MoveActor( VTKViewer_Actor* theActor)
555 RemoveActor(theActor);
556 InsertActor(theActor,true);