QGIS API Documentation  3.20.0-Odense (decaadbb31)
qgsmodelviewtoolpan.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmodelviewtoolpan.cpp
3  ------------------------------------
4  Date : March 2020
5  Copyright : (C) 2020 Nyall Dawson
6  Email : nyall dot dawson at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgsmodelviewtoolpan.h"
17 #include "qgsmodelviewmouseevent.h"
18 #include "qgsmodelgraphicsview.h"
19 #include <QScrollBar>
20 
21 QgsModelViewToolPan::QgsModelViewToolPan( QgsModelGraphicsView *view )
22  : QgsModelViewTool( view, tr( "Pan" ) )
23 {
24  setCursor( Qt::OpenHandCursor );
25 }
26 
28 {
29  mMousePressStartPos = event->pos();
30 
31  if ( event->button() != Qt::LeftButton )
32  {
33  event->ignore();
34  return;
35  }
36 
37  mIsPanning = true;
38  mLastMousePos = event->pos();
39  view()->setCursor( Qt::ClosedHandCursor );
40 }
41 
43 {
44  if ( !mIsPanning )
45  {
46  event->ignore();
47  return;
48  }
49 
50  view()->horizontalScrollBar()->setValue( view()->horizontalScrollBar()->value() - ( event->x() - mLastMousePos.x() ) );
51  view()->verticalScrollBar()->setValue( view()->verticalScrollBar()->value() - ( event->y() - mLastMousePos.y() ) );
52  mLastMousePos = event->pos();
53 }
54 
56 {
57  bool clickOnly = !isClickAndDrag( mMousePressStartPos, event->pos() );
58 
59  if ( event->button() == Qt::MiddleButton && clickOnly )
60  {
61  //middle mouse button click = recenter on point
62 
63  //get current visible part of scene
64  QRect viewportRect( 0, 0, view()->viewport()->width(), view()->viewport()->height() );
65  QgsRectangle visibleRect = QgsRectangle( view()->mapToScene( viewportRect ).boundingRect() );
66  QPointF scenePoint = event->modelPoint();
67  visibleRect.scale( 1, scenePoint.x(), scenePoint.y() );
68  QRectF boundsRect = visibleRect.toRectF();
69 
70  //zoom view to fit desired bounds
71  view()->fitInView( boundsRect, Qt::KeepAspectRatio );
72  return;
73  }
74 
75  if ( !mIsPanning || event->button() != Qt::LeftButton )
76  {
77  event->ignore();
78  return;
79  }
80 
81  mIsPanning = false;
82  view()->setCursor( Qt::OpenHandCursor );
83 }
84 
86 {
87  mIsPanning = false;
89 }
A QgsModelViewMouseEvent is the result of a user interaction with the mouse on a QgsModelGraphicsView...
void modelMoveEvent(QgsModelViewMouseEvent *event) override
Mouse move event for overriding.
void deactivate() override
Called when tool is deactivated.
void modelPressEvent(QgsModelViewMouseEvent *event) override
Mouse press event for overriding.
QgsModelViewToolPan(QgsModelGraphicsView *view)
Constructor for QgsModelViewToolPan.
void modelReleaseEvent(QgsModelViewMouseEvent *event) override
Mouse release event for overriding.
Abstract base class for all model designer view tools.
QgsModelGraphicsView * view() const
Returns the view associated with the tool.
bool isClickAndDrag(QPoint startViewPoint, QPoint endViewPoint) const
Returns true if a mouse press/release operation which started at startViewPoint and ended at endViewP...
void setCursor(const QCursor &cursor)
Sets a user defined cursor for use when the tool is active.
virtual void deactivate()
Called when tool is deactivated.
A rectangle specified with double values.
Definition: qgsrectangle.h:42
void scale(double scaleFactor, const QgsPointXY *c=nullptr)
Scale the rectangle around its center point.
Definition: qgsrectangle.h:256
QRectF toRectF() const
Returns a QRectF with same coordinates as the rectangle.
Definition: qgsrectangle.h:500