Back to index

salome-gui  6.5.0
VTKViewer_ViewWindow.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 #include "VTKViewer_ViewWindow.h"
00024 #include "VTKViewer_ViewModel.h"
00025 #include "VTKViewer_RenderWindow.h"
00026 #include "VTKViewer_RenderWindowInteractor.h"
00027 #include "VTKViewer_InteractorStyle.h"
00028 #include "VTKViewer_Trihedron.h"
00029 #include "VTKViewer_Transform.h"
00030 #include "VTKViewer_Utilities.h"
00031 #include "VTKViewer_Texture.h"
00032 #include "VTKViewer_OpenGLRenderer.h"
00033 
00034 #include <SUIT_Session.h>
00035 #include <SUIT_MessageBox.h>
00036 #include <SUIT_Tools.h>
00037 #include <SUIT_ResourceMgr.h>
00038 
00039 #include <QFileInfo>
00040 #include <QImage>
00041 
00042 #include <vtkCamera.h>
00043 #include <vtkJPEGReader.h>
00044 #include <vtkBMPReader.h>
00045 #include <vtkTIFFReader.h>
00046 #include <vtkPNGReader.h>
00047 #include <vtkMetaImageReader.h>
00048 #include <vtkImageMapToColors.h>
00049 #include <vtkTexture.h>
00050 
00051 #include <QtxToolBar.h>
00052 #include <QtxMultiAction.h>
00053 
00055 VTKViewer_ViewWindow::VTKViewer_ViewWindow( SUIT_Desktop* theDesktop, 
00056                                             VTKViewer_Viewer* theModel,
00057                                             VTKViewer_InteractorStyle* iStyle,
00058                                             VTKViewer_RenderWindowInteractor* rw )
00059 : SUIT_ViewWindow( theDesktop )
00060 {
00061   myModel = theModel;
00062 
00063   myTrihedron = VTKViewer_Trihedron::New();
00064   myTransform = VTKViewer_Transform::New();
00065   myRenderer  = VTKViewer_OpenGLRenderer::New() ;
00066 
00067   myTrihedron->AddToRender( myRenderer );
00068 
00069   myRenderWindow = new VTKViewer_RenderWindow( this, "RenderWindow" );
00070   setCentralWidget(myRenderWindow);
00071   myRenderWindow->setFocusPolicy( Qt::StrongFocus );
00072   myRenderWindow->setFocus();
00073 
00074   myRenderWindow->getRenderWindow()->AddRenderer( myRenderer );
00075 
00076   myRenderer->GetActiveCamera()->ParallelProjectionOn();
00077   myRenderer->LightFollowCameraOn();
00078   myRenderer->TwoSidedLightingOn();
00079 
00080   // Create an interactor.
00081   myRWInteractor = rw ? rw : VTKViewer_RenderWindowInteractor::New();
00082   myRWInteractor->SetRenderWindow( myRenderWindow->getRenderWindow() );
00083 
00084   VTKViewer_InteractorStyle* RWS = iStyle ? iStyle : VTKViewer_InteractorStyle::New();
00085   RWS->setGUIWindow( myRenderWindow );
00086   myRWInteractor->SetInteractorStyle( RWS ); 
00087 
00088   myRWInteractor->Initialize();
00089   RWS->setTriedron( myTrihedron );
00090   RWS->FindPokedRenderer( 0, 0 );
00091 
00092   setCentralWidget( myRenderWindow );
00093 
00094   myToolBar = new QtxToolBar( true, tr("LBL_TOOLBAR_LABEL"), this );
00095   myToolBar->setFloatable( false );
00096 
00097   createActions();
00098   createToolBar();
00099 
00100   connect( myRenderWindow, SIGNAL(KeyPressed( QKeyEvent* )),
00101            this,           SLOT(onKeyPressed( QKeyEvent* )) );
00102   connect( myRenderWindow, SIGNAL(KeyReleased( QKeyEvent* )),
00103            this,           SLOT(onKeyReleased( QKeyEvent* )) );
00104   connect( myRenderWindow, SIGNAL(MouseButtonPressed( QMouseEvent* )),
00105            this,           SLOT(onMousePressed( QMouseEvent* )) );
00106   connect( myRenderWindow, SIGNAL(MouseButtonReleased( QMouseEvent* )),
00107            this,           SLOT(onMouseReleased( QMouseEvent* )) );
00108   connect( myRenderWindow, SIGNAL(MouseDoubleClicked( QMouseEvent* )),
00109            this,           SLOT(onMouseDoubleClicked( QMouseEvent* )) );
00110   connect( myRenderWindow, SIGNAL(MouseMove( QMouseEvent* )),
00111            this,           SLOT(onMouseMoving( QMouseEvent* )) );
00112   connect( myRWInteractor, SIGNAL(RenderWindowModified()),
00113            myRenderWindow, SLOT(update()) );
00114 
00115   connect( myRenderWindow, SIGNAL(contextMenuRequested( QContextMenuEvent * )),
00116            this,           SIGNAL(contextMenuRequested( QContextMenuEvent * )) );
00117 
00118   connect( myRWInteractor, SIGNAL(contextMenuRequested( QContextMenuEvent * )),
00119            this,           SIGNAL(contextMenuRequested( QContextMenuEvent * )) );
00120 
00121 
00122   // set default background
00123   setBackground( Qtx::BackgroundData( Qt::black ) );
00124   // reset view
00125   onResetView();
00126 }
00127 
00129 VTKViewer_ViewWindow::~VTKViewer_ViewWindow()
00130 {
00131   myTransform->Delete();
00132   // In order to ensure that the interactor unregisters
00133   // this RenderWindow, we assign a NULL RenderWindow to 
00134   // it before deleting it.
00135   myRWInteractor->SetRenderWindow( NULL );
00136   myRWInteractor->Delete();
00137   
00138   //m_RW->Delete() ;
00139   myRenderer->RemoveAllViewProps();
00140   //m_Renderer->Delete() ;
00141   myTrihedron->Delete();
00142 }
00143 
00145 bool VTKViewer_ViewWindow::isTrihedronDisplayed(){
00146   return myTrihedron->GetVisibility() == VTKViewer_Trihedron::eOn;
00147 }
00148 
00150 void VTKViewer_ViewWindow::activateZoom()
00151 {
00152   myRWInteractor->GetInteractorStyle()->startZoom();
00153 }
00154 
00156 void VTKViewer_ViewWindow::activatePanning()
00157 {
00158   myRWInteractor->GetInteractorStyle()->startPan();
00159 }
00160 
00162 void VTKViewer_ViewWindow::activateRotation()
00163 {
00164   myRWInteractor->GetInteractorStyle()->startRotate();
00165 }
00166 
00168 void VTKViewer_ViewWindow::activateGlobalPanning()
00169 {
00170   //if(myTrihedron->GetVisibleActorCount(myRenderer))
00171   myRWInteractor->GetInteractorStyle()->startGlobalPan();
00172 }
00173 
00175 void VTKViewer_ViewWindow::activateWindowFit()
00176 {
00177   myRWInteractor->GetInteractorStyle()->startFitArea();
00178 }
00179 
00181 void VTKViewer_ViewWindow::createActions()
00182 {
00183   if (!myActionsMap.isEmpty()) return;
00184   
00185   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
00186   
00187   QtxAction* aAction;
00188 
00190   aAction = new QtxAction(tr("MNU_DUMP_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_DUMP" ) ),
00191                            tr( "MNU_DUMP_VIEW" ), 0, this);
00192   aAction->setStatusTip(tr("DSC_DUMP_VIEW"));
00193   connect(aAction, SIGNAL(activated()), this, SLOT(onDumpView()));
00194   myActionsMap[ DumpId ] = aAction;
00195 
00197   aAction = new QtxAction(tr("MNU_FITALL"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_FITALL" ) ),
00198                            tr( "MNU_FITALL" ), 0, this);
00199   aAction->setStatusTip(tr("DSC_FITALL"));
00200   connect(aAction, SIGNAL(activated()), this, SLOT(onFitAll()));
00201   myActionsMap[ FitAllId ] = aAction;
00202 
00204   aAction = new QtxAction(tr("MNU_FITRECT"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_FITAREA" ) ),
00205                            tr( "MNU_FITRECT" ), 0, this);
00206   aAction->setStatusTip(tr("DSC_FITRECT"));
00207   connect(aAction, SIGNAL(activated()), this, SLOT(activateWindowFit()));
00208   myActionsMap[ FitRectId ] = aAction;
00209 
00211   aAction = new QtxAction(tr("MNU_ZOOM_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_ZOOM" ) ),
00212                            tr( "MNU_ZOOM_VIEW" ), 0, this);
00213   aAction->setStatusTip(tr("DSC_ZOOM_VIEW"));
00214   connect(aAction, SIGNAL(activated()), this, SLOT(activateZoom()));
00215   myActionsMap[ ZoomId ] = aAction;
00216 
00218   aAction = new QtxAction(tr("MNU_PAN_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_PAN" ) ),
00219                            tr( "MNU_PAN_VIEW" ), 0, this);
00220   aAction->setStatusTip(tr("DSC_PAN_VIEW"));
00221   connect(aAction, SIGNAL(activated()), this, SLOT(activatePanning()));
00222   myActionsMap[ PanId ] = aAction;
00223 
00225   aAction = new QtxAction(tr("MNU_GLOBALPAN_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_GLOBALPAN" ) ),
00226                            tr( "MNU_GLOBALPAN_VIEW" ), 0, this);
00227   aAction->setStatusTip(tr("DSC_GLOBALPAN_VIEW"));
00228   connect(aAction, SIGNAL(activated()), this, SLOT(activateGlobalPanning()));
00229   myActionsMap[ GlobalPanId ] = aAction;
00230 
00232   aAction = new QtxAction(tr("MNU_ROTATE_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_ROTATE" ) ),
00233                            tr( "MNU_ROTATE_VIEW" ), 0, this);
00234   aAction->setStatusTip(tr("DSC_ROTATE_VIEW"));
00235   connect(aAction, SIGNAL(activated()), this, SLOT(activateRotation()));
00236   myActionsMap[ RotationId ] = aAction;
00237 
00239   aAction = new QtxAction(tr("MNU_FRONT_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_FRONT" ) ),
00240                            tr( "MNU_FRONT_VIEW" ), 0, this);
00241   aAction->setStatusTip(tr("DSC_FRONT_VIEW"));
00242   connect(aAction, SIGNAL(activated()), this, SLOT(onFrontView()));
00243   myActionsMap[ FrontId ] = aAction;
00244 
00246   aAction = new QtxAction(tr("MNU_BACK_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_BACK" ) ),
00247                            tr( "MNU_BACK_VIEW" ), 0, this);
00248   aAction->setStatusTip(tr("DSC_BACK_VIEW"));
00249   connect(aAction, SIGNAL(activated()), this, SLOT(onBackView()));
00250   myActionsMap[ BackId ] = aAction;
00251 
00253   aAction = new QtxAction(tr("MNU_TOP_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_TOP" ) ),
00254                            tr( "MNU_TOP_VIEW" ), 0, this);
00255   aAction->setStatusTip(tr("DSC_TOP_VIEW"));
00256   connect(aAction, SIGNAL(activated()), this, SLOT(onTopView()));
00257   myActionsMap[ TopId ] = aAction;
00258 
00260   aAction = new QtxAction(tr("MNU_BOTTOM_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_BOTTOM" ) ),
00261                            tr( "MNU_BOTTOM_VIEW" ), 0, this);
00262   aAction->setStatusTip(tr("DSC_BOTTOM_VIEW"));
00263   connect(aAction, SIGNAL(activated()), this, SLOT(onBottomView()));
00264   myActionsMap[ BottomId ] = aAction;
00265 
00267   aAction = new QtxAction(tr("MNU_LEFT_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_LEFT" ) ),
00268                            tr( "MNU_LEFT_VIEW" ), 0, this);
00269   aAction->setStatusTip(tr("DSC_LEFT_VIEW"));
00270   connect(aAction, SIGNAL(activated()), this, SLOT(onLeftView()));
00271   myActionsMap[ LeftId ] = aAction;
00272 
00274   aAction = new QtxAction(tr("MNU_RIGHT_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_RIGHT" ) ),
00275                            tr( "MNU_RIGHT_VIEW" ), 0, this);
00276   aAction->setStatusTip(tr("DSC_RIGHT_VIEW"));
00277   connect(aAction, SIGNAL(activated()), this, SLOT(onRightView()));
00278   myActionsMap[ RightId ] = aAction;
00279 
00280   // \li Rotate anticlockwise
00281   aAction = new QtxAction(tr("MNU_ANTICLOCKWISE_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_ANTICLOCKWISE" ) ),
00282                        tr( "MNU_ANTICLOCKWISE_VIEW" ), 0, this);
00283   aAction->setStatusTip(tr("DSC_ANTICLOCKWISE_VIEW"));
00284   connect(aAction, SIGNAL(triggered()), this, SLOT(onAntiClockWiseView()));
00285   myActionsMap[ AntiClockWiseId ] = aAction;
00286 
00287   // \li Rotate clockwise
00288   aAction = new QtxAction(tr("MNU_CLOCKWISE_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_CLOCKWISE" ) ),
00289                        tr( "MNU_CLOCKWISE_VIEW" ), 0, this);
00290   aAction->setStatusTip(tr("DSC_CLOCKWISE_VIEW"));
00291   connect(aAction, SIGNAL(triggered()), this, SLOT(onClockWiseView()));
00292   myActionsMap[ ClockWiseId ] = aAction;
00293 
00295   aAction = new QtxAction(tr("MNU_RESET_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_RESET" ) ),
00296                            tr( "MNU_RESET_VIEW" ), 0, this);
00297   aAction->setStatusTip(tr("DSC_RESET_VIEW"));
00298   connect(aAction, SIGNAL(activated()), this, SLOT(onResetView()));
00299   myActionsMap[ ResetId ] = aAction;
00300 
00302   aAction = new QtxAction(tr("MNU_SHOW_TRIHEDRON"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_TRIHEDRON" ) ),
00303                            tr( "MNU_SHOW_TRIHEDRON" ), 0, this);
00304   aAction->setStatusTip(tr("DSC_SHOW_TRIHEDRON"));
00305   connect(aAction, SIGNAL(activated()), this, SLOT(onTrihedronShow()));
00306   myActionsMap[ TrihedronShowId ] = aAction;
00307 }
00308 
00310 void VTKViewer_ViewWindow::createToolBar()
00311 {
00312   myToolBar->addAction( myActionsMap[DumpId] );
00313   myToolBar->addAction( myActionsMap[TrihedronShowId] );
00314 
00315   QtxMultiAction* aScaleAction = new QtxMultiAction( this );
00316   aScaleAction->insertAction( myActionsMap[FitAllId] );
00317   aScaleAction->insertAction( myActionsMap[FitRectId] );
00318   aScaleAction->insertAction( myActionsMap[ZoomId] );
00319   myToolBar->addAction( aScaleAction );
00320 
00321   QtxMultiAction* aPanningAction = new QtxMultiAction( this );
00322   aPanningAction->insertAction( myActionsMap[PanId] );
00323   aPanningAction->insertAction( myActionsMap[GlobalPanId] );
00324   myToolBar->addAction( aPanningAction );
00325 
00326   myToolBar->addAction( myActionsMap[RotationId] );
00327 
00328   QtxMultiAction* aViewsAction = new QtxMultiAction(myToolBar);
00329   aViewsAction->insertAction( myActionsMap[FrontId] );
00330   aViewsAction->insertAction( myActionsMap[BackId] );
00331   aViewsAction->insertAction( myActionsMap[TopId] );
00332   aViewsAction->insertAction( myActionsMap[BottomId] );
00333   aViewsAction->insertAction( myActionsMap[LeftId] );
00334   aViewsAction->insertAction( myActionsMap[RightId] );
00335   myToolBar->addAction( aViewsAction );
00336 
00337   myToolBar->addAction( myActionsMap[AntiClockWiseId] );
00338   myToolBar->addAction( myActionsMap[ClockWiseId] );
00339 
00340   myToolBar->addAction( myActionsMap[ResetId] );
00341 }
00342 
00344 void VTKViewer_ViewWindow::onFrontView()
00345 {
00346   vtkCamera* camera = myRenderer->GetActiveCamera();
00347   camera->SetPosition(1,0,0);
00348   camera->SetViewUp(0,0,1);
00349   camera->SetFocalPoint(0,0,0);
00350   onFitAll();
00351 }
00352 
00354 void VTKViewer_ViewWindow::onBackView()
00355 {
00356   vtkCamera* camera = myRenderer->GetActiveCamera();
00357   camera->SetPosition(-1,0,0);
00358   camera->SetViewUp(0,0,1);
00359   camera->SetFocalPoint(0,0,0);
00360   onFitAll();
00361 }
00362 
00364 void VTKViewer_ViewWindow::onTopView()
00365 {
00366   vtkCamera* camera = myRenderer->GetActiveCamera();
00367   camera->SetPosition(0,0,1);
00368   camera->SetViewUp(0,1,0);
00369   camera->SetFocalPoint(0,0,0);
00370   onFitAll();
00371 }
00372 
00374 void VTKViewer_ViewWindow::onBottomView()
00375 {
00376   vtkCamera* camera = myRenderer->GetActiveCamera();
00377   camera->SetPosition(0,0,-1);
00378   camera->SetViewUp(0,1,0);
00379   camera->SetFocalPoint(0,0,0);
00380   onFitAll();
00381 }
00382 
00384 void VTKViewer_ViewWindow::onLeftView()
00385 {
00386   vtkCamera* camera = myRenderer->GetActiveCamera(); 
00387   camera->SetPosition(0,-1,0);
00388   camera->SetViewUp(0,0,1);
00389   camera->SetFocalPoint(0,0,0);
00390   onFitAll();
00391 }
00392 
00394 void VTKViewer_ViewWindow::onRightView()
00395 {
00396   vtkCamera* camera = myRenderer->GetActiveCamera();
00397   camera->SetPosition(0,1,0);
00398   camera->SetViewUp(0,0,1);
00399   camera->SetFocalPoint(0,0,0);
00400   onFitAll();
00401 }
00402 
00406 void VTKViewer_ViewWindow::onClockWiseView()
00407 {
00408   vtkCamera* aCamera = myRenderer->GetActiveCamera(); 
00409   aCamera->Roll(-90);
00410   aCamera->OrthogonalizeViewUp();
00411   Repaint();
00412 }
00413 
00417 void VTKViewer_ViewWindow::onAntiClockWiseView()
00418 {
00419   vtkCamera* aCamera = myRenderer->GetActiveCamera(); 
00420   aCamera->Roll(90);
00421   aCamera->OrthogonalizeViewUp();
00422   Repaint();
00423 }
00424 
00426 void VTKViewer_ViewWindow::onResetView()
00427 {
00428   int aTriedronIsVisible = isTrihedronDisplayed();
00429   myTrihedron->SetVisibility( VTKViewer_Trihedron::eOnlyLineOn );
00430   ::ResetCamera(myRenderer,true);  
00431   vtkCamera* aCamera = myRenderer->GetActiveCamera();
00432   aCamera->SetPosition(1,-1,1);
00433   aCamera->SetViewUp(0,0,1);
00434   ::ResetCamera(myRenderer,true);  
00435   if(aTriedronIsVisible) myTrihedron->VisibilityOn();
00436   else myTrihedron->VisibilityOff();
00437   static vtkFloatingPointType aCoeff = 3.0;
00438   aCamera->SetParallelScale(aCoeff*aCamera->GetParallelScale());
00439   Repaint();
00440 }
00441 
00443 void VTKViewer_ViewWindow::onFitAll()
00444 {
00445   myRWInteractor->GetInteractorStyle()->ViewFitAll();
00446   Repaint();
00447 }
00448 
00450 void VTKViewer_ViewWindow::setBackgroundColor( const QColor& c )
00451 {
00452   Qtx::BackgroundData bg = background();
00453   bg.setColor( c );
00454   setBackground( bg );
00455 }
00456 
00458 QColor VTKViewer_ViewWindow::backgroundColor() const
00459 {
00460   return background().color();
00461 }
00462 
00464 void VTKViewer_ViewWindow::setBackground( const Qtx::BackgroundData& bgData )
00465 {
00466   bool ok = false;
00467  
00468   if ( bgData.isValid() ) {
00469     switch ( bgData.mode() ) {
00470     case Qtx::ColorBackground:
00471       {
00472        QColor c = bgData.color();
00473        if ( c.isValid() ) {
00474          // show solid-colored background
00475          getRenderer()->SetTexturedBackground( false );  // cancel texture mode
00476          getRenderer()->SetGradientBackground( false );  // cancel gradient mode
00477          getRenderer()->SetBackground( c.red()/255.0,
00478                                    c.green()/255.0,
00479                                    c.blue()/255.0 ); // set background color
00480          ok = true;
00481        }
00482        break;
00483       }
00484     case Qtx::SimpleGradientBackground:
00485       {
00486        QColor c1, c2;
00487        int type = bgData.gradient( c1, c2 );
00488         if ( c1.isValid() )
00489         {
00490           if ( !c2.isValid() )
00491             c2 = c1;
00492 
00493           // show two-color gradient background
00494           getRenderer()->SetTexturedBackground( false );    // cancel texture mode
00495           getRenderer()->SetGradientBackground( true );     // switch to gradient mode
00496 
00497           VTKViewer_OpenGLRenderer* aRenderer =
00498             VTKViewer_OpenGLRenderer::SafeDownCast( getRenderer() );
00499           if( aRenderer )
00500           {
00501             aRenderer->SetGradientType( type );
00502             aRenderer->SetBackground( c1.redF(), c1.greenF(), c1.blueF() );
00503             aRenderer->SetBackground2( c2.redF(), c2.greenF(), c2.blueF() );
00504             ok = true;
00505           }
00506         }
00507        break;
00508       }
00509     case Qtx::CustomGradientBackground:
00510       {
00511        // NOT IMPLEMENTED YET
00512        getRenderer()->SetTexturedBackground( false );  // cancel texture mode
00513        getRenderer()->SetGradientBackground( false );  // cancel gradient mode
00514        // .........
00515        break;
00516       }
00517     default:
00518       break;
00519     }
00520     if ( bgData.isTextureShown() ) {
00521       QString fileName;
00522       int textureMode = bgData.texture( fileName );
00523       QFileInfo fi( fileName );
00524       if ( !fileName.isEmpty() && fi.exists() ) {
00525        // read texture from file
00526        QString extension = fi.suffix().toLower();
00527        vtkImageReader2* aReader = 0;
00528        if ( extension == "jpg" || extension == "jpeg" )
00529          aReader = vtkJPEGReader::New();
00530        else if ( extension == "bmp" )
00531          aReader = vtkBMPReader::New();
00532        else if ( extension == "tif" || extension == "tiff" )
00533          aReader = vtkTIFFReader::New();
00534        else if ( extension == "png" )
00535          aReader = vtkPNGReader::New();
00536        else if ( extension == "mhd" || extension == "mha" )
00537          aReader = vtkMetaImageReader::New();           
00538        if ( aReader ) {
00539          // create texture
00540          aReader->SetFileName( fi.absoluteFilePath().toLatin1().constData() );
00541          aReader->Update();
00542          
00543          VTKViewer_Texture* aTexture = VTKViewer_Texture::New();
00544          vtkImageMapToColors* aMap = 0;
00545          vtkAlgorithmOutput* anOutput;
00546          /*
00547          // special processing for BMP reader
00548          vtkBMPReader* aBMPReader = (vtkBMPReader*)aReader;
00549          if ( aBMPReader ) {
00550            // Special processing for BMP file
00551            aBMPReader->SetAllow8BitBMP(1);
00552            
00553            aMap = vtkImageMapToColors::New();
00554            aMap->SetInputConnection( aBMPReader->GetOutputPort() );
00555            aMap->SetLookupTable( (vtkScalarsToColors*)aBMPReader->GetLookupTable() );
00556            aMap->SetOutputFormatToRGB();
00557            
00558            anOutput = aMap->GetOutputPort();
00559          }
00560          else {
00561           }
00562          */
00563          anOutput = aReader->GetOutputPort( 0 );
00564          aTexture->SetInputConnection( anOutput );
00565          // set texture mode
00566          // VSR: Currently, VTK only supports Stretch mode, so below code will give
00567          // the same results for all modes
00568          switch ( textureMode ) {
00569          case Qtx::TileTexture:
00570            aTexture->RepeatOn();
00571            aTexture->EdgeClampOff();
00572            aTexture->InterpolateOff();
00573            break;
00574          case Qtx::StretchTexture:
00575            aTexture->RepeatOff();
00576            aTexture->EdgeClampOff();
00577            aTexture->InterpolateOn();
00578            break;
00579          case Qtx::CenterTexture:
00580          default:
00581            aTexture->RepeatOff();
00582            aTexture->EdgeClampOn();
00583            aTexture->InterpolateOff();
00584            break;
00585          }
00586          // show textured background
00587          getRenderer()->SetTexturedBackground( true );     // switch to texture mode
00588          getRenderer()->SetBackgroundTexture( aTexture );  // set texture image
00589          
00590          // clean-up resources
00591          if ( aMap )
00592            aMap->Delete();
00593          aReader->Delete();
00594          aTexture->Delete();
00595          ok = true;
00596        }
00597       }
00598     }
00599   }
00600 
00601   if ( ok )
00602     myBackground = bgData;
00603 }
00604 
00606 Qtx::BackgroundData VTKViewer_ViewWindow::background() const
00607 {
00608   return myBackground;
00609 }
00610 
00612 void VTKViewer_ViewWindow::Repaint(bool theUpdateTrihedron)
00613 {
00614   if (theUpdateTrihedron) onAdjustTrihedron();
00615   myRenderWindow->update();
00616 }
00617 
00619 void VTKViewer_ViewWindow::GetScale( double theScale[3] ) {
00620   myTransform->GetScale( theScale );
00621 }
00622 
00624 void VTKViewer_ViewWindow::SetScale( double theScale[3] ) {
00625   myTransform->SetMatrixScale( theScale[0], theScale[1], theScale[2] );
00626   myRWInteractor->Render();
00627   Repaint();
00628 }
00629 
00631 void VTKViewer_ViewWindow::onAdjustTrihedron(){   
00632   if( !isTrihedronDisplayed() ) 
00633     return;
00634   int aVisibleNum = myTrihedron->GetVisibleActorCount(myRenderer);
00635   if(aVisibleNum){
00636     // calculating diagonal of visible props of the renderer
00637     vtkFloatingPointType bnd[6];
00638     myTrihedron->VisibilityOff();
00639     ::ComputeVisiblePropBounds(myRenderer,bnd);
00640     myTrihedron->VisibilityOn();
00641     vtkFloatingPointType aLength = 0;
00642     static bool CalcByDiag = false;
00643     if(CalcByDiag){
00644       aLength = sqrt((bnd[1]-bnd[0])*(bnd[1]-bnd[0])+
00645                      (bnd[3]-bnd[2])*(bnd[3]-bnd[2])+
00646                      (bnd[5]-bnd[4])*(bnd[5]-bnd[4]));
00647     }else{
00648       aLength = bnd[1]-bnd[0];
00649       aLength = qMax((bnd[3]-bnd[2]),aLength);
00650       aLength = qMax((bnd[5]-bnd[4]),aLength);
00651     }
00652    
00653     static vtkFloatingPointType aSizeInPercents = 105;
00654     QString aSetting;// = SUIT_CONFIG->getSetting("Viewer:TrihedronSize");
00655     if(!aSetting.isEmpty()) aSizeInPercents = aSetting.toFloat();
00656 
00657     static vtkFloatingPointType EPS_SIZE = 5.0E-3;
00658     vtkFloatingPointType aSize = myTrihedron->GetSize();
00659     vtkFloatingPointType aNewSize = aLength*aSizeInPercents/100.0;
00660     // if the new trihedron size have sufficient difference, then apply the value
00661     if(fabs(aNewSize-aSize) > aSize*EPS_SIZE || fabs(aNewSize-aSize) > aNewSize*EPS_SIZE){
00662       myTrihedron->SetSize(aNewSize);
00663     }
00664   }
00665   ::ResetCameraClippingRange(myRenderer);
00666 }
00667 
00669 void VTKViewer_ViewWindow::onKeyPressed(QKeyEvent* event)
00670 {
00671   emit keyPressed( this, event );
00672 }
00673 
00675 void VTKViewer_ViewWindow::onKeyReleased(QKeyEvent* event)
00676 {
00677   emit keyReleased( this, event );
00678 }
00679 
00681 void VTKViewer_ViewWindow::onMousePressed(QMouseEvent* event)
00682 {
00683   emit mousePressed(this, event);
00684 }
00685 
00687 void VTKViewer_ViewWindow::onMouseReleased(QMouseEvent* event)
00688 {
00689   emit mouseReleased( this, event );
00690 }
00691 
00693 void VTKViewer_ViewWindow::onMouseMoving(QMouseEvent* event)
00694 {
00695   emit mouseMoving( this, event );
00696 }
00697 
00699 void VTKViewer_ViewWindow::onMouseDoubleClicked( QMouseEvent* event )
00700 {
00701   emit mouseDoubleClicked( this, event );
00702 }
00703 
00707 void VTKViewer_ViewWindow::InsertActor( VTKViewer_Actor* theActor, bool theMoveInternalActors ){
00708   theActor->AddToRender(myRenderer);
00709   theActor->SetTransform(myTransform);
00710   if(theMoveInternalActors) 
00711     myRWInteractor->MoveInternalActors();
00712 }
00713 
00717 void VTKViewer_ViewWindow::AddActor( VTKViewer_Actor* theActor, bool theUpdate /*=false*/ ){
00718   InsertActor(theActor);
00719   if(theUpdate) 
00720     Repaint();
00721 }
00722 
00724 void VTKViewer_ViewWindow::RemoveActor( VTKViewer_Actor* theActor, bool theUpdate /*=false*/ ){
00725   theActor->RemoveFromRender(myRenderer);
00726   if(theUpdate) 
00727     Repaint();
00728 }
00729 
00731 void VTKViewer_ViewWindow::MoveActor( VTKViewer_Actor* theActor)
00732 {
00733   RemoveActor(theActor);
00734   InsertActor(theActor,true);
00735 }
00736 
00738 void VTKViewer_ViewWindow::onTrihedronShow()
00739 {
00740   if (isTrihedronDisplayed())
00741     myTrihedron->VisibilityOff();
00742   else
00743     myTrihedron->VisibilityOn();
00744   myRenderWindow->update();
00745 }
00746 
00748 QImage VTKViewer_ViewWindow::dumpView()
00749 {
00750   QPixmap px = QPixmap::grabWindow( myRenderWindow->winId() );
00751   return px.toImage();
00752 }
00753 
00756 QString VTKViewer_ViewWindow::getVisualParameters()
00757 {
00758   double pos[3], focalPnt[3], viewUp[3], parScale, scale[3];
00759 
00760   vtkCamera* camera = myRenderer->GetActiveCamera();
00761   camera->GetPosition( pos );
00762   camera->GetFocalPoint( focalPnt );
00763   camera->GetViewUp( viewUp );
00764   parScale = camera->GetParallelScale();
00765   GetScale( scale );
00766 
00767   QString retStr;
00768   retStr.sprintf( "%.12e*%.12e*%.12e*%.12e*%.12e*%.12e*%.12e*%.12e*%.12e*%.12e*%.12e*%.12e*%.12e", 
00769                   pos[0], pos[1], pos[2], focalPnt[0], focalPnt[1], focalPnt[2], viewUp[0], viewUp[1], 
00770                   viewUp[2], parScale, scale[0], scale[1], scale[2] );
00771   return retStr;
00772 }
00773 
00776 void VTKViewer_ViewWindow::setVisualParameters( const QString& parameters )
00777 {
00778   QStringList paramsLst = parameters.split( '*' );
00779   if ( paramsLst.size() == 13 ) {
00780     double pos[3], focalPnt[3], viewUp[3], parScale, scale[3];
00781     pos[0] = paramsLst[0].toDouble();
00782     pos[1] = paramsLst[1].toDouble();
00783     pos[2] = paramsLst[2].toDouble();
00784     focalPnt[0] = paramsLst[3].toDouble();
00785     focalPnt[1] = paramsLst[4].toDouble();
00786     focalPnt[2] = paramsLst[5].toDouble();
00787     viewUp[0] = paramsLst[6].toDouble();
00788     viewUp[1] = paramsLst[7].toDouble();
00789     viewUp[2] = paramsLst[8].toDouble();
00790     parScale = paramsLst[9].toDouble();
00791     scale[0] = paramsLst[10].toDouble();
00792     scale[1] = paramsLst[11].toDouble();
00793     scale[2] = paramsLst[12].toDouble();
00794 
00795     vtkCamera* camera = myRenderer->GetActiveCamera();
00796     camera->SetPosition( pos );
00797     camera->SetFocalPoint( focalPnt );
00798     camera->SetViewUp( viewUp );
00799     camera->SetParallelScale( parScale );
00800     myTransform->SetMatrixScale( scale[0], scale[1], scale[2] );
00801     myRWInteractor->Render();
00802   }
00803 }