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 // Construction/Destruction
24 //////////////////////////////////////////////////////////////////////
25 VTKViewer_ViewWindow::VTKViewer_ViewWindow( SUIT_Desktop* theDesktop,
26 VTKViewer_Viewer* theModel,
27 VTKViewer_InteractorStyle* iStyle,
28 VTKViewer_RenderWindowInteractor* rw )
29 : SUIT_ViewWindow( theDesktop )
33 myTrihedron = VTKViewer_Trihedron::New();
34 myTransform = VTKViewer_Transform::New();
35 myRenderer = vtkRenderer::New() ;
37 myTrihedron->AddToRender( myRenderer );
39 myRenderWindow = new VTKViewer_RenderWindow( this, "RenderWindow" );
40 setCentralWidget(myRenderWindow);
41 myRenderWindow->setFocusPolicy( StrongFocus );
42 myRenderWindow->setFocus();
44 myRenderWindow->getRenderWindow()->AddRenderer( myRenderer );
46 myRenderer->GetActiveCamera()->ParallelProjectionOn();
47 myRenderer->LightFollowCameraOn();
48 myRenderer->TwoSidedLightingOn();
50 // Set BackgroundColor
51 QString BgrColorRed = "0";//SUIT_CONFIG->getSetting("VTKViewer:BackgroundColorRed");
52 QString BgrColorGreen = "0";//SUIT_CONFIG->getSetting("VTKViewer:BackgroundColorGreen");
53 QString BgrColorBlue = "0";//SUIT_CONFIG->getSetting("VTKViewer:BackgroundColorBlue");
55 if( !BgrColorRed.isEmpty() && !BgrColorGreen.isEmpty() && !BgrColorBlue.isEmpty() )
56 myRenderer->SetBackground( BgrColorRed.toInt()/255., BgrColorGreen.toInt()/255., BgrColorBlue.toInt()/255. );
58 myRenderer->SetBackground( 0, 0, 0 );
60 // Create an interactor.
61 myRWInteractor = rw ? rw : VTKViewer_RenderWindowInteractor::New();
62 myRWInteractor->SetRenderWindow( myRenderWindow->getRenderWindow() );
64 VTKViewer_InteractorStyle* RWS = iStyle ? iStyle : VTKViewer_InteractorStyle::New();
65 RWS->setGUIWindow( myRenderWindow );
66 myRWInteractor->SetInteractorStyle( RWS );
68 myRWInteractor->Initialize();
69 RWS->setTriedron( myTrihedron );
70 RWS->FindPokedRenderer( 0, 0 );
72 setCentralWidget( myRenderWindow );
74 myToolBar = new QToolBar(this);
75 myToolBar->setCloseMode(QDockWindow::Undocked);
76 myToolBar->setLabel(tr("LBL_TOOLBAR_LABEL"));
81 connect( myRenderWindow, SIGNAL(KeyPressed( QKeyEvent* )),
82 this, SLOT(onKeyPressed( QKeyEvent* )) );
83 connect( myRenderWindow, SIGNAL(KeyReleased( QKeyEvent* )),
84 this, SLOT(onKeyReleased( QKeyEvent* )) );
85 connect( myRenderWindow, SIGNAL(MouseButtonPressed( QMouseEvent* )),
86 this, SLOT(onMousePressed( QMouseEvent* )) );
87 connect( myRenderWindow, SIGNAL(MouseButtonReleased( QMouseEvent* )),
88 this, SLOT(onMouseReleased( QMouseEvent* )) );
89 connect( myRenderWindow, SIGNAL(MouseDoubleClicked( QMouseEvent* )),
90 this, SLOT(onMouseDoubleClicked( QMouseEvent* )) );
91 connect( myRenderWindow, SIGNAL(MouseMove( QMouseEvent* )),
92 this, SLOT(onMouseMoving( QMouseEvent* )) );
93 connect( myRWInteractor, SIGNAL(RenderWindowModified()),
94 myRenderWindow, SLOT(update()) );
96 connect( myRenderWindow, SIGNAL(contextMenuRequested( QContextMenuEvent * )),
97 this, SIGNAL(contextMenuRequested( QContextMenuEvent * )) );
99 connect( myRWInteractor, SIGNAL(contextMenuRequested( QContextMenuEvent * )),
100 this, SIGNAL(contextMenuRequested( QContextMenuEvent * )) );
106 VTKViewer_ViewWindow::~VTKViewer_ViewWindow()
108 myTransform->Delete();
109 // In order to ensure that the interactor unregisters
110 // this RenderWindow, we assign a NULL RenderWindow to
111 // it before deleting it.
112 myRWInteractor->SetRenderWindow( NULL );
113 myRWInteractor->Delete();
116 myRenderer->RemoveAllProps();
117 //m_Renderer->Delete() ;
118 myTrihedron->Delete();
121 bool VTKViewer_ViewWindow::isTrihedronDisplayed(){
122 return myTrihedron->GetVisibility() == VTKViewer_Trihedron::eOn;
126 Activates 'zooming' transformation
128 void VTKViewer_ViewWindow::activateZoom()
130 myRWInteractor->GetInteractorStyle()->startZoom();
133 //****************************************************************
135 Activates 'panning' transformation
137 void VTKViewer_ViewWindow::activatePanning()
139 myRWInteractor->GetInteractorStyle()->startPan();
142 //****************************************************************
144 Activates 'rotation' transformation
146 void VTKViewer_ViewWindow::activateRotation()
148 myRWInteractor->GetInteractorStyle()->startRotate();
151 //****************************************************************
152 void VTKViewer_ViewWindow::activateGlobalPanning()
154 //if(myTrihedron->GetVisibleActorCount(myRenderer))
155 myRWInteractor->GetInteractorStyle()->startGlobalPan();
158 //****************************************************************
160 Activates 'fit area' transformation
162 void VTKViewer_ViewWindow::activateWindowFit()
164 myRWInteractor->GetInteractorStyle()->startFitArea();
167 //****************************************************************
168 void VTKViewer_ViewWindow::createActions()
170 if (!myActionsMap.isEmpty()) return;
172 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
177 aAction = new QtxAction(tr("MNU_DUMP_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_DUMP" ) ),
178 tr( "MNU_DUMP_VIEW" ), 0, this);
179 aAction->setStatusTip(tr("DSC_DUMP_VIEW"));
180 connect(aAction, SIGNAL(activated()), this, SLOT(onDumpView()));
181 myActionsMap[ DumpId ] = aAction;
184 aAction = new QtxAction(tr("MNU_FITALL"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_FITALL" ) ),
185 tr( "MNU_FITALL" ), 0, this);
186 aAction->setStatusTip(tr("DSC_FITALL"));
187 connect(aAction, SIGNAL(activated()), this, SLOT(onFitAll()));
188 myActionsMap[ FitAllId ] = aAction;
191 aAction = new QtxAction(tr("MNU_FITRECT"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_FITAREA" ) ),
192 tr( "MNU_FITRECT" ), 0, this);
193 aAction->setStatusTip(tr("DSC_FITRECT"));
194 connect(aAction, SIGNAL(activated()), this, SLOT(activateWindowFit()));
195 myActionsMap[ FitRectId ] = aAction;
198 aAction = new QtxAction(tr("MNU_ZOOM_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_ZOOM" ) ),
199 tr( "MNU_ZOOM_VIEW" ), 0, this);
200 aAction->setStatusTip(tr("DSC_ZOOM_VIEW"));
201 connect(aAction, SIGNAL(activated()), this, SLOT(activateZoom()));
202 myActionsMap[ ZoomId ] = aAction;
205 aAction = new QtxAction(tr("MNU_PAN_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_PAN" ) ),
206 tr( "MNU_PAN_VIEW" ), 0, this);
207 aAction->setStatusTip(tr("DSC_PAN_VIEW"));
208 connect(aAction, SIGNAL(activated()), this, SLOT(activatePanning()));
209 myActionsMap[ PanId ] = aAction;
212 aAction = new QtxAction(tr("MNU_GLOBALPAN_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_GLOBALPAN" ) ),
213 tr( "MNU_GLOBALPAN_VIEW" ), 0, this);
214 aAction->setStatusTip(tr("DSC_GLOBALPAN_VIEW"));
215 connect(aAction, SIGNAL(activated()), this, SLOT(activateGlobalPanning()));
216 myActionsMap[ GlobalPanId ] = aAction;
219 aAction = new QtxAction(tr("MNU_ROTATE_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_ROTATE" ) ),
220 tr( "MNU_ROTATE_VIEW" ), 0, this);
221 aAction->setStatusTip(tr("DSC_ROTATE_VIEW"));
222 connect(aAction, SIGNAL(activated()), this, SLOT(activateRotation()));
223 myActionsMap[ RotationId ] = aAction;
226 aAction = new QtxAction(tr("MNU_FRONT_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_FRONT" ) ),
227 tr( "MNU_FRONT_VIEW" ), 0, this);
228 aAction->setStatusTip(tr("DSC_FRONT_VIEW"));
229 connect(aAction, SIGNAL(activated()), this, SLOT(onFrontView()));
230 myActionsMap[ FrontId ] = aAction;
232 aAction = new QtxAction(tr("MNU_BACK_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_BACK" ) ),
233 tr( "MNU_BACK_VIEW" ), 0, this);
234 aAction->setStatusTip(tr("DSC_BACK_VIEW"));
235 connect(aAction, SIGNAL(activated()), this, SLOT(onBackView()));
236 myActionsMap[ BackId ] = aAction;
238 aAction = new QtxAction(tr("MNU_TOP_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_TOP" ) ),
239 tr( "MNU_TOP_VIEW" ), 0, this);
240 aAction->setStatusTip(tr("DSC_TOP_VIEW"));
241 connect(aAction, SIGNAL(activated()), this, SLOT(onTopView()));
242 myActionsMap[ TopId ] = aAction;
244 aAction = new QtxAction(tr("MNU_BOTTOM_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_BOTTOM" ) ),
245 tr( "MNU_BOTTOM_VIEW" ), 0, this);
246 aAction->setStatusTip(tr("DSC_BOTTOM_VIEW"));
247 connect(aAction, SIGNAL(activated()), this, SLOT(onBottomView()));
248 myActionsMap[ BottomId ] = aAction;
250 aAction = new QtxAction(tr("MNU_LEFT_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_LEFT" ) ),
251 tr( "MNU_LEFT_VIEW" ), 0, this);
252 aAction->setStatusTip(tr("DSC_LEFT_VIEW"));
253 connect(aAction, SIGNAL(activated()), this, SLOT(onLeftView()));
254 myActionsMap[ LeftId ] = aAction;
256 aAction = new QtxAction(tr("MNU_RIGHT_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_RIGHT" ) ),
257 tr( "MNU_RIGHT_VIEW" ), 0, this);
258 aAction->setStatusTip(tr("DSC_RIGHT_VIEW"));
259 connect(aAction, SIGNAL(activated()), this, SLOT(onRightView()));
260 myActionsMap[ RightId ] = aAction;
263 aAction = new QtxAction(tr("MNU_RESET_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_RESET" ) ),
264 tr( "MNU_RESET_VIEW" ), 0, this);
265 aAction->setStatusTip(tr("DSC_RESET_VIEW"));
266 connect(aAction, SIGNAL(activated()), this, SLOT(onResetView()));
267 myActionsMap[ ResetId ] = aAction;
269 aAction = new QtxAction(tr("MNU_SHOW_TRIHEDRON"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_TRIHEDRON" ) ),
270 tr( "MNU_SHOW_TRIHEDRON" ), 0, this);
271 aAction->setStatusTip(tr("DSC_SHOW_TRIHEDRON"));
272 connect(aAction, SIGNAL(activated()), this, SLOT(onTrihedronShow()));
273 myActionsMap[ TrihedronShowId ] = aAction;
276 //****************************************************************
277 void VTKViewer_ViewWindow::createToolBar()
279 myActionsMap[DumpId]->addTo(myToolBar);
280 myActionsMap[TrihedronShowId]->addTo(myToolBar);
282 SUIT_ToolButton* aScaleBtn = new SUIT_ToolButton(myToolBar);
283 aScaleBtn->AddAction(myActionsMap[FitAllId]);
284 aScaleBtn->AddAction(myActionsMap[FitRectId]);
285 aScaleBtn->AddAction(myActionsMap[ZoomId]);
287 SUIT_ToolButton* aPanningBtn = new SUIT_ToolButton(myToolBar);
288 aPanningBtn->AddAction(myActionsMap[PanId]);
289 aPanningBtn->AddAction(myActionsMap[GlobalPanId]);
291 myActionsMap[RotationId]->addTo(myToolBar);
293 SUIT_ToolButton* aViewsBtn = new SUIT_ToolButton(myToolBar);
294 aViewsBtn->AddAction(myActionsMap[FrontId]);
295 aViewsBtn->AddAction(myActionsMap[BackId]);
296 aViewsBtn->AddAction(myActionsMap[TopId]);
297 aViewsBtn->AddAction(myActionsMap[BottomId]);
298 aViewsBtn->AddAction(myActionsMap[LeftId]);
299 aViewsBtn->AddAction(myActionsMap[RightId]);
301 myActionsMap[ResetId]->addTo(myToolBar);
304 //****************************************************************
305 void VTKViewer_ViewWindow::onFrontView()
307 vtkCamera* camera = myRenderer->GetActiveCamera();
308 camera->SetPosition(1,0,0);
309 camera->SetViewUp(0,0,1);
310 camera->SetFocalPoint(0,0,0);
314 //****************************************************************
315 void VTKViewer_ViewWindow::onBackView()
317 vtkCamera* camera = myRenderer->GetActiveCamera();
318 camera->SetPosition(-1,0,0);
319 camera->SetViewUp(0,0,1);
320 camera->SetFocalPoint(0,0,0);
324 //****************************************************************
325 void VTKViewer_ViewWindow::onTopView()
327 vtkCamera* camera = myRenderer->GetActiveCamera();
328 camera->SetPosition(0,0,1);
329 camera->SetViewUp(0,1,0);
330 camera->SetFocalPoint(0,0,0);
334 //****************************************************************
335 void VTKViewer_ViewWindow::onBottomView()
337 vtkCamera* camera = myRenderer->GetActiveCamera();
338 camera->SetPosition(0,0,-1);
339 camera->SetViewUp(0,1,0);
340 camera->SetFocalPoint(0,0,0);
344 //****************************************************************
345 void VTKViewer_ViewWindow::onLeftView()
347 vtkCamera* camera = myRenderer->GetActiveCamera();
348 camera->SetPosition(0,-1,0);
349 camera->SetViewUp(0,0,1);
350 camera->SetFocalPoint(0,0,0);
354 //****************************************************************
355 void VTKViewer_ViewWindow::onRightView()
357 vtkCamera* camera = myRenderer->GetActiveCamera();
358 camera->SetPosition(0,1,0);
359 camera->SetViewUp(0,0,1);
360 camera->SetFocalPoint(0,0,0);
364 //****************************************************************
365 void VTKViewer_ViewWindow::onResetView()
367 int aTriedronIsVisible = isTrihedronDisplayed();
368 myTrihedron->SetVisibility( VTKViewer_Trihedron::eOnlyLineOn );
369 ::ResetCamera(myRenderer,true);
370 vtkCamera* aCamera = myRenderer->GetActiveCamera();
371 aCamera->SetPosition(1,-1,1);
372 aCamera->SetViewUp(0,0,1);
373 ::ResetCamera(myRenderer,true);
374 if(aTriedronIsVisible) myTrihedron->VisibilityOn();
375 else myTrihedron->VisibilityOff();
376 static float aCoeff = 3.0;
377 aCamera->SetParallelScale(aCoeff*aCamera->GetParallelScale());
381 //****************************************************************
382 void VTKViewer_ViewWindow::onFitAll()
384 myRWInteractor->GetInteractorStyle()->ViewFitAll();
388 //****************************************************************
390 Dumps 3d-Viewer contents into image file
391 File format is defined by file's extension; supported formats : PNG, BMP, GIF, JPG
393 void VTKViewer_ViewWindow::onDumpView()
395 QApplication::setOverrideCursor( Qt::waitCursor );
396 QPixmap px = QPixmap::grabWindow(myRenderWindow->winId());
397 QApplication::restoreOverrideCursor();
399 SUIT_Application* app = getViewManager()->study()->application();
401 QString aFileName = app->getFileName( false, QString::null, tr("VTK_IMAGE_FILES"), tr("INF_APP_DUMP_VIEW"), 0 );
403 if ( !aFileName.isNull() ) {
404 QApplication::setOverrideCursor( Qt::waitCursor );
405 QString fmt = SUIT_Tools::extension( aFileName ).upper();
407 fmt = QString("BMP"); // default format
410 bool bOk = px.save(aFileName, fmt.latin1());
411 QApplication::restoreOverrideCursor();
413 SUIT_MessageBox::error1(this, tr("ERROR"), tr("ERR_DOC_CANT_SAVE_FILE"), tr("BUT_OK"));
418 //****************************************************************
420 Set background of the viewport
422 void VTKViewer_ViewWindow::setBackgroundColor( const QColor& color )
425 myRenderer->SetBackground( color.red()/255., color.green()/255., color.blue()/255. );
428 //****************************************************************
430 Returns background of the viewport
432 QColor VTKViewer_ViewWindow::backgroundColor() const
436 myRenderer->GetBackground( backint );
437 return QColor(int(backint[0]*255), int(backint[1]*255), int(backint[2]*255));
439 return SUIT_ViewWindow::backgroundColor();
442 //****************************************************************
443 void VTKViewer_ViewWindow::Repaint(bool theUpdateTrihedron)
445 if (theUpdateTrihedron) onAdjustTrihedron();
446 myRenderWindow->update();
449 //****************************************************************
450 void VTKViewer_ViewWindow::GetScale( double theScale[3] ) {
451 myTransform->GetScale( theScale );
454 //****************************************************************
455 void VTKViewer_ViewWindow::SetScale( double theScale[3] ) {
456 myTransform->SetScale( theScale[0], theScale[1], theScale[2] );
457 myRWInteractor->Render();
461 //****************************************************************
462 void VTKViewer_ViewWindow::onAdjustTrihedron(){
463 if( !isTrihedronDisplayed() )
465 int aVisibleNum = myTrihedron->GetVisibleActorCount(myRenderer);
467 // calculating diagonal of visible props of the renderer
469 myTrihedron->VisibilityOff();
470 ::ComputeVisiblePropBounds(myRenderer,bnd);
471 myTrihedron->VisibilityOn();
473 static bool CalcByDiag = false;
475 aLength = sqrt((bnd[1]-bnd[0])*(bnd[1]-bnd[0])+
476 (bnd[3]-bnd[2])*(bnd[3]-bnd[2])+
477 (bnd[5]-bnd[4])*(bnd[5]-bnd[4]));
479 aLength = bnd[1]-bnd[0];
480 aLength = max((bnd[3]-bnd[2]),aLength);
481 aLength = max((bnd[5]-bnd[4]),aLength);
484 static float aSizeInPercents = 105;
485 QString aSetting;// = SUIT_CONFIG->getSetting("Viewer:TrihedronSize");
486 if(!aSetting.isEmpty()) aSizeInPercents = aSetting.toFloat();
488 static float EPS_SIZE = 5.0E-3;
489 float aSize = myTrihedron->GetSize();
490 float aNewSize = aLength*aSizeInPercents/100.0;
491 // if the new trihedron size have sufficient difference, then apply the value
492 if(fabs(aNewSize-aSize) > aSize*EPS_SIZE || fabs(aNewSize-aSize) > aNewSize*EPS_SIZE){
493 myTrihedron->SetSize(aNewSize);
496 ::ResetCameraClippingRange(myRenderer);
499 //****************************************************************
500 void VTKViewer_ViewWindow::onKeyPressed(QKeyEvent* event)
502 emit keyPressed( this, event );
505 //****************************************************************
506 void VTKViewer_ViewWindow::onKeyReleased(QKeyEvent* event)
508 emit keyReleased( this, event );
511 //****************************************************************
512 void VTKViewer_ViewWindow::onMousePressed(QMouseEvent* event)
514 emit mousePressed(this, event);
517 //****************************************************************
518 void VTKViewer_ViewWindow::onMouseReleased(QMouseEvent* event)
520 emit mouseReleased( this, event );
523 //****************************************************************
524 void VTKViewer_ViewWindow::onMouseMoving(QMouseEvent* event)
526 emit mouseMoving( this, event );
529 //****************************************************************
530 void VTKViewer_ViewWindow::onMouseDoubleClicked( QMouseEvent* event )
532 emit mouseDoubleClicked( this, event );
535 void VTKViewer_ViewWindow::InsertActor( VTKViewer_Actor* theActor, bool theMoveInternalActors ){
536 theActor->AddToRender(myRenderer);
537 theActor->SetTransform(myTransform);
538 if(theMoveInternalActors)
539 myRWInteractor->MoveInternalActors();
542 void VTKViewer_ViewWindow::AddActor( VTKViewer_Actor* theActor, bool theUpdate /*=false*/ ){
543 InsertActor(theActor);
548 void VTKViewer_ViewWindow::RemoveActor( VTKViewer_Actor* theActor, bool theUpdate /*=false*/ ){
549 theActor->RemoveFromRender(myRenderer);
554 void VTKViewer_ViewWindow::MoveActor( VTKViewer_Actor* theActor)
556 RemoveActor(theActor);
557 InsertActor(theActor,true);
560 //****************************************************************
561 void VTKViewer_ViewWindow::onTrihedronShow()
563 if (isTrihedronDisplayed())
564 myTrihedron->VisibilityOff();
566 myTrihedron->VisibilityOn();
567 myRenderWindow->update();