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