QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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  const 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  const double sfx = static_cast<double>( zoomRectSize.width() ) / canvasSize.width();
129  const double sfy = static_cast<double>( zoomRectSize.height() ) / canvasSize.height();
130  const double sf = std::max( sfx, sfy );
131 
132  const QgsMapToPixel *m2p = mCanvas->getCoordinateTransform();
133  const 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 }
QgsMapToolZoom::deactivate
void deactivate() override
called when map tool is being deactivated
Definition: qgsmaptoolzoom.cpp:141
QgsMapSettings::outputSize
QSize outputSize() const
Returns the size of the resulting map image, in pixels.
Definition: qgsmapsettings.cpp:239
qgsmapcanvas.h
qgsmaptopixel.h
QgsRubberBand
A class for drawing transient features (e.g. digitizing lines) on the map.
Definition: qgsrubberband.h:51
QgsMapToolZoom::mZoomInCursor
QCursor mZoomInCursor
Definition: qgsmaptoolzoom.h:64
QgsMapTool::setCursor
virtual void setCursor(const QCursor &cursor)
Sets a user defined cursor.
Definition: qgsmaptool.cpp:160
QgsMapCanvas
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:89
QgsMapTool::deactivate
virtual void deactivate()
called when map tool is being deactivated
Definition: qgsmaptool.cpp:110
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:110
QgsMapToolZoom::canvasReleaseEvent
void canvasReleaseEvent(QgsMapMouseEvent *e) override
Mouse release event for overriding. Default implementation does nothing.
Definition: qgsmaptoolzoom.cpp:91
QgsMapToPixel::toMapCoordinates
QgsPointXY toMapCoordinates(int x, int y) const
Transforms device coordinates to map (world) coordinates.
Definition: qgsmaptopixel.h:173
QgsMapTool::mCanvas
QPointer< QgsMapCanvas > mCanvas
The pointer to the map canvas.
Definition: qgsmaptool.h:336
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. Map tools are user interactive tools for manipulating the map ...
Definition: qgsmaptool.h:70
qgsmaptoolzoom.h
QgsRubberBand::setColor
void setColor(const QColor &color)
Sets the color for the rubberband.
Definition: qgsrubberband.cpp:54
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
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:91
QgsPointXY
A class to represent a 2D point.
Definition: qgspointxy.h:58
QgsMapMouseEvent
A QgsMapMouseEvent is the result of a user interaction with the mouse on a QgsMapCanvas....
Definition: qgsmapmouseevent.h:35
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:419
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. The rendering itself is don...
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:115
QgsMapTool::mToolName
QString mToolName
The translated name of the map tool.
Definition: qgsmaptool.h:354