QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 
52 {
53  if ( !( e->buttons() & Qt::LeftButton ) )
54  return;
55 
56  setZoomMode( e->modifiers().testFlag( Qt::AltModifier ) ^ mNativeZoomOut );
57 
58  if ( !mDragging )
59  {
60  mDragging = true;
61  delete mRubberBand;
63  QColor color( Qt::blue );
64  color.setAlpha( 63 );
65  mRubberBand->setColor( color );
66  mZoomRect.setTopLeft( e->pos() );
67  }
68 
69  mZoomRect.setBottomRight( e->pos() );
70  if ( mRubberBand )
71  {
73  mRubberBand->show();
74  }
75 }
76 
77 
79 {
80  if ( e->button() != Qt::LeftButton )
81  return;
82 
83  mZoomRect.setRect( 0, 0, 0, 0 );
84 }
85 
86 
88 {
89  if ( e->button() != Qt::LeftButton )
90  return;
91 
92  setZoomMode( e->modifiers().testFlag( Qt::AltModifier ) ^ mNativeZoomOut );
93 
94  // We are not really dragging in this case. This is sometimes caused by
95  // a pen based computer reporting a press, move, and release, all the
96  // one point.
97  bool tooShort = ( mZoomRect.topLeft() - mZoomRect.bottomRight() ).manhattanLength() < mMinPixelZoom;
98  if ( !mDragging || tooShort )
99  {
100  mDragging = false;
101  delete mRubberBand;
102  mRubberBand = nullptr;
103 
104  // change to zoom in/out by the default multiple
105  mCanvas->zoomWithCenter( e->x(), e->y(), !mZoomOut );
106  }
107  else
108  {
109  mDragging = false;
110  delete mRubberBand;
111  mRubberBand = nullptr;
112 
113  // store the rectangle
114  mZoomRect.setRight( e->pos().x() );
115  mZoomRect.setBottom( e->pos().y() );
116 
117  //account for bottom right -> top left dragging
118  mZoomRect = mZoomRect.normalized();
119 
120  // set center and zoom
121  const QSize &zoomRectSize = mZoomRect.size();
122  const QgsMapSettings &mapSettings = mCanvas->mapSettings();
123  const QSize &canvasSize = mapSettings.outputSize();
124  double sfx = static_cast<double>( zoomRectSize.width() ) / canvasSize.width();
125  double sfy = static_cast<double>( zoomRectSize.height() ) / canvasSize.height();
126  double sf = std::max( sfx, sfy );
127 
129  QgsPointXY c = m2p->toMapCoordinates( mZoomRect.center() );
130 
131  mCanvas->zoomByFactor( mZoomOut ? 1.0 / sf : sf, &c );
132 
133  mCanvas->refresh();
134  }
135 }
136 
138 {
139  delete mRubberBand;
140  mRubberBand = nullptr;
141 
143 }
144 
145 void QgsMapToolZoom::setZoomMode( bool zoomOut, bool force )
146 {
147  if ( !force && zoomOut == mZoomOut )
148  return;
149 
150  mZoomOut = zoomOut;
152 }
153 
154 void QgsMapToolZoom::keyPressEvent( QKeyEvent *e )
155 {
156  if ( e->key() == Qt::Key_Alt )
157  {
158  setZoomMode( !mNativeZoomOut );
159  }
160 }
161 
163 {
164  // key press events are not caught wile the mouse is pressed
165  // this is detected in map canvas move event
166 
167  if ( e->key() == Qt::Key_Alt )
168  {
169  setZoomMode( mNativeZoomOut );
170  }
171 }
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
void refresh()
Repaints the canvas map.
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:73
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:37
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.
bool mZoomOut
indicates whether we&#39;re zooming in or out
A class for drawing transient features (e.g.
Definition: qgsrubberband.h:40
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
void zoomByFactor(double scaleFactor, const QgsPointXY *center=nullptr)
Zoom with the factor supplied.
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
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
QSize outputSize() const
Returns the size of the resulting map image.
QgsPointXY toMapCoordinates(int x, int y) const
Transform device coordinates to map (world) coordinates.