1 // Copyright (C) 2007-2008 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
22 // Author : OPEN CASCADE
23 // File: GLViewer_ViewFrame.cxx
24 // Created: November, 2004
25 //#include <GLViewerAfx.h>
27 #include "GLViewer_ViewFrame.h"
28 #include "GLViewer_Viewer.h"
29 #include "GLViewer_ViewPort2d.h"
31 #include <QtxToolBar.h>
32 #include <QtxMultiAction.h>
33 #include <QtxActionToolMgr.h>
35 #include <SUIT_Desktop.h>
36 #include <SUIT_Session.h>
37 #include <SUIT_ResourceMgr.h>
38 #include <SUIT_MessageBox.h>
41 #include <QFileDialog>
43 #include <QHBoxLayout>
46 #include <QMouseEvent>
48 #include <QWheelEvent>
51 #include <Standard_Integer.hxx>
59 GLViewer_ViewFrame::GLViewer_ViewFrame( SUIT_Desktop* d, GLViewer_Viewer* vw )
60 : SUIT_ViewWindow( d ),
64 QFrame* client = new QFrame( this );
65 setCentralWidget( client );
67 QBoxLayout* layout = new QHBoxLayout( client );
68 layout->setMargin( 0 );
69 layout->setSpacing( 0 );
71 GLViewer_ViewPort2d* vp = new GLViewer_ViewPort2d( client, this );
73 setBackgroundColor( Qt::white );
74 layout->addWidget( vp );
83 GLViewer_ViewFrame::~GLViewer_ViewFrame()
88 Creates actions of GL view frame
90 void GLViewer_ViewFrame::createActions()
92 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
96 aAction = new QtxAction(tr("MNU_DUMP_VIEW"), aResMgr->loadPixmap( "GLViewer", tr( "ICON_GL_DUMP" ) ),
97 tr( "MNU_DUMP_VIEW" ), 0, this);
98 aAction->setStatusTip(tr("DSC_DUMP_VIEW"));
99 connect(aAction, SIGNAL(activated()), this, SLOT(onDumpView()));
100 toolMgr()->registerAction( aAction, DumpId );
103 aAction = new QtxAction(tr("MNU_FITALL"), aResMgr->loadPixmap( "GLViewer", tr( "ICON_GL_FITALL" ) ),
104 tr( "MNU_FITALL" ), 0, this);
105 aAction->setStatusTip(tr("DSC_FITALL"));
106 connect(aAction, SIGNAL(activated()), this, SLOT(onViewFitAll()));
107 toolMgr()->registerAction( aAction, FitAllId );
110 aAction = new QtxAction(tr("MNU_FITRECT"), aResMgr->loadPixmap( "GLViewer", tr( "ICON_GL_FITAREA" ) ),
111 tr( "MNU_FITRECT" ), 0, this);
112 aAction->setStatusTip(tr("DSC_FITRECT"));
113 connect(aAction, SIGNAL(activated()), this, SLOT(onViewFitArea()));
114 toolMgr()->registerAction( aAction, FitRectId );
117 aAction = new QtxAction(tr("MNU_FITSELECT"), aResMgr->loadPixmap( "GLViewer", tr( "ICON_GL_FITSELECT" ) ),
118 tr( "MNU_FITSELECT" ), 0, this);
119 aAction->setStatusTip(tr("DSC_FITSELECT"));
120 connect(aAction, SIGNAL(activated()), this, SLOT(onViewFitSelect()));
121 toolMgr()->registerAction( aAction, FitSelectId );
124 aAction = new QtxAction(tr("MNU_ZOOM_VIEW"), aResMgr->loadPixmap( "GLViewer", tr( "ICON_GL_ZOOM" ) ),
125 tr( "MNU_ZOOM_VIEW" ), 0, this);
126 aAction->setStatusTip(tr("DSC_ZOOM_VIEW"));
127 connect(aAction, SIGNAL(activated()), this, SLOT(onViewZoom()));
128 toolMgr()->registerAction( aAction, ZoomId );
131 aAction = new QtxAction(tr("MNU_PAN_VIEW"), aResMgr->loadPixmap( "GLViewer", tr( "ICON_GL_PAN" ) ),
132 tr( "MNU_PAN_VIEW" ), 0, this);
133 aAction->setStatusTip(tr("DSC_PAN_VIEW"));
134 connect(aAction, SIGNAL(activated()), this, SLOT(onViewPan()));
135 toolMgr()->registerAction( aAction, PanId );
138 aAction = new QtxAction(tr("MNU_GLOBALPAN_VIEW"), aResMgr->loadPixmap( "GLViewer", tr( "ICON_GL_GLOBALPAN" ) ),
139 tr( "MNU_GLOBALPAN_VIEW" ), 0, this);
140 aAction->setStatusTip(tr("DSC_GLOBALPAN_VIEW"));
141 connect(aAction, SIGNAL(activated()), this, SLOT(onViewGlobalPan()));
142 toolMgr()->registerAction( aAction, GlobalPanId );
144 aAction = new QtxAction(tr("MNU_RESET_VIEW"), aResMgr->loadPixmap( "GLViewer", tr( "ICON_GL_RESET" ) ),
145 tr( "MNU_RESET_VIEW" ), 0, this);
146 aAction->setStatusTip(tr("DSC_RESET_VIEW"));
147 connect(aAction, SIGNAL(activated()), this, SLOT(onViewReset()));
148 toolMgr()->registerAction( aAction, ResetId );
152 Creates toolbar of GL view frame
154 void GLViewer_ViewFrame::createToolBar()
156 int tid = toolMgr()->createToolBar( tr("LBL_TOOLBAR_LABEL") );
157 toolMgr()->append( DumpId, tid );
159 QtxMultiAction* aScaleAction = new QtxMultiAction( this );
160 aScaleAction->insertAction( toolMgr()->action( FitAllId ) );
161 aScaleAction->insertAction( toolMgr()->action( FitRectId ) );
162 aScaleAction->insertAction( toolMgr()->action( FitSelectId ) );
163 aScaleAction->insertAction( toolMgr()->action( ZoomId ) );
164 toolMgr()->append( aScaleAction, tid );
166 QtxMultiAction* aPanAction = new QtxMultiAction( this );
167 aPanAction->insertAction( toolMgr()->action( PanId ) );
168 aPanAction->insertAction( toolMgr()->action( GlobalPanId ) );
169 toolMgr()->append( aPanAction, tid );
171 toolMgr()->append( toolMgr()->action( ResetId ), tid );
175 Sets the viewport for this frame
177 void GLViewer_ViewFrame::setViewPort( GLViewer_ViewPort* vp )
184 disconnect( myVP, SIGNAL( vpDrawExternal( QPainter* ) ), this, SIGNAL( vfDrawExternal( QPainter* ) ) );
185 disconnect( myVP, SIGNAL( vpMouseEvent( QMouseEvent* ) ), this, SLOT( mouseEvent( QMouseEvent* ) ) );
186 disconnect( myVP, SIGNAL( vpKeyEvent( QKeyEvent* ) ), this, SLOT( keyEvent( QKeyEvent* ) ) );
187 disconnect( myVP, SIGNAL( vpWheelEvent( QWheelEvent* ) ), this, SLOT( wheelEvent( QWheelEvent* ) ) );
188 disconnect( myVP, SIGNAL( contextMenuRequested( QContextMenuEvent* ) ),
189 this, SIGNAL( contextMenuRequested( QContextMenuEvent* ) ) );
194 connect( myVP, SIGNAL( vpDrawExternal( QPainter* ) ), this, SIGNAL( vfDrawExternal( QPainter* ) ) );
195 connect( myVP, SIGNAL( vpMouseEvent( QMouseEvent* ) ), this, SLOT( mouseEvent( QMouseEvent* ) ) );
196 connect( myVP, SIGNAL( vpKeyEvent( QKeyEvent* ) ), this, SLOT( keyEvent( QKeyEvent* ) ) );
197 connect( myVP, SIGNAL( vpWheelEvent( QWheelEvent* ) ), this, SLOT( wheelEvent( QWheelEvent* ) ) );
198 connect( myVP, SIGNAL( contextMenuRequested( QContextMenuEvent* ) ),
199 this, SIGNAL( contextMenuRequested( QContextMenuEvent* ) ) );
204 Returns the viewport of this frame. [ public ]
206 GLViewer_ViewPort* GLViewer_ViewFrame::getViewPort() const
212 Set background of the viewport. [ public ]
214 void GLViewer_ViewFrame::setBackgroundColor( const QColor& color )
217 myVP->setBackgroundColor( color );
221 Returns background of the viewport. [ public ]
223 QColor GLViewer_ViewFrame::backgroundColor() const
226 return myVP->backgroundColor();
227 return palette().color( backgroundRole() );
231 Sets the viewer for this view. [ public ]
233 void GLViewer_ViewFrame::setViewer( GLViewer_Viewer* v )
239 Returns the viewer of this view. [ public ]
241 GLViewer_Viewer* GLViewer_ViewFrame::getViewer() const
247 Returns the preferred view size. [ virtual public ]
249 QSize GLViewer_ViewFrame::sizeHint() const
251 QWidget* p = parentWidget();
252 if ( p && p->inherits( "QWorkspaceChild" ) )
253 p = p->parentWidget();
255 return QMainWindow::sizeHint();
256 return QSize( 9 * p->width() / 10 , 9 * p->height() / 10 );
260 Called by viewer's 'update()' method. Does nothing by default [ virtual public ]
262 void GLViewer_ViewFrame::onUpdate( int )
266 //#include <windows.h>
269 SLOT: called on dump view operation is activated, stores scene to raster file
272 QImage GLViewer_ViewFrame::dumpView()
276 GLViewer_Widget* aWidget = ((GLViewer_ViewPort2d*)myVP)->getGLWidget();
278 img = aWidget->grabFrameBuffer();
286 void GLViewer_ViewFrame::onViewPan()
288 myViewer->activateTransform( GLViewer_Viewer::Pan );
294 void GLViewer_ViewFrame::onViewZoom()
296 myViewer->activateTransform( GLViewer_Viewer::Zoom );
302 void GLViewer_ViewFrame::onViewFitAll()
304 myViewer->activateTransform( GLViewer_Viewer::FitAll );
310 void GLViewer_ViewFrame::onViewFitArea()
312 myViewer->activateTransform( GLViewer_Viewer::FitRect );
318 void GLViewer_ViewFrame::onViewFitSelect()
320 myViewer->activateTransform( GLViewer_Viewer::FitSelect );
326 void GLViewer_ViewFrame::onViewGlobalPan()
328 myViewer->activateTransform( GLViewer_Viewer::PanGlobal );
334 void GLViewer_ViewFrame::onViewRotate()
336 //myViewer->activateTransform( GLViewer_Viewer::Rotate );
340 Start reset default view aspects
342 void GLViewer_ViewFrame::onViewReset()
344 myViewer->activateTransform( GLViewer_Viewer::Reset );
348 Dispatches mouse events
350 void GLViewer_ViewFrame::mouseEvent( QMouseEvent* e )
354 case QEvent::MouseButtonPress:
355 emit mousePressed( this, e );
357 case QEvent::MouseButtonRelease:
358 emit mouseReleased( this, e );
360 case QEvent::MouseButtonDblClick:
361 emit mouseDoubleClicked( this, e );
363 case QEvent::MouseMove:
364 emit mouseMoving( this, e );
372 Dispatches key events
374 void GLViewer_ViewFrame::keyEvent( QKeyEvent* e )
378 case QEvent::KeyPress:
379 emit keyPressed( this, e );
381 case QEvent::KeyRelease:
382 emit keyReleased( this, e );
390 Dispatches wheel events
392 void GLViewer_ViewFrame::wheelEvent( QWheelEvent* e )
397 emit wheeling( this, e );
405 \return the visual parameters of this view as a formated string
407 QString GLViewer_ViewFrame::getVisualParameters()
410 if ( myVP && myVP->inherits( "GLViewer_ViewPort2d" ) ) {
411 GLViewer_ViewPort2d* vp2d = (GLViewer_ViewPort2d*)myVP;
412 GLfloat xSc, ySc, xPan, yPan;
413 vp2d->getScale( xSc, ySc );
414 vp2d->getPan( xPan, yPan );
415 retStr.sprintf( "%.12e*%.12e*%.12e*%.12e", xSc, ySc, xPan, yPan );
421 The method restores visual parameters of this view from a formated string
423 void GLViewer_ViewFrame::setVisualParameters( const QString& parameters )
425 QStringList paramsLst = parameters.split( '*' );
426 if ( myVP && myVP->inherits( "GLViewer_ViewPort2d" ) && paramsLst.size() == 4) {
427 GLViewer_ViewPort2d* vp2d = (GLViewer_ViewPort2d*)myVP;
429 GLfloat xSc, ySc, xPan, yPan;
430 xSc = paramsLst[0].toDouble();
431 ySc = paramsLst[1].toDouble();
432 xPan = paramsLst[2].toDouble();
433 yPan = paramsLst[3].toDouble();
435 vp2d->getGLWidget()->setScale( xSc, ySc, 1. );
436 vp2d->getGLWidget()->setPan( xPan, yPan, 0. );