QGIS API Documentation  2.99.0-Master (13a3f2f)
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 "qgsmaptoolpan.h"
17 #include "qgsmapcanvas.h"
18 #include "qgscursors.h"
19 #include "qgsmaptopixel.h"
20 #include <QBitmap>
21 #include <QCursor>
22 #include <QMouseEvent>
23 
24 
26  : QgsMapTool( canvas )
27  , mDragging( false )
28 {
29  mToolName = tr( "Pan" );
30  // set cursor
31  mCursor = QCursor( Qt::OpenHandCursor );
32 }
33 
35 {
36  mCanvas->ungrabGesture( Qt::PinchGesture );
37 }
38 
40 {
41  mCanvas->grabGesture( Qt::PinchGesture );
43 }
44 
46 {
47  mCanvas->ungrabGesture( Qt::PinchGesture );
49 }
50 
52 {
53  if ( e->button() == Qt::LeftButton )
54  mCanvas->setCursor( QCursor( Qt::ClosedHandCursor ) );
55 }
56 
57 
59 {
60  if ( !mPinching )
61  {
62  if ( ( e->buttons() & Qt::LeftButton ) )
63  {
64  mDragging = true;
65  // move map and other canvas items
66  mCanvas->panAction( e );
67  }
68  }
69 }
70 
72 {
73  if ( !mPinching )
74  {
75  if ( e->button() == Qt::LeftButton )
76  {
77  if ( mDragging )
78  {
79  mCanvas->panActionEnd( e->pos() );
80  mDragging = false;
81  }
82  else // add pan to mouse cursor
83  {
84  // transform the mouse pos to map coordinates
85  QgsPoint center = mCanvas->getCoordinateTransform()->toMapPoint( e->x(), e->y() );
86  mCanvas->setCenter( center );
87  mCanvas->refresh();
88  }
89  }
90  }
91  mCanvas->setCursor( mCursor );
92 }
93 
95 {
96  if ( !QTouchDevice::devices().isEmpty() && !mPinching )
97  {
98  mCanvas->zoomWithCenter( e->x(), e->y(), true );
99  }
100 }
101 
102 bool QgsMapToolPan::gestureEvent( QGestureEvent *event )
103 {
104  if ( QTouchDevice::devices().isEmpty() )
105  return true; // no touch support
106 
107  qDebug() << "gesture " << event;
108  if ( QGesture *gesture = event->gesture( Qt::PinchGesture ) )
109  {
110  mPinching = true;
111  pinchTriggered( static_cast<QPinchGesture *>( gesture ) );
112  }
113  return true;
114 }
115 
116 void QgsMapToolPan::pinchTriggered( QPinchGesture *gesture )
117 {
118  if ( gesture->state() == Qt::GestureFinished )
119  {
120  //a very small totalScaleFactor indicates a two finger tap (pinch gesture without pinching)
121  if ( 0.98 < gesture->totalScaleFactor() && gesture->totalScaleFactor() < 1.02 )
122  {
123  mCanvas->zoomOut();
124  }
125  else
126  {
127  //Transfor global coordinates to widget coordinates
128  QPoint pos = gesture->centerPoint().toPoint();
129  pos = mCanvas->mapFromGlobal( pos );
130  // transform the mouse pos to map coordinates
131  QgsPoint center = mCanvas->getCoordinateTransform()->toMapPoint( pos.x(), pos.y() );
132  QgsRectangle r = mCanvas->extent();
133  r.scale( 1 / gesture->totalScaleFactor(), &center );
134  mCanvas->setExtent( r );
135  mCanvas->refresh();
136  }
137  mPinching = false;
138  }
139 }
A rectangle specified with double values.
Definition: qgsrectangle.h:38
virtual 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 scale(double scaleFactor, const QgsPoint *c=nullptr)
Scale the rectangle around its center point.
QgsPoint toMapPoint(double x, double y) const
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
virtual 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:207
virtual void activate()
called when set as currently active map tool
Definition: qgsmaptool.cpp:84
QgsMapCanvas * mCanvas
pointer to map canvas
Definition: qgsmaptool.h:193
QCursor mCursor
cursor used in map tool
Definition: qgsmaptool.h:196
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:100
A class to represent a point.
Definition: qgspoint.h:37
void zoomOut()
Zoom out with fixed factor.
Abstract base class for all map tools.
Definition: qgsmaptool.h:50
void panAction(QMouseEvent *event)
Called when mouse is moving and pan is activated.
virtual 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)
Set the extent of the map canvas.
const QgsMapToPixel * getCoordinateTransform()
Get the current coordinate transform.
void setCenter(const QgsPoint &center)
Set the center of the map canvas, in geographical coordinates.
void deactivate() override
called when map tool is being deactivated
void activate() override
called when set as currently active map tool
virtual void canvasReleaseEvent(QgsMapMouseEvent *e) override
Mouse release event for overriding. Default implementation does nothing.
void panActionEnd(QPoint releasePoint)
Ends pan action and redraws the canvas.