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