QGIS API Documentation  2.2.0-Valmiera
 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 }