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_ViewTransformer.h"
22 #include "GraphicsView_Scene.h"
23 #include "GraphicsView_ViewPort.h"
24 #include "GraphicsView_Viewer.h"
26 #include <QGraphicsSceneMouseEvent>
28 #include <QRubberBand>
30 int GraphicsView_ViewTransformer::panBtn = Qt::MidButton;
31 int GraphicsView_ViewTransformer::zoomBtn = Qt::LeftButton;
32 int GraphicsView_ViewTransformer::fitRectBtn = Qt::LeftButton;
33 int GraphicsView_ViewTransformer::panGlobalBtn = Qt::LeftButton;
34 int GraphicsView_ViewTransformer::acccelKey = Qt::ControlModifier;
36 //=======================================================================
37 // Name : GraphicsView_ViewTransformer
38 // Purpose : Constructor
39 //=======================================================================
40 GraphicsView_ViewTransformer::GraphicsView_ViewTransformer( GraphicsView_Viewer* v, int type )
44 myMajorBtn( Qt::NoButton ),
48 if( myType == GraphicsView_Viewer::Pan ||
49 myType == GraphicsView_Viewer::Zoom ||
50 myType == GraphicsView_Viewer::PanGlobal ||
51 myType == GraphicsView_Viewer::FitRect )
52 initTransform( true );
55 //=======================================================================
56 // Name : GraphicsView_ViewTransformer
57 // Purpose : Destructor
58 //=======================================================================
59 GraphicsView_ViewTransformer::~GraphicsView_ViewTransformer()
61 if( myType == GraphicsView_Viewer::Pan ||
62 myType == GraphicsView_Viewer::Zoom ||
63 myType == GraphicsView_Viewer::PanGlobal ||
64 myType == GraphicsView_Viewer::FitRect )
65 initTransform( false );
70 //================================================================
71 // Function : initTransform
73 //================================================================
74 void GraphicsView_ViewTransformer::initTransform( bool init )
76 if( GraphicsView_ViewPort* aViewPort = myViewer->getActiveViewPort() )
78 if( GraphicsView_Scene* aScene = aViewPort->getScene() )
82 mySavedCursor = aViewPort->cursor();
83 mySavedMouseTrack = aViewPort->hasMouseTracking();
84 aViewPort->setMouseTracking( false );
85 aScene->installEventFilter( this );
89 aScene->removeEventFilter( this );
90 aViewPort->setMouseTracking( mySavedMouseTrack );
91 aViewPort->setCursor( mySavedCursor );
97 //================================================================
100 //================================================================
101 void GraphicsView_ViewTransformer::exec()
103 GraphicsView_ViewPort* aViewPort = myViewer->getActiveViewPort();
109 case GraphicsView_Viewer::Zoom:
110 myMajorBtn = zoomButton();
111 aViewPort->setCursor( *aViewPort->getZoomCursor() );
113 case GraphicsView_Viewer::Pan:
114 myMajorBtn = panButton();
115 aViewPort->setCursor( *aViewPort->getPanCursor() );
117 case GraphicsView_Viewer::PanGlobal:
118 myMajorBtn = panGlobalButton();
119 aViewPort->setCursor( *aViewPort->getPanglCursor() );
120 aViewPort->fitAll( true );
122 case GraphicsView_Viewer::FitRect:
123 myMajorBtn = fitRectButton();
124 aViewPort->setCursor( *aViewPort->getHandCursor() );
126 case GraphicsView_Viewer::Reset:
128 onTransform( Finished );
130 case GraphicsView_Viewer::FitAll:
132 onTransform( Finished );
134 case GraphicsView_Viewer::FitSelect:
135 aViewPort->fitSelect();
136 onTransform( Finished );
138 case GraphicsView_Viewer::FitWidth:
139 aViewPort->fitWidth();
140 onTransform( Finished );
146 //================================================================
147 // Function : eventFilter
149 //================================================================
150 bool GraphicsView_ViewTransformer::eventFilter( QObject* o, QEvent* e )
154 case QEvent::GraphicsSceneMouseMove:
155 case QEvent::GraphicsSceneMousePress:
156 case QEvent::GraphicsSceneMouseRelease:
158 TransformState state = InProcess;
159 QGraphicsSceneMouseEvent* me = ( QGraphicsSceneMouseEvent* )e;
161 myButtonState = me->buttons();
162 if ( e->type() == QEvent::GraphicsSceneMousePress )
163 myButtonState |= me->button();
165 if ( e->type() == QEvent::GraphicsSceneMouseRelease )
166 myButtonState |= me->button();
168 int mouseOnlyState = ( myButtonState & ( Qt::LeftButton | Qt::MidButton | Qt::RightButton ) );
169 if ( myStart.isNull() )
172 myStart = myViewer->getActiveViewPort()->mapFromScene( me->scenePos() );
173 myMajorBtn = mouseOnlyState;
176 if ( e->type() == QEvent::GraphicsSceneMouseRelease )
179 myCurr = myViewer->getActiveViewPort()->mapFromScene( me->scenePos() );
180 onTransform( state );
185 return QObject::eventFilter( o, e );
188 //================================================================
189 // Function : onTransform
191 //================================================================
192 void GraphicsView_ViewTransformer::onTransform( TransformState state )
194 GraphicsView_ViewPort* aViewPort = myViewer->getActiveViewPort();
195 if( !aViewPort || aViewPort->isTransforming() )
198 bool doTrsf = ( myButtonState & myMajorBtn );
201 case GraphicsView_Viewer::Zoom:
202 if ( state != Finished && doTrsf )
204 aViewPort->zoom( myStart.x(), myStart.y(), myCurr.x(), myCurr.y() );
208 case GraphicsView_Viewer::Pan:
209 if ( state != Finished && doTrsf )
211 aViewPort->pan( myCurr.x() - myStart.x(), myStart.y() - myCurr.y() );
215 case GraphicsView_Viewer::PanGlobal:
216 if ( state == Finished )
218 QPointF aPoint = aViewPort->mapToScene( myCurr.toPoint() );
219 aViewPort->setCenter( aPoint.x(), aPoint.y() );
222 case GraphicsView_Viewer::FitRect:
225 QRectF aRect( qMin( myStart.x(), myCurr.x() ), qMin( myStart.y(), myCurr.y() ),
226 qAbs( myStart.x() - myCurr.x() ), qAbs( myStart.y() - myCurr.y() ) );
227 if ( !aRect.isEmpty() )
232 aRect = aViewPort->mapToScene( aRect.toRect() ).boundingRect();
233 aViewPort->fitRect( aRect );
246 if ( state == Finished )
247 myViewer->activateTransform( GraphicsView_Viewer::NoTransform );
250 //================================================================
251 // Function : drawRect
253 //================================================================
254 void GraphicsView_ViewTransformer::drawRect(const QRectF& theRect)
258 myRectBand = new QRubberBand( QRubberBand::Rectangle, myViewer->getActiveViewPort() );
260 palette.setColor(myRectBand->foregroundRole(), Qt::white);
261 myRectBand->setPalette(palette);
265 myRectBand->setGeometry( theRect.toRect() );
266 myRectBand->setVisible( theRect.isValid() );
269 //================================================================
270 // Function : endDrawRect
272 //================================================================
273 void GraphicsView_ViewTransformer::endDrawRect()
283 //================================================================
284 // Function : GraphicsView_ViewTransformer
286 //================================================================
287 int GraphicsView_ViewTransformer::type() const