Quantum GIS API Documentation
1.8
|
00001 /*************************************************************************** 00002 qgsmaptooltouch.cpp - map tool for zooming and panning using qgestures 00003 ---------------------- 00004 begin : February 2012 00005 copyright : (C) 2012 by Marco Bernasocchi 00006 email : marco at bernawebdesign.ch 00007 *************************************************************************** 00008 * * 00009 * This program is free software; you can redistribute it and/or modify * 00010 * it under the terms of the GNU General Public License as published by * 00011 * the Free Software Foundation; either version 2 of the License, or * 00012 * (at your option) any later version. * 00013 * * 00014 ***************************************************************************/ 00015 00016 #include "qgsmaptooltouch.h" 00017 #include "qgsmapcanvas.h" 00018 #include "qgscursors.h" 00019 #include "qgsmaptopixel.h" 00020 #include <QBitmap> 00021 #include <QCursor> 00022 #include <QMouseEvent> 00023 #include <qgslogger.h> 00024 00025 00026 QgsMapToolTouch::QgsMapToolTouch( QgsMapCanvas* canvas ) 00027 : QgsMapTool( canvas ), mDragging( false ), mPinching( false ) 00028 { 00029 // set cursor 00030 // QBitmap panBmp = QBitmap::fromData( QSize( 16, 16 ), pan_bits ); 00031 // QBitmap panBmpMask = QBitmap::fromData( QSize( 16, 16 ), pan_mask_bits ); 00032 // mCursor = QCursor( panBmp, panBmpMask, 5, 5 ); 00033 } 00034 00035 QgsMapToolTouch::~QgsMapToolTouch() 00036 { 00037 mCanvas->ungrabGesture( Qt::PinchGesture ); 00038 } 00039 00040 void QgsMapToolTouch::activate() 00041 { 00042 mCanvas->grabGesture( Qt::PinchGesture ); 00043 QgsMapTool::activate(); 00044 } 00045 00046 void QgsMapToolTouch::deactivate() 00047 { 00048 mCanvas->ungrabGesture( Qt::PinchGesture ); 00049 QgsMapTool::deactivate(); 00050 } 00051 00052 void QgsMapToolTouch::canvasMoveEvent( QMouseEvent * e ) 00053 { 00054 if ( !mPinching ) 00055 { 00056 if (( e->buttons() & Qt::LeftButton ) ) 00057 { 00058 mDragging = true; 00059 // move map and other canvas items 00060 mCanvas->panAction( e ); 00061 } 00062 } 00063 } 00064 00065 void QgsMapToolTouch::canvasReleaseEvent( QMouseEvent * e ) 00066 { 00067 if ( !mPinching ) 00068 { 00069 if ( e->button() == Qt::LeftButton ) 00070 { 00071 if ( mDragging ) 00072 { 00073 mCanvas->panActionEnd( e->pos() ); 00074 mDragging = false; 00075 } 00076 else // add pan to mouse cursor 00077 { 00078 // transform the mouse pos to map coordinates 00079 QgsPoint center = mCanvas->getCoordinateTransform()->toMapPoint( e->x(), e->y() ); 00080 mCanvas->setExtent( QgsRectangle( center, center ) ); 00081 mCanvas->refresh(); 00082 } 00083 } 00084 } 00085 } 00086 00087 void QgsMapToolTouch::canvasDoubleClickEvent( QMouseEvent *e ) 00088 { 00089 if ( !mPinching ) 00090 { 00091 mCanvas->zoomWithCenter( e->x(), e->y(), true ); 00092 } 00093 } 00094 00095 bool QgsMapToolTouch::gestureEvent( QGestureEvent *event ) 00096 { 00097 qDebug() << "gesture " << event; 00098 if ( QGesture *gesture = event->gesture( Qt::PinchGesture ) ) 00099 { 00100 mPinching = true; 00101 pinchTriggered( static_cast<QPinchGesture *>( gesture ) ); 00102 } 00103 return true; 00104 } 00105 00106 00107 void QgsMapToolTouch::pinchTriggered( QPinchGesture *gesture ) 00108 { 00109 if ( gesture->state() == Qt::GestureFinished ) 00110 { 00111 //a very small totalScaleFactor indicates a two finger tap (pinch gesture without pinching) 00112 if ( 0.98 < gesture->totalScaleFactor() && gesture->totalScaleFactor() < 1.02 ) 00113 { 00114 mCanvas->zoomOut(); 00115 } 00116 else 00117 { 00118 //Transfor global coordinates to widget coordinates 00119 QPoint pos = gesture->centerPoint().toPoint(); 00120 pos = mCanvas->mapFromGlobal( pos ); 00121 // transform the mouse pos to map coordinates 00122 QgsPoint center = mCanvas->getCoordinateTransform()->toMapPoint( pos.x(), pos.y() ); 00123 QgsRectangle r = mCanvas->extent(); 00124 r.scale( 1 / gesture->totalScaleFactor(), ¢er ); 00125 mCanvas->setExtent( r ); 00126 mCanvas->refresh(); 00127 } 00128 mPinching = false; 00129 } 00130 }