1 // Copyright (C) 2013-2016 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, or (at your option) any later version.
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, QWidget* w )
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 );
62 myViewPort = dynamic_cast<GraphicsView_ViewPort*>(w);
64 myViewPort = new GraphicsView_ViewPort( aFrame );
66 aLayout->addWidget( myViewPort );
69 myToolBarId = createToolBar();
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* ) ) );
80 connect( myViewPort, SIGNAL( vpSketchingFinished( QPainterPath ) ),
81 this, SIGNAL( sketchingFinished( QPainterPath ) ) );
84 //=======================================================================
85 // Name : GraphicsView_ViewFrame
86 // Purpose : Destructor
87 //=======================================================================
88 GraphicsView_ViewFrame::~GraphicsView_ViewFrame()
92 //================================================================
93 // Function : createActions
95 //================================================================
96 void GraphicsView_ViewFrame::createActions()
98 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
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 );
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 );
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 );
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 );
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 );
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 );
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 );
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 );
168 //================================================================
169 // Function : createToolBar
171 //================================================================
172 int GraphicsView_ViewFrame::createToolBar()
174 int tid = toolMgr()->createToolBar( tr("LBL_TOOLBAR_LABEL"), // title (language-dependant)
175 QString( "GraphicsViewViewOperations" ), // name (language-independant)
176 false ); // disable floatable toolbar
177 toolMgr()->append( DumpId, tid );
179 myScaleAction = new QtxMultiAction( this );
180 myScaleAction->insertAction( toolMgr()->action( FitAllId ) );
181 myScaleAction->insertAction( toolMgr()->action( FitRectId ) );
182 myScaleAction->insertAction( toolMgr()->action( FitSelectId ) );
183 myScaleAction->insertAction( toolMgr()->action( ZoomId ) );
184 toolMgr()->append( myScaleAction, tid );
186 myPanAction = new QtxMultiAction( this );
187 myPanAction->insertAction( toolMgr()->action( PanId ) );
188 myPanAction->insertAction( toolMgr()->action( GlobalPanId ) );
189 toolMgr()->append( myPanAction, tid );
191 toolMgr()->append( toolMgr()->action( ResetId ), tid );
196 //================================================================
197 // Function : getToolBarId
199 //================================================================
200 int GraphicsView_ViewFrame::getToolBarId()
205 //================================================================
206 // Function : dumpView
208 //================================================================
209 QImage GraphicsView_ViewFrame::dumpView()
211 return myViewPort->dumpView();
214 //================================================================
215 // Function : dumpViewToPSFormat
217 //================================================================
218 bool GraphicsView_ViewFrame::dumpViewToPSFormat(const QString& fileName)
220 return myViewPort->dumpViewToPSFormat(fileName);
223 //================================================================
224 // Function : getVisualParameters
226 //================================================================
227 QString GraphicsView_ViewFrame::getVisualParameters()
229 QTransform aTransform = myViewPort->transform();
232 aString.sprintf( "%.3f*%.3f*%.3f*%.3f*%.3f*%.3f*%.3f*%.3f*%.3f",
233 aTransform.m11(), aTransform.m12(), aTransform.m13(),
234 aTransform.m21(), aTransform.m22(), aTransform.m23(),
235 aTransform.m31(), aTransform.m32(), aTransform.m33() );
239 //================================================================
240 // Function : setVisualParameters
242 //================================================================
243 void GraphicsView_ViewFrame::setVisualParameters( const QString& theParameters )
245 QStringList aList = theParameters.split( '*' );
246 if( aList.size() < 9 )
250 QTransform aTransform( aList[0].toDouble( &anIsOk[0] ),
251 aList[1].toDouble( &anIsOk[1] ),
252 aList[2].toDouble( &anIsOk[2] ),
253 aList[3].toDouble( &anIsOk[3] ),
254 aList[4].toDouble( &anIsOk[4] ),
255 aList[5].toDouble( &anIsOk[5] ),
256 aList[6].toDouble( &anIsOk[6] ),
257 aList[7].toDouble( &anIsOk[7] ),
258 aList[8].toDouble( &anIsOk[8] ) );
259 for( int i = 0; i < 9; i++ )
263 myViewPort->setTransform( aTransform );
264 myViewPort->applyTransform();
267 //================================================================
268 // Function : expandToolBarActions
270 //================================================================
271 void GraphicsView_ViewFrame::expandToolBarActions()
273 QList<QtxMultiAction*> anExpandableActions;
274 anExpandableActions.append( myScaleAction );
275 anExpandableActions.append( myPanAction );
277 QListIterator<QtxMultiAction*> anIter( anExpandableActions );
278 while( anIter.hasNext() )
280 if( QtxMultiAction* aMultiAction = anIter.next() )
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 );
288 int anId = toolMgr()->actionId( aMultiAction );
289 toolMgr()->remove( anId, myToolBarId );
294 //================================================================
295 // Function : onViewPan
297 //================================================================
298 void GraphicsView_ViewFrame::onViewPan()
300 myViewer->activateTransform( GraphicsView_Viewer::Pan );
303 //================================================================
304 // Function : onViewZoom
306 //================================================================
307 void GraphicsView_ViewFrame::onViewZoom()
309 myViewer->activateTransform( GraphicsView_Viewer::Zoom );
312 //================================================================
313 // Function : onViewFitAll
315 //================================================================
316 void GraphicsView_ViewFrame::onViewFitAll()
318 myViewer->activateTransform( GraphicsView_Viewer::FitAll );
321 //================================================================
322 // Function : onViewFitArea
324 //================================================================
325 void GraphicsView_ViewFrame::onViewFitArea()
327 myViewer->activateTransform( GraphicsView_Viewer::FitRect );
330 //================================================================
331 // Function : onViewFitSelect
333 //================================================================
334 void GraphicsView_ViewFrame::onViewFitSelect()
336 myViewer->activateTransform( GraphicsView_Viewer::FitSelect );
339 //================================================================
340 // Function : onViewGlobalPan
342 //================================================================
343 void GraphicsView_ViewFrame::onViewGlobalPan()
345 myViewer->activateTransform( GraphicsView_Viewer::PanGlobal );
348 //================================================================
349 // Function : onViewReset
351 //================================================================
352 void GraphicsView_ViewFrame::onViewReset()
354 myViewer->activateTransform( GraphicsView_Viewer::Reset );
357 //================================================================
358 // Function : keyEvent
360 //================================================================
361 void GraphicsView_ViewFrame::keyEvent( QKeyEvent* e )
365 case QEvent::KeyPress:
366 emit keyPressed( e );
368 case QEvent::KeyRelease:
369 emit keyReleased( e );
376 //================================================================
377 // Function : mouseEvent
379 //================================================================
380 void GraphicsView_ViewFrame::mouseEvent( QGraphicsSceneMouseEvent* e )
384 case QEvent::GraphicsSceneMousePress:
385 emit mousePressed( e );
387 case QEvent::GraphicsSceneMouseMove:
388 emit mouseMoving( e );
390 case QEvent::GraphicsSceneMouseRelease:
391 emit mouseReleased( e );
393 case QEvent::GraphicsSceneMouseDoubleClick:
394 emit mouseDoubleClicked( e );
401 //================================================================
402 // Function : wheelEvent
404 //================================================================
405 void GraphicsView_ViewFrame::wheelEvent( QGraphicsSceneWheelEvent* e )
409 case QEvent::GraphicsSceneWheel:
417 //================================================================
418 // Function : contextMenuEvent
420 //================================================================
421 void GraphicsView_ViewFrame::contextMenuEvent( QGraphicsSceneContextMenuEvent* e )
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 );