QGIS API Documentation  3.13.0-Master (788156190c)
qgsmaptoolpan.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmaptoolpan.h - map tool for panning in map canvas
3  ---------------------
4  begin : January 2006
5  copyright : (C) 2006 by Martin Dobias
6  email : wonder.sk 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 <QBitmap>
17 #include <QCursor>
18 
19 #include "qgsmaptoolpan.h"
20 #include "qgsmapcanvas.h"
21 #include "qgsmaptopixel.h"
22 #include "qgsmapmouseevent.h"
23 #include "qgsproject.h"
24 #include "qgslogger.h"
25 
26 
28  : QgsMapTool( canvas )
29  , mDragging( false )
30 {
31  mToolName = tr( "Pan" );
32  // set cursor
33  mCursor = QCursor( Qt::OpenHandCursor );
34 }
35 
37 {
38  mCanvas->ungrabGesture( Qt::PinchGesture );
39 }
40 
42 {
43  mCanvas->grabGesture( Qt::PinchGesture );
45 }
46 
48 {
49  mCanvas->ungrabGesture( Qt::PinchGesture );
51 }
52 
54 {
55  if ( e->button() == Qt::LeftButton )
56  {
57  mCanvas->setCursor( QCursor( Qt::ClosedHandCursor ) );
58  mCanvas->panActionStart( e->pos() );
59  }
60 }
61 
62 
64 {
65  if ( !mPinching )
66  {
67  if ( ( e->buttons() & Qt::LeftButton ) )
68  {
69  mDragging = true;
70  // move map and other canvas items
71  mCanvas->panAction( e );
72  }
73  }
74 }
75 
77 {
78  if ( !mPinching )
79  {
80  if ( e->button() == Qt::LeftButton )
81  {
82  if ( mDragging )
83  {
84  mCanvas->panActionEnd( e->pos() );
85  mDragging = false;
86  }
87  else // add pan to mouse cursor
88  {
89  // transform the mouse pos to map coordinates
90  const QgsPointXY prevCenter = mCanvas->center();
91  QgsPointXY center = mCanvas->getCoordinateTransform()->toMapCoordinates( e->x(), e->y() );
92  mCanvas->setCenter( center );
93  mCanvas->refresh();
94 
95  QgsDistanceArea da;
96  da.setEllipsoid( QgsProject::instance()->ellipsoid() );
98  emit panDistanceBearingChanged( da.measureLine( center, prevCenter ), da.lengthUnits(), da.bearing( center, prevCenter ) * 180 / M_PI );
99  }
100  }
101  }
102  mCanvas->setCursor( mCursor );
103 }
104 
106 {
107  if ( !QTouchDevice::devices().isEmpty() && !mPinching )
108  {
109  mCanvas->zoomWithCenter( e->x(), e->y(), true );
110  }
111 }
112 
113 bool QgsMapToolPan::gestureEvent( QGestureEvent *event )
114 {
115  if ( QTouchDevice::devices().isEmpty() )
116  return true; // no touch support
117 
118  if ( QGesture *gesture = event->gesture( Qt::PinchGesture ) )
119  {
120  mPinching = true;
121  pinchTriggered( static_cast<QPinchGesture *>( gesture ) );
122  }
123  return true;
124 }
125 
126 void QgsMapToolPan::pinchTriggered( QPinchGesture *gesture )
127 {
128  if ( gesture->state() == Qt::GestureFinished )
129  {
130  //a very small totalScaleFactor indicates a two finger tap (pinch gesture without pinching)
131  if ( 0.98 < gesture->totalScaleFactor() && gesture->totalScaleFactor() < 1.02 )
132  {
133  mCanvas->zoomOut();
134  }
135  else
136  {
137  //Transfor global coordinates to widget coordinates
138  QPoint pos = gesture->centerPoint().toPoint();
139  pos = mCanvas->mapFromGlobal( pos );
140  // transform the mouse pos to map coordinates
141  QgsPointXY center = mCanvas->getCoordinateTransform()->toMapCoordinates( pos.x(), pos.y() );
142  QgsRectangle r = mCanvas->extent();
143  r.scale( 1 / gesture->totalScaleFactor(), &center );
144  mCanvas->setExtent( r );
145  mCanvas->refresh();
146  }
147  mPinching = false;
148  }
149 }
QgsMapTool::mCanvas
QgsMapCanvas * mCanvas
pointer to map canvas
Definition: qgsmaptool.h:241
QgsMapToolPan::gestureEvent
bool gestureEvent(QGestureEvent *e) override
gesture event for overriding. Default implementation does nothing.
Definition: qgsmaptoolpan.cpp:113
QgsMapCanvas::extent
QgsRectangle extent() const
Returns the current zoom extent of the map canvas.
Definition: qgsmapcanvas.cpp:865
QgsMapCanvas::refresh
void refresh()
Repaints the canvas map.
Definition: qgsmapcanvas.cpp:508
qgsmapcanvas.h
QgsMapCanvas::zoomOut
void zoomOut()
Zoom out with fixed factor.
Definition: qgsmapcanvas.cpp:1719
qgsmaptopixel.h
QgsMapCanvas::mapSettings
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
Definition: qgsmapcanvas.cpp:380
QgsMapToolPan::deactivate
void deactivate() override
called when map tool is being deactivated
Definition: qgsmaptoolpan.cpp:47
QgsMapCanvas
Definition: qgsmapcanvas.h:78
QgsMapCanvas::panActionEnd
void panActionEnd(QPoint releasePoint)
Ends pan action and redraws the canvas.
Definition: qgsmapcanvas.cpp:2061
QgsMapTool::deactivate
virtual void deactivate()
called when map tool is being deactivated
Definition: qgsmaptool.cpp:99
QgsMapCanvas::center
QgsPointXY center() const
Gets map center, in geographical coordinates.
Definition: qgsmapcanvas.cpp:963
QgsProject::transformContext
QgsCoordinateTransformContext transformContext
Definition: qgsproject.h:99
QgsProject::instance
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:457
QgsMapCanvas::zoomWithCenter
void zoomWithCenter(int x, int y, bool zoomIn)
Zooms in/out with a given center.
Definition: qgsmapcanvas.cpp:1730
QgsMapCanvas::panAction
void panAction(QMouseEvent *event)
Called when mouse is moving and pan is activated.
Definition: qgsmapcanvas.cpp:2089
QgsMapToolPan::canvasMoveEvent
void canvasMoveEvent(QgsMapMouseEvent *e) override
Mouse move event for overriding. Default implementation does nothing.
Definition: qgsmaptoolpan.cpp:63
QgsMapToolPan::canvasReleaseEvent
void canvasReleaseEvent(QgsMapMouseEvent *e) override
Mouse release event for overriding. Default implementation does nothing.
Definition: qgsmaptoolpan.cpp:76
QgsMapToPixel::toMapCoordinates
QgsPointXY toMapCoordinates(int x, int y) const
Transform device coordinates to map (world) coordinates.
Definition: qgsmaptopixel.cpp:108
QgsRectangle
Definition: qgsrectangle.h:41
QgsMapTool
Definition: qgsmaptool.h:62
QgsRectangle::scale
void scale(double scaleFactor, const QgsPointXY *c=nullptr)
Scale the rectangle around its center point.
Definition: qgsrectangle.h:235
QgsDistanceArea::setEllipsoid
bool setEllipsoid(const QString &ellipsoid)
Sets the ellipsoid by its acronym.
Definition: qgsdistancearea.cpp:66
qgsmaptoolpan.h
QgsDistanceArea::bearing
double bearing(const QgsPointXY &p1, const QgsPointXY &p2) const
Computes the bearing (in radians) between two points.
Definition: qgsdistancearea.cpp:842
QgsDistanceArea::measureLine
double measureLine(const QVector< QgsPointXY > &points) const
Measures the length of a line with multiple segments.
Definition: qgsdistancearea.cpp:273
QgsDistanceArea::lengthUnits
QgsUnitTypes::DistanceUnit lengthUnits() const
Returns the units of distance for length calculations made by this object.
Definition: qgsdistancearea.cpp:789
QgsMapCanvas::setCenter
void setCenter(const QgsPointXY &center)
Set the center of the map canvas, in geographical coordinates.
Definition: qgsmapcanvas.cpp:945
QgsDistanceArea::setSourceCrs
void setSourceCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets source spatial reference system crs.
Definition: qgsdistancearea.cpp:60
QgsMapCanvas::setExtent
void setExtent(const QgsRectangle &r, bool magnified=false)
Sets the extent of the map canvas to the specified rectangle.
Definition: qgsmapcanvas.cpp:876
QgsMapCanvas::getCoordinateTransform
const QgsMapToPixel * getCoordinateTransform()
Gets the current coordinate transform.
Definition: qgsmapcanvas.cpp:324
QgsMapTool::activate
virtual void activate()
called when set as currently active map tool
Definition: qgsmaptool.cpp:83
QgsPointXY
Definition: qgspointxy.h:43
QgsMapTool::mCursor
QCursor mCursor
cursor used in map tool
Definition: qgsmaptool.h:244
QgsMapToolPan::panDistanceBearingChanged
void panDistanceBearingChanged(double distance, QgsUnitTypes::DistanceUnit unit, double bearing)
Emitted whenever the distance or bearing of an in-progress panning operation is changed.
QgsMapSettings::destinationCrs
QgsCoordinateReferenceSystem destinationCrs() const
returns CRS of destination coordinate reference system
Definition: qgsmapsettings.cpp:317
QgsMapToolPan::canvasPressEvent
void canvasPressEvent(QgsMapMouseEvent *e) override
Mouse press event for overriding. Default implementation does nothing.
Definition: qgsmaptoolpan.cpp:53
QgsMapMouseEvent
Definition: qgsmapmouseevent.h:35
QgsMapToolPan::~QgsMapToolPan
~QgsMapToolPan() override
Definition: qgsmaptoolpan.cpp:36
QgsDistanceArea
Definition: qgsdistancearea.h:49
qgslogger.h
QgsMapCanvas::panActionStart
void panActionStart(QPoint releasePoint)
Starts a pan action.
Definition: qgsmapcanvas.cpp:2080
qgsmapmouseevent.h
QgsMapToolPan::activate
void activate() override
called when set as currently active map tool
Definition: qgsmaptoolpan.cpp:41
QgsMapToolPan::canvasDoubleClickEvent
void canvasDoubleClickEvent(QgsMapMouseEvent *e) override
Mouse double-click event for overriding. Default implementation does nothing.
Definition: qgsmaptoolpan.cpp:105
qgsproject.h
QgsMapTool::mToolName
QString mToolName
translated name of the map tool
Definition: qgsmaptool.h:259
QgsMapToolPan::QgsMapToolPan
QgsMapToolPan(QgsMapCanvas *canvas)
constructor
Definition: qgsmaptoolpan.cpp:27