QGIS API Documentation  3.2.0-Bonn (bc43194)
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 "qgsrubberband.h"
20 
21 #include <QMouseEvent>
22 #include <QRect>
23 #include <QColor>
24 #include <QCursor>
25 #include <QPixmap>
26 #include "qgslogger.h"
27 
28 
30  : QgsMapTool( canvas )
31  , mZoomOut( zoomOut )
32  , mDragging( false )
33 
34 {
35  mToolName = tr( "Zoom" );
36  // set the cursor
37  mCursor = zoomOut ? QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomOut ) :
38  QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomIn );
39 }
40 
42 {
43  delete mRubberBand;
44 }
45 
46 
48 {
49  if ( !( e->buttons() & Qt::LeftButton ) )
50  return;
51 
52  if ( !mDragging )
53  {
54  mDragging = true;
55  delete mRubberBand;
57  QColor color( Qt::blue );
58  color.setAlpha( 63 );
59  mRubberBand->setColor( color );
60  mZoomRect.setTopLeft( e->pos() );
61  }
62  mZoomRect.setBottomRight( e->pos() );
63  if ( mRubberBand )
64  {
66  mRubberBand->show();
67  }
68 }
69 
70 
72 {
73  if ( e->button() != Qt::LeftButton )
74  return;
75 
76  mZoomRect.setRect( 0, 0, 0, 0 );
77 }
78 
79 
81 {
82  if ( e->button() != Qt::LeftButton )
83  return;
84 
85  bool zoomOut = mZoomOut;
86  if ( e->modifiers() & Qt::AltModifier )
87  zoomOut = !zoomOut;
88 
89  // We are not really dragging in this case. This is sometimes caused by
90  // a pen based computer reporting a press, move, and release, all the
91  // one point.
92  if ( mDragging && ( mZoomRect.topLeft() == mZoomRect.bottomRight() ) )
93  {
94  mDragging = false;
95  delete mRubberBand;
96  mRubberBand = nullptr;
97  }
98 
99  if ( mDragging )
100  {
101  mDragging = false;
102  delete mRubberBand;
103  mRubberBand = nullptr;
104 
105  // store the rectangle
106  mZoomRect.setRight( e->pos().x() );
107  mZoomRect.setBottom( e->pos().y() );
108 
109  //account for bottom right -> top left dragging
110  mZoomRect = mZoomRect.normalized();
111 
112  // set center and zoom
113  const QSize &zoomRectSize = mZoomRect.size();
114  const QgsMapSettings &mapSettings = mCanvas->mapSettings();
115  const QSize &canvasSize = mapSettings.outputSize();
116  double sfx = ( double )zoomRectSize.width() / canvasSize.width();
117  double sfy = ( double )zoomRectSize.height() / canvasSize.height();
118  double sf = std::max( sfx, sfy );
119 
121  QgsPointXY c = m2p->toMapCoordinates( mZoomRect.center() );
122 
123  mCanvas->zoomByFactor( zoomOut ? 1.0 / sf : sf, &c );
124 
125  mCanvas->refresh();
126  }
127  else // not dragging
128  {
129  // change to zoom in/out by the default multiple
130  mCanvas->zoomWithCenter( e->x(), e->y(), !zoomOut );
131  }
132 }
133 
135 {
136  delete mRubberBand;
137  mRubberBand = nullptr;
138 
140 }
void deactivate() override
called when map tool is being deactivated
void zoomWithCenter(int x, int y, bool zoomIn)
Zooms in/out with a given center.
void setToCanvasRectangle(QRect rect)
Sets this rubber band to a map canvas rectangle.
~QgsMapToolZoom() override
A class to represent a 2D point.
Definition: qgspointxy.h:43
A QgsMapMouseEvent is the result of a user interaction with the mouse on a QgsMapCanvas.
void refresh()
Repaints the canvas map.
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:74
The QgsMapSettings class contains configuration for rendering of the map.
QString mToolName
translated name of the map tool
Definition: qgsmaptool.h:254
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
QgsMapCanvas * mCanvas
pointer to map canvas
Definition: qgsmaptool.h:236
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:36
void canvasReleaseEvent(QgsMapMouseEvent *e) override
Mouse release event for overriding. Default implementation does nothing.
QCursor mCursor
cursor used in map tool
Definition: qgsmaptool.h:239
void canvasMoveEvent(QgsMapMouseEvent *e) override
Mouse move event for overriding. Default implementation does nothing.
bool mZoomOut
indicates whether we&#39;re zooming in or out
A class for drawing transient features (e.g.
Definition: qgsrubberband.h:37
void canvasPressEvent(QgsMapMouseEvent *e) override
Mouse press event for overriding. Default implementation does nothing.
virtual void deactivate()
called when map tool is being deactivated
Definition: qgsmaptool.cpp:97
void zoomByFactor(double scaleFactor, const QgsPointXY *center=nullptr)
Zoom with the factor supplied.
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
Abstract base class for all map tools.
Definition: qgsmaptool.h:63
QRect mZoomRect
stores actual zoom rect
QgsRubberBand * mRubberBand
bool mDragging
Flag to indicate a map canvas drag operation is taking place.
const QgsMapToPixel * getCoordinateTransform()
Gets the current coordinate transform.
void setColor(const QColor &color)
Sets the color for the rubberband.
static QCursor getThemeCursor(Cursor cursor)
Helper to get a theme cursor.
QgsMapToolZoom(QgsMapCanvas *canvas, bool zoomOut)
constructor
QSize outputSize() const
Returns the size of the resulting map image.
QgsPointXY toMapCoordinates(int x, int y) const