QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 
29 
30 
32  : QgsMapTool( canvas )
33  , mZoomOut( zoomOut )
34  , mNativeZoomOut( zoomOut )
35  , mDragging( false )
36  , mZoomOutCursor( QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomOut ) )
37  , mZoomInCursor( QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomIn ) )
38 
39 {
40  mToolName = tr( "Zoom" );
41  setZoomMode( mNativeZoomOut, true );
42 }
43 
45 {
46  delete mRubberBand;
47 }
48 
49 
51 {
52  if ( !( e->buttons() & Qt::LeftButton ) )
53  return;
54 
55  setZoomMode( e->modifiers().testFlag( Qt::AltModifier ) ^ mNativeZoomOut );
56 
57  if ( !mDragging )
58  {
59  mDragging = true;
60  delete mRubberBand;
62  QColor color( Qt::blue );
63  color.setAlpha( 63 );
64  mRubberBand->setColor( color );
65  mZoomRect.setTopLeft( e->pos() );
66  }
67 
68  mZoomRect.setBottomRight( e->pos() );
69  if ( mRubberBand )
70  {
72  mRubberBand->show();
73  }
74 }
75 
76 
78 {
79  if ( e->button() != Qt::LeftButton )
80  return;
81 
82  mZoomRect.setRect( 0, 0, 0, 0 );
83 }
84 
85 
87 {
88  if ( e->button() != Qt::LeftButton )
89  return;
90 
91  setZoomMode( e->modifiers().testFlag( Qt::AltModifier ) ^ mNativeZoomOut );
92 
93  // We are not really dragging in this case. This is sometimes caused by
94  // a pen based computer reporting a press, move, and release, all the
95  // one point.
96  bool tooShort = ( mZoomRect.topLeft() - mZoomRect.bottomRight() ).manhattanLength() < mMinPixelZoom;
97  if ( !mDragging || tooShort )
98  {
99  mDragging = false;
100  delete mRubberBand;
101  mRubberBand = nullptr;
102 
103  // change to zoom in/out by the default multiple
104  mCanvas->zoomWithCenter( e->x(), e->y(), !mZoomOut );
105  }
106  else
107  {
108  mDragging = false;
109  delete mRubberBand;
110  mRubberBand = nullptr;
111 
112  // store the rectangle
113  mZoomRect.setRight( e->pos().x() );
114  mZoomRect.setBottom( e->pos().y() );
115 
116  //account for bottom right -> top left dragging
117  mZoomRect = mZoomRect.normalized();
118 
119  // set center and zoom
120  const QSize &zoomRectSize = mZoomRect.size();
121  const QgsMapSettings &mapSettings = mCanvas->mapSettings();
122  const QSize &canvasSize = mapSettings.outputSize();
123  double sfx = static_cast<double>( zoomRectSize.width() ) / canvasSize.width();
124  double sfy = static_cast<double>( zoomRectSize.height() ) / canvasSize.height();
125  double sf = std::max( sfx, sfy );
126 
128  QgsPointXY c = m2p->toMapCoordinates( mZoomRect.center() );
129 
130  mCanvas->zoomByFactor( mZoomOut ? 1.0 / sf : sf, &c );
131 
132  mCanvas->refresh();
133  }
134 }
135 
137 {
138  delete mRubberBand;
139  mRubberBand = nullptr;
140 
142 }
143 
144 void QgsMapToolZoom::setZoomMode( bool zoomOut, bool force )
145 {
146  if ( !force && zoomOut == mZoomOut )
147  return;
148 
149  mZoomOut = zoomOut;
151 }
152 
153 void QgsMapToolZoom::keyPressEvent( QKeyEvent *e )
154 {
155  if ( e->key() == Qt::Key_Alt )
156  {
157  setZoomMode( !mNativeZoomOut );
158  }
159 }
160 
162 {
163  // key press events are not caught wile the mouse is pressed
164  // this is detected in map canvas move event
165 
166  if ( e->key() == Qt::Key_Alt )
167  {
168  setZoomMode( mNativeZoomOut );
169  }
170 }
void deactivate() override
called when map tool is being deactivated
Extends QApplication to provide access to QGIS specific resources such as theme paths, database paths etc.
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.
QCursor mZoomInCursor
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
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:259
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:241
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.
void canvasMoveEvent(QgsMapMouseEvent *e) override
Mouse move event for overriding. Default implementation does nothing.
QSize outputSize() const
Returns the size of the resulting map image.
bool mZoomOut
indicates whether we&#39;re zooming in or out
A class for drawing transient features (e.g.
Definition: qgsrubberband.h:47
void canvasPressEvent(QgsMapMouseEvent *e) override
Mouse press event for overriding. Default implementation does nothing.
void keyReleaseEvent(QKeyEvent *e) override
Key event for overriding. Default implementation does nothing.
virtual void deactivate()
called when map tool is being deactivated
Definition: qgsmaptool.cpp:99
QgsPointXY toMapCoordinates(int x, int y) const
Transform device coordinates to map (world) coordinates.
void zoomByFactor(double scaleFactor, const QgsPointXY *center=nullptr)
Zoom with the factor supplied.
Abstract base class for all map tools.
Definition: qgsmaptool.h:62
QRect mZoomRect
stores actual zoom rect
bool mNativeZoomOut
native tool
void keyPressEvent(QKeyEvent *e) override
Key event for overriding. Default implementation does nothing.
QgsRubberBand * mRubberBand
bool mDragging
Flag to indicate a map canvas drag operation is taking place.
const QgsMapToPixel * getCoordinateTransform()
Gets the current coordinate transform.
QCursor mZoomOutCursor
virtual void setCursor(const QCursor &cursor)
Sets a user defined cursor.
Definition: qgsmaptool.cpp:149
void setColor(const QColor &color)
Sets the color for the rubberband.
QgsMapToolZoom(QgsMapCanvas *canvas, bool zoomOut)
constructor