QGIS API Documentation  2.4.0-Chugiak
 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 }
A rectangle specified with double values.
Definition: qgsrectangle.h:35
void zoomWithCenter(int x, int y, bool zoomIn)
Zooms in/out with a given center.
const char * zoom_in[]
Bitmap cursors for map operations.
Definition: qgscursors.cpp:21
virtual void deactivate()
called when map tool is being deactivated
void setXMaximum(double x)
Set the maximum x value.
Definition: qgsrectangle.h:169
#define QgsDebugMsg(str)
Definition: qgslogger.h:36
void setExtent(const QgsRectangle &r)
Set the extent of the map canvas.
void refresh()
Repaints the canvas map.
virtual void canvasMoveEvent(QMouseEvent *e)
Overridden mouse move event.
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:104
double x() const
Definition: qgspoint.h:110
QgsMapCanvas * mCanvas
pointer to map canvas
Definition: qgsmaptool.h:184
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:33
QCursor mCursor
cursor used in map tool
Definition: qgsmaptool.h:187
const char * zoom_out[]
Definition: qgscursors.cpp:45
bool mZoomOut
indicates whether we're zooming in or out
A class for drawing transient features (e.g.
Definition: qgsrubberband.h:32
void setYMinimum(double y)
Set the minimum y value.
Definition: qgsrectangle.h:174
virtual void canvasPressEvent(QMouseEvent *e)
Overridden mouse press event.
QString toString() const
String representation of the point (x,y)
Definition: qgspoint.cpp:121
A class to represent a point geometry.
Definition: qgspoint.h:63
virtual void canvasReleaseEvent(QMouseEvent *e)
Overridden mouse release event.
QgsPoint toMapCoordinates(int x, int y) const
Abstract base class for all map tools.
Definition: qgsmaptool.h:48
QRect mZoomRect
stores actual zoom rect
void setToCanvasRectangle(const QRect &rect)
Sets this rubber band to a map canvas rectangle.
void setYMaximum(double y)
Set the maximum y value.
Definition: qgsrectangle.h:179
QgsRubberBand * mRubberBand
bool mDragging
Flag to indicate a map canvas drag operation is taking place.
const QgsMapToPixel * getCoordinateTransform()
Get the current coordinate transform.
double y() const
Definition: qgspoint.h:118
QgsRectangle extent() const
Returns the current zoom exent of the map canvas.
void normalize()
Normalize the rectangle so it has non-negative width/height.
double width() const
Width of the rectangle.
Definition: qgsrectangle.h:204
QString toString(bool automaticPrecision=false) const
returns string representation of form xmin,ymin xmax,ymax
QgsPoint center() const
Center point of the rectangle.
Definition: qgsrectangle.h:214
QgsMapToolZoom(QgsMapCanvas *canvas, bool zoomOut)
constructor
void setXMinimum(double x)
Set the minimum x value.
Definition: qgsrectangle.h:164
double height() const
Height of the rectangle.
Definition: qgsrectangle.h:209
void scale(double scaleFactor, const QgsPoint *c=0)
Scale the rectangle around its center point.