Back to index

salome-gui  6.5.0
VTKViewer_RenderWindow.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_RenderWindow.h"
00024 
00025 #include <QColorDialog>
00026 #include <QContextMenuEvent>
00027 
00028 #include <stdlib.h>
00029 #include <math.h>
00030 
00031 #include <vtkRenderWindowInteractor.h>
00032 #include <vtkRendererCollection.h>
00033 #include <vtkCamera.h>
00034 #ifndef WIN32
00035 #include <QX11Info>
00036 #include <vtkXOpenGLRenderWindow.h>
00037 //#include <GL/gl.h>
00038 //#include <GL/glu.h>
00039 //#include <qgl.h>
00040 #endif
00041 
00046 VTKViewer_RenderWindow::VTKViewer_RenderWindow(QWidget* parent, const char* name) :
00047 QWidget(parent, Qt::FramelessWindowHint )
00048 {
00049   setObjectName( name );
00050   setAttribute( Qt::WA_DeleteOnClose );
00051 
00052   myRW = vtkRenderWindow::New();
00053 #ifndef WIN32
00054   myRW->SetDisplayId((void*)(QX11Info::display()));
00055 #endif
00056   myRW->SetWindowId((void*)winId());
00057   myRW->DoubleBufferOn();
00058   setMouseTracking(true);
00059 }
00060 
00062 VTKViewer_RenderWindow::~VTKViewer_RenderWindow() 
00063 {
00064   myRW->Delete();
00065 }
00066 
00068 void VTKViewer_RenderWindow::paintEvent(QPaintEvent* theEvent) 
00069 {
00070   myRW->Render();
00071 }
00072 
00074 void VTKViewer_RenderWindow::resizeEvent(QResizeEvent* theEvent) 
00075 {
00076   int aWidth = myRW->GetSize()[0], aHeight = myRW->GetSize()[1];
00077   if(vtkRenderWindowInteractor* aRWI = myRW->GetInteractor())
00078     aRWI->UpdateSize(width(), height());
00079   if(aWidth != width() || aHeight != height()){
00080     vtkRendererCollection * aRenderers = myRW->GetRenderers();
00081     aRenderers->InitTraversal();
00082     double aCoeff = 1.0;
00083     if(vtkRenderer *aRenderer = aRenderers->GetNextItem()){
00084       vtkCamera *aCamera = aRenderer->GetActiveCamera();
00085       double aScale = aCamera->GetParallelScale();
00086       if((aWidth - width())*(aHeight - height()) > 0)
00087         aCoeff = sqrt(double(aWidth)/double(width())*double(height())/double(aHeight));
00088       else
00089         aCoeff = double(aWidth)/double(width());
00090       aCamera->SetParallelScale(aScale*aCoeff);
00091     }
00092   }
00093 }
00094 
00096 QPaintEngine* VTKViewer_RenderWindow::paintEngine() const
00097 {
00098   return 0;
00099 }
00100 
00102 void VTKViewer_RenderWindow::mouseMoveEvent(QMouseEvent* event) 
00103 {
00104   emit MouseMove(event) ;
00105 }
00106 
00108 void VTKViewer_RenderWindow::mousePressEvent(QMouseEvent* event) 
00109 {
00110   emit MouseButtonPressed( event );
00111 }
00112 
00114 void VTKViewer_RenderWindow::mouseReleaseEvent( QMouseEvent *event )
00115 {
00116   emit MouseButtonReleased( event );
00117 }
00118 
00120 void VTKViewer_RenderWindow::mouseDoubleClickEvent( QMouseEvent* event )
00121 {
00122   emit MouseDoubleClicked( event );
00123 }
00124 
00126 void VTKViewer_RenderWindow::keyPressEvent (QKeyEvent* event) 
00127 {
00128   emit KeyPressed(event) ;
00129 }
00130 
00132 void VTKViewer_RenderWindow::keyReleaseEvent (QKeyEvent * event) 
00133 {
00134   emit KeyReleased(event) ;
00135 }
00136 
00138 void VTKViewer_RenderWindow::wheelEvent(QWheelEvent* event)
00139 {
00140   emit WheelMoved(event) ;
00141 }
00142 
00144 void VTKViewer_RenderWindow::onChangeBackgroundColor()
00145 {
00146   //float red, green, blue;
00147   vtkFloatingPointType backint[3];
00148 
00149   vtkRendererCollection * theRenderers = myRW->GetRenderers();
00150   theRenderers->InitTraversal();
00151   vtkRenderer * theRenderer = theRenderers->GetNextItem();
00152   theRenderer->GetBackground(backint);
00153 
00154   QColor selColor = QColorDialog::getColor ( QColor(int(backint[0]*255), int(backint[1]*255), int(backint[2]*255)), NULL );     
00155   if ( selColor.isValid() ) {
00156     theRenderer->SetBackground( selColor.red()/255., selColor.green()/255., selColor.blue()/255. ); 
00157     /* VSR : PAL5420 ---------------------------------------------------
00158     SUIT_CONFIG->addSetting( "VTKViewer:BackgroundColorRed",   selColor.red() );
00159     SUIT_CONFIG->addSetting( "VTKViewer:BackgroundColorGreen", selColor.green() );
00160     SUIT_CONFIG->addSetting( "VTKViewer:BackgroundColorBlue",  selColor.blue() );
00161     VSR : PAL5420 --------------------------------------------------- */
00162   }
00163 }
00164 
00166 void VTKViewer_RenderWindow::contextMenuEvent ( QContextMenuEvent * e )
00167 {
00168   if ( e->reason() != QContextMenuEvent::Mouse )
00169     emit contextMenuRequested( e );
00170 }