QGIS API Documentation  2.0.1-Dufour
 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 <QCursor>
25 #include <QPixmap>
26 #include "qgslogger.h"
27 
28 
30  : QgsMapTool( canvas ), mZoomOut( zoomOut ), mDragging( false ), mRubberBand( 0 )
31 {
32  // set the cursor
33  QPixmap myZoomQPixmap = QPixmap(( const char ** )( zoomOut ? zoom_out : zoom_in ) );
34  mCursor = QCursor( myZoomQPixmap, 7, 7 );
35 }
36 
38 {
39  delete mRubberBand;
40 }
41 
42 
43 void QgsMapToolZoom::canvasMoveEvent( QMouseEvent * e )
44 {
45  if ( !( e->buttons() & Qt::LeftButton ) )
46  return;
47 
48  if ( !mDragging )
49  {
50  mDragging = true;
51  delete mRubberBand;
53  mZoomRect.setTopLeft( e->pos() );
54  }
55  mZoomRect.setBottomRight( e->pos() );
56  if ( mRubberBand )
57  {
59  mRubberBand->show();
60  }
61 }
62 
63 
64 void QgsMapToolZoom::canvasPressEvent( QMouseEvent * e )
65 {
66  if ( e->button() != Qt::LeftButton )
67  return;
68 
69  mZoomRect.setRect( 0, 0, 0, 0 );
70 }
71 
72 
73 void QgsMapToolZoom::canvasReleaseEvent( QMouseEvent * e )
74 {
75  if ( e->button() != Qt::LeftButton )
76  return;
77 
78  // We are not really dragging in this case. This is sometimes caused by
79  // a pen based computer reporting a press, move, and release, all the
80  // one point.
81  if ( mDragging && ( mZoomRect.topLeft() == mZoomRect.bottomRight() ) )
82  {
83  mDragging = false;
84  delete mRubberBand;
85  mRubberBand = 0;
86  }
87 
88  if ( mDragging )
89  {
90  mDragging = false;
91  delete mRubberBand;
92  mRubberBand = 0;
93 
94  // store the rectangle
95  mZoomRect.setRight( e->pos().x() );
96  mZoomRect.setBottom( e->pos().y() );
97 
98  const QgsMapToPixel* coordinateTransform = mCanvas->getCoordinateTransform();
99 
100  // set the extent to the zoomBox
101  QgsPoint ll = coordinateTransform->toMapCoordinates( mZoomRect.left(), mZoomRect.bottom() );
102  QgsPoint ur = coordinateTransform->toMapCoordinates( mZoomRect.right(), mZoomRect.top() );
103 
104  QgsRectangle r;
105  r.setXMinimum( ll.x() );
106  r.setYMinimum( ll.y() );
107  r.setXMaximum( ur.x() );
108  r.setYMaximum( ur.y() );
109  r.normalize();
110 
111  // prevent zooming to an empty extent
112  if ( r.width() == 0 || r.height() == 0 )
113  {
114  return;
115  }
116 
117  if ( mZoomOut )
118  {
119  QgsPoint cer = r.center();
120  QgsRectangle extent = mCanvas->extent();
121 
122  double sf;
123  if ( mZoomRect.width() > mZoomRect.height() )
124  {
125  sf = extent.width() / r.width();
126  }
127  else
128  {
129  sf = extent.height() / r.height();
130  }
131  sf = sf * 2.0;
132  r.scale( sf );
133 
134  QgsDebugMsg( QString( "Extent scaled by %1 to %2" ).arg( sf ).arg( r.toString().toLocal8Bit().constData() ) );
135  QgsDebugMsg( QString( "Center of currentExtent after scaling is %1" ).arg( r.center().toString().toLocal8Bit().constData() ) );
136 
137  }
138 
139  mCanvas->setExtent( r );
140  mCanvas->refresh();
141  }
142  else // not dragging
143  {
144  // change to zoom in/out by the default multiple
145  mCanvas->zoomWithCenter( e->x(), e->y(), !mZoomOut );
146  }
147 }
148 
150 {
151  delete mRubberBand;
152  mRubberBand = 0;
153 }