Salome HOME
Synchronize adm files
[modules/gui.git] / src / GLViewer / GLViewer_ViewFrame.cxx
1 // Copyright (C) 2007-2014  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(activated()), 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(activated()), 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(activated()), 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(activated()), 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(activated()), 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(activated()), 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(activated()), 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(activated()), 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"), false );
152   toolMgr()->append( DumpId, tid );
153
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 );
160
161   QtxMultiAction* aPanAction = new QtxMultiAction( this );
162   aPanAction->insertAction( toolMgr()->action( PanId ) );
163   aPanAction->insertAction( toolMgr()->action( GlobalPanId ) );
164   toolMgr()->append( aPanAction, tid );
165
166   toolMgr()->append( toolMgr()->action( ResetId ), tid );
167 }
168
169 /*!
170     Sets the viewport for this frame
171 */
172 void GLViewer_ViewFrame::setViewPort( GLViewer_ViewPort* vp )
173 {
174     if ( myVP == vp )
175         return;
176
177     if ( myVP )
178     {
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* ) ) );
185     }
186     myVP = vp;
187     if ( myVP )
188     {
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* ) ) );
195     }
196 }
197
198 /*!
199     Returns the viewport of this frame. [ public ]
200 */
201 GLViewer_ViewPort* GLViewer_ViewFrame::getViewPort() const
202 {
203     return myVP;
204 }
205
206 /*!
207     Set background of the viewport. [ public ]
208 */
209 void GLViewer_ViewFrame::setBackgroundColor( const QColor& color )
210 {
211     if ( myVP )
212         myVP->setBackgroundColor( color );
213 }
214
215 /*!
216     Returns background of the viewport. [ public ]
217 */
218 QColor GLViewer_ViewFrame::backgroundColor() const
219 {
220     if ( myVP )
221         return myVP->backgroundColor();
222     return palette().color( backgroundRole() );
223 }
224
225 /*!
226     Sets the viewer for this view. [ public ]
227 */
228 void GLViewer_ViewFrame::setViewer( GLViewer_Viewer* v )
229 {
230     myViewer = v;
231 }
232
233 /*!
234     Returns the viewer of this view. [ public ]
235 */
236 GLViewer_Viewer* GLViewer_ViewFrame::getViewer() const
237 {
238     return myViewer;
239 }
240
241 /*!
242     Returns the preferred view size. [ virtual public ]
243 */
244 QSize GLViewer_ViewFrame::sizeHint() const
245 {
246     QWidget* p = parentWidget();
247     if ( p && p->inherits( "QWorkspaceChild" ) )
248         p = p->parentWidget();
249     if ( !p )
250         return QMainWindow::sizeHint();
251     return QSize( 9 * p->width() / 10 , 9 * p->height() / 10  );
252 }
253
254 /*!
255    Called by viewer's 'update()' method. Does nothing by default [ virtual public ]
256 */
257 void GLViewer_ViewFrame::onUpdate( int )
258 {
259 }
260
261 //#include <windows.h>
262
263 /*!
264   SLOT: called on dump view operation is activated, stores scene to raster file
265 */
266
267 QImage GLViewer_ViewFrame::dumpView()
268 {
269   QImage img;
270
271   GLViewer_Widget* aWidget = ((GLViewer_ViewPort2d*)myVP)->getGLWidget();
272   if ( aWidget )
273     img = aWidget->grabFrameBuffer();
274
275   return img;
276 }
277
278 /*!
279   Start panning
280 */
281 void GLViewer_ViewFrame::onViewPan()
282 {
283     myViewer->activateTransform( GLViewer_Viewer::Pan );
284 }
285
286 /*!
287   Start zooming
288 */
289 void GLViewer_ViewFrame::onViewZoom()
290 {
291     myViewer->activateTransform( GLViewer_Viewer::Zoom );
292 }
293
294 /*!
295   Start fit all
296 */
297 void GLViewer_ViewFrame::onViewFitAll()
298 {
299     myViewer->activateTransform( GLViewer_Viewer::FitAll );
300 }
301
302 /*!
303   Start fit area
304 */
305 void GLViewer_ViewFrame::onViewFitArea()
306 {
307     myViewer->activateTransform( GLViewer_Viewer::FitRect );
308 }
309
310 /*!
311   Start fit selected
312 */
313 void GLViewer_ViewFrame::onViewFitSelect()
314 {
315     myViewer->activateTransform( GLViewer_Viewer::FitSelect );
316 }
317
318 /*!
319   Start global panning
320 */
321 void GLViewer_ViewFrame::onViewGlobalPan()
322 {
323     myViewer->activateTransform( GLViewer_Viewer::PanGlobal );
324 }
325
326 /*!
327   Start rotating
328 */
329 void GLViewer_ViewFrame::onViewRotate()
330 {
331     //myViewer->activateTransform( GLViewer_Viewer::Rotate );
332 }
333
334 /*!
335   Start reset default view aspects
336 */
337 void GLViewer_ViewFrame::onViewReset()
338 {
339     myViewer->activateTransform( GLViewer_Viewer::Reset );
340 }
341
342 /*!
343   Dispatches mouse events
344 */
345 void GLViewer_ViewFrame::mouseEvent( QMouseEvent* e )
346 {
347   switch ( e->type() )
348   {
349   case QEvent::MouseButtonPress:
350     emit mousePressed( this, e );
351     break;
352   case QEvent::MouseButtonRelease:
353     emit mouseReleased( this, e );
354     break;
355   case QEvent::MouseButtonDblClick:
356     emit mouseDoubleClicked( this, e );
357     break;
358   case QEvent::MouseMove:
359     emit mouseMoving( this, e );
360     break;
361   default:
362     break;
363   }
364 }
365
366 /*!
367   Dispatches key events
368 */
369 void GLViewer_ViewFrame::keyEvent( QKeyEvent* e )
370 {
371   switch ( e->type() )
372   {
373   case QEvent::KeyPress:
374     emit keyPressed( this, e );
375     break;
376   case QEvent::KeyRelease:
377     emit keyReleased( this, e );
378     break;
379   default:
380     break;
381   }
382 }
383
384 /*!
385   Dispatches wheel events
386 */
387 void GLViewer_ViewFrame::wheelEvent( QWheelEvent* e )
388 {
389   switch ( e->type() )
390   {
391   case QEvent::Wheel:
392     emit wheeling( this, e );
393     break;
394   default:
395     break;
396   }
397 }
398
399 /*!
400   \return the visual parameters of this view as a formated string
401 */
402 QString GLViewer_ViewFrame::getVisualParameters()
403 {
404   QString retStr;
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 );
411   }
412   return retStr;
413 }
414
415 /*!
416   The method restores visual parameters of this view from a formated string
417 */
418 void GLViewer_ViewFrame::setVisualParameters( const QString& parameters )
419 {
420   QStringList paramsLst = parameters.split( '*' );
421   if ( myVP && myVP->inherits( "GLViewer_ViewPort2d" ) && paramsLst.size() == 4) {
422     GLViewer_ViewPort2d* vp2d = (GLViewer_ViewPort2d*)myVP;
423
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();
429
430     vp2d->getGLWidget()->setScale( xSc, ySc, 1. );
431     vp2d->getGLWidget()->setPan( xPan, yPan, 0. );
432   }
433 }