Salome HOME
updated copyright message
[modules/gui.git] / src / GLViewer / GLViewer_ViewFrame.cxx
1 // Copyright (C) 2007-2023  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "GLViewer_ViewFrame.h"
24 #include "GLViewer_Viewer.h"
25 #include "GLViewer_ViewPort2d.h"
26
27 #include <QtxToolBar.h>
28 #include <QtxMultiAction.h>
29 #include <QtxActionToolMgr.h>
30
31 #include <SUIT_Desktop.h>
32 #include <SUIT_Session.h>
33 #include <SUIT_ResourceMgr.h>
34 #include <SUIT_MessageBox.h>
35
36 #include <QColor>
37 #include <QFileDialog>
38 #include <QImage>
39 #include <QHBoxLayout>
40 #include <QString>
41 #include <QFrame>
42 #include <QMouseEvent>
43 #include <QKeyEvent>
44 #include <QWheelEvent>
45
46 #ifdef WIN32
47 #include <Standard_Integer.hxx>
48 #include <iostream>
49 #endif
50
51 /*!
52     Constructor
53 */
54 GLViewer_ViewFrame::GLViewer_ViewFrame( SUIT_Desktop* d, GLViewer_Viewer* vw )
55 : SUIT_ViewWindow( d ),
56 myViewer( vw ),
57 myVP( 0 )
58 {
59     QFrame* client = new QFrame( this );
60     setCentralWidget( client );
61
62     QBoxLayout* layout = new QHBoxLayout( client );
63     layout->setMargin( 0 );
64     layout->setSpacing( 0 );
65
66     GLViewer_ViewPort2d* vp = new GLViewer_ViewPort2d( client, this );
67     setViewPort( vp );
68     setBackgroundColor( Qt::white );
69     layout->addWidget( vp );
70
71     createActions();
72     createToolBar();
73 }
74
75 /*!
76     Destructor
77 */
78 GLViewer_ViewFrame::~GLViewer_ViewFrame()
79 {
80 }
81
82 /*!
83   Creates actions of GL view frame
84 */
85 void GLViewer_ViewFrame::createActions()
86 {
87   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
88   QtxAction* aAction;
89
90   // Dump view
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 );
96
97   // FitAll
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 );
103
104   // FitRect
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 );
110
111   // FitSelect
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 );
117
118   // Zoom
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 );
124
125   // Panning
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 );
131
132   // Global Panning
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 );
138
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 );
144 }
145
146 /*!
147   Creates toolbar of GL view frame
148 */
149 void GLViewer_ViewFrame::createToolBar()
150 {
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 );
155
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 );
162
163   QtxMultiAction* aPanAction = new QtxMultiAction( this );
164   aPanAction->insertAction( toolMgr()->action( PanId ) );
165   aPanAction->insertAction( toolMgr()->action( GlobalPanId ) );
166   toolMgr()->append( aPanAction, tid );
167
168   toolMgr()->append( toolMgr()->action( ResetId ), tid );
169 }
170
171 /*!
172     Sets the viewport for this frame
173 */
174 void GLViewer_ViewFrame::setViewPort( GLViewer_ViewPort* vp )
175 {
176     if ( myVP == vp )
177         return;
178
179     if ( myVP )
180     {
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* ) ) );
187     }
188     myVP = vp;
189     if ( myVP )
190     {
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* ) ) );
197     }
198 }
199
200 /*!
201     Returns the viewport of this frame. [ public ]
202 */
203 GLViewer_ViewPort* GLViewer_ViewFrame::getViewPort() const
204 {
205     return myVP;
206 }
207
208 /*!
209     Set background of the viewport. [ public ]
210 */
211 void GLViewer_ViewFrame::setBackgroundColor( const QColor& color )
212 {
213     if ( myVP )
214         myVP->setBackgroundColor( color );
215 }
216
217 /*!
218     Returns background of the viewport. [ public ]
219 */
220 QColor GLViewer_ViewFrame::backgroundColor() const
221 {
222     if ( myVP )
223         return myVP->backgroundColor();
224     return palette().color( backgroundRole() );
225 }
226
227 /*!
228     Sets the viewer for this view. [ public ]
229 */
230 void GLViewer_ViewFrame::setViewer( GLViewer_Viewer* v )
231 {
232     myViewer = v;
233 }
234
235 /*!
236     Returns the viewer of this view. [ public ]
237 */
238 GLViewer_Viewer* GLViewer_ViewFrame::getViewer() const
239 {
240     return myViewer;
241 }
242
243 /*!
244     Returns the preferred view size. [ virtual public ]
245 */
246 QSize GLViewer_ViewFrame::sizeHint() const
247 {
248     QWidget* p = parentWidget();
249     if ( p && p->inherits( "QWorkspaceChild" ) )
250         p = p->parentWidget();
251     if ( !p )
252         return QMainWindow::sizeHint();
253     return QSize( 9 * p->width() / 10 , 9 * p->height() / 10  );
254 }
255
256 /*!
257    Called by viewer's 'update()' method. Does nothing by default [ virtual public ]
258 */
259 void GLViewer_ViewFrame::onUpdate( int )
260 {
261 }
262
263 //#include <windows.h>
264
265 /*!
266   SLOT: called on dump view operation is activated, stores scene to raster file
267 */
268
269 QImage GLViewer_ViewFrame::dumpView()
270 {
271   QImage img;
272
273   GLViewer_Widget* aWidget = ((GLViewer_ViewPort2d*)myVP)->getGLWidget();
274   if ( aWidget )
275     img = aWidget->grabFrameBuffer();
276
277   return img;
278 }
279
280 /*!
281   Start panning
282 */
283 void GLViewer_ViewFrame::onViewPan()
284 {
285     myViewer->activateTransform( GLViewer_Viewer::Pan );
286 }
287
288 /*!
289   Start zooming
290 */
291 void GLViewer_ViewFrame::onViewZoom()
292 {
293     myViewer->activateTransform( GLViewer_Viewer::Zoom );
294 }
295
296 /*!
297   Start fit all
298 */
299 void GLViewer_ViewFrame::onViewFitAll()
300 {
301     myViewer->activateTransform( GLViewer_Viewer::FitAll );
302 }
303
304 /*!
305   Start fit area
306 */
307 void GLViewer_ViewFrame::onViewFitArea()
308 {
309     myViewer->activateTransform( GLViewer_Viewer::FitRect );
310 }
311
312 /*!
313   Start fit selected
314 */
315 void GLViewer_ViewFrame::onViewFitSelect()
316 {
317     myViewer->activateTransform( GLViewer_Viewer::FitSelect );
318 }
319
320 /*!
321   Start global panning
322 */
323 void GLViewer_ViewFrame::onViewGlobalPan()
324 {
325     myViewer->activateTransform( GLViewer_Viewer::PanGlobal );
326 }
327
328 /*!
329   Start rotating
330 */
331 void GLViewer_ViewFrame::onViewRotate()
332 {
333     //myViewer->activateTransform( GLViewer_Viewer::Rotate );
334 }
335
336 /*!
337   Start reset default view aspects
338 */
339 void GLViewer_ViewFrame::onViewReset()
340 {
341     myViewer->activateTransform( GLViewer_Viewer::Reset );
342 }
343
344 /*!
345   Dispatches mouse events
346 */
347 void GLViewer_ViewFrame::mouseEvent( QMouseEvent* e )
348 {
349   switch ( e->type() )
350   {
351   case QEvent::MouseButtonPress:
352     emit mousePressed( this, e );
353     break;
354   case QEvent::MouseButtonRelease:
355     emit mouseReleased( this, e );
356     break;
357   case QEvent::MouseButtonDblClick:
358     emit mouseDoubleClicked( this, e );
359     break;
360   case QEvent::MouseMove:
361     emit mouseMoving( this, e );
362     break;
363   default:
364     break;
365   }
366 }
367
368 /*!
369   Dispatches key events
370 */
371 void GLViewer_ViewFrame::keyEvent( QKeyEvent* e )
372 {
373   switch ( e->type() )
374   {
375   case QEvent::KeyPress:
376     emit keyPressed( this, e );
377     break;
378   case QEvent::KeyRelease:
379     emit keyReleased( this, e );
380     break;
381   default:
382     break;
383   }
384 }
385
386 /*!
387   Dispatches wheel events
388 */
389 void GLViewer_ViewFrame::wheelEvent( QWheelEvent* e )
390 {
391   switch ( e->type() )
392   {
393   case QEvent::Wheel:
394     emit wheeling( this, e );
395     break;
396   default:
397     break;
398   }
399 }
400
401 /*!
402   \return the visual parameters of this view as a formated string
403 */
404 QString GLViewer_ViewFrame::getVisualParameters()
405 {
406   QString retStr;
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 );
413   }
414   return retStr;
415 }
416
417 /*!
418   The method restores visual parameters of this view from a formated string
419 */
420 void GLViewer_ViewFrame::setVisualParameters( const QString& parameters )
421 {
422   QStringList paramsLst = parameters.split( '*' );
423   if ( myVP && myVP->inherits( "GLViewer_ViewPort2d" ) && paramsLst.size() == 4) {
424     GLViewer_ViewPort2d* vp2d = (GLViewer_ViewPort2d*)myVP;
425
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();
431
432     vp2d->getGLWidget()->setScale( xSc, ySc, 1. );
433     vp2d->getGLWidget()->setPan( xPan, yPan, 0. );
434   }
435 }