Back to index

salome-gui  6.5.0
GLViewer_ViewFrame.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 "GLViewer_ViewFrame.h"
00024 #include "GLViewer_Viewer.h"
00025 #include "GLViewer_ViewPort2d.h"
00026 
00027 #include <QtxToolBar.h>
00028 #include <QtxMultiAction.h>
00029 #include <QtxActionToolMgr.h>
00030 
00031 #include <SUIT_Desktop.h>
00032 #include <SUIT_Session.h>
00033 #include <SUIT_ResourceMgr.h>
00034 #include <SUIT_MessageBox.h>
00035 
00036 #include <QColor>
00037 #include <QFileDialog>
00038 #include <QImage>
00039 #include <QHBoxLayout>
00040 #include <QString>
00041 #include <QFrame>
00042 #include <QMouseEvent>
00043 #include <QKeyEvent>
00044 #include <QWheelEvent>
00045 
00046 #ifdef WIN32
00047 #include <Standard_Integer.hxx>
00048 #include <iostream>
00049 #endif
00050 
00054 GLViewer_ViewFrame::GLViewer_ViewFrame( SUIT_Desktop* d, GLViewer_Viewer* vw )
00055 : SUIT_ViewWindow( d ),
00056 myViewer( vw ),
00057 myVP( 0 )
00058 {
00059     QFrame* client = new QFrame( this );
00060     setCentralWidget( client );
00061 
00062     QBoxLayout* layout = new QHBoxLayout( client );
00063     layout->setMargin( 0 );
00064     layout->setSpacing( 0 );
00065 
00066     GLViewer_ViewPort2d* vp = new GLViewer_ViewPort2d( client, this );
00067     setViewPort( vp );
00068     setBackgroundColor( Qt::white );
00069     layout->addWidget( vp );
00070 
00071     createActions();
00072     createToolBar();
00073 }
00074 
00078 GLViewer_ViewFrame::~GLViewer_ViewFrame()
00079 {
00080 }
00081 
00085 void GLViewer_ViewFrame::createActions()
00086 {
00087   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
00088   QtxAction* aAction;
00089 
00090   // Dump view
00091   aAction = new QtxAction(tr("MNU_DUMP_VIEW"), aResMgr->loadPixmap( "GLViewer", tr( "ICON_GL_DUMP" ) ),
00092                           tr( "MNU_DUMP_VIEW" ), 0, this);
00093   aAction->setStatusTip(tr("DSC_DUMP_VIEW"));
00094   connect(aAction, SIGNAL(activated()), this, SLOT(onDumpView()));
00095   toolMgr()->registerAction( aAction, DumpId );
00096 
00097   // FitAll
00098   aAction = new QtxAction(tr("MNU_FITALL"), aResMgr->loadPixmap( "GLViewer", tr( "ICON_GL_FITALL" ) ),
00099                           tr( "MNU_FITALL" ), 0, this);
00100   aAction->setStatusTip(tr("DSC_FITALL"));
00101   connect(aAction, SIGNAL(activated()), this, SLOT(onViewFitAll()));
00102   toolMgr()->registerAction( aAction, FitAllId );
00103 
00104   // FitRect
00105   aAction = new QtxAction(tr("MNU_FITRECT"), aResMgr->loadPixmap( "GLViewer", tr( "ICON_GL_FITAREA" ) ),
00106                           tr( "MNU_FITRECT" ), 0, this);
00107   aAction->setStatusTip(tr("DSC_FITRECT"));
00108   connect(aAction, SIGNAL(activated()), this, SLOT(onViewFitArea()));
00109   toolMgr()->registerAction( aAction, FitRectId );
00110 
00111   // FitSelect
00112   aAction = new QtxAction(tr("MNU_FITSELECT"), aResMgr->loadPixmap( "GLViewer", tr( "ICON_GL_FITSELECT" ) ),
00113                           tr( "MNU_FITSELECT" ), 0, this);
00114   aAction->setStatusTip(tr("DSC_FITSELECT"));
00115   connect(aAction, SIGNAL(activated()), this, SLOT(onViewFitSelect()));
00116   toolMgr()->registerAction( aAction, FitSelectId );
00117 
00118   // Zoom
00119   aAction = new QtxAction(tr("MNU_ZOOM_VIEW"), aResMgr->loadPixmap( "GLViewer", tr( "ICON_GL_ZOOM" ) ),
00120                           tr( "MNU_ZOOM_VIEW" ), 0, this);
00121   aAction->setStatusTip(tr("DSC_ZOOM_VIEW"));
00122   connect(aAction, SIGNAL(activated()), this, SLOT(onViewZoom()));
00123   toolMgr()->registerAction( aAction, ZoomId );
00124 
00125   // Panning
00126   aAction = new QtxAction(tr("MNU_PAN_VIEW"), aResMgr->loadPixmap( "GLViewer", tr( "ICON_GL_PAN" ) ),
00127                           tr( "MNU_PAN_VIEW" ), 0, this);
00128   aAction->setStatusTip(tr("DSC_PAN_VIEW"));
00129   connect(aAction, SIGNAL(activated()), this, SLOT(onViewPan()));
00130   toolMgr()->registerAction( aAction, PanId );
00131 
00132   // Global Panning
00133   aAction = new QtxAction(tr("MNU_GLOBALPAN_VIEW"), aResMgr->loadPixmap( "GLViewer", tr( "ICON_GL_GLOBALPAN" ) ),
00134                           tr( "MNU_GLOBALPAN_VIEW" ), 0, this);
00135   aAction->setStatusTip(tr("DSC_GLOBALPAN_VIEW"));
00136   connect(aAction, SIGNAL(activated()), this, SLOT(onViewGlobalPan()));
00137   toolMgr()->registerAction( aAction, GlobalPanId );
00138 
00139   aAction = new QtxAction(tr("MNU_RESET_VIEW"), aResMgr->loadPixmap( "GLViewer", tr( "ICON_GL_RESET" ) ),
00140                           tr( "MNU_RESET_VIEW" ), 0, this);
00141   aAction->setStatusTip(tr("DSC_RESET_VIEW"));
00142   connect(aAction, SIGNAL(activated()), this, SLOT(onViewReset()));
00143   toolMgr()->registerAction( aAction, ResetId );
00144 }
00145 
00149 void GLViewer_ViewFrame::createToolBar()
00150 {
00151   int tid = toolMgr()->createToolBar( tr("LBL_TOOLBAR_LABEL"), false );
00152   toolMgr()->append( DumpId, tid );
00153 
00154   QtxMultiAction* aScaleAction = new QtxMultiAction( this );
00155   aScaleAction->insertAction( toolMgr()->action( FitAllId ) );
00156   aScaleAction->insertAction( toolMgr()->action( FitRectId ) );
00157   aScaleAction->insertAction( toolMgr()->action( FitSelectId ) );
00158   aScaleAction->insertAction( toolMgr()->action( ZoomId ) );
00159   toolMgr()->append( aScaleAction, tid );
00160 
00161   QtxMultiAction* aPanAction = new QtxMultiAction( this );
00162   aPanAction->insertAction( toolMgr()->action( PanId ) );
00163   aPanAction->insertAction( toolMgr()->action( GlobalPanId ) );
00164   toolMgr()->append( aPanAction, tid );
00165 
00166   toolMgr()->append( toolMgr()->action( ResetId ), tid );
00167 }
00168 
00172 void GLViewer_ViewFrame::setViewPort( GLViewer_ViewPort* vp )
00173 {
00174     if ( myVP == vp )
00175         return;
00176 
00177     if ( myVP )
00178     {
00179         disconnect( myVP, SIGNAL( vpDrawExternal( QPainter* ) ), this, SIGNAL( vfDrawExternal( QPainter* ) ) );
00180         disconnect( myVP, SIGNAL( vpMouseEvent( QMouseEvent* ) ), this, SLOT( mouseEvent( QMouseEvent* ) ) );
00181         disconnect( myVP, SIGNAL( vpKeyEvent( QKeyEvent* ) ), this, SLOT( keyEvent( QKeyEvent* ) ) );
00182         disconnect( myVP, SIGNAL( vpWheelEvent( QWheelEvent* ) ), this, SLOT( wheelEvent( QWheelEvent* ) ) );
00183         disconnect( myVP, SIGNAL( contextMenuRequested( QContextMenuEvent* ) ),
00184                     this, SIGNAL( contextMenuRequested( QContextMenuEvent* ) ) );
00185     }
00186     myVP = vp;
00187     if ( myVP )
00188     {
00189         connect( myVP, SIGNAL( vpDrawExternal( QPainter* ) ), this, SIGNAL( vfDrawExternal( QPainter* ) ) );
00190         connect( myVP, SIGNAL( vpMouseEvent( QMouseEvent* ) ), this, SLOT( mouseEvent( QMouseEvent* ) ) );
00191         connect( myVP, SIGNAL( vpKeyEvent( QKeyEvent* ) ), this, SLOT( keyEvent( QKeyEvent* ) ) );
00192         connect( myVP, SIGNAL( vpWheelEvent( QWheelEvent* ) ), this, SLOT( wheelEvent( QWheelEvent* ) ) );
00193         connect( myVP, SIGNAL( contextMenuRequested( QContextMenuEvent* ) ),
00194                  this, SIGNAL( contextMenuRequested( QContextMenuEvent* ) ) );
00195     }
00196 }
00197 
00201 GLViewer_ViewPort* GLViewer_ViewFrame::getViewPort() const
00202 {
00203     return myVP;
00204 }
00205 
00209 void GLViewer_ViewFrame::setBackgroundColor( const QColor& color )
00210 {
00211     if ( myVP )
00212         myVP->setBackgroundColor( color );
00213 }
00214 
00218 QColor GLViewer_ViewFrame::backgroundColor() const
00219 {
00220     if ( myVP )
00221         return myVP->backgroundColor();
00222     return palette().color( backgroundRole() );
00223 }
00224 
00228 void GLViewer_ViewFrame::setViewer( GLViewer_Viewer* v )
00229 {
00230     myViewer = v;
00231 }
00232 
00236 GLViewer_Viewer* GLViewer_ViewFrame::getViewer() const
00237 {
00238     return myViewer;
00239 }
00240 
00244 QSize GLViewer_ViewFrame::sizeHint() const
00245 {
00246     QWidget* p = parentWidget();
00247     if ( p && p->inherits( "QWorkspaceChild" ) )
00248         p = p->parentWidget();
00249     if ( !p )
00250         return QMainWindow::sizeHint();
00251     return QSize( 9 * p->width() / 10 , 9 * p->height() / 10  );
00252 }
00253 
00257 void GLViewer_ViewFrame::onUpdate( int )
00258 {
00259 }
00260 
00261 //#include <windows.h>
00262 
00267 QImage GLViewer_ViewFrame::dumpView()
00268 {
00269   QImage img;
00270 
00271   GLViewer_Widget* aWidget = ((GLViewer_ViewPort2d*)myVP)->getGLWidget();
00272   if ( aWidget )
00273     img = aWidget->grabFrameBuffer();
00274 
00275   return img;
00276 }
00277 
00281 void GLViewer_ViewFrame::onViewPan()
00282 {
00283     myViewer->activateTransform( GLViewer_Viewer::Pan );
00284 }
00285 
00289 void GLViewer_ViewFrame::onViewZoom()
00290 {
00291     myViewer->activateTransform( GLViewer_Viewer::Zoom );
00292 }
00293 
00297 void GLViewer_ViewFrame::onViewFitAll()
00298 {
00299     myViewer->activateTransform( GLViewer_Viewer::FitAll );
00300 }
00301 
00305 void GLViewer_ViewFrame::onViewFitArea()
00306 {
00307     myViewer->activateTransform( GLViewer_Viewer::FitRect );
00308 }
00309 
00313 void GLViewer_ViewFrame::onViewFitSelect()
00314 {
00315     myViewer->activateTransform( GLViewer_Viewer::FitSelect );
00316 }
00317 
00321 void GLViewer_ViewFrame::onViewGlobalPan()
00322 {
00323     myViewer->activateTransform( GLViewer_Viewer::PanGlobal );
00324 }
00325 
00329 void GLViewer_ViewFrame::onViewRotate()
00330 {
00331     //myViewer->activateTransform( GLViewer_Viewer::Rotate );
00332 }
00333 
00337 void GLViewer_ViewFrame::onViewReset()
00338 {
00339     myViewer->activateTransform( GLViewer_Viewer::Reset );
00340 }
00341 
00345 void GLViewer_ViewFrame::mouseEvent( QMouseEvent* e )
00346 {
00347   switch ( e->type() )
00348   {
00349   case QEvent::MouseButtonPress:
00350     emit mousePressed( this, e );
00351     break;
00352   case QEvent::MouseButtonRelease:
00353     emit mouseReleased( this, e );
00354     break;
00355   case QEvent::MouseButtonDblClick:
00356     emit mouseDoubleClicked( this, e );
00357     break;
00358   case QEvent::MouseMove:
00359     emit mouseMoving( this, e );
00360     break;
00361   default:
00362     break;
00363   }
00364 }
00365 
00369 void GLViewer_ViewFrame::keyEvent( QKeyEvent* e )
00370 {
00371   switch ( e->type() )
00372   {
00373   case QEvent::KeyPress:
00374     emit keyPressed( this, e );
00375     break;
00376   case QEvent::KeyRelease:
00377     emit keyReleased( this, e );
00378     break;
00379   default:
00380     break;
00381   }
00382 }
00383 
00387 void GLViewer_ViewFrame::wheelEvent( QWheelEvent* e )
00388 {
00389   switch ( e->type() )
00390   {
00391   case QEvent::Wheel:
00392     emit wheeling( this, e );
00393     break;
00394   default:
00395     break;
00396   }
00397 }
00398 
00402 QString GLViewer_ViewFrame::getVisualParameters()
00403 {
00404   QString retStr;
00405   if ( myVP && myVP->inherits( "GLViewer_ViewPort2d" ) ) {
00406     GLViewer_ViewPort2d* vp2d = (GLViewer_ViewPort2d*)myVP;
00407     GLfloat xSc, ySc, xPan, yPan;
00408     vp2d->getScale( xSc, ySc );
00409     vp2d->getPan( xPan, yPan );
00410     retStr.sprintf( "%.12e*%.12e*%.12e*%.12e", xSc, ySc, xPan, yPan );
00411   }
00412   return retStr;
00413 }
00414 
00418 void GLViewer_ViewFrame::setVisualParameters( const QString& parameters )
00419 {
00420   QStringList paramsLst = parameters.split( '*' );
00421   if ( myVP && myVP->inherits( "GLViewer_ViewPort2d" ) && paramsLst.size() == 4) {
00422     GLViewer_ViewPort2d* vp2d = (GLViewer_ViewPort2d*)myVP;
00423 
00424     GLfloat xSc, ySc, xPan, yPan;
00425     xSc = paramsLst[0].toDouble();
00426     ySc = paramsLst[1].toDouble();
00427     xPan = paramsLst[2].toDouble();
00428     yPan = paramsLst[3].toDouble();
00429 
00430     vp2d->getGLWidget()->setScale( xSc, ySc, 1. );
00431     vp2d->getGLWidget()->setPan( xPan, yPan, 0. );
00432   }
00433 }