QGIS API Documentation  2.7.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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  const QgsMapToPixel* coordinateTransform = mCanvas->getCoordinateTransform();
107 
108  // set the extent to the zoomBox
109  QgsPoint ll = coordinateTransform->toMapCoordinates( mZoomRect.left(), mZoomRect.bottom() );
110  QgsPoint ur = coordinateTransform->toMapCoordinates( mZoomRect.right(), mZoomRect.top() );
111 
112  QgsRectangle r;
113  r.setXMinimum( ll.x() );
114  r.setYMinimum( ll.y() );
115  r.setXMaximum( ur.x() );
116  r.setYMaximum( ur.y() );
117  r.normalize();
118 
119  // prevent zooming to an empty extent
120  if ( r.width() == 0 || r.height() == 0 )
121  {
122  return;
123  }
124 
125  if ( mZoomOut )
126  {
127  QgsPoint cer = r.center();
128  QgsRectangle extent = mCanvas->extent();
129 
130  double sf;
131  if ( mZoomRect.width() > mZoomRect.height() )
132  {
133  sf = extent.width() / r.width();
134  }
135  else
136  {
137  sf = extent.height() / r.height();
138  }
139  sf = sf * 2.0;
140  r.scale( sf );
141 
142  QgsDebugMsg( QString( "Extent scaled by %1 to %2" ).arg( sf ).arg( r.toString().toLocal8Bit().constData() ) );
143  QgsDebugMsg( QString( "Center of currentExtent after scaling is %1" ).arg( r.center().toString().toLocal8Bit().constData() ) );
144 
145  }
146 
147  mCanvas->setExtent( r );
148  mCanvas->refresh();
149  }
150  else // not dragging
151  {
152  // change to zoom in/out by the default multiple
153  mCanvas->zoomWithCenter( e->x(), e->y(), !mZoomOut );
154  }
155 }
156 
158 {
159  delete mRubberBand;
160  mRubberBand = 0;
161 }
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:163
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
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:126
QString mToolName
translated name of the map tool
Definition: qgsmaptool.h:202
QgsMapCanvas * mCanvas
pointer to map canvas
Definition: qgsmaptool.h:188
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:34
QCursor mCursor
cursor used in map tool
Definition: qgsmaptool.h:191
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:168
virtual void canvasPressEvent(QMouseEvent *e)
Overridden mouse press event.
QString toString() const
String representation of the point (x,y)
Definition: qgspoint.cpp:126
A class to represent a point.
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:173
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:134
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:198
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:208
QgsMapToolZoom(QgsMapCanvas *canvas, bool zoomOut)
constructor
void setXMinimum(double x)
Set the minimum x value.
Definition: qgsrectangle.h:158
double height() const
Height of the rectangle.
Definition: qgsrectangle.h:203
#define tr(sourceText)
void scale(double scaleFactor, const QgsPoint *c=0)
Scale the rectangle around its center point.