QGIS API Documentation 4.1.0-Master (009143bf4b4)
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 "qgsmaptoolzoom.h"
18
19#include "qgsapplication.h"
20#include "qgslogger.h"
21#include "qgsmapcanvas.h"
22#include "qgsmapmouseevent.h"
23#include "qgsmaptopixel.h"
24#include "qgsrubberband.h"
25
26#include <QColor>
27#include <QCursor>
28#include <QPixmap>
29#include <QRect>
30
31#include "moc_qgsmaptoolzoom.cpp"
32
35 , mZoomOut( zoomOut )
36 , mNativeZoomOut( zoomOut )
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
47
52
54{
55 if ( !( e->buttons() & Qt::LeftButton ) )
56 return;
57
58 setZoomMode( e->modifiers().testFlag( Qt::AltModifier ) ^ mNativeZoomOut );
59
60 if ( !mDragging )
61 {
62 mDragging = true;
64
65 QColor color( Qt::blue );
66 color.setAlpha( 63 );
67 mRubberBand->setColor( color );
68 mZoomRect.setTopLeft( e->pos() );
69 }
70
71 mZoomRect.setBottomRight( e->pos() );
72 if ( mRubberBand )
73 {
74 mRubberBand->setToCanvasRectangle( mZoomRect );
75 mRubberBand->show();
76 }
77}
78
79
81{
82 if ( e->button() != Qt::LeftButton )
83 return;
84
85 mZoomRect.setTopLeft( e->pos() );
86 mZoomRect.setBottomRight( e->pos() );
87}
88
89
91{
92 if ( e->button() != Qt::LeftButton )
93 return;
94
95 if ( mCanceled )
96 {
97 mCanceled = false;
98 mDragging = false;
99 return;
100 }
101
102 setZoomMode( e->modifiers().testFlag( Qt::AltModifier ) ^ mNativeZoomOut );
103
104 // We are not really dragging in this case. This is sometimes caused by
105 // a pen based computer reporting a press, move, and release, all the
106 // one point.
107 const bool tooShort = ( mZoomRect.topLeft() - mZoomRect.bottomRight() ).manhattanLength() < mMinPixelZoom;
108 if ( !mDragging || tooShort )
109 {
110 mDragging = false;
111 mRubberBand.reset();
112
113
114 // change to zoom in/out by the default multiple
115 mCanvas->zoomWithCenter( e->x(), e->y(), !mZoomOut );
116 }
117 else
118 {
119 mDragging = false;
120 mRubberBand.reset();
121
122
123 // store the rectangle
124 mZoomRect.setRight( e->pos().x() );
125 mZoomRect.setBottom( e->pos().y() );
126
127 //account for bottom right -> top left dragging
128 mZoomRect = mZoomRect.normalized();
129
130 // set center and zoom
131 const QSize &zoomRectSize = mZoomRect.size();
132 const QgsMapSettings &mapSettings = mCanvas->mapSettings();
133 const QSize &canvasSize = mapSettings.outputSize();
134 const double sfx = static_cast<double>( zoomRectSize.width() ) / canvasSize.width();
135 const double sfy = static_cast<double>( zoomRectSize.height() ) / canvasSize.height();
136 const double sf = std::max( sfx, sfy );
137
138 const QgsMapToPixel *m2p = mCanvas->getCoordinateTransform();
139 const QgsPointXY c = m2p->toMapCoordinates( mZoomRect.center() );
140
141 mCanvas->zoomByFactor( mZoomOut ? 1.0 / sf : sf, &c );
142
143 mCanvas->refresh();
144 }
145}
146
148{
149 mRubberBand.reset();
150
151
153}
154
155void QgsMapToolZoom::setZoomMode( bool zoomOut, bool force )
156{
157 if ( !force && zoomOut == mZoomOut )
158 return;
159
160 mZoomOut = zoomOut;
162}
163
165{
166 if ( e->key() == Qt::Key_Alt )
167 {
168 setZoomMode( !mNativeZoomOut );
169 }
170}
171
173{
174 // key press events are not caught wile the mouse is pressed
175 // this is detected in map canvas move event
176
177 if ( e->key() == Qt::Key_Alt )
178 {
179 setZoomMode( mNativeZoomOut );
180 }
181
182 if ( e->key() == Qt::Key_Escape && mDragging )
183 {
184 mCanceled = true;
185 mRubberBand.reset();
186 }
187}
@ Polygon
Polygons.
Definition qgis.h:382
Extends QApplication to provide access to QGIS specific resources such as theme paths,...
A mouse event which is the result of a user interaction with a QgsMapCanvas.
Contains configuration for rendering maps.
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.
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.
QObjectUniquePtr< QgsRubberBand > mRubberBand
void keyReleaseEvent(QKeyEvent *e) override
Key event for overriding. Default implementation does nothing.
QgsMapCanvas * canvas() const
returns pointer to the tool's map canvas
QgsMapTool(QgsMapCanvas *canvas)
Constructor takes a map canvas as a parameter.
QFlags< Flag > Flags
Definition qgsmaptool.h:116
virtual void setCursor(const QCursor &cursor)
Sets a user defined cursor.
QPointer< QgsMapCanvas > mCanvas
The pointer to the map canvas.
Definition qgsmaptool.h:380
QString mToolName
The translated name of the map tool.
Definition qgsmaptool.h:398
friend class QgsMapCanvas
Definition qgsmaptool.h:400
@ 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
Represents a 2D point.
Definition qgspointxy.h:62
Responsible for drawing transient features (e.g.
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