Back to index

salome-gui  6.5.0
VTKViewer_ViewModel.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_ViewModel.h"
00024 #include "VTKViewer_ViewWindow.h"
00025 #include "VTKViewer_ViewManager.h"
00026 #include "VTKViewer_RenderWindowInteractor.h"
00027 
00028 #include "QtxBackgroundTool.h"
00029 #include "SUIT_ViewWindow.h"
00030 #include "SUIT_Desktop.h"
00031 #include "SUIT_Session.h"
00032 
00033 #include <QColorDialog>
00034 #include <QMenu>
00035 #include <QMouseEvent>
00036 #include <QToolBar>
00037 
00038 // VSR: Uncomment below line to allow texture background support in VTK viewer
00039 #define VTK_ENABLE_TEXTURED_BACKGROUND
00040 
00041 bool _InitializeVtkWarningsCall()
00042 {
00043   char* isOn = getenv( "VTK_WARNINGS_IS_ON" );  
00044   if ( !isOn || strcmp( isOn, "1" ) )
00045     vtkObject::GlobalWarningDisplayOff();
00046 
00047   delete isOn;
00048   return vtkObject::GetGlobalWarningDisplay();
00049 }
00050 static bool _InitializeVtkWarnings = _InitializeVtkWarningsCall();
00051 
00053 VTKViewer_Viewer::VTKViewer_Viewer()
00054 : SUIT_ViewModel(),
00055   myDefaultBackground( Qtx::BackgroundData( Qt::black ) )
00056 {
00057 }
00058 
00060 VTKViewer_Viewer::~VTKViewer_Viewer() 
00061 {
00062 }
00063 
00065 QString VTKViewer_Viewer::backgroundData( QStringList& gradList, QIntList& idList, QIntList& txtList )
00066 {
00067   gradList << tr( "GT_HORIZONTALGRADIENT" )
00068            << tr( "GT_VERTICALGRADIENT" )
00069            << tr( "GT_FIRSTDIAGONALGRADIENT" )
00070            << tr( "GT_SECONDDIAGONALGRADIENT" )
00071            << tr( "GT_FIRSTCORNERGRADIENT" )
00072            << tr( "GT_SECONDCORNERGRADIENT" )
00073            << tr( "GT_THIRDCORNERGRADIENT" )
00074            << tr( "GT_FOURTHCORNERGRADIENT" );
00075   idList   << HorizontalGradient
00076            << VerticalGradient
00077            << FirstDiagonalGradient
00078            << SecondDiagonalGradient
00079            << FirstCornerGradient
00080            << SecondCornerGradient
00081            << ThirdCornerGradient
00082            << FourthCornerGradient;
00083 #ifdef VTK_ENABLE_TEXTURED_BACKGROUND
00084   txtList  << Qtx::CenterTexture << Qtx::TileTexture << Qtx::StretchTexture;
00085 #endif
00086   return tr("BG_IMAGE_FILES");
00087 }
00088 
00090 QColor VTKViewer_Viewer::backgroundColor() const
00091 {
00092   return background().color();
00093 }
00094 
00096 void VTKViewer_Viewer::setBackgroundColor( const QColor& c )
00097 {
00098   Qtx::BackgroundData bg = background();
00099   bg.setColor( c );
00100   setBackground( bg );
00101 }
00102 
00104 Qtx::BackgroundData VTKViewer_Viewer::background() const
00105 {
00106   return myDefaultBackground;
00107 }
00108 
00110 void VTKViewer_Viewer::setBackground( const Qtx::BackgroundData& theBackground )
00111 {
00112   myDefaultBackground = theBackground.isValid() ? theBackground : Qtx::BackgroundData( Qt::black );
00113 }
00114 
00116 SUIT_ViewWindow* VTKViewer_Viewer::createView( SUIT_Desktop* theDesktop )
00117 {
00118   VTKViewer_ViewWindow* vw = new VTKViewer_ViewWindow( theDesktop, this );
00119   vw->setBackground( myDefaultBackground );
00120   return vw;
00121 }
00122 
00124 void VTKViewer_Viewer::setViewManager(SUIT_ViewManager* theViewManager)
00125 {
00126   SUIT_ViewModel::setViewManager(theViewManager);
00127   connect(theViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), 
00128           this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
00129 
00130   connect(theViewManager, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)), 
00131           this, SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
00132 
00133   connect(theViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)), 
00134           this, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
00135 }
00136 
00138 void VTKViewer_Viewer::contextMenuPopup(QMenu* thePopup)
00139 {
00140   thePopup->addAction( tr( "MEN_DUMP_VIEW" ), this, SLOT( onDumpView() ) );
00141   thePopup->addAction( tr( "MEN_CHANGE_BACKGROUND" ), this, SLOT( onChangeBackground() ) );
00142 
00143   thePopup->addSeparator();
00144 
00145   VTKViewer_ViewWindow* aView = (VTKViewer_ViewWindow*)(myViewManager->getActiveView());
00146   if ( aView && !aView->getToolBar()->isVisible() )
00147     thePopup->addAction( tr( "MEN_SHOW_TOOLBAR" ), this, SLOT( onShowToolbar() ) );
00148 }
00149 
00151 void VTKViewer_Viewer::onMousePress(SUIT_ViewWindow* vw, QMouseEvent* event)
00152 {
00153   VTKViewer_RenderWindowInteractor* rwi = 0;
00154   if ( vw && vw->inherits( "VTKViewer_ViewWindow" ) )
00155     rwi = ((VTKViewer_ViewWindow*)vw)->getRWInteractor();
00156   if ( !rwi )
00157     return;
00158 
00159   switch(event->buttons()) {
00160   case Qt::LeftButton:
00161     rwi->LeftButtonPressed(event) ;
00162     break ;
00163   case Qt::MidButton:
00164     rwi->MiddleButtonPressed(event) ;
00165     break ;
00166   case Qt::RightButton:
00167     rwi->RightButtonPressed(event) ;
00168     break;
00169   default:
00170     break ;
00171   }
00172 }
00173 
00175 void VTKViewer_Viewer::onMouseMove(SUIT_ViewWindow* vw, QMouseEvent* event)
00176 {
00177   VTKViewer_RenderWindowInteractor* rwi = 0;
00178   if ( vw && vw->inherits( "VTKViewer_ViewWindow" ) )
00179     rwi = ((VTKViewer_ViewWindow*)vw)->getRWInteractor();
00180   if ( rwi )
00181     rwi->MouseMove( event );
00182 }
00183 
00185 void VTKViewer_Viewer::onMouseRelease(SUIT_ViewWindow* vw, QMouseEvent* event)
00186 {
00187   VTKViewer_RenderWindowInteractor* rwi = 0;
00188   if ( vw && vw->inherits( "VTKViewer_ViewWindow" ) )
00189     rwi = ((VTKViewer_ViewWindow*)vw)->getRWInteractor();
00190   if ( !rwi )
00191     return;
00192 
00193   switch(event->buttons()) {
00194   case Qt::LeftButton:
00195     rwi->LeftButtonReleased(event) ;
00196     break ;
00197   case Qt::MidButton:
00198     rwi->MiddleButtonReleased(event) ;
00199     break ;
00200   case Qt::RightButton:
00201     rwi->RightButtonReleased(event) ;
00202     break;
00203   default:
00204     break ;
00205   }
00206 }
00207 
00209 void VTKViewer_Viewer::enableSelection(bool isEnabled)
00210 {
00211   mySelectionEnabled = isEnabled;
00213 }
00214 
00216 void VTKViewer_Viewer::enableMultiselection(bool isEnable)
00217 {
00218   myMultiSelectionEnabled = isEnable;
00220 }
00221 
00223 void VTKViewer_Viewer::onDumpView()
00224 {
00225   VTKViewer_ViewWindow* aView = (VTKViewer_ViewWindow*)(myViewManager->getActiveView());
00226   if ( aView )
00227     aView->onDumpView();
00228 }
00229 
00231 void VTKViewer_Viewer::onChangeBackground()
00232 {
00233   VTKViewer_ViewWindow* aView = (VTKViewer_ViewWindow*)(myViewManager->getActiveView());
00234   if ( !aView )
00235     return;
00236 
00237   // get supported gradient types
00238   QStringList gradList;
00239   QIntList    idList, txtList;
00240   QString     formats = backgroundData( gradList, idList, txtList );
00241 
00242   // invoke dialog box
00243   Qtx::BackgroundData bgData = QtxBackgroundDialog::getBackground( aView->background(),  // initial background
00244                                                            aView,                // parent for dialog box
00245                                                            txtList,              // allowed texture modes
00246                                                            true,                 // enable solid color mode
00247                                                            true,                 // enable gradient mode
00248                                                            false,                // disable custom gradient mode
00249                                                            !txtList.isEmpty(),   // enable texture mode
00250                                                            gradList,             // gradient names
00251                                                            idList,               // gradient identifiers
00252                                                            formats );            // image formats
00253 
00254   // set chosen background data to the viewer
00255   if ( bgData.isValid() )
00256     aView->setBackground( bgData );
00257 }
00258 
00260 void VTKViewer_Viewer::onShowToolbar() {
00261   VTKViewer_ViewWindow* aView = (VTKViewer_ViewWindow*)(myViewManager->getActiveView());
00262   if ( aView )
00263     aView->getToolBar()->show();    
00264 }