1 // Copyright (C) 2007-2023 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, or (at your option) any later version.
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(triggered()), 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(triggered()), 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(triggered()), 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(triggered()), 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(triggered()), 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(triggered()), 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(triggered()), 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(triggered()), 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" ), // title (language-dependant)
152 QString( "GLViewerViewOperations" ), // name (language-independant)
153 false ); // disable floatable toolbar
154 toolMgr()->append( DumpId, tid );
156 QtxMultiAction* aScaleAction = new QtxMultiAction( this );
157 aScaleAction->insertAction( toolMgr()->action( FitAllId ) );
158 aScaleAction->insertAction( toolMgr()->action( FitRectId ) );
159 aScaleAction->insertAction( toolMgr()->action( FitSelectId ) );
160 aScaleAction->insertAction( toolMgr()->action( ZoomId ) );
161 toolMgr()->append( aScaleAction, tid );
163 QtxMultiAction* aPanAction = new QtxMultiAction( this );
164 aPanAction->insertAction( toolMgr()->action( PanId ) );
165 aPanAction->insertAction( toolMgr()->action( GlobalPanId ) );
166 toolMgr()->append( aPanAction, tid );
168 toolMgr()->append( toolMgr()->action( ResetId ), tid );
172 Sets the viewport for this frame
174 void GLViewer_ViewFrame::setViewPort( GLViewer_ViewPort* vp )
181 disconnect( myVP, SIGNAL( vpDrawExternal( QPainter* ) ), this, SIGNAL( vfDrawExternal( QPainter* ) ) );
182 disconnect( myVP, SIGNAL( vpMouseEvent( QMouseEvent* ) ), this, SLOT( mouseEvent( QMouseEvent* ) ) );
183 disconnect( myVP, SIGNAL( vpKeyEvent( QKeyEvent* ) ), this, SLOT( keyEvent( QKeyEvent* ) ) );
184 disconnect( myVP, SIGNAL( vpWheelEvent( QWheelEvent* ) ), this, SLOT( wheelEvent( QWheelEvent* ) ) );
185 disconnect( myVP, SIGNAL( contextMenuRequested( QContextMenuEvent* ) ),
186 this, SIGNAL( contextMenuRequested( QContextMenuEvent* ) ) );
191 connect( myVP, SIGNAL( vpDrawExternal( QPainter* ) ), this, SIGNAL( vfDrawExternal( QPainter* ) ) );
192 connect( myVP, SIGNAL( vpMouseEvent( QMouseEvent* ) ), this, SLOT( mouseEvent( QMouseEvent* ) ) );
193 connect( myVP, SIGNAL( vpKeyEvent( QKeyEvent* ) ), this, SLOT( keyEvent( QKeyEvent* ) ) );
194 connect( myVP, SIGNAL( vpWheelEvent( QWheelEvent* ) ), this, SLOT( wheelEvent( QWheelEvent* ) ) );
195 connect( myVP, SIGNAL( contextMenuRequested( QContextMenuEvent* ) ),
196 this, SIGNAL( contextMenuRequested( QContextMenuEvent* ) ) );
201 Returns the viewport of this frame. [ public ]
203 GLViewer_ViewPort* GLViewer_ViewFrame::getViewPort() const
209 Set background of the viewport. [ public ]
211 void GLViewer_ViewFrame::setBackgroundColor( const QColor& color )
214 myVP->setBackgroundColor( color );
218 Returns background of the viewport. [ public ]
220 QColor GLViewer_ViewFrame::backgroundColor() const
223 return myVP->backgroundColor();
224 return palette().color( backgroundRole() );
228 Sets the viewer for this view. [ public ]
230 void GLViewer_ViewFrame::setViewer( GLViewer_Viewer* v )
236 Returns the viewer of this view. [ public ]
238 GLViewer_Viewer* GLViewer_ViewFrame::getViewer() const
244 Returns the preferred view size. [ virtual public ]
246 QSize GLViewer_ViewFrame::sizeHint() const
248 QWidget* p = parentWidget();
249 if ( p && p->inherits( "QWorkspaceChild" ) )
250 p = p->parentWidget();
252 return QMainWindow::sizeHint();
253 return QSize( 9 * p->width() / 10 , 9 * p->height() / 10 );
257 Called by viewer's 'update()' method. Does nothing by default [ virtual public ]
259 void GLViewer_ViewFrame::onUpdate( int )
263 //#include <windows.h>
266 SLOT: called on dump view operation is activated, stores scene to raster file
269 QImage GLViewer_ViewFrame::dumpView()
273 GLViewer_Widget* aWidget = ((GLViewer_ViewPort2d*)myVP)->getGLWidget();
275 img = aWidget->grabFrameBuffer();
283 void GLViewer_ViewFrame::onViewPan()
285 myViewer->activateTransform( GLViewer_Viewer::Pan );
291 void GLViewer_ViewFrame::onViewZoom()
293 myViewer->activateTransform( GLViewer_Viewer::Zoom );
299 void GLViewer_ViewFrame::onViewFitAll()
301 myViewer->activateTransform( GLViewer_Viewer::FitAll );
307 void GLViewer_ViewFrame::onViewFitArea()
309 myViewer->activateTransform( GLViewer_Viewer::FitRect );
315 void GLViewer_ViewFrame::onViewFitSelect()
317 myViewer->activateTransform( GLViewer_Viewer::FitSelect );
323 void GLViewer_ViewFrame::onViewGlobalPan()
325 myViewer->activateTransform( GLViewer_Viewer::PanGlobal );
331 void GLViewer_ViewFrame::onViewRotate()
333 //myViewer->activateTransform( GLViewer_Viewer::Rotate );
337 Start reset default view aspects
339 void GLViewer_ViewFrame::onViewReset()
341 myViewer->activateTransform( GLViewer_Viewer::Reset );
345 Dispatches mouse events
347 void GLViewer_ViewFrame::mouseEvent( QMouseEvent* e )
351 case QEvent::MouseButtonPress:
352 emit mousePressed( this, e );
354 case QEvent::MouseButtonRelease:
355 emit mouseReleased( this, e );
357 case QEvent::MouseButtonDblClick:
358 emit mouseDoubleClicked( this, e );
360 case QEvent::MouseMove:
361 emit mouseMoving( this, e );
369 Dispatches key events
371 void GLViewer_ViewFrame::keyEvent( QKeyEvent* e )
375 case QEvent::KeyPress:
376 emit keyPressed( this, e );
378 case QEvent::KeyRelease:
379 emit keyReleased( this, e );
387 Dispatches wheel events
389 void GLViewer_ViewFrame::wheelEvent( QWheelEvent* e )
394 emit wheeling( this, e );
402 \return the visual parameters of this view as a formated string
404 QString GLViewer_ViewFrame::getVisualParameters()
407 if ( myVP && myVP->inherits( "GLViewer_ViewPort2d" ) ) {
408 GLViewer_ViewPort2d* vp2d = (GLViewer_ViewPort2d*)myVP;
409 GLfloat xSc, ySc, xPan, yPan;
410 vp2d->getScale( xSc, ySc );
411 vp2d->getPan( xPan, yPan );
412 retStr.sprintf( "%.12e*%.12e*%.12e*%.12e", xSc, ySc, xPan, yPan );
418 The method restores visual parameters of this view from a formated string
420 void GLViewer_ViewFrame::setVisualParameters( const QString& parameters )
422 QStringList paramsLst = parameters.split( '*' );
423 if ( myVP && myVP->inherits( "GLViewer_ViewPort2d" ) && paramsLst.size() == 4) {
424 GLViewer_ViewPort2d* vp2d = (GLViewer_ViewPort2d*)myVP;
426 GLfloat xSc, ySc, xPan, yPan;
427 xSc = paramsLst[0].toDouble();
428 ySc = paramsLst[1].toDouble();
429 xPan = paramsLst[2].toDouble();
430 yPan = paramsLst[3].toDouble();
432 vp2d->getGLWidget()->setScale( xSc, ySc, 1. );
433 vp2d->getGLWidget()->setPan( xPan, yPan, 0. );