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;
274 //****************************************************************
275 void VTKViewer_ViewWindow::createToolBar()
277 myActionsMap[DumpId]->addTo(myToolBar);
279 SUIT_ToolButton* aScaleBtn = new SUIT_ToolButton(myToolBar);
280 aScaleBtn->AddAction(myActionsMap[FitAllId]);
281 aScaleBtn->AddAction(myActionsMap[FitRectId]);
282 aScaleBtn->AddAction(myActionsMap[ZoomId]);
284 SUIT_ToolButton* aPanningBtn = new SUIT_ToolButton(myToolBar);
285 aPanningBtn->AddAction(myActionsMap[PanId]);
286 aPanningBtn->AddAction(myActionsMap[GlobalPanId]);
288 myActionsMap[RotationId]->addTo(myToolBar);
290 SUIT_ToolButton* aViewsBtn = new SUIT_ToolButton(myToolBar);
291 aViewsBtn->AddAction(myActionsMap[FrontId]);
292 aViewsBtn->AddAction(myActionsMap[BackId]);
293 aViewsBtn->AddAction(myActionsMap[TopId]);
294 aViewsBtn->AddAction(myActionsMap[BottomId]);
295 aViewsBtn->AddAction(myActionsMap[LeftId]);
296 aViewsBtn->AddAction(myActionsMap[RightId]);
298 myActionsMap[ResetId]->addTo(myToolBar);
301 //****************************************************************
302 void VTKViewer_ViewWindow::onFrontView()
304 vtkCamera* camera = myRenderer->GetActiveCamera();
305 camera->SetPosition(1,0,0);
306 camera->SetViewUp(0,0,1);
307 camera->SetFocalPoint(0,0,0);
311 //****************************************************************
312 void VTKViewer_ViewWindow::onBackView()
314 vtkCamera* camera = myRenderer->GetActiveCamera();
315 camera->SetPosition(-1,0,0);
316 camera->SetViewUp(0,0,1);
317 camera->SetFocalPoint(0,0,0);
321 //****************************************************************
322 void VTKViewer_ViewWindow::onTopView()
324 vtkCamera* camera = myRenderer->GetActiveCamera();
325 camera->SetPosition(0,0,1);
326 camera->SetViewUp(0,1,0);
327 camera->SetFocalPoint(0,0,0);
331 //****************************************************************
332 void VTKViewer_ViewWindow::onBottomView()
334 vtkCamera* camera = myRenderer->GetActiveCamera();
335 camera->SetPosition(0,0,-1);
336 camera->SetViewUp(0,1,0);
337 camera->SetFocalPoint(0,0,0);
341 //****************************************************************
342 void VTKViewer_ViewWindow::onLeftView()
344 vtkCamera* camera = myRenderer->GetActiveCamera();
345 camera->SetPosition(0,-1,0);
346 camera->SetViewUp(0,0,1);
347 camera->SetFocalPoint(0,0,0);
351 //****************************************************************
352 void VTKViewer_ViewWindow::onRightView()
354 vtkCamera* camera = myRenderer->GetActiveCamera();
355 camera->SetPosition(0,1,0);
356 camera->SetViewUp(0,0,1);
357 camera->SetFocalPoint(0,0,0);
361 //****************************************************************
362 void VTKViewer_ViewWindow::onResetView()
364 int aTriedronIsVisible = isTrihedronDisplayed();
365 myTrihedron->SetVisibility( VTKViewer_Trihedron::eOnlyLineOn );
366 ::ResetCamera(myRenderer,true);
367 vtkCamera* aCamera = myRenderer->GetActiveCamera();
368 aCamera->SetPosition(1,-1,1);
369 aCamera->SetViewUp(0,0,1);
370 ::ResetCamera(myRenderer,true);
371 if(aTriedronIsVisible) myTrihedron->VisibilityOn();
372 else myTrihedron->VisibilityOff();
373 static float aCoeff = 3.0;
374 aCamera->SetParallelScale(aCoeff*aCamera->GetParallelScale());
378 //****************************************************************
379 void VTKViewer_ViewWindow::onFitAll()
381 myRWInteractor->GetInteractorStyle()->ViewFitAll();
385 //****************************************************************
387 Dumps 3d-Viewer contents into image file
388 File format is defined by file's extension; supported formats : PNG, BMP, GIF, JPG
390 void VTKViewer_ViewWindow::onDumpView()
392 QApplication::setOverrideCursor( Qt::waitCursor );
393 QPixmap px = QPixmap::grabWindow(myRenderWindow->winId());
394 QApplication::restoreOverrideCursor();
396 SUIT_Application* app = getViewManager()->study()->application();
398 QString aFileName = app->getFileName( false, QString::null, tr("VTK_IMAGE_FILES"), tr("INF_APP_DUMP_VIEW"), 0 );
400 if ( !aFileName.isNull() ) {
401 QApplication::setOverrideCursor( Qt::waitCursor );
402 QString fmt = SUIT_Tools::extension( aFileName ).upper();
404 fmt = QString("BMP"); // default format
407 bool bOk = px.save(aFileName, fmt.latin1());
408 QApplication::restoreOverrideCursor();
410 SUIT_MessageBox::error1(this, tr("ERROR"), tr("ERR_DOC_CANT_SAVE_FILE"), tr("BUT_OK"));
415 //****************************************************************
417 Set background of the viewport
419 void VTKViewer_ViewWindow::setBackgroundColor( const QColor& color )
422 myRenderer->SetBackground( color.red()/255., color.green()/255., color.blue()/255. );
425 //****************************************************************
427 Returns background of the viewport
429 QColor VTKViewer_ViewWindow::backgroundColor() const
433 myRenderer->GetBackground( backint );
434 return QColor(int(backint[0]*255), int(backint[1]*255), int(backint[2]*255));
436 return SUIT_ViewWindow::backgroundColor();
439 //****************************************************************
440 void VTKViewer_ViewWindow::Repaint(bool theUpdateTrihedron)
442 if (theUpdateTrihedron) onAdjustTrihedron();
443 myRenderWindow->update();
446 //****************************************************************
447 void VTKViewer_ViewWindow::GetScale( double theScale[3] ) {
448 myTransform->GetScale( theScale );
451 //****************************************************************
452 void VTKViewer_ViewWindow::SetScale( double theScale[3] ) {
453 myTransform->SetScale( theScale[0], theScale[1], theScale[2] );
454 myRWInteractor->Render();
458 //****************************************************************
459 void VTKViewer_ViewWindow::onAdjustTrihedron(){
460 if( !isTrihedronDisplayed() )
462 int aVisibleNum = myTrihedron->GetVisibleActorCount(myRenderer);
464 // calculating diagonal of visible props of the renderer
466 myTrihedron->VisibilityOff();
467 ::ComputeVisiblePropBounds(myRenderer,bnd);
468 myTrihedron->VisibilityOn();
470 static bool CalcByDiag = false;
472 aLength = sqrt((bnd[1]-bnd[0])*(bnd[1]-bnd[0])+
473 (bnd[3]-bnd[2])*(bnd[3]-bnd[2])+
474 (bnd[5]-bnd[4])*(bnd[5]-bnd[4]));
476 aLength = bnd[1]-bnd[0];
477 aLength = max((bnd[3]-bnd[2]),aLength);
478 aLength = max((bnd[5]-bnd[4]),aLength);
481 static float aSizeInPercents = 105;
482 QString aSetting;// = SUIT_CONFIG->getSetting("Viewer:TrihedronSize");
483 if(!aSetting.isEmpty()) aSizeInPercents = aSetting.toFloat();
485 static float EPS_SIZE = 5.0E-3;
486 float aSize = myTrihedron->GetSize();
487 float aNewSize = aLength*aSizeInPercents/100.0;
488 // if the new trihedron size have sufficient difference, then apply the value
489 if(fabs(aNewSize-aSize) > aSize*EPS_SIZE || fabs(aNewSize-aSize) > aNewSize*EPS_SIZE){
490 myTrihedron->SetSize(aNewSize);
493 ::ResetCameraClippingRange(myRenderer);
496 //****************************************************************
497 void VTKViewer_ViewWindow::onKeyPressed(QKeyEvent* event)
499 emit keyPressed( this, event );
502 //****************************************************************
503 void VTKViewer_ViewWindow::onKeyReleased(QKeyEvent* event)
505 emit keyReleased( this, event );
508 //****************************************************************
509 void VTKViewer_ViewWindow::onMousePressed(QMouseEvent* event)
511 emit mousePressed(this, event);
514 //****************************************************************
515 void VTKViewer_ViewWindow::onMouseReleased(QMouseEvent* event)
517 emit mouseReleased( this, event );
520 //****************************************************************
521 void VTKViewer_ViewWindow::onMouseMoving(QMouseEvent* event)
523 emit mouseMoving( this, event );
526 //****************************************************************
527 void VTKViewer_ViewWindow::onMouseDoubleClicked( QMouseEvent* event )
529 emit mouseDoubleClicked( this, event );
532 void VTKViewer_ViewWindow::InsertActor( VTKViewer_Actor* theActor, bool theMoveInternalActors ){
533 theActor->AddToRender(myRenderer);
534 theActor->SetTransform(myTransform);
535 if(theMoveInternalActors)
536 myRWInteractor->MoveInternalActors();
539 void VTKViewer_ViewWindow::AddActor( VTKViewer_Actor* theActor, bool theUpdate /*=false*/ ){
540 InsertActor(theActor);
545 void VTKViewer_ViewWindow::RemoveActor( VTKViewer_Actor* theActor, bool theUpdate /*=false*/ ){
546 theActor->RemoveFromRender(myRenderer);
551 void VTKViewer_ViewWindow::MoveActor( VTKViewer_Actor* theActor)
553 RemoveActor(theActor);
554 InsertActor(theActor,true);