QGIS API Documentation  2.4.0-Chugiak
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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 
52 void QgsMapToolTouch::canvasMoveEvent( QMouseEvent * e )
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 
65 void QgsMapToolTouch::canvasReleaseEvent( QMouseEvent * e )
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 
95 bool QgsMapToolTouch::gestureEvent( QGestureEvent *event )
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 }
bool mDragging
Flag to indicate a map canvas drag operation is taking place.
A rectangle specified with double values.
Definition: qgsrectangle.h:35
void pinchTriggered(QPinchGesture *gesture)
void zoomWithCenter(int x, int y, bool zoomIn)
Zooms in/out with a given center.
bool gestureEvent(QGestureEvent *event)
void setExtent(const QgsRectangle &r)
Set the extent of the map canvas.
void refresh()
Repaints the canvas map.
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:104
virtual void activate()
called when set as currently active map tool
Definition: qgsmaptool.cpp:77
QgsMapCanvas * mCanvas
pointer to map canvas
Definition: qgsmaptool.h:184
bool mPinching
Flag to indicate a pinch gesture is taking place.
virtual void canvasMoveEvent(QMouseEvent *e)
Overridden mouse move event.
QgsMapToolTouch(QgsMapCanvas *canvas)
constructor
virtual void deactivate()
called when map tool is being deactivated
Definition: qgsmaptool.cpp:91
A class to represent a point geometry.
Definition: qgspoint.h:63
void zoomOut()
Zoom out with fixed factor.
Abstract base class for all map tools.
Definition: qgsmaptool.h:48
void panAction(QMouseEvent *event)
Called when mouse is moving and pan is activated.
const QgsMapToPixel * getCoordinateTransform()
Get the current coordinate transform.
virtual void canvasReleaseEvent(QMouseEvent *e)
Overridden mouse release event.
virtual void canvasDoubleClickEvent(QMouseEvent *e)
Overridden Mouse double click event.
QgsRectangle extent() const
Returns the current zoom exent of the map canvas.
void activate()
called when set as currently active map tool
void deactivate()
called when map tool is being deactivated
QgsPoint toMapPoint(double x, double y) const
void panActionEnd(QPoint releasePoint)
Ends pan action and redraws the canvas.
void scale(double scaleFactor, const QgsPoint *c=0)
Scale the rectangle around its center point.