QGIS API Documentation  2.11.0-Master
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 
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  }
64  if ( mRubberBand )
65  {
67  mRubberBand->show();
68  }
69 }
70 
71 
73 {
74  if ( e->button() != Qt::LeftButton )
75  return;
76 
77  mZoomRect.setRect( 0, 0, 0, 0 );
78 }
79 
80 
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  //account for bottom right -> top left dragging
108 
109  // set center and zoom
110  const QSize& zoomRectSize = mZoomRect.size();
111  const QgsMapSettings& mapSettings = mCanvas->mapSettings();
112  const QSize& canvasSize = mapSettings.outputSize();
113  double sfx = ( double )zoomRectSize.width() / canvasSize.width();
114  double sfy = ( double )zoomRectSize.height() / canvasSize.height();
115  double sf = qMax( sfx, sfy );
116 
119 
120  mCanvas->zoomByFactor( mZoomOut ? 1.0 / sf : sf, &c );
121 
122  mCanvas->refresh();
123  }
124  else // not dragging
125  {
126  // change to zoom in/out by the default multiple
127  mCanvas->zoomWithCenter( e->x(), e->y(), !mZoomOut );
128  }
129 }
130 
132 {
133  delete mRubberBand;
134  mRubberBand = 0;
135 
137 }
virtual void deactivate() override
Flag to indicate a map canvas drag operation is taking place.
void setBottom(int y)
QSize size() const
QRect normalized() const
void zoomWithCenter(int x, int y, bool zoomIn)
Zooms in/out with a given center.
int width() const
void zoomByFactor(double scaleFactor, const QgsPoint *center=0)
Zoom with the factor supplied.
const char * zoom_in[]
Bitmap cursors for map operations.
Definition: qgscursors.cpp:21
void setBottomRight(const QPoint &position)
int x() const
int y() const
QPoint bottomRight() const
const QgsMapSettings & mapSettings() const
Get access to properties used for map rendering.
void refresh()
Repaints the canvas map.
void setAlpha(int alpha)
Qt::MouseButtons buttons() const
QString tr(const char *sourceText, const char *disambiguation, int n)
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:105
int x() const
int y() const
virtual void canvasReleaseEvent(QMouseEvent *e) override
Overridden mouse release event.
The QgsMapSettings class contains configuration for rendering of the map.
QString mToolName
translated name of the map tool
Definition: qgsmaptool.h:206
QgsMapCanvas * mCanvas
pointer to map canvas
Definition: qgsmaptool.h:192
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:34
QCursor mCursor
cursor used in map tool
Definition: qgsmaptool.h:195
const char * zoom_out[]
Definition: qgscursors.cpp:45
QSize outputSize() const
Return the size of the resulting map image.
bool mZoomOut
indicates whether we're zooming in or out
A class for drawing transient features (e.g.
Definition: qgsrubberband.h:32
Qt::MouseButton button() const
QPoint center() const
virtual void deactivate()
called when map tool is being deactivated
Definition: qgsmaptool.cpp:99
A class to represent a point.
Definition: qgspoint.h:63
QgsPoint toMapCoordinates(int x, int y) const
Abstract base class for all map tools.
Definition: qgsmaptool.h:49
void setTopLeft(const QPoint &position)
void setRight(int x)
QRect mZoomRect
stores actual zoom rect
void setToCanvasRectangle(const QRect &rect)
Sets this rubber band to a map canvas rectangle.
void setRect(int x, int y, int width, int height)
QgsRubberBand * mRubberBand
bool mDragging
Flag to indicate a map canvas drag operation is taking place.
int height() const
QPoint topLeft() const
const QgsMapToPixel * getCoordinateTransform()
Get the current coordinate transform.
void setColor(const QColor &color)
Set the color for the rubberband.
const QPoint & pos() const
virtual void canvasMoveEvent(QMouseEvent *e) override
Overridden mouse move event.
virtual void canvasPressEvent(QMouseEvent *e) override
Overridden mouse press event.
QgsMapToolZoom(QgsMapCanvas *canvas, bool zoomOut)
constructor