]> SALOME platform Git repositories - modules/gui.git/blob - src/GraphicsView/GraphicsView_ViewFrame.cxx
Salome HOME
Patch for non-restricted zoom-in.
[modules/gui.git] / src / GraphicsView / GraphicsView_ViewFrame.cxx
1 // Copyright (C) 2013-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "GraphicsView_ViewFrame.h"
21
22 #include "GraphicsView_Viewer.h"
23 #include "GraphicsView_ViewPort.h"
24
25 #include <QtxAction.h>
26 #include <QtxActionToolMgr.h>
27 #include <QtxMultiAction.h>
28 #include <QtxToolBar.h>
29
30 #include <SUIT_Desktop.h>
31 #include <SUIT_MessageBox.h>
32 #include <SUIT_Session.h>
33 #include <SUIT_ResourceMgr.h>
34
35 #include <QColor>
36 #include <QFrame>
37 #include <QFileDialog>
38 #include <QGraphicsSceneMouseEvent>
39 #include <QGraphicsSceneWheelEvent>
40 #include <QHBoxLayout>
41 #include <QImage>
42 #include <QMouseEvent>
43 #include <QString>
44 #include <QWheelEvent>
45
46 //=======================================================================
47 // Name    : GraphicsView_ViewFrame
48 // Purpose : Constructor
49 //=======================================================================
50 GraphicsView_ViewFrame::GraphicsView_ViewFrame( SUIT_Desktop* d, GraphicsView_Viewer* vw, QWidget* w )
51 : SUIT_ViewWindow( d ),
52   myViewer( vw ), myToolBarId( -1 )
53 {
54   QFrame* aFrame = new QFrame( this );
55   setCentralWidget( aFrame );
56
57   QBoxLayout* aLayout = new QHBoxLayout( aFrame );
58   aLayout->setMargin( 0 );
59   aLayout->setSpacing( 0 );
60
61   if( w )
62     myViewPort = dynamic_cast<GraphicsView_ViewPort*>(w);
63   else
64     myViewPort = new GraphicsView_ViewPort( aFrame );
65
66   aLayout->addWidget( myViewPort );
67
68   connect( myViewPort, SIGNAL( vpKeyEvent( QKeyEvent* ) ),
69            this, SLOT( keyEvent( QKeyEvent* ) ) );
70   connect( myViewPort, SIGNAL( vpMouseEvent( QGraphicsSceneMouseEvent* ) ),
71            this, SLOT( mouseEvent( QGraphicsSceneMouseEvent* ) ) );
72   connect( myViewPort, SIGNAL( vpWheelEvent( QGraphicsSceneWheelEvent* ) ),
73            this, SLOT( wheelEvent( QGraphicsSceneWheelEvent* ) ) );
74   connect( myViewPort, SIGNAL( vpContextMenuEvent( QGraphicsSceneContextMenuEvent* ) ),
75            this, SLOT( contextMenuEvent( QGraphicsSceneContextMenuEvent* ) ) );
76
77   connect( myViewPort, SIGNAL( vpSketchingFinished( QPainterPath ) ),
78            this, SIGNAL( sketchingFinished( QPainterPath ) ) );
79 }
80
81 //=======================================================================
82 // Name    : GraphicsView_ViewFrame
83 // Purpose : Destructor
84 //=======================================================================
85 GraphicsView_ViewFrame::~GraphicsView_ViewFrame()
86 {
87 }
88
89 //================================================================
90 // Function : createActions
91 // Purpose  : 
92 //================================================================
93 void GraphicsView_ViewFrame::createActions()
94 {
95   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
96   QtxAction* anAction;
97
98   // Dump view
99   anAction = new QtxAction( tr( "MNU_DUMP_VIEW" ),
100                            aResMgr->loadPixmap( "GraphicsView", tr( "ICON_GV_DUMP" ) ),
101                            tr( "MNU_DUMP_VIEW" ), 0, this );
102   anAction->setStatusTip( tr( "DSC_DUMP_VIEW" ) );
103   connect( anAction, SIGNAL( triggered( bool ) ), this, SLOT( onDumpView() ) );
104   toolMgr()->registerAction( anAction, DumpId );
105
106   // FitAll
107   anAction = new QtxAction( tr( "MNU_FITALL" ),
108                            aResMgr->loadPixmap( "GraphicsView", tr( "ICON_GV_FITALL" ) ),
109                            tr( "MNU_FITALL" ), 0, this );
110   anAction->setStatusTip( tr( "DSC_FITALL" ) );
111   connect( anAction, SIGNAL( triggered( bool ) ), this, SLOT( onViewFitAll() ) );
112   toolMgr()->registerAction( anAction, FitAllId );
113
114   // FitRect
115   anAction = new QtxAction( tr( "MNU_FITRECT" ),
116                            aResMgr->loadPixmap( "GraphicsView", tr( "ICON_GV_FITAREA" ) ),
117                            tr( "MNU_FITRECT" ), 0, this );
118   anAction->setStatusTip( tr( "DSC_FITRECT" ) );
119   connect( anAction, SIGNAL( triggered( bool ) ), this, SLOT( onViewFitArea() ) );
120   toolMgr()->registerAction( anAction, FitRectId );
121
122   // FitSelect
123   anAction = new QtxAction( tr( "MNU_FITSELECT" ),
124                            aResMgr->loadPixmap( "GraphicsView", tr( "ICON_GV_FITSELECT" ) ),
125                            tr( "MNU_FITSELECT" ), 0, this );
126   anAction->setStatusTip( tr( "DSC_FITSELECT" ) );
127   connect( anAction, SIGNAL( triggered( bool ) ), this, SLOT( onViewFitSelect() ) );
128   toolMgr()->registerAction( anAction, FitSelectId );
129
130   // Zoom
131   anAction = new QtxAction( tr( "MNU_ZOOM_VIEW" ),
132                            aResMgr->loadPixmap( "GraphicsView", tr( "ICON_GV_ZOOM" ) ),
133                            tr( "MNU_ZOOM_VIEW" ), 0, this );
134   anAction->setStatusTip( tr( "DSC_ZOOM_VIEW" ) );
135   connect( anAction, SIGNAL( triggered( bool ) ), this, SLOT( onViewZoom() ) );
136   toolMgr()->registerAction( anAction, ZoomId );
137
138   // Panning
139   anAction = new QtxAction( tr( "MNU_PAN_VIEW" ),
140                            aResMgr->loadPixmap( "GraphicsView", tr( "ICON_GV_PAN" ) ),
141                            tr( "MNU_PAN_VIEW" ), 0, this );
142   anAction->setStatusTip( tr( "DSC_PAN_VIEW" ) );
143   connect( anAction, SIGNAL( triggered( bool ) ), this, SLOT( onViewPan() ) );
144   toolMgr()->registerAction( anAction, PanId );
145
146   // Global Panning
147   anAction = new QtxAction( tr( "MNU_GLOBALPAN_VIEW" ),
148                            aResMgr->loadPixmap( "GraphicsView", tr( "ICON_GV_GLOBALPAN" ) ),
149                            tr( "MNU_GLOBALPAN_VIEW" ), 0, this );
150   anAction->setStatusTip( tr( "DSC_GLOBALPAN_VIEW" ) );
151   connect( anAction, SIGNAL( triggered( bool ) ), this, SLOT( onViewGlobalPan() ) );
152   toolMgr()->registerAction( anAction, GlobalPanId );
153
154   // Reset (obsolete)
155   /*
156   anAction = new QtxAction( tr( "MNU_RESET_VIEW" ),
157                            aResMgr->loadPixmap( "GraphicsView", tr( "ICON_GV_RESET" ) ),
158                            tr( "MNU_RESET_VIEW" ), 0, this );
159   anAction->setStatusTip( tr( "DSC_RESET_VIEW" ) );
160   connect( anAction, SIGNAL( triggered( bool ) ), this, SLOT( onViewReset() ) );
161   toolMgr()->registerAction( anAction, ResetId );
162   */
163 }
164
165 //================================================================
166 // Function : createToolBar
167 // Purpose  : 
168 //================================================================
169 int GraphicsView_ViewFrame::createToolBar()
170 {
171   int tid = toolMgr()->createToolBar( tr("LBL_TOOLBAR_LABEL"),                 // title (language-dependant)
172                                       QString( "GraphicsViewViewOperations" ), // name (language-independant)
173                                       false );                                 // disable floatable toolbar
174   toolMgr()->append( DumpId, tid );
175
176   QtxMultiAction* aScaleAction = new QtxMultiAction( this );
177   aScaleAction->insertAction( toolMgr()->action( FitAllId ) );
178   aScaleAction->insertAction( toolMgr()->action( FitRectId ) );
179   aScaleAction->insertAction( toolMgr()->action( FitSelectId ) );
180   aScaleAction->insertAction( toolMgr()->action( ZoomId ) );
181   toolMgr()->append( aScaleAction, tid );
182   myScaleAction = aScaleAction;
183
184   QtxMultiAction* aPanAction = new QtxMultiAction( this );
185   aPanAction->insertAction( toolMgr()->action( PanId ) );
186   aPanAction->insertAction( toolMgr()->action( GlobalPanId ) );
187   toolMgr()->append( aPanAction, tid );
188   myPanAction = aPanAction;
189
190   toolMgr()->append( toolMgr()->action( ResetId ), tid );
191
192   return tid;
193 }
194
195 //================================================================
196 // Function : getToolBarId
197 // Purpose  :
198 //================================================================
199 int GraphicsView_ViewFrame::getToolBarId()
200 {
201   return myToolBarId;
202 }
203
204 //================================================================
205 // Function : dumpView
206 // Purpose  : 
207 //================================================================
208 QImage GraphicsView_ViewFrame::dumpView()
209 {
210   return myViewPort->dumpView();
211 }
212
213 //================================================================
214 // Function : dumpViewToPSFormat
215 // Purpose  : 
216 //================================================================
217 bool GraphicsView_ViewFrame::dumpViewToPSFormat(const QString& fileName)
218 {
219   return myViewPort->dumpViewToPSFormat(fileName);
220 }
221
222 //================================================================
223 // Function : getVisualParameters
224 // Purpose  : 
225 //================================================================
226 QString GraphicsView_ViewFrame::getVisualParameters()
227 {
228   QTransform aTransform = myViewPort->transform();
229
230   QString aString;
231   aString.sprintf( "%.3f*%.3f*%.3f*%.3f*%.3f*%.3f*%.3f*%.3f*%.3f",
232     aTransform.m11(), aTransform.m12(), aTransform.m13(),
233     aTransform.m21(), aTransform.m22(), aTransform.m23(),
234     aTransform.m31(), aTransform.m32(), aTransform.m33() );
235   return aString;
236 }
237
238 //================================================================
239 // Function : setVisualParameters
240 // Purpose  : 
241 //================================================================
242 void GraphicsView_ViewFrame::setVisualParameters( const QString& theParameters )
243 {
244   QStringList aList = theParameters.split( '*' );
245   if( aList.size() < 9 )
246     return;
247
248   bool anIsOk[9];
249   QTransform aTransform( aList[0].toDouble( &anIsOk[0] ),
250                          aList[1].toDouble( &anIsOk[1] ),
251                          aList[2].toDouble( &anIsOk[2] ),
252                          aList[3].toDouble( &anIsOk[3] ),
253                          aList[4].toDouble( &anIsOk[4] ),
254                          aList[5].toDouble( &anIsOk[5] ),
255                          aList[6].toDouble( &anIsOk[6] ),
256                          aList[7].toDouble( &anIsOk[7] ),
257                          aList[8].toDouble( &anIsOk[8] ) );
258   for( int i = 0; i < 9; i++ )
259     if( !anIsOk[ i ] )
260       return;
261
262   myViewPort->setTransform( aTransform );
263   myViewPort->applyTransform();
264 }
265
266 //================================================================
267 // Function : expandToolBarActions
268 // Purpose  : 
269 //================================================================
270 void GraphicsView_ViewFrame::expandToolBarActions()
271 {
272   QList<QAction*> anExpandableActions;
273   anExpandableActions.append( myScaleAction );
274   anExpandableActions.append( myPanAction );
275
276   QListIterator<QAction*> anIter( anExpandableActions );
277   while( anIter.hasNext() )
278   {
279     QtxMultiAction* aMultiAction = dynamic_cast<QtxMultiAction*>( anIter.next() );
280     if( aMultiAction )
281     {
282       QList<QAction*> aLocalActions = aMultiAction->actions();
283       QListIterator<QAction*> aLocalIter( aLocalActions );
284       while( aLocalIter.hasNext() )
285         if( QAction* anAction = aLocalIter.next() )
286           toolMgr()->append( anAction, myToolBarId );
287
288       int anId = toolMgr()->actionId( aMultiAction );
289       toolMgr()->remove( anId, myToolBarId );
290     }
291   }
292 }
293
294 //================================================================
295 // Function : onViewPan
296 // Purpose  : 
297 //================================================================
298 void GraphicsView_ViewFrame::onViewPan()
299 {
300   myViewer->activateTransform( GraphicsView_Viewer::Pan );
301 }
302
303 //================================================================
304 // Function : onViewZoom
305 // Purpose  : 
306 //================================================================
307 void GraphicsView_ViewFrame::onViewZoom()
308 {
309   myViewer->activateTransform( GraphicsView_Viewer::Zoom );
310 }
311
312 //================================================================
313 // Function : onViewFitAll
314 // Purpose  : 
315 //================================================================
316 void GraphicsView_ViewFrame::onViewFitAll()
317 {
318   myViewer->activateTransform( GraphicsView_Viewer::FitAll );
319 }
320
321 //================================================================
322 // Function : onViewFitArea
323 // Purpose  : 
324 //================================================================
325 void GraphicsView_ViewFrame::onViewFitArea()
326 {
327   myViewer->activateTransform( GraphicsView_Viewer::FitRect );
328 }
329
330 //================================================================
331 // Function : onViewFitSelect
332 // Purpose  : 
333 //================================================================
334 void GraphicsView_ViewFrame::onViewFitSelect()
335 {
336   myViewer->activateTransform( GraphicsView_Viewer::FitSelect );
337 }
338
339 //================================================================
340 // Function : onViewGlobalPan
341 // Purpose  : 
342 //================================================================
343 void GraphicsView_ViewFrame::onViewGlobalPan()
344 {
345   myViewer->activateTransform( GraphicsView_Viewer::PanGlobal );
346 }
347
348 //================================================================
349 // Function : onViewReset
350 // Purpose  : 
351 //================================================================
352 void GraphicsView_ViewFrame::onViewReset()
353 {
354   myViewer->activateTransform( GraphicsView_Viewer::Reset );
355 }
356
357 //================================================================
358 // Function : keyEvent
359 // Purpose  : 
360 //================================================================
361 void GraphicsView_ViewFrame::keyEvent( QKeyEvent* e )
362 {
363   switch ( e->type() )
364   {
365     case QEvent::KeyPress:
366       emit keyPressed( e );
367       break;
368     case QEvent::KeyRelease:
369       emit keyReleased( e );
370       break;
371     default:
372       break;
373   }
374 }
375
376 //================================================================
377 // Function : mouseEvent
378 // Purpose  : 
379 //================================================================
380 void GraphicsView_ViewFrame::mouseEvent( QGraphicsSceneMouseEvent* e )
381 {
382   switch ( e->type() )
383   {
384     case QEvent::GraphicsSceneMousePress:
385       emit mousePressed( e );
386       break;
387     case QEvent::GraphicsSceneMouseMove:
388       emit mouseMoving( e );
389       break;
390     case QEvent::GraphicsSceneMouseRelease:
391       emit mouseReleased( e );
392       break;
393     case QEvent::GraphicsSceneMouseDoubleClick:
394       emit mouseDoubleClicked( e );
395       break;
396     default:
397       break;
398   }
399 }
400
401 //================================================================
402 // Function : wheelEvent
403 // Purpose  : 
404 //================================================================
405 void GraphicsView_ViewFrame::wheelEvent( QGraphicsSceneWheelEvent* e )
406 {
407   switch ( e->type() )
408   {
409     case QEvent::GraphicsSceneWheel:
410       emit wheeling( e );
411       break;
412     default:
413       break;
414   }
415 }
416
417 //================================================================
418 // Function : contextMenuEvent
419 // Purpose  : 
420 //================================================================
421 void GraphicsView_ViewFrame::contextMenuEvent( QGraphicsSceneContextMenuEvent* e )
422 {
423   QPoint aPos = myViewPort->mapFromScene( e->scenePos() );
424   QContextMenuEvent* anEvent = new QContextMenuEvent( (QContextMenuEvent::Reason)e->reason(),
425                                                       aPos, e->screenPos(), e->modifiers() );
426   emit contextMenuRequested( anEvent );
427   delete anEvent;
428 }
429
430 /*!
431   \brief Handle show event.
432
433   Emits Show() signal.
434
435   \param theEvent show event
436 */
437 void GraphicsView_ViewFrame::showEvent( QShowEvent* theEvent )
438 {
439   if( myToolBarId < 0 )
440   {
441     createActions();
442     myToolBarId = createToolBar();
443   }
444
445   emit Show( theEvent );
446 }
447
448 /*!
449   \brief Handle hide event.
450
451   Emits Hide() signal.
452
453   \param theEvent hide event
454 */
455 void GraphicsView_ViewFrame::hideEvent( QHideEvent* theEvent )
456 {
457   emit Hide( theEvent );
458 }