QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
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 "moc_qgsmaptoolpan.cpp"
20#include "qgsmapcanvas.h"
21#include "qgsmaptopixel.h"
22#include "qgsmapmouseevent.h"
23#include "qgsproject.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 if ( mCanvas )
38 mCanvas->ungrabGesture( Qt::PinchGesture );
39}
40
42{
43 mCanvas->grabGesture( Qt::PinchGesture );
45}
46
48{
49 mCanvas->ungrabGesture( Qt::PinchGesture );
51}
52
57
59{
60 if ( e->button() == Qt::LeftButton )
61 {
62 mCanvas->setCursor( QCursor( Qt::ClosedHandCursor ) );
63 mCanvas->panActionStart( e->pos() );
64 }
65}
66
67
69{
70 if ( !mPinching )
71 {
72 if ( ( e->buttons() & Qt::LeftButton ) )
73 {
74 mDragging = true;
75 // move map and other canvas items
76 mCanvas->panAction( e );
77 }
78 }
79}
80
82{
83 if ( !mPinching )
84 {
85 if ( e->button() == Qt::LeftButton )
86 {
87 if ( mDragging )
88 {
89 mCanvas->panActionEnd( e->pos() );
90 mDragging = false;
91 }
92 else // add pan to mouse cursor
93 {
95 {
96 // transform the mouse pos to map coordinates
97 const QgsPointXY prevCenter = mCanvas->center();
98 const QgsPointXY center = mCanvas->getCoordinateTransform()->toMapCoordinates( e->x(), e->y() );
99 mCanvas->setCenter( center );
100 mCanvas->refresh();
101
103 da.setEllipsoid( QgsProject::instance()->ellipsoid() );
104 da.setSourceCrs( mCanvas->mapSettings().destinationCrs(), QgsProject::instance()->transformContext() );
105 try
106 {
107 emit panDistanceBearingChanged( da.measureLine( center, prevCenter ), da.lengthUnits(), da.bearing( center, prevCenter ) * 180 / M_PI );
108 }
109 catch ( QgsCsException & )
110 {}
111 }
112 }
113 }
114 }
115 mCanvas->setCursor( mCursor );
116}
117
119{
120 if ( !mPinching )
121 {
122 mCanvas->zoomWithCenter( e->x(), e->y(), true );
123 }
124}
125
126bool QgsMapToolPan::gestureEvent( QGestureEvent *event )
127{
128 if ( QGesture *gesture = event->gesture( Qt::PinchGesture ) )
129 {
130 mPinching = true;
131 pinchTriggered( static_cast<QPinchGesture *>( gesture ) );
132 }
133 return true;
134}
135
136void QgsMapToolPan::pinchTriggered( QPinchGesture *gesture )
137{
138 if ( gesture->state() == Qt::GestureFinished )
139 {
140 //a very small totalScaleFactor indicates a two finger tap (pinch gesture without pinching)
141 if ( 0.98 < gesture->totalScaleFactor() && gesture->totalScaleFactor() < 1.02 )
142 {
143 mCanvas->zoomOut();
144 }
145 else
146 {
147 //Transfor global coordinates to widget coordinates
148 QPoint pos = gesture->centerPoint().toPoint();
149 pos = mCanvas->mapFromGlobal( pos );
150 // transform the mouse pos to map coordinates
151 const QgsPointXY center = mCanvas->getCoordinateTransform()->toMapCoordinates( pos.x(), pos.y() );
152 QgsRectangle r = mCanvas->extent();
153 r.scale( 1 / gesture->totalScaleFactor(), &center );
154 mCanvas->setExtent( r );
155 mCanvas->refresh();
156 }
157 mPinching = false;
158 }
159}
Custom exception class for Coordinate Reference System related exceptions.
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
double bearing(const QgsPointXY &p1, const QgsPointXY &p2) const
Computes the bearing (in radians) between two points.
double measureLine(const QVector< QgsPointXY > &points) const
Measures the length of a line with multiple segments.
void setSourceCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets source spatial reference system crs.
Qgis::DistanceUnit lengthUnits() const
Returns the units of distance for length calculations made by this object.
bool setEllipsoid(const QString &ellipsoid)
Sets the ellipsoid by its acronym.
@ MapPanOnSingleClick
A map pan interaction caused by a single click and release on the map canvas.
Map canvas is a class for displaying all GIS data types on a canvas.
A QgsMapMouseEvent is the result of a user interaction with the mouse on a QgsMapCanvas.
~QgsMapToolPan() override
void canvasPressEvent(QgsMapMouseEvent *e) override
Mouse press event for overriding. Default implementation does nothing.
void deactivate() override
called when map tool is being deactivated
QgsMapToolPan(QgsMapCanvas *canvas)
constructor
void canvasDoubleClickEvent(QgsMapMouseEvent *e) override
Mouse double-click event for overriding. Default implementation does nothing.
void panDistanceBearingChanged(double distance, Qgis::DistanceUnit unit, double bearing)
Emitted whenever the distance or bearing of an in-progress panning operation is changed.
void canvasMoveEvent(QgsMapMouseEvent *e) override
Mouse move event for overriding. Default implementation does nothing.
void canvasReleaseEvent(QgsMapMouseEvent *e) override
Mouse release event for overriding. Default implementation does nothing.
bool gestureEvent(QGestureEvent *e) override
gesture event for overriding. Default implementation does nothing.
void activate() override
called when set as currently active map tool
Flags flags() const override
Returns the flags for the map tool.
Abstract base class for all map tools.
Definition qgsmaptool.h:71
QPointer< QgsMapCanvas > mCanvas
The pointer to the map canvas.
Definition qgsmaptool.h:341
QFlags< Flag > Flags
Definition qgsmaptool.h:116
QString mToolName
The translated name of the map tool.
Definition qgsmaptool.h:359
@ AllowZoomRect
Allow zooming by rectangle (by holding shift and dragging) while the tool is active.
Definition qgsmaptool.h:113
@ ShowContextMenu
Show a context menu when right-clicking with the tool (since QGIS 3.14). See populateContextMenu().
Definition qgsmaptool.h:114
virtual void activate()
called when set as currently active map tool
QCursor mCursor
The cursor used in the map tool.
Definition qgsmaptool.h:344
virtual void deactivate()
called when map tool is being deactivated
A class to represent a 2D point.
Definition qgspointxy.h:60
static QgsProject * instance()
Returns the QgsProject singleton instance.
QgsCoordinateTransformContext transformContext
Definition qgsproject.h:113
A rectangle specified with double values.
void scale(double scaleFactor, const QgsPointXY *c=nullptr)
Scale the rectangle around its center point.