QGIS API Documentation  3.12.1-BucureČ™ti (121cc00ff0)
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 }
~QgsMapToolPan() override
QgsUnitTypes::DistanceUnit lengthUnits() const
Returns the units of distance for length calculations made by this object.
A rectangle specified with double values.
Definition: qgsrectangle.h:41
void canvasDoubleClickEvent(QgsMapMouseEvent *e) override
Mouse double-click event for overriding. Default implementation does nothing.
void zoomWithCenter(int x, int y, bool zoomIn)
Zooms in/out with a given center.
void setCenter(const QgsPointXY &center)
Set the center of the map canvas, in geographical coordinates.
void panActionStart(QPoint releasePoint)
Starts a pan action.
A class to represent a 2D point.
Definition: qgspointxy.h:43
void scale(double scaleFactor, const QgsPointXY *c=nullptr)
Scale the rectangle around its center point.
Definition: qgsrectangle.h:235
A QgsMapMouseEvent is the result of a user interaction with the mouse on a QgsMapCanvas.
void refresh()
Repaints the canvas map.
bool setEllipsoid(const QString &ellipsoid)
Sets the ellipsoid by its acronym.
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:75
QgsCoordinateReferenceSystem destinationCrs() const
returns CRS of destination coordinate reference system
void canvasPressEvent(QgsMapMouseEvent *e) override
Mouse press event for overriding. Default implementation does nothing.
bool gestureEvent(QGestureEvent *e) override
gesture event for overriding. Default implementation does nothing.
QString mToolName
translated name of the map tool
Definition: qgsmaptool.h:259
virtual void activate()
called when set as currently active map tool
Definition: qgsmaptool.cpp:83
QgsMapCanvas * mCanvas
pointer to map canvas
Definition: qgsmaptool.h:241
QCursor mCursor
cursor used in map tool
Definition: qgsmaptool.h:244
void panDistanceBearingChanged(double distance, QgsUnitTypes::DistanceUnit unit, double bearing)
Emitted whenever the distance or bearing of an in-progress panning operation is changed.
QgsRectangle extent() const
Returns the current zoom extent of the map canvas.
virtual void deactivate()
called when map tool is being deactivated
Definition: qgsmaptool.cpp:99
void zoomOut()
Zoom out with fixed factor.
QgsCoordinateTransformContext transformContext
Definition: qgsproject.h:98
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
Abstract base class for all map tools.
Definition: qgsmaptool.h:62
A general purpose distance and area calculator, capable of performing ellipsoid based calculations...
QgsPointXY center() const
Gets map center, in geographical coordinates.
void panAction(QMouseEvent *event)
Called when mouse is moving and pan is activated.
void canvasMoveEvent(QgsMapMouseEvent *e) override
Mouse move event for overriding. Default implementation does nothing.
QgsMapToolPan(QgsMapCanvas *canvas)
constructor
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:450
void setExtent(const QgsRectangle &r, bool magnified=false)
Sets the extent of the map canvas.
const QgsMapToPixel * getCoordinateTransform()
Gets the current coordinate transform.
void deactivate() override
called when map tool is being deactivated
void setSourceCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets source spatial reference system crs.
void activate() override
called when set as currently active map tool
double bearing(const QgsPointXY &p1, const QgsPointXY &p2) const
Computes the bearing (in radians) between two points.
void canvasReleaseEvent(QgsMapMouseEvent *e) override
Mouse release event for overriding. Default implementation does nothing.
QgsPointXY toMapCoordinates(int x, int y) const
Transform device coordinates to map (world) coordinates.
double measureLine(const QVector< QgsPointXY > &points) const
Measures the length of a line with multiple segments.
void panActionEnd(QPoint releasePoint)
Ends pan action and redraws the canvas.