QGIS API Documentation  2.9.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsmaptoolzoom.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmaptoolzoom.cpp - map tool for zooming
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 "qgsmaptoolzoom.h"
17 #include "qgsmapcanvas.h"
18 #include "qgsmaptopixel.h"
19 #include "qgscursors.h"
20 #include "qgsrubberband.h"
21 
22 #include <QMouseEvent>
23 #include <QRect>
24 #include <QColor>
25 #include <QCursor>
26 #include <QPixmap>
27 #include "qgslogger.h"
28 
29 
31  : QgsMapTool( canvas )
32  , mZoomOut( zoomOut )
33  , mDragging( false )
34  , mRubberBand( 0 )
35 {
36  mToolName = tr( "Zoom" );
37  // set the cursor
38  QPixmap myZoomQPixmap = QPixmap(( const char ** )( zoomOut ? zoom_out : zoom_in ) );
39  mCursor = QCursor( myZoomQPixmap, 7, 7 );
40 }
41 
43 {
44  delete mRubberBand;
45 }
46 
47 
48 void QgsMapToolZoom::canvasMoveEvent( QMouseEvent * e )
49 {
50  if ( !( e->buttons() & Qt::LeftButton ) )
51  return;
52 
53  if ( !mDragging )
54  {
55  mDragging = true;
56  delete mRubberBand;
58  QColor color( Qt::blue );
59  color.setAlpha( 63 );
60  mRubberBand->setColor( color );
61  mZoomRect.setTopLeft( e->pos() );
62  }
63  mZoomRect.setBottomRight( e->pos() );
64  if ( mRubberBand )
65  {
67  mRubberBand->show();
68  }
69 }
70 
71 
72 void QgsMapToolZoom::canvasPressEvent( QMouseEvent * e )
73 {
74  if ( e->button() != Qt::LeftButton )
75  return;
76 
77  mZoomRect.setRect( 0, 0, 0, 0 );
78 }
79 
80 
81 void QgsMapToolZoom::canvasReleaseEvent( QMouseEvent * e )
82 {
83  if ( e->button() != Qt::LeftButton )
84  return;
85 
86  // We are not really dragging in this case. This is sometimes caused by
87  // a pen based computer reporting a press, move, and release, all the
88  // one point.
89  if ( mDragging && ( mZoomRect.topLeft() == mZoomRect.bottomRight() ) )
90  {
91  mDragging = false;
92  delete mRubberBand;
93  mRubberBand = 0;
94  }
95 
96  if ( mDragging )
97  {
98  mDragging = false;
99  delete mRubberBand;
100  mRubberBand = 0;
101 
102  // store the rectangle
103  mZoomRect.setRight( e->pos().x() );
104  mZoomRect.setBottom( e->pos().y() );
105 
106  // set center and zoom
107  const QSize& zoomRectSize = mZoomRect.size();
108  const QgsMapSettings& mapSettings = mCanvas->mapSettings();
109  const QSize& canvasSize = mapSettings.outputSize();
110  double sfx = ( double )zoomRectSize.width() / canvasSize.width();
111  double sfy = ( double )zoomRectSize.height() / canvasSize.height();
112  double sf = qMax( sfx, sfy );
113 
115  QgsPoint c = m2p->toMapCoordinates( mZoomRect.center() );
116 
117  mCanvas->setCenter( c );
118  mCanvas->zoomByFactor( mZoomOut ? 1.0 / sf : sf );
119 
120  mCanvas->refresh();
121  }
122  else // not dragging
123  {
124  // change to zoom in/out by the default multiple
125  mCanvas->zoomWithCenter( e->x(), e->y(), !mZoomOut );
126  }
127 }
128 
130 {
131  delete mRubberBand;
132  mRubberBand = 0;
133 
135 }