1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "GLViewer_ViewFrame.h"
24 #include "GLViewer_Viewer.h"
25 #include "GLViewer_ViewPort2d.h"
27 #include <QtxToolBar.h>
28 #include <QtxMultiAction.h>
29 #include <QtxActionToolMgr.h>
31 #include <SUIT_Desktop.h>
32 #include <SUIT_Session.h>
33 #include <SUIT_ResourceMgr.h>
34 #include <SUIT_MessageBox.h>
37 #include <QFileDialog>
39 #include <QHBoxLayout>
42 #include <QMouseEvent>
44 #include <QWheelEvent>
47 #include <Standard_Integer.hxx>
54 GLViewer_ViewFrame::GLViewer_ViewFrame( SUIT_Desktop* d, GLViewer_Viewer* vw )
55 : SUIT_ViewWindow( d ),
59 QFrame* client = new QFrame( this );
60 setCentralWidget( client );
62 QBoxLayout* layout = new QHBoxLayout( client );
63 layout->setMargin( 0 );
64 layout->setSpacing( 0 );
66 GLViewer_ViewPort2d* vp = new GLViewer_ViewPort2d( client, this );
68 setBackgroundColor( Qt::white );
69 layout->addWidget( vp );
78 GLViewer_ViewFrame::~GLViewer_ViewFrame()
83 Creates actions of GL view frame
85 void GLViewer_ViewFrame::createActions()
87 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
91 aAction = new QtxAction(tr("MNU_DUMP_VIEW"), aResMgr->loadPixmap( "GLViewer", tr( "ICON_GL_DUMP" ) ),
92 tr( "MNU_DUMP_VIEW" ), 0, this);
93 aAction->setStatusTip(tr("DSC_DUMP_VIEW"));
94 connect(aAction, SIGNAL(activated()), this, SLOT(onDumpView()));
95 toolMgr()->registerAction( aAction, DumpId );
98 aAction = new QtxAction(tr("MNU_FITALL"), aResMgr->loadPixmap( "GLViewer", tr( "ICON_GL_FITALL" ) ),
99 tr( "MNU_FITALL" ), 0, this);
100 aAction->setStatusTip(tr("DSC_FITALL"));
101 connect(aAction, SIGNAL(activated()), this, SLOT(onViewFitAll()));
102 toolMgr()->registerAction( aAction, FitAllId );
105 aAction = new QtxAction(tr("MNU_FITRECT"), aResMgr->loadPixmap( "GLViewer", tr( "ICON_GL_FITAREA" ) ),
106 tr( "MNU_FITRECT" ), 0, this);
107 aAction->setStatusTip(tr("DSC_FITRECT"));
108 connect(aAction, SIGNAL(activated()), this, SLOT(onViewFitArea()));
109 toolMgr()->registerAction( aAction, FitRectId );
112 aAction = new QtxAction(tr("MNU_FITSELECT"), aResMgr->loadPixmap( "GLViewer", tr( "ICON_GL_FITSELECT" ) ),
113 tr( "MNU_FITSELECT" ), 0, this);
114 aAction->setStatusTip(tr("DSC_FITSELECT"));
115 connect(aAction, SIGNAL(activated()), this, SLOT(onViewFitSelect()));
116 toolMgr()->registerAction( aAction, FitSelectId );
119 aAction = new QtxAction(tr("MNU_ZOOM_VIEW"), aResMgr->loadPixmap( "GLViewer", tr( "ICON_GL_ZOOM" ) ),
120 tr( "MNU_ZOOM_VIEW" ), 0, this);
121 aAction->setStatusTip(tr("DSC_ZOOM_VIEW"));
122 connect(aAction, SIGNAL(activated()), this, SLOT(onViewZoom()));
123 toolMgr()->registerAction( aAction, ZoomId );
126 aAction = new QtxAction(tr("MNU_PAN_VIEW"), aResMgr->loadPixmap( "GLViewer", tr( "ICON_GL_PAN" ) ),
127 tr( "MNU_PAN_VIEW" ), 0, this);
128 aAction->setStatusTip(tr("DSC_PAN_VIEW"));
129 connect(aAction, SIGNAL(activated()), this, SLOT(onViewPan()));
130 toolMgr()->registerAction( aAction, PanId );
133 aAction = new QtxAction(tr("MNU_GLOBALPAN_VIEW"), aResMgr->loadPixmap( "GLViewer", tr( "ICON_GL_GLOBALPAN" ) ),
134 tr( "MNU_GLOBALPAN_VIEW" ), 0, this);
135 aAction->setStatusTip(tr("DSC_GLOBALPAN_VIEW"));
136 connect(aAction, SIGNAL(activated()), this, SLOT(onViewGlobalPan()));
137 toolMgr()->registerAction( aAction, GlobalPanId );
139 aAction = new QtxAction(tr("MNU_RESET_VIEW"), aResMgr->loadPixmap( "GLViewer", tr( "ICON_GL_RESET" ) ),
140 tr( "MNU_RESET_VIEW" ), 0, this);
141 aAction->setStatusTip(tr("DSC_RESET_VIEW"));
142 connect(aAction, SIGNAL(activated()), this, SLOT(onViewReset()));
143 toolMgr()->registerAction( aAction, ResetId );
147 Creates toolbar of GL view frame
149 void GLViewer_ViewFrame::createToolBar()
151 int tid = toolMgr()->createToolBar( tr("LBL_TOOLBAR_LABEL"), false );
152 toolMgr()->append( DumpId, tid );
154 QtxMultiAction* aScaleAction = new QtxMultiAction( this );
155 aScaleAction->insertAction( toolMgr()->action( FitAllId ) );
156 aScaleAction->insertAction( toolMgr()->action( FitRectId ) );
157 aScaleAction->insertAction( toolMgr()->action( FitSelectId ) );
158 aScaleAction->insertAction( toolMgr()->action( ZoomId ) );
159 toolMgr()->append( aScaleAction, tid );
161 QtxMultiAction* aPanAction = new QtxMultiAction( this );
162 aPanAction->insertAction( toolMgr()->action( PanId ) );
163 aPanAction->insertAction( toolMgr()->action( GlobalPanId ) );
164 toolMgr()->append( aPanAction, tid );
166 toolMgr()->append( toolMgr()->action( ResetId ), tid );
170 Sets the viewport for this frame
172 void GLViewer_ViewFrame::setViewPort( GLViewer_ViewPort* vp )
179 disconnect( myVP, SIGNAL( vpDrawExternal( QPainter* ) ), this, SIGNAL( vfDrawExternal( QPainter* ) ) );
180 disconnect( myVP, SIGNAL( vpMouseEvent( QMouseEvent* ) ), this, SLOT( mouseEvent( QMouseEvent* ) ) );
181 disconnect( myVP, SIGNAL( vpKeyEvent( QKeyEvent* ) ), this, SLOT( keyEvent( QKeyEvent* ) ) );
182 disconnect( myVP, SIGNAL( vpWheelEvent( QWheelEvent* ) ), this, SLOT( wheelEvent( QWheelEvent* ) ) );
183 disconnect( myVP, SIGNAL( contextMenuRequested( QContextMenuEvent* ) ),
184 this, SIGNAL( contextMenuRequested( QContextMenuEvent* ) ) );
189 connect( myVP, SIGNAL( vpDrawExternal( QPainter* ) ), this, SIGNAL( vfDrawExternal( QPainter* ) ) );
190 connect( myVP, SIGNAL( vpMouseEvent( QMouseEvent* ) ), this, SLOT( mouseEvent( QMouseEvent* ) ) );
191 connect( myVP, SIGNAL( vpKeyEvent( QKeyEvent* ) ), this, SLOT( keyEvent( QKeyEvent* ) ) );
192 connect( myVP, SIGNAL( vpWheelEvent( QWheelEvent* ) ), this, SLOT( wheelEvent( QWheelEvent* ) ) );
193 connect( myVP, SIGNAL( contextMenuRequested( QContextMenuEvent* ) ),
194 this, SIGNAL( contextMenuRequested( QContextMenuEvent* ) ) );
199 Returns the viewport of this frame. [ public ]
201 GLViewer_ViewPort* GLViewer_ViewFrame::getViewPort() const
207 Set background of the viewport. [ public ]
209 void GLViewer_ViewFrame::setBackgroundColor( const QColor& color )
212 myVP->setBackgroundColor( color );
216 Returns background of the viewport. [ public ]
218 QColor GLViewer_ViewFrame::backgroundColor() const
221 return myVP->backgroundColor();
222 return palette().color( backgroundRole() );
226 Sets the viewer for this view. [ public ]
228 void GLViewer_ViewFrame::setViewer( GLViewer_Viewer* v )
234 Returns the viewer of this view. [ public ]
236 GLViewer_Viewer* GLViewer_ViewFrame::getViewer() const
242 Returns the preferred view size. [ virtual public ]
244 QSize GLViewer_ViewFrame::sizeHint() const
246 QWidget* p = parentWidget();
247 if ( p && p->inherits( "QWorkspaceChild" ) )
248 p = p->parentWidget();
250 return QMainWindow::sizeHint();
251 return QSize( 9 * p->width() / 10 , 9 * p->height() / 10 );
255 Called by viewer's 'update()' method. Does nothing by default [ virtual public ]
257 void GLViewer_ViewFrame::onUpdate( int )
261 //#include <windows.h>
264 SLOT: called on dump view operation is activated, stores scene to raster file
267 QImage GLViewer_ViewFrame::dumpView()
271 GLViewer_Widget* aWidget = ((GLViewer_ViewPort2d*)myVP)->getGLWidget();
273 img = aWidget->grabFrameBuffer();
281 void GLViewer_ViewFrame::onViewPan()
283 myViewer->activateTransform( GLViewer_Viewer::Pan );
289 void GLViewer_ViewFrame::onViewZoom()
291 myViewer->activateTransform( GLViewer_Viewer::Zoom );
297 void GLViewer_ViewFrame::onViewFitAll()
299 myViewer->activateTransform( GLViewer_Viewer::FitAll );
305 void GLViewer_ViewFrame::onViewFitArea()
307 myViewer->activateTransform( GLViewer_Viewer::FitRect );
313 void GLViewer_ViewFrame::onViewFitSelect()
315 myViewer->activateTransform( GLViewer_Viewer::FitSelect );
321 void GLViewer_ViewFrame::onViewGlobalPan()
323 myViewer->activateTransform( GLViewer_Viewer::PanGlobal );
329 void GLViewer_ViewFrame::onViewRotate()
331 //myViewer->activateTransform( GLViewer_Viewer::Rotate );
335 Start reset default view aspects
337 void GLViewer_ViewFrame::onViewReset()
339 myViewer->activateTransform( GLViewer_Viewer::Reset );
343 Dispatches mouse events
345 void GLViewer_ViewFrame::mouseEvent( QMouseEvent* e )
349 case QEvent::MouseButtonPress:
350 emit mousePressed( this, e );
352 case QEvent::MouseButtonRelease:
353 emit mouseReleased( this, e );
355 case QEvent::MouseButtonDblClick:
356 emit mouseDoubleClicked( this, e );
358 case QEvent::MouseMove:
359 emit mouseMoving( this, e );
367 Dispatches key events
369 void GLViewer_ViewFrame::keyEvent( QKeyEvent* e )
373 case QEvent::KeyPress:
374 emit keyPressed( this, e );
376 case QEvent::KeyRelease:
377 emit keyReleased( this, e );
385 Dispatches wheel events
387 void GLViewer_ViewFrame::wheelEvent( QWheelEvent* e )
392 emit wheeling( this, e );
400 \return the visual parameters of this view as a formated string
402 QString GLViewer_ViewFrame::getVisualParameters()
405 if ( myVP && myVP->inherits( "GLViewer_ViewPort2d" ) ) {
406 GLViewer_ViewPort2d* vp2d = (GLViewer_ViewPort2d*)myVP;
407 GLfloat xSc, ySc, xPan, yPan;
408 vp2d->getScale( xSc, ySc );
409 vp2d->getPan( xPan, yPan );
410 retStr.sprintf( "%.12e*%.12e*%.12e*%.12e", xSc, ySc, xPan, yPan );
416 The method restores visual parameters of this view from a formated string
418 void GLViewer_ViewFrame::setVisualParameters( const QString& parameters )
420 QStringList paramsLst = parameters.split( '*' );
421 if ( myVP && myVP->inherits( "GLViewer_ViewPort2d" ) && paramsLst.size() == 4) {
422 GLViewer_ViewPort2d* vp2d = (GLViewer_ViewPort2d*)myVP;
424 GLfloat xSc, ySc, xPan, yPan;
425 xSc = paramsLst[0].toDouble();
426 ySc = paramsLst[1].toDouble();
427 xPan = paramsLst[2].toDouble();
428 yPan = paramsLst[3].toDouble();
430 vp2d->getGLWidget()->setScale( xSc, ySc, 1. );
431 vp2d->getGLWidget()->setPan( xPan, yPan, 0. );