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