QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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  if ( e->button() == Qt::LeftButton )
37  mCanvas->setCursor( QCursor( Qt::ClosedHandCursor ) );
38 }
39 
40 
42 {
43  if (( e->buttons() & Qt::LeftButton ) )
44  {
45  mDragging = true;
46  // move map and other canvas items
47  mCanvas->panAction( e );
48  }
49 }
50 
52 {
53  if ( e->button() == Qt::LeftButton )
54  {
55  if ( mDragging )
56  {
57  mCanvas->panActionEnd( e->pos() );
58  mDragging = false;
59  }
60  else // add pan to mouse cursor
61  {
62  // transform the mouse pos to map coordinates
63  QgsPoint center = mCanvas->getCoordinateTransform()->toMapPoint( e->x(), e->y() );
64  mCanvas->setCenter( center );
65  mCanvas->refresh();
66  }
67  }
69 }
void setCursor(const QCursor &)
int x() const
int y() const
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.
Qt::MouseButtons buttons() const
QString tr(const char *sourceText, const char *disambiguation, int n)
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:109
virtual void canvasPressEvent(QgsMapMouseEvent *e) override
Mouse press event.
QString mToolName
translated name of the map tool
Definition: qgsmaptool.h:227
QgsMapCanvas * mCanvas
pointer to map canvas
Definition: qgsmaptool.h:213
QCursor mCursor
cursor used in map tool
Definition: qgsmaptool.h:216
Qt::MouseButton button() const
A class to represent a point.
Definition: qgspoint.h:117
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
Overridden mouse move event.
QgsMapToolPan(QgsMapCanvas *canvas)
constructor
const QgsMapToPixel * getCoordinateTransform()
Get the current coordinate transform.
void setCenter(const QgsPoint &center)
Set the center of the map canvas, in geographical coordinates.
const QPoint & pos() const
virtual void canvasReleaseEvent(QgsMapMouseEvent *e) override
Overridden mouse release event.
void panActionEnd(QPoint releasePoint)
Ends pan action and redraws the canvas.