QGIS API Documentation  3.13.0-Master (788156190c)
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 }
QgsMapTool::mCanvas
QgsMapCanvas * mCanvas
pointer to map canvas
Definition: qgsmaptool.h:241
QgsMapToolZoom::deactivate
void deactivate() override
called when map tool is being deactivated
Definition: qgsmaptoolzoom.cpp:137
QgsMapCanvas::refresh
void refresh()
Repaints the canvas map.
Definition: qgsmapcanvas.cpp:508
QgsMapSettings::outputSize
QSize outputSize() const
Returns the size of the resulting map image.
Definition: qgsmapsettings.cpp:234
qgsmapcanvas.h
QgsMapCanvas::zoomByFactor
void zoomByFactor(double scaleFactor, const QgsPointXY *center=nullptr, bool ignoreScaleLock=false)
Zoom with the factor supplied.
Definition: qgsmapcanvas.cpp:2306
qgsmaptopixel.h
QgsMapCanvas::mapSettings
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
Definition: qgsmapcanvas.cpp:380
QgsRubberBand
Definition: qgsrubberband.h:49
QgsMapToolZoom::mZoomInCursor
QCursor mZoomInCursor
Definition: qgsmaptoolzoom.h:64
QgsMapTool::setCursor
virtual void setCursor(const QCursor &cursor)
Sets a user defined cursor.
Definition: qgsmaptool.cpp:149
QgsMapCanvas
Definition: qgsmapcanvas.h:78
QgsMapTool::deactivate
virtual void deactivate()
called when map tool is being deactivated
Definition: qgsmaptool.cpp:99
QgsMapToolZoom::canvasPressEvent
void canvasPressEvent(QgsMapMouseEvent *e) override
Mouse press event for overriding. Default implementation does nothing.
Definition: qgsmaptoolzoom.cpp:78
QgsMapToolZoom::canvasReleaseEvent
void canvasReleaseEvent(QgsMapMouseEvent *e) override
Mouse release event for overriding. Default implementation does nothing.
Definition: qgsmaptoolzoom.cpp:87
QgsMapCanvas::zoomWithCenter
void zoomWithCenter(int x, int y, bool zoomIn)
Zooms in/out with a given center.
Definition: qgsmapcanvas.cpp:1730
QgsMapToPixel::toMapCoordinates
QgsPointXY toMapCoordinates(int x, int y) const
Transform device coordinates to map (world) coordinates.
Definition: qgsmaptopixel.cpp:108
QgsMapToolZoom::mMinPixelZoom
int mMinPixelZoom
Definition: qgsmaptoolzoom.h:51
QgsWkbTypes::PolygonGeometry
@ PolygonGeometry
Definition: qgswkbtypes.h:143
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
Definition: qgsmaptool.h:62
qgsmaptoolzoom.h
QgsRubberBand::setColor
void setColor(const QColor &color)
Sets the color for the rubberband.
Definition: qgsrubberband.cpp:46
QgsMapToolZoom::canvasMoveEvent
void canvasMoveEvent(QgsMapMouseEvent *e) override
Mouse move event for overriding. Default implementation does nothing.
Definition: qgsmaptoolzoom.cpp:51
QgsMapToolZoom::keyReleaseEvent
void keyReleaseEvent(QKeyEvent *e) override
Key event for overriding. Default implementation does nothing.
Definition: qgsmaptoolzoom.cpp:162
QgsMapCanvas::getCoordinateTransform
const QgsMapToPixel * getCoordinateTransform()
Gets the current coordinate transform.
Definition: qgsmapcanvas.cpp:324
QgsMapToolZoom::mZoomOutCursor
QCursor mZoomOutCursor
Definition: qgsmaptoolzoom.h:63
QgsApplication
Definition: qgsapplication.h:77
QgsPointXY
Definition: qgspointxy.h:43
QgsMapMouseEvent
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
Definition: qgsmaptopixel.h:37
QgsRubberBand::setToCanvasRectangle
void setToCanvasRectangle(QRect rect)
Sets this rubber band to a map canvas rectangle.
Definition: qgsrubberband.cpp:372
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:154
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
Definition: qgsmapsettings.h:86
qgsmapmouseevent.h
QgsMapTool::mToolName
QString mToolName
translated name of the map tool
Definition: qgsmaptool.h:259