QGIS API Documentation  3.6.0-Noosa (5873452)
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 
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 
53 {
54  if ( e->button() == Qt::LeftButton )
55  mCanvas->setCursor( QCursor( Qt::ClosedHandCursor ) );
56 }
57 
58 
60 {
61  if ( !mPinching )
62  {
63  if ( ( e->buttons() & Qt::LeftButton ) )
64  {
65  mDragging = true;
66  // move map and other canvas items
67  mCanvas->panAction( e );
68  }
69  }
70 }
71 
73 {
74  if ( !mPinching )
75  {
76  if ( e->button() == Qt::LeftButton )
77  {
78  if ( mDragging )
79  {
80  mCanvas->panActionEnd( e->pos() );
81  mDragging = false;
82  }
83  else // add pan to mouse cursor
84  {
85  // transform the mouse pos to map coordinates
86  QgsPointXY center = mCanvas->getCoordinateTransform()->toMapCoordinates( e->x(), e->y() );
87  mCanvas->setCenter( center );
88  mCanvas->refresh();
89  }
90  }
91  }
92  mCanvas->setCursor( mCursor );
93 }
94 
96 {
97  if ( !QTouchDevice::devices().isEmpty() && !mPinching )
98  {
99  mCanvas->zoomWithCenter( e->x(), e->y(), true );
100  }
101 }
102 
103 bool QgsMapToolPan::gestureEvent( QGestureEvent *event )
104 {
105  if ( QTouchDevice::devices().isEmpty() )
106  return true; // no touch support
107 
108  qDebug() << "gesture " << event;
109  if ( QGesture *gesture = event->gesture( Qt::PinchGesture ) )
110  {
111  mPinching = true;
112  pinchTriggered( static_cast<QPinchGesture *>( gesture ) );
113  }
114  return true;
115 }
116 
117 void QgsMapToolPan::pinchTriggered( QPinchGesture *gesture )
118 {
119  if ( gesture->state() == Qt::GestureFinished )
120  {
121  //a very small totalScaleFactor indicates a two finger tap (pinch gesture without pinching)
122  if ( 0.98 < gesture->totalScaleFactor() && gesture->totalScaleFactor() < 1.02 )
123  {
124  mCanvas->zoomOut();
125  }
126  else
127  {
128  //Transfor global coordinates to widget coordinates
129  QPoint pos = gesture->centerPoint().toPoint();
130  pos = mCanvas->mapFromGlobal( pos );
131  // transform the mouse pos to map coordinates
132  QgsPointXY center = mCanvas->getCoordinateTransform()->toMapCoordinates( pos.x(), pos.y() );
133  QgsRectangle r = mCanvas->extent();
134  r.scale( 1 / gesture->totalScaleFactor(), &center );
135  mCanvas->setExtent( r );
136  mCanvas->refresh();
137  }
138  mPinching = false;
139  }
140 }
~QgsMapToolPan() override
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.
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.
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:73
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:82
QgsMapCanvas * mCanvas
pointer to map canvas
Definition: qgsmaptool.h:241
QCursor mCursor
cursor used in map tool
Definition: qgsmaptool.h:244
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:98
void zoomOut()
Zoom out with fixed factor.
Abstract base class for all map tools.
Definition: qgsmaptool.h:62
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
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 activate() override
called when set as currently active map tool
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.
void panActionEnd(QPoint releasePoint)
Ends pan action and redraws the canvas.