QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 
17 #include <QRect>
18 #include <QColor>
19 #include <QCursor>
20 #include <QPixmap>
21 
22 #include "qgsmaptoolzoom.h"
23 #include "qgsmapcanvas.h"
24 #include "qgsmaptopixel.h"
25 #include "qgsrubberband.h"
26 #include "qgslogger.h"
27 #include "qgsmapmouseevent.h"
28 #include "qgsapplication.h"
29 
30 
31 
33  : QgsMapTool( canvas )
34  , mZoomOut( zoomOut )
35  , mNativeZoomOut( zoomOut )
36  , mDragging( false )
37  , mZoomOutCursor( QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomOut ) )
38  , mZoomInCursor( QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomIn ) )
39 
40 {
41  mToolName = tr( "Zoom" );
42  setZoomMode( mNativeZoomOut, true );
43 }
44 
46 {
47  delete mRubberBand;
48 }
49 
50 QgsMapTool::Flags QgsMapToolZoom::flags() const
51 {
53 }
54 
56 {
57  if ( !( e->buttons() & Qt::LeftButton ) )
58  return;
59 
60  setZoomMode( e->modifiers().testFlag( Qt::AltModifier ) ^ mNativeZoomOut );
61 
62  if ( !mDragging )
63  {
64  mDragging = true;
65  delete mRubberBand;
67  QColor color( Qt::blue );
68  color.setAlpha( 63 );
69  mRubberBand->setColor( color );
70  mZoomRect.setTopLeft( e->pos() );
71  }
72 
73  mZoomRect.setBottomRight( e->pos() );
74  if ( mRubberBand )
75  {
77  mRubberBand->show();
78  }
79 }
80 
81 
83 {
84  if ( e->button() != Qt::LeftButton )
85  return;
86 
87  mZoomRect.setRect( 0, 0, 0, 0 );
88 }
89 
90 
92 {
93  if ( e->button() != Qt::LeftButton )
94  return;
95 
96  setZoomMode( e->modifiers().testFlag( Qt::AltModifier ) ^ mNativeZoomOut );
97 
98  // We are not really dragging in this case. This is sometimes caused by
99  // a pen based computer reporting a press, move, and release, all the
100  // one point.
101  bool tooShort = ( mZoomRect.topLeft() - mZoomRect.bottomRight() ).manhattanLength() < mMinPixelZoom;
102  if ( !mDragging || tooShort )
103  {
104  mDragging = false;
105  delete mRubberBand;
106  mRubberBand = nullptr;
107 
108  // change to zoom in/out by the default multiple
109  mCanvas->zoomWithCenter( e->x(), e->y(), !mZoomOut );
110  }
111  else
112  {
113  mDragging = false;
114  delete mRubberBand;
115  mRubberBand = nullptr;
116 
117  // store the rectangle
118  mZoomRect.setRight( e->pos().x() );
119  mZoomRect.setBottom( e->pos().y() );
120 
121  //account for bottom right -> top left dragging
122  mZoomRect = mZoomRect.normalized();
123 
124  // set center and zoom
125  const QSize &zoomRectSize = mZoomRect.size();
126  const QgsMapSettings &mapSettings = mCanvas->mapSettings();
127  const QSize &canvasSize = mapSettings.outputSize();
128  double sfx = static_cast<double>( zoomRectSize.width() ) / canvasSize.width();
129  double sfy = static_cast<double>( zoomRectSize.height() ) / canvasSize.height();
130  double sf = std::max( sfx, sfy );
131 
133  QgsPointXY c = m2p->toMapCoordinates( mZoomRect.center() );
134 
135  mCanvas->zoomByFactor( mZoomOut ? 1.0 / sf : sf, &c );
136 
137  mCanvas->refresh();
138  }
139 }
140 
142 {
143  delete mRubberBand;
144  mRubberBand = nullptr;
145 
147 }
148 
149 void QgsMapToolZoom::setZoomMode( bool zoomOut, bool force )
150 {
151  if ( !force && zoomOut == mZoomOut )
152  return;
153 
154  mZoomOut = zoomOut;
156 }
157 
158 void QgsMapToolZoom::keyPressEvent( QKeyEvent *e )
159 {
160  if ( e->key() == Qt::Key_Alt )
161  {
162  setZoomMode( !mNativeZoomOut );
163  }
164 }
165 
167 {
168  // key press events are not caught wile the mouse is pressed
169  // this is detected in map canvas move event
170 
171  if ( e->key() == Qt::Key_Alt )
172  {
173  setZoomMode( mNativeZoomOut );
174  }
175 }
QgsMapTool::mCanvas
QgsMapCanvas * mCanvas
pointer to map canvas
Definition: qgsmaptool.h:264
QgsMapToolZoom::deactivate
void deactivate() override
called when map tool is being deactivated
Definition: qgsmaptoolzoom.cpp:141
QgsMapCanvas::refresh
void refresh()
Repaints the canvas map.
Definition: qgsmapcanvas.cpp:525
QgsMapSettings::outputSize
QSize outputSize() const
Returns the size of the resulting map image.
Definition: qgsmapsettings.cpp:235
qgsmapcanvas.h
QgsMapCanvas::zoomByFactor
void zoomByFactor(double scaleFactor, const QgsPointXY *center=nullptr, bool ignoreScaleLock=false)
Zoom with the factor supplied.
Definition: qgsmapcanvas.cpp:2536
qgsmaptopixel.h
QgsMapCanvas::mapSettings
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
Definition: qgsmapcanvas.cpp:391
QgsRubberBand
A class for drawing transient features (e.g.
Definition: qgsrubberband.h:50
QgsMapToolZoom::mZoomInCursor
QCursor mZoomInCursor
Definition: qgsmaptoolzoom.h:64
QgsMapTool::setCursor
virtual void setCursor(const QCursor &cursor)
Sets a user defined cursor.
Definition: qgsmaptool.cpp:146
QgsMapCanvas
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:85
QgsMapTool::deactivate
virtual void deactivate()
called when map tool is being deactivated
Definition: qgsmaptool.cpp:96
QgsMapToolZoom::canvasPressEvent
void canvasPressEvent(QgsMapMouseEvent *e) override
Mouse press event for overriding. Default implementation does nothing.
Definition: qgsmaptoolzoom.cpp:82
QgsMapTool::Transient
@ Transient
Definition: qgsmaptool.h:91
QgsMapToolZoom::canvasReleaseEvent
void canvasReleaseEvent(QgsMapMouseEvent *e) override
Mouse release event for overriding. Default implementation does nothing.
Definition: qgsmaptoolzoom.cpp:91
QgsMapCanvas::zoomWithCenter
void zoomWithCenter(int x, int y, bool zoomIn)
Zooms in/out with a given center.
Definition: qgsmapcanvas.cpp:1948
QgsMapToPixel::toMapCoordinates
QgsPointXY toMapCoordinates(int x, int y) const
Transform device coordinates to map (world) coordinates.
Definition: qgsmaptopixel.cpp:108
QgsMapToolZoom::mMinPixelZoom
int mMinPixelZoom
Definition: qgsmaptoolzoom.h:51
QgsMapToolZoom::flags
Flags flags() const override
Returns the flags for the map tool.
Definition: qgsmaptoolzoom.cpp:50
QgsWkbTypes::PolygonGeometry
@ PolygonGeometry
Definition: qgswkbtypes.h:144
QgsMapToolZoom::~QgsMapToolZoom
~QgsMapToolZoom() override
Definition: qgsmaptoolzoom.cpp:45
qgsrubberband.h
QgsMapToolZoom::mZoomRect
QRect mZoomRect
stores actual zoom rect
Definition: qgsmaptoolzoom.h:49
qgsapplication.h
QgsMapToolZoom::mZoomOut
bool mZoomOut
indicates whether we're zooming in or out
Definition: qgsmaptoolzoom.h:54
QgsMapTool
Abstract base class for all map tools.
Definition: qgsmaptool.h:64
qgsmaptoolzoom.h
QgsRubberBand::setColor
void setColor(const QColor &color)
Sets the color for the rubberband.
Definition: qgsrubberband.cpp:46
QgsMapToolZoom::canvasMoveEvent
void canvasMoveEvent(QgsMapMouseEvent *e) override
Mouse move event for overriding. Default implementation does nothing.
Definition: qgsmaptoolzoom.cpp:55
QgsMapToolZoom::keyReleaseEvent
void keyReleaseEvent(QKeyEvent *e) override
Key event for overriding. Default implementation does nothing.
Definition: qgsmaptoolzoom.cpp:166
QgsMapCanvas::getCoordinateTransform
const QgsMapToPixel * getCoordinateTransform()
Gets the current coordinate transform.
Definition: qgsmapcanvas.cpp:335
QgsMapToolZoom::mZoomOutCursor
QCursor mZoomOutCursor
Definition: qgsmaptoolzoom.h:63
QgsApplication
Extends QApplication to provide access to QGIS specific resources such as theme paths,...
Definition: qgsapplication.h:83
QgsPointXY
A class to represent a 2D point.
Definition: qgspointxy.h:44
QgsMapMouseEvent
A QgsMapMouseEvent is the result of a user interaction with the mouse on a QgsMapCanvas.
Definition: qgsmapmouseevent.h:36
c
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
Definition: porting_processing.dox:1
QgsMapToPixel
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:38
QgsRubberBand::setToCanvasRectangle
void setToCanvasRectangle(QRect rect)
Sets this rubber band to a map canvas rectangle.
Definition: qgsrubberband.cpp:401
QgsMapToolZoom::mRubberBand
QgsRubberBand * mRubberBand
Definition: qgsmaptoolzoom.h:61
QgsMapToolZoom::keyPressEvent
void keyPressEvent(QKeyEvent *e) override
Key event for overriding. Default implementation does nothing.
Definition: qgsmaptoolzoom.cpp:158
QgsMapToolZoom::mNativeZoomOut
bool mNativeZoomOut
native tool
Definition: qgsmaptoolzoom.h:56
QgsMapToolZoom::mDragging
bool mDragging
Flag to indicate a map canvas drag operation is taking place.
Definition: qgsmaptoolzoom.h:59
QgsMapToolZoom::QgsMapToolZoom
QgsMapToolZoom(QgsMapCanvas *canvas, bool zoomOut)
constructor
Definition: qgsmaptoolzoom.cpp:32
qgslogger.h
QgsMapSettings
The QgsMapSettings class contains configuration for rendering of the map.
Definition: qgsmapsettings.h:88
qgsmapmouseevent.h
QgsMapTool::ShowContextMenu
@ ShowContextMenu
Show a context menu when right-clicking with the tool (since QGIS 3.14). See populateContextMenu().
Definition: qgsmaptool.h:96
QgsMapTool::mToolName
QString mToolName
translated name of the map tool
Definition: qgsmaptool.h:282