1 // Copyright (C) 2013 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "GraphicsView_ViewFrame.h"
22 #include "GraphicsView_Viewer.h"
23 #include "GraphicsView_ViewPort.h"
25 #include <QtxAction.h>
26 #include <QtxActionToolMgr.h>
27 #include <QtxMultiAction.h>
28 #include <QtxToolBar.h>
30 #include <SUIT_Desktop.h>
31 #include <SUIT_MessageBox.h>
32 #include <SUIT_Session.h>
33 #include <SUIT_ResourceMgr.h>
37 #include <QFileDialog>
38 #include <QGraphicsSceneMouseEvent>
39 #include <QGraphicsSceneWheelEvent>
40 #include <QHBoxLayout>
42 #include <QMouseEvent>
44 #include <QWheelEvent>
46 //=======================================================================
47 // Name : GraphicsView_ViewFrame
48 // Purpose : Constructor
49 //=======================================================================
50 GraphicsView_ViewFrame::GraphicsView_ViewFrame( SUIT_Desktop* d, GraphicsView_Viewer* vw )
51 : SUIT_ViewWindow( d ),
54 QFrame* aFrame = new QFrame( this );
55 setCentralWidget( aFrame );
57 QBoxLayout* aLayout = new QHBoxLayout( aFrame );
58 aLayout->setMargin( 0 );
59 aLayout->setSpacing( 0 );
61 myViewPort = new GraphicsView_ViewPort( aFrame );
62 aLayout->addWidget( myViewPort );
65 myToolBarId = createToolBar();
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* ) ) );
76 connect( myViewPort, SIGNAL( vpSketchingFinished( QPainterPath ) ),
77 this, SIGNAL( sketchingFinished( QPainterPath ) ) );
80 //=======================================================================
81 // Name : GraphicsView_ViewFrame
82 // Purpose : Destructor
83 //=======================================================================
84 GraphicsView_ViewFrame::~GraphicsView_ViewFrame()
88 //================================================================
89 // Function : createActions
91 //================================================================
92 void GraphicsView_ViewFrame::createActions()
94 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
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 );
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 );
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 );
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 );
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 );
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 );
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 );
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 );
164 //================================================================
165 // Function : createToolBar
167 //================================================================
168 int GraphicsView_ViewFrame::createToolBar()
170 int tid = toolMgr()->createToolBar( tr("LBL_TOOLBAR_LABEL") );
171 toolMgr()->append( DumpId, tid );
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 );
180 myPanAction = new QtxMultiAction( this );
181 myPanAction->insertAction( toolMgr()->action( PanId ) );
182 myPanAction->insertAction( toolMgr()->action( GlobalPanId ) );
183 toolMgr()->append( myPanAction, tid );
185 toolMgr()->append( toolMgr()->action( ResetId ), tid );
190 //================================================================
191 // Function : dumpView
193 //================================================================
194 QImage GraphicsView_ViewFrame::dumpView()
196 return myViewPort->dumpView();
199 //================================================================
200 // Function : getVisualParameters
202 //================================================================
203 QString GraphicsView_ViewFrame::getVisualParameters()
205 QTransform aTransform = myViewPort->transform();
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() );
215 //================================================================
216 // Function : setVisualParameters
218 //================================================================
219 void GraphicsView_ViewFrame::setVisualParameters( const QString& theParameters )
221 QStringList aList = theParameters.split( '*' );
222 if( aList.size() < 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++ )
239 myViewPort->setTransform( aTransform );
240 myViewPort->applyTransform();
243 //================================================================
244 // Function : expandToolBarActions
246 //================================================================
247 void GraphicsView_ViewFrame::expandToolBarActions()
249 QList<QtxMultiAction*> anExpandableActions;
250 anExpandableActions.append( myScaleAction );
251 anExpandableActions.append( myPanAction );
253 QListIterator<QtxMultiAction*> anIter( anExpandableActions );
254 while( anIter.hasNext() )
256 if( QtxMultiAction* aMultiAction = anIter.next() )
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 );
264 int anId = toolMgr()->actionId( aMultiAction );
265 toolMgr()->remove( anId, myToolBarId );
270 //================================================================
271 // Function : onViewPan
273 //================================================================
274 void GraphicsView_ViewFrame::onViewPan()
276 myViewer->activateTransform( GraphicsView_Viewer::Pan );
279 //================================================================
280 // Function : onViewZoom
282 //================================================================
283 void GraphicsView_ViewFrame::onViewZoom()
285 myViewer->activateTransform( GraphicsView_Viewer::Zoom );
288 //================================================================
289 // Function : onViewFitAll
291 //================================================================
292 void GraphicsView_ViewFrame::onViewFitAll()
294 myViewer->activateTransform( GraphicsView_Viewer::FitAll );
297 //================================================================
298 // Function : onViewFitArea
300 //================================================================
301 void GraphicsView_ViewFrame::onViewFitArea()
303 myViewer->activateTransform( GraphicsView_Viewer::FitRect );
306 //================================================================
307 // Function : onViewFitSelect
309 //================================================================
310 void GraphicsView_ViewFrame::onViewFitSelect()
312 myViewer->activateTransform( GraphicsView_Viewer::FitSelect );
315 //================================================================
316 // Function : onViewGlobalPan
318 //================================================================
319 void GraphicsView_ViewFrame::onViewGlobalPan()
321 myViewer->activateTransform( GraphicsView_Viewer::PanGlobal );
324 //================================================================
325 // Function : onViewReset
327 //================================================================
328 void GraphicsView_ViewFrame::onViewReset()
330 myViewer->activateTransform( GraphicsView_Viewer::Reset );
333 //================================================================
334 // Function : keyEvent
336 //================================================================
337 void GraphicsView_ViewFrame::keyEvent( QKeyEvent* e )
341 case QEvent::KeyPress:
342 emit keyPressed( e );
344 case QEvent::KeyRelease:
345 emit keyReleased( e );
352 //================================================================
353 // Function : mouseEvent
355 //================================================================
356 void GraphicsView_ViewFrame::mouseEvent( QGraphicsSceneMouseEvent* e )
360 case QEvent::GraphicsSceneMousePress:
361 emit mousePressed( e );
363 case QEvent::GraphicsSceneMouseMove:
364 emit mouseMoving( e );
366 case QEvent::GraphicsSceneMouseRelease:
367 emit mouseReleased( e );
369 case QEvent::GraphicsSceneMouseDoubleClick:
370 emit mouseDoubleClicked( e );
377 //================================================================
378 // Function : wheelEvent
380 //================================================================
381 void GraphicsView_ViewFrame::wheelEvent( QGraphicsSceneWheelEvent* e )
385 case QEvent::GraphicsSceneWheel:
393 //================================================================
394 // Function : contextMenuEvent
396 //================================================================
397 void GraphicsView_ViewFrame::contextMenuEvent( QGraphicsSceneContextMenuEvent* e )
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 );