QGIS API Documentation  2.11.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
qgsmaptooltouch.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmaptooltouch.cpp - map tool for zooming and panning using qgestures
3  ----------------------
4  begin : February 2012
5  copyright : (C) 2012 by Marco Bernasocchi
6  email : marco at bernawebdesign.ch
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 "qgsmaptooltouch.h"
17 #include "qgsmapcanvas.h"
18 #include "qgscursors.h"
19 #include "qgsmaptopixel.h"
20 #include <QBitmap>
21 #include <QCursor>
22 #include <QMouseEvent>
23 #include <qgslogger.h>
24 
25 
27  : QgsMapTool( canvas ), mDragging( false ), mPinching( false )
28 {
29  // set cursor
30 // QBitmap panBmp = QBitmap::fromData( QSize( 16, 16 ), pan_bits );
31 // QBitmap panBmpMask = QBitmap::fromData( QSize( 16, 16 ), pan_mask_bits );
32 // mCursor = QCursor( panBmp, panBmpMask, 5, 5 );
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 ( !mPinching )
55  {
56  if (( e->buttons() & Qt::LeftButton ) )
57  {
58  mDragging = true;
59  // move map and other canvas items
60  mCanvas->panAction( e );
61  }
62  }
63 }
64 
66 {
67  if ( !mPinching )
68  {
69  if ( e->button() == Qt::LeftButton )
70  {
71  if ( mDragging )
72  {
73  mCanvas->panActionEnd( e->pos() );
74  mDragging = false;
75  }
76  else // add pan to mouse cursor
77  {
78  // transform the mouse pos to map coordinates
79  QgsPoint center = mCanvas->getCoordinateTransform()->toMapPoint( e->x(), e->y() );
80  mCanvas->setExtent( QgsRectangle( center, center ) );
81  mCanvas->refresh();
82  }
83  }
84  }
85 }
86 
88 {
89  if ( !mPinching )
90  {
91  mCanvas->zoomWithCenter( e->x(), e->y(), true );
92  }
93 }
94 
96 {
97  qDebug() << "gesture " << event;
98  if ( QGesture *gesture = event->gesture( Qt::PinchGesture ) )
99  {
100  mPinching = true;
101  pinchTriggered( static_cast<QPinchGesture *>( gesture ) );
102  }
103  return true;
104 }
105 
106 
107 void QgsMapToolTouch::pinchTriggered( QPinchGesture *gesture )
108 {
109  if ( gesture->state() == Qt::GestureFinished )
110  {
111  //a very small totalScaleFactor indicates a two finger tap (pinch gesture without pinching)
112  if ( 0.98 < gesture->totalScaleFactor() && gesture->totalScaleFactor() < 1.02 )
113  {
114  mCanvas->zoomOut();
115  }
116  else
117  {
118  //Transfor global coordinates to widget coordinates
119  QPoint pos = gesture->centerPoint().toPoint();
120  pos = mCanvas->mapFromGlobal( pos );
121  // transform the mouse pos to map coordinates
122  QgsPoint center = mCanvas->getCoordinateTransform()->toMapPoint( pos.x(), pos.y() );
123  QgsRectangle r = mCanvas->extent();
124  r.scale( 1 / gesture->totalScaleFactor(), &center );
125  mCanvas->setExtent( r );
126  mCanvas->refresh();
127  }
128  mPinching = false;
129  }
130 }
A rectangle specified with double values.
Definition: qgsrectangle.h:35
void zoomWithCenter(int x, int y, bool zoomIn)
Zooms in/out with a given center.
bool gestureEvent(QGestureEvent *event) override
void deactivate() override
called when map tool is being deactivated
void setExtent(const QgsRectangle &r)
Set the extent of the map canvas.
int x() const
int y() const
void refresh()
Repaints the canvas map.
Qt::MouseButtons buttons() const
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:105
int x() const
int y() const
QGesture * gesture(Qt::GestureType type) const
virtual bool event(QEvent *e)
virtual void activate()
called when set as currently active map tool
Definition: qgsmaptool.cpp:83
QgsMapCanvas * mCanvas
pointer to map canvas
Definition: qgsmaptool.h:192
virtual void canvasReleaseEvent(QMouseEvent *e) override
Overridden mouse release event.
Qt::MouseButton button() const
QgsMapToolTouch(QgsMapCanvas *canvas)
constructor
virtual void deactivate()
called when map tool is being deactivated
Definition: qgsmaptool.cpp:99
A class to represent a point.
Definition: qgspoint.h:63
void zoomOut()
Zoom out with fixed factor.
Abstract base class for all map tools.
Definition: qgsmaptool.h:49
virtual void canvasMoveEvent(QMouseEvent *e) override
Overridden mouse move event.
void panAction(QMouseEvent *event)
Called when mouse is moving and pan is activated.
QPoint mapFromGlobal(const QPoint &pos) const
const QgsMapToPixel * getCoordinateTransform()
Get the current coordinate transform.
void activate() override
called when set as currently active map tool
QgsRectangle extent() const
Returns the current zoom exent of the map canvas.
const QPoint & pos() const
virtual void canvasDoubleClickEvent(QMouseEvent *e) override
Overridden Mouse double click event.
void ungrabGesture(Qt::GestureType gesture)
QgsPoint toMapPoint(double x, double y) const
void panActionEnd(QPoint releasePoint)
Ends pan action and redraws the canvas.
void grabGesture(Qt::GestureType gesture, QFlags< Qt::GestureFlag > flags)
void scale(double scaleFactor, const QgsPoint *c=0)
Scale the rectangle around its center point.