Salome HOME
9e84bb2276a023685095fda8594f8e884fc8c0e8
[modules/gui.git] / src / GraphicsView / GraphicsView_ViewFrame.cxx
1 // Copyright (C) 2013-2014  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 )
51 : SUIT_ViewWindow( d ),
52   myViewer( vw )
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   myViewPort = new GraphicsView_ViewPort( aFrame );
62   aLayout->addWidget( myViewPort );
63
64   createActions();
65   myToolBarId = createToolBar();
66
67   connect( myViewPort, SIGNAL( vpKeyEvent( QKeyEvent* ) ),
68            this, SLOT( keyEvent( QKeyEvent* ) ) );
69   connect( myViewPort, SIGNAL( vpMouseEvent( QGraphicsSceneMouseEvent* ) ),
70            this, SLOT( mouseEvent( QGraphicsSceneMouseEvent* ) ) );
71   connect( myViewPort, SIGNAL( vpWheelEvent( QGraphicsSceneWheelEvent* ) ),
72            this, SLOT( wheelEvent( QGraphicsSceneWheelEvent* ) ) );
73   connect( myViewPort, SIGNAL( vpContextMenuEvent( QGraphicsSceneContextMenuEvent* ) ),
74            this, SLOT( contextMenuEvent( QGraphicsSceneContextMenuEvent* ) ) );
75
76   connect( myViewPort, SIGNAL( vpSketchingFinished( QPainterPath ) ),
77            this, SIGNAL( sketchingFinished( QPainterPath ) ) );
78 }
79
80 //=======================================================================
81 // Name    : GraphicsView_ViewFrame
82 // Purpose : Destructor
83 //=======================================================================
84 GraphicsView_ViewFrame::~GraphicsView_ViewFrame()
85 {
86 }
87
88 //================================================================
89 // Function : createActions
90 // Purpose  : 
91 //================================================================
92 void GraphicsView_ViewFrame::createActions()
93 {
94   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
95   QtxAction* anAction;
96
97   // Dump view
98   anAction = new QtxAction( tr( "MNU_DUMP_VIEW" ),
99                            aResMgr->loadPixmap( "GraphicsView", tr( "ICON_GV_DUMP" ) ),
100                            tr( "MNU_DUMP_VIEW" ), 0, this );
101   anAction->setStatusTip( tr( "DSC_DUMP_VIEW" ) );
102   connect( anAction, SIGNAL( triggered( bool ) ), this, SLOT( onDumpView() ) );
103   toolMgr()->registerAction( anAction, DumpId );
104
105   // FitAll
106   anAction = new QtxAction( tr( "MNU_FITALL" ),
107                            aResMgr->loadPixmap( "GraphicsView", tr( "ICON_GV_FITALL" ) ),
108                            tr( "MNU_FITALL" ), 0, this );
109   anAction->setStatusTip( tr( "DSC_FITALL" ) );
110   connect( anAction, SIGNAL( triggered( bool ) ), this, SLOT( onViewFitAll() ) );
111   toolMgr()->registerAction( anAction, FitAllId );
112
113   // FitRect
114   anAction = new QtxAction( tr( "MNU_FITRECT" ),
115                            aResMgr->loadPixmap( "GraphicsView", tr( "ICON_GV_FITAREA" ) ),
116                            tr( "MNU_FITRECT" ), 0, this );
117   anAction->setStatusTip( tr( "DSC_FITRECT" ) );
118   connect( anAction, SIGNAL( triggered( bool ) ), this, SLOT( onViewFitArea() ) );
119   toolMgr()->registerAction( anAction, FitRectId );
120
121   // FitSelect
122   anAction = new QtxAction( tr( "MNU_FITSELECT" ),
123                            aResMgr->loadPixmap( "GraphicsView", tr( "ICON_GV_FITSELECT" ) ),
124                            tr( "MNU_FITSELECT" ), 0, this );
125   anAction->setStatusTip( tr( "DSC_FITSELECT" ) );
126   connect( anAction, SIGNAL( triggered( bool ) ), this, SLOT( onViewFitSelect() ) );
127   toolMgr()->registerAction( anAction, FitSelectId );
128
129   // Zoom
130   anAction = new QtxAction( tr( "MNU_ZOOM_VIEW" ),
131                            aResMgr->loadPixmap( "GraphicsView", tr( "ICON_GV_ZOOM" ) ),
132                            tr( "MNU_ZOOM_VIEW" ), 0, this );
133   anAction->setStatusTip( tr( "DSC_ZOOM_VIEW" ) );
134   connect( anAction, SIGNAL( triggered( bool ) ), this, SLOT( onViewZoom() ) );
135   toolMgr()->registerAction( anAction, ZoomId );
136
137   // Panning
138   anAction = new QtxAction( tr( "MNU_PAN_VIEW" ),
139                            aResMgr->loadPixmap( "GraphicsView", tr( "ICON_GV_PAN" ) ),
140                            tr( "MNU_PAN_VIEW" ), 0, this );
141   anAction->setStatusTip( tr( "DSC_PAN_VIEW" ) );
142   connect( anAction, SIGNAL( triggered( bool ) ), this, SLOT( onViewPan() ) );
143   toolMgr()->registerAction( anAction, PanId );
144
145   // Global Panning
146   anAction = new QtxAction( tr( "MNU_GLOBALPAN_VIEW" ),
147                            aResMgr->loadPixmap( "GraphicsView", tr( "ICON_GV_GLOBALPAN" ) ),
148                            tr( "MNU_GLOBALPAN_VIEW" ), 0, this );
149   anAction->setStatusTip( tr( "DSC_GLOBALPAN_VIEW" ) );
150   connect( anAction, SIGNAL( triggered( bool ) ), this, SLOT( onViewGlobalPan() ) );
151   toolMgr()->registerAction( anAction, GlobalPanId );
152
153   // Reset (obsolete)
154   /*
155   anAction = new QtxAction( tr( "MNU_RESET_VIEW" ),
156                            aResMgr->loadPixmap( "GraphicsView", tr( "ICON_GV_RESET" ) ),
157                            tr( "MNU_RESET_VIEW" ), 0, this );
158   anAction->setStatusTip( tr( "DSC_RESET_VIEW" ) );
159   connect( anAction, SIGNAL( triggered( bool ) ), this, SLOT( onViewReset() ) );
160   toolMgr()->registerAction( anAction, ResetId );
161   */
162 }
163
164 //================================================================
165 // Function : createToolBar
166 // Purpose  : 
167 //================================================================
168 int GraphicsView_ViewFrame::createToolBar()
169 {
170   int tid = toolMgr()->createToolBar( tr("LBL_TOOLBAR_LABEL") );
171   toolMgr()->append( DumpId, tid );
172
173   myScaleAction = new QtxMultiAction( this );
174   myScaleAction->insertAction( toolMgr()->action( FitAllId ) );
175   myScaleAction->insertAction( toolMgr()->action( FitRectId ) );
176   myScaleAction->insertAction( toolMgr()->action( FitSelectId ) );
177   myScaleAction->insertAction( toolMgr()->action( ZoomId ) );
178   toolMgr()->append( myScaleAction, tid );
179
180   myPanAction = new QtxMultiAction( this );
181   myPanAction->insertAction( toolMgr()->action( PanId ) );
182   myPanAction->insertAction( toolMgr()->action( GlobalPanId ) );
183   toolMgr()->append( myPanAction, tid );
184
185   toolMgr()->append( toolMgr()->action( ResetId ), tid );
186
187   return tid;
188 }
189
190 //================================================================
191 // Function : dumpView
192 // Purpose  : 
193 //================================================================
194 QImage GraphicsView_ViewFrame::dumpView()
195 {
196   return myViewPort->dumpView();
197 }
198
199 //================================================================
200 // Function : getVisualParameters
201 // Purpose  : 
202 //================================================================
203 QString GraphicsView_ViewFrame::getVisualParameters()
204 {
205   QTransform aTransform = myViewPort->transform();
206
207   QString aString;
208   aString.sprintf( "%.3f*%.3f*%.3f*%.3f*%.3f*%.3f*%.3f*%.3f*%.3f",
209     aTransform.m11(), aTransform.m12(), aTransform.m13(),
210     aTransform.m21(), aTransform.m22(), aTransform.m23(),
211     aTransform.m31(), aTransform.m32(), aTransform.m33() );
212   return aString;
213 }
214
215 //================================================================
216 // Function : setVisualParameters
217 // Purpose  : 
218 //================================================================
219 void GraphicsView_ViewFrame::setVisualParameters( const QString& theParameters )
220 {
221   QStringList aList = theParameters.split( '*' );
222   if( aList.size() < 9 )
223     return;
224
225   bool anIsOk[9];
226   QTransform aTransform( aList[0].toDouble( &anIsOk[0] ),
227                          aList[1].toDouble( &anIsOk[1] ),
228                          aList[2].toDouble( &anIsOk[2] ),
229                          aList[3].toDouble( &anIsOk[3] ),
230                          aList[4].toDouble( &anIsOk[4] ),
231                          aList[5].toDouble( &anIsOk[5] ),
232                          aList[6].toDouble( &anIsOk[6] ),
233                          aList[7].toDouble( &anIsOk[7] ),
234                          aList[8].toDouble( &anIsOk[8] ) );
235   for( int i = 0; i < 9; i++ )
236     if( !anIsOk[ i ] )
237       return;
238
239   myViewPort->setTransform( aTransform );
240   myViewPort->applyTransform();
241 }
242
243 //================================================================
244 // Function : expandToolBarActions
245 // Purpose  : 
246 //================================================================
247 void GraphicsView_ViewFrame::expandToolBarActions()
248 {
249   QList<QtxMultiAction*> anExpandableActions;
250   anExpandableActions.append( myScaleAction );
251   anExpandableActions.append( myPanAction );
252
253   QListIterator<QtxMultiAction*> anIter( anExpandableActions );
254   while( anIter.hasNext() )
255   {
256     if( QtxMultiAction* aMultiAction = anIter.next() )
257     {
258       QList<QAction*> aLocalActions = aMultiAction->actions();
259       QListIterator<QAction*> aLocalIter( aLocalActions );
260       while( aLocalIter.hasNext() )
261         if( QAction* anAction = aLocalIter.next() )
262           toolMgr()->append( anAction, myToolBarId );
263
264       int anId = toolMgr()->actionId( aMultiAction );
265       toolMgr()->remove( anId, myToolBarId );
266     }
267   }
268 }
269
270 //================================================================
271 // Function : onViewPan
272 // Purpose  : 
273 //================================================================
274 void GraphicsView_ViewFrame::onViewPan()
275 {
276   myViewer->activateTransform( GraphicsView_Viewer::Pan );
277 }
278
279 //================================================================
280 // Function : onViewZoom
281 // Purpose  : 
282 //================================================================
283 void GraphicsView_ViewFrame::onViewZoom()
284 {
285   myViewer->activateTransform( GraphicsView_Viewer::Zoom );
286 }
287
288 //================================================================
289 // Function : onViewFitAll
290 // Purpose  : 
291 //================================================================
292 void GraphicsView_ViewFrame::onViewFitAll()
293 {
294   myViewer->activateTransform( GraphicsView_Viewer::FitAll );
295 }
296
297 //================================================================
298 // Function : onViewFitArea
299 // Purpose  : 
300 //================================================================
301 void GraphicsView_ViewFrame::onViewFitArea()
302 {
303   myViewer->activateTransform( GraphicsView_Viewer::FitRect );
304 }
305
306 //================================================================
307 // Function : onViewFitSelect
308 // Purpose  : 
309 //================================================================
310 void GraphicsView_ViewFrame::onViewFitSelect()
311 {
312   myViewer->activateTransform( GraphicsView_Viewer::FitSelect );
313 }
314
315 //================================================================
316 // Function : onViewGlobalPan
317 // Purpose  : 
318 //================================================================
319 void GraphicsView_ViewFrame::onViewGlobalPan()
320 {
321   myViewer->activateTransform( GraphicsView_Viewer::PanGlobal );
322 }
323
324 //================================================================
325 // Function : onViewReset
326 // Purpose  : 
327 //================================================================
328 void GraphicsView_ViewFrame::onViewReset()
329 {
330   myViewer->activateTransform( GraphicsView_Viewer::Reset );
331 }
332
333 //================================================================
334 // Function : keyEvent
335 // Purpose  : 
336 //================================================================
337 void GraphicsView_ViewFrame::keyEvent( QKeyEvent* e )
338 {
339   switch ( e->type() )
340   {
341     case QEvent::KeyPress:
342       emit keyPressed( e );
343       break;
344     case QEvent::KeyRelease:
345       emit keyReleased( e );
346       break;
347     default:
348       break;
349   }
350 }
351
352 //================================================================
353 // Function : mouseEvent
354 // Purpose  : 
355 //================================================================
356 void GraphicsView_ViewFrame::mouseEvent( QGraphicsSceneMouseEvent* e )
357 {
358   switch ( e->type() )
359   {
360     case QEvent::GraphicsSceneMousePress:
361       emit mousePressed( e );
362       break;
363     case QEvent::GraphicsSceneMouseMove:
364       emit mouseMoving( e );
365       break;
366     case QEvent::GraphicsSceneMouseRelease:
367       emit mouseReleased( e );
368       break;
369     case QEvent::GraphicsSceneMouseDoubleClick:
370       emit mouseDoubleClicked( e );
371       break;
372     default:
373       break;
374   }
375 }
376
377 //================================================================
378 // Function : wheelEvent
379 // Purpose  : 
380 //================================================================
381 void GraphicsView_ViewFrame::wheelEvent( QGraphicsSceneWheelEvent* e )
382 {
383   switch ( e->type() )
384   {
385     case QEvent::GraphicsSceneWheel:
386       emit wheeling( e );
387       break;
388     default:
389       break;
390   }
391 }
392
393 //================================================================
394 // Function : contextMenuEvent
395 // Purpose  : 
396 //================================================================
397 void GraphicsView_ViewFrame::contextMenuEvent( QGraphicsSceneContextMenuEvent* e )
398 {
399   QPoint aPos = myViewPort->mapFromScene( e->scenePos() );
400   QContextMenuEvent* anEvent = new QContextMenuEvent( (QContextMenuEvent::Reason)e->reason(),
401                                                       aPos, e->screenPos(), e->modifiers() );
402   emit contextMenuRequested( anEvent );
403   delete anEvent;
404 }