QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 #include "qgsmaptoolpan.h"
19 #include "qgsmapcanvas.h"
20 #include "qgsmaptopixel.h"
21 #include "qgsmapmouseevent.h"
22 #include "qgsproject.h"
23 #include "qgslogger.h"
24 
25 
27  : QgsMapTool( canvas )
28  , mDragging( false )
29 {
30  mToolName = tr( "Pan" );
31  // set cursor
32  mCursor = QCursor( Qt::OpenHandCursor );
33 }
34 
36 {
37  mCanvas->ungrabGesture( Qt::PinchGesture );
38 }
39 
41 {
42  mCanvas->grabGesture( Qt::PinchGesture );
44 }
45 
47 {
48  mCanvas->ungrabGesture( Qt::PinchGesture );
50 }
51 
52 QgsMapTool::Flags QgsMapToolPan::flags() const
53 {
55 }
56 
58 {
59  if ( e->button() == Qt::LeftButton )
60  {
61  mCanvas->setCursor( QCursor( Qt::ClosedHandCursor ) );
62  mCanvas->panActionStart( e->pos() );
63  }
64 }
65 
66 
68 {
69  if ( !mPinching )
70  {
71  if ( ( e->buttons() & Qt::LeftButton ) )
72  {
73  mDragging = true;
74  // move map and other canvas items
75  mCanvas->panAction( e );
76  }
77  }
78 }
79 
81 {
82  if ( !mPinching )
83  {
84  if ( e->button() == Qt::LeftButton )
85  {
86  if ( mDragging )
87  {
88  mCanvas->panActionEnd( e->pos() );
89  mDragging = false;
90  }
91  else // add pan to mouse cursor
92  {
94  {
95  // transform the mouse pos to map coordinates
96  const QgsPointXY prevCenter = mCanvas->center();
97  QgsPointXY center = mCanvas->getCoordinateTransform()->toMapCoordinates( e->x(), e->y() );
98  mCanvas->setCenter( center );
99  mCanvas->refresh();
100 
101  QgsDistanceArea da;
102  da.setEllipsoid( QgsProject::instance()->ellipsoid() );
104  emit panDistanceBearingChanged( da.measureLine( center, prevCenter ), da.lengthUnits(), da.bearing( center, prevCenter ) * 180 / M_PI );
105  }
106  }
107  }
108  }
109  mCanvas->setCursor( mCursor );
110 }
111 
113 {
114  if ( !QTouchDevice::devices().isEmpty() && !mPinching )
115  {
116  mCanvas->zoomWithCenter( e->x(), e->y(), true );
117  }
118 }
119 
120 bool QgsMapToolPan::gestureEvent( QGestureEvent *event )
121 {
122  if ( QTouchDevice::devices().isEmpty() )
123  return true; // no touch support
124 
125  if ( QGesture *gesture = event->gesture( Qt::PinchGesture ) )
126  {
127  mPinching = true;
128  pinchTriggered( static_cast<QPinchGesture *>( gesture ) );
129  }
130  return true;
131 }
132 
133 void QgsMapToolPan::pinchTriggered( QPinchGesture *gesture )
134 {
135  if ( gesture->state() == Qt::GestureFinished )
136  {
137  //a very small totalScaleFactor indicates a two finger tap (pinch gesture without pinching)
138  if ( 0.98 < gesture->totalScaleFactor() && gesture->totalScaleFactor() < 1.02 )
139  {
140  mCanvas->zoomOut();
141  }
142  else
143  {
144  //Transfor global coordinates to widget coordinates
145  QPoint pos = gesture->centerPoint().toPoint();
146  pos = mCanvas->mapFromGlobal( pos );
147  // transform the mouse pos to map coordinates
148  QgsPointXY center = mCanvas->getCoordinateTransform()->toMapCoordinates( pos.x(), pos.y() );
149  QgsRectangle r = mCanvas->extent();
150  r.scale( 1 / gesture->totalScaleFactor(), &center );
151  mCanvas->setExtent( r );
152  mCanvas->refresh();
153  }
154  mPinching = false;
155  }
156 }
QgsMapTool::mCanvas
QgsMapCanvas * mCanvas
pointer to map canvas
Definition: qgsmaptool.h:264
QgsMapToolPan::gestureEvent
bool gestureEvent(QGestureEvent *e) override
gesture event for overriding. Default implementation does nothing.
Definition: qgsmaptoolpan.cpp:120
QgsMapCanvas::extent
QgsRectangle extent() const
Returns the current zoom extent of the map canvas.
Definition: qgsmapcanvas.cpp:1056
QgsMapCanvas::refresh
void refresh()
Repaints the canvas map.
Definition: qgsmapcanvas.cpp:525
qgsmapcanvas.h
QgsMapCanvas::zoomOut
void zoomOut()
Zoom out with fixed factor.
Definition: qgsmapcanvas.cpp:1937
qgsmaptopixel.h
QgsMapCanvas::mapSettings
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
Definition: qgsmapcanvas.cpp:391
QgsMapToolPan::deactivate
void deactivate() override
called when map tool is being deactivated
Definition: qgsmaptoolpan.cpp:46
QgsMapCanvas
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:85
QgsMapCanvas::panActionEnd
void panActionEnd(QPoint releasePoint)
Ends pan action and redraws the canvas.
Definition: qgsmapcanvas.cpp:2291
QgsMapTool::deactivate
virtual void deactivate()
called when map tool is being deactivated
Definition: qgsmaptool.cpp:96
QgsMapCanvas::center
QgsPointXY center() const
Gets map center, in geographical coordinates.
Definition: qgsmapcanvas.cpp:1165
QgsProject::transformContext
QgsCoordinateTransformContext transformContext
Definition: qgsproject.h:101
QgsProject::instance
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:468
QgsMapToolPan::flags
Flags flags() const override
Returns the flags for the map tool.
Definition: qgsmaptoolpan.cpp:52
QgsMapTool::Transient
@ Transient
Definition: qgsmaptool.h:91
QgsMapCanvas::zoomWithCenter
void zoomWithCenter(int x, int y, bool zoomIn)
Zooms in/out with a given center.
Definition: qgsmapcanvas.cpp:1948
QgsMapCanvas::panAction
void panAction(QMouseEvent *event)
Called when mouse is moving and pan is activated.
Definition: qgsmapcanvas.cpp:2319
QgsMapToolPan::canvasMoveEvent
void canvasMoveEvent(QgsMapMouseEvent *e) override
Mouse move event for overriding. Default implementation does nothing.
Definition: qgsmaptoolpan.cpp:67
QgsMapToolPan::canvasReleaseEvent
void canvasReleaseEvent(QgsMapMouseEvent *e) override
Mouse release event for overriding. Default implementation does nothing.
Definition: qgsmaptoolpan.cpp:80
QgsMapToPixel::toMapCoordinates
QgsPointXY toMapCoordinates(int x, int y) const
Transform device coordinates to map (world) coordinates.
Definition: qgsmaptopixel.cpp:108
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:42
QgsMapCanvas::allowInteraction
bool allowInteraction(QgsMapCanvasInteractionBlocker::Interaction interaction) const
Returns true if the specified interaction is currently permitted on the canvas.
Definition: qgsmapcanvas.cpp:954
QgsMapTool
Abstract base class for all map tools.
Definition: qgsmaptool.h:64
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:1150
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:1067
QgsMapCanvas::getCoordinateTransform
const QgsMapToPixel * getCoordinateTransform()
Gets the current coordinate transform.
Definition: qgsmapcanvas.cpp:335
QgsMapTool::activate
virtual void activate()
called when set as currently active map tool
Definition: qgsmaptool.cpp:80
QgsPointXY
A class to represent a 2D point.
Definition: qgspointxy.h:44
QgsMapTool::mCursor
QCursor mCursor
cursor used in map tool
Definition: qgsmaptool.h:267
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:318
QgsMapToolPan::canvasPressEvent
void canvasPressEvent(QgsMapMouseEvent *e) override
Mouse press event for overriding. Default implementation does nothing.
Definition: qgsmaptoolpan.cpp:57
QgsMapMouseEvent
A QgsMapMouseEvent is the result of a user interaction with the mouse on a QgsMapCanvas.
Definition: qgsmapmouseevent.h:36
QgsMapToolPan::~QgsMapToolPan
~QgsMapToolPan() override
Definition: qgsmaptoolpan.cpp:35
QgsDistanceArea
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
Definition: qgsdistancearea.h:50
QgsMapTool::AllowZoomRect
@ AllowZoomRect
Allow zooming by rectangle (by holding shift and dragging) while the tool is active.
Definition: qgsmaptool.h:95
qgslogger.h
QgsMapCanvas::panActionStart
void panActionStart(QPoint releasePoint)
Starts a pan action.
Definition: qgsmapcanvas.cpp:2310
qgsmapmouseevent.h
QgsMapToolPan::activate
void activate() override
called when set as currently active map tool
Definition: qgsmaptoolpan.cpp:40
QgsMapToolPan::canvasDoubleClickEvent
void canvasDoubleClickEvent(QgsMapMouseEvent *e) override
Mouse double-click event for overriding. Default implementation does nothing.
Definition: qgsmaptoolpan.cpp:112
QgsMapTool::ShowContextMenu
@ ShowContextMenu
Show a context menu when right-clicking with the tool (since QGIS 3.14). See populateContextMenu().
Definition: qgsmaptool.h:96
qgsproject.h
QgsMapCanvasInteractionBlocker::Interaction::MapPanOnSingleClick
@ MapPanOnSingleClick
A map pan interaction caused by a single click and release on the map canvas.
QgsMapTool::mToolName
QString mToolName
translated name of the map tool
Definition: qgsmaptool.h:282
QgsMapToolPan::QgsMapToolPan
QgsMapToolPan(QgsMapCanvas *canvas)
constructor
Definition: qgsmaptoolpan.cpp:26