QGIS API Documentation  3.2.0-Bonn (bc43194)
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 "qgsmaptopixel.h"
19 #include <QBitmap>
20 #include <QCursor>
21 #include <QMouseEvent>
22 
23 
25  : QgsMapTool( canvas )
26  , mDragging( false )
27 {
28  mToolName = tr( "Pan" );
29  // set cursor
30  mCursor = QCursor( Qt::OpenHandCursor );
31 }
32 
34 {
35  mCanvas->ungrabGesture( Qt::PinchGesture );
36 }
37 
39 {
40  mCanvas->grabGesture( Qt::PinchGesture );
42 }
43 
45 {
46  mCanvas->ungrabGesture( Qt::PinchGesture );
48 }
49 
51 {
52  if ( e->button() == Qt::LeftButton )
53  mCanvas->setCursor( QCursor( Qt::ClosedHandCursor ) );
54 }
55 
56 
58 {
59  if ( !mPinching )
60  {
61  if ( ( e->buttons() & Qt::LeftButton ) )
62  {
63  mDragging = true;
64  // move map and other canvas items
65  mCanvas->panAction( e );
66  }
67  }
68 }
69 
71 {
72  if ( !mPinching )
73  {
74  if ( e->button() == Qt::LeftButton )
75  {
76  if ( mDragging )
77  {
78  mCanvas->panActionEnd( e->pos() );
79  mDragging = false;
80  }
81  else // add pan to mouse cursor
82  {
83  // transform the mouse pos to map coordinates
84  QgsPointXY center = mCanvas->getCoordinateTransform()->toMapPoint( e->x(), e->y() );
85  mCanvas->setCenter( center );
86  mCanvas->refresh();
87  }
88  }
89  }
90  mCanvas->setCursor( mCursor );
91 }
92 
94 {
95  if ( !QTouchDevice::devices().isEmpty() && !mPinching )
96  {
97  mCanvas->zoomWithCenter( e->x(), e->y(), true );
98  }
99 }
100 
101 bool QgsMapToolPan::gestureEvent( QGestureEvent *event )
102 {
103  if ( QTouchDevice::devices().isEmpty() )
104  return true; // no touch support
105 
106  qDebug() << "gesture " << event;
107  if ( QGesture *gesture = event->gesture( Qt::PinchGesture ) )
108  {
109  mPinching = true;
110  pinchTriggered( static_cast<QPinchGesture *>( gesture ) );
111  }
112  return true;
113 }
114 
115 void QgsMapToolPan::pinchTriggered( QPinchGesture *gesture )
116 {
117  if ( gesture->state() == Qt::GestureFinished )
118  {
119  //a very small totalScaleFactor indicates a two finger tap (pinch gesture without pinching)
120  if ( 0.98 < gesture->totalScaleFactor() && gesture->totalScaleFactor() < 1.02 )
121  {
122  mCanvas->zoomOut();
123  }
124  else
125  {
126  //Transfor global coordinates to widget coordinates
127  QPoint pos = gesture->centerPoint().toPoint();
128  pos = mCanvas->mapFromGlobal( pos );
129  // transform the mouse pos to map coordinates
130  QgsPointXY center = mCanvas->getCoordinateTransform()->toMapPoint( pos.x(), pos.y() );
131  QgsRectangle r = mCanvas->extent();
132  r.scale( 1 / gesture->totalScaleFactor(), &center );
133  mCanvas->setExtent( r );
134  mCanvas->refresh();
135  }
136  mPinching = false;
137  }
138 }
~QgsMapToolPan() override
A rectangle specified with double values.
Definition: qgsrectangle.h:40
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:234
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:74
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:254
virtual void activate()
called when set as currently active map tool
Definition: qgsmaptool.cpp:81
QgsMapCanvas * mCanvas
pointer to map canvas
Definition: qgsmaptool.h:236
QCursor mCursor
cursor used in map tool
Definition: qgsmaptool.h:239
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:97
void zoomOut()
Zoom out with fixed factor.
Abstract base class for all map tools.
Definition: qgsmaptool.h:63
QgsPointXY toMapPoint(double x, double y) const
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.
void panActionEnd(QPoint releasePoint)
Ends pan action and redraws the canvas.