QGIS API Documentation  2.99.0-Master (9f5e33a)
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 #include "qgsmaptoolzoom.h"
17 #include "qgsmapcanvas.h"
18 #include "qgsmaptopixel.h"
19 #include "qgscursors.h"
20 #include "qgsrubberband.h"
21 
22 #include <QMouseEvent>
23 #include <QRect>
24 #include <QColor>
25 #include <QCursor>
26 #include <QPixmap>
27 #include "qgslogger.h"
28 
29 
31  : QgsMapTool( canvas )
32  , mZoomOut( zoomOut )
33  , mDragging( false )
34  , mRubberBand( nullptr )
35 {
36  mToolName = tr( "Zoom" );
37  // set the cursor
38  QPixmap myZoomQPixmap = QPixmap( ( const char ** )( zoomOut ? zoom_out : zoom_in ) );
39  mCursor = QCursor( myZoomQPixmap, 7, 7 );
40 }
41 
43 {
44  delete mRubberBand;
45 }
46 
47 
49 {
50  if ( !( e->buttons() & Qt::LeftButton ) )
51  return;
52 
53  if ( !mDragging )
54  {
55  mDragging = true;
56  delete mRubberBand;
58  QColor color( Qt::blue );
59  color.setAlpha( 63 );
60  mRubberBand->setColor( color );
61  mZoomRect.setTopLeft( e->pos() );
62  }
63  mZoomRect.setBottomRight( e->pos() );
64  if ( mRubberBand )
65  {
67  mRubberBand->show();
68  }
69 }
70 
71 
73 {
74  if ( e->button() != Qt::LeftButton )
75  return;
76 
77  mZoomRect.setRect( 0, 0, 0, 0 );
78 }
79 
80 
82 {
83  if ( e->button() != Qt::LeftButton )
84  return;
85 
86  bool zoomOut = mZoomOut;
87  if ( e->modifiers() & Qt::AltModifier )
88  zoomOut = !zoomOut;
89 
90  // We are not really dragging in this case. This is sometimes caused by
91  // a pen based computer reporting a press, move, and release, all the
92  // one point.
93  if ( mDragging && ( mZoomRect.topLeft() == mZoomRect.bottomRight() ) )
94  {
95  mDragging = false;
96  delete mRubberBand;
97  mRubberBand = nullptr;
98  }
99 
100  if ( mDragging )
101  {
102  mDragging = false;
103  delete mRubberBand;
104  mRubberBand = nullptr;
105 
106  // store the rectangle
107  mZoomRect.setRight( e->pos().x() );
108  mZoomRect.setBottom( e->pos().y() );
109 
110  //account for bottom right -> top left dragging
111  mZoomRect = mZoomRect.normalized();
112 
113  // set center and zoom
114  const QSize &zoomRectSize = mZoomRect.size();
115  const QgsMapSettings &mapSettings = mCanvas->mapSettings();
116  const QSize &canvasSize = mapSettings.outputSize();
117  double sfx = ( double )zoomRectSize.width() / canvasSize.width();
118  double sfy = ( double )zoomRectSize.height() / canvasSize.height();
119  double sf = qMax( sfx, sfy );
120 
122  QgsPointXY c = m2p->toMapCoordinates( mZoomRect.center() );
123 
124  mCanvas->zoomByFactor( zoomOut ? 1.0 / sf : sf, &c );
125 
126  mCanvas->refresh();
127  }
128  else // not dragging
129  {
130  // change to zoom in/out by the default multiple
131  mCanvas->zoomWithCenter( e->x(), e->y(), !zoomOut );
132  }
133 }
134 
136 {
137  delete mRubberBand;
138  mRubberBand = nullptr;
139 
141 }
virtual void deactivate() override
called when map tool is being deactivated
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.
const char * zoom_in[]
Bitmap cursors for map operations.
Definition: qgscursors.cpp:21
A class to represent a 2D point.
Definition: qgspointxy.h:42
A QgsMapMouseEvent is the result of a user interaction with the mouse on a QgsMapCanvas.
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:234
QgsMapCanvas * mCanvas
pointer to map canvas
Definition: qgsmaptool.h:220
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:35
virtual void canvasReleaseEvent(QgsMapMouseEvent *e) override
Mouse release event for overriding. Default implementation does nothing.
QCursor mCursor
cursor used in map tool
Definition: qgsmaptool.h:223
virtual void canvasMoveEvent(QgsMapMouseEvent *e) override
Mouse move event for overriding. Default implementation does nothing.
const char * zoom_out[]
Definition: qgscursors.cpp:45
bool mZoomOut
indicates whether we&#39;re zooming in or out
A class for drawing transient features (e.g.
Definition: qgsrubberband.h:36
virtual void canvasPressEvent(QgsMapMouseEvent *e) override
Mouse press event for overriding. Default implementation does nothing.
virtual void deactivate()
called when map tool is being deactivated
Definition: qgsmaptool.cpp:100
void zoomByFactor(double scaleFactor, const QgsPointXY *center=nullptr)
Zoom with the factor supplied.
const QgsMapSettings & mapSettings() const
Get access to properties used for map rendering.
Abstract base class for all map tools.
Definition: qgsmaptool.h:62
QRect mZoomRect
stores actual zoom rect
QgsRubberBand * mRubberBand
bool mDragging
Flag to indicate a map canvas drag operation is taking place.
const QgsMapToPixel * getCoordinateTransform()
Get the current coordinate transform.
void setColor(const QColor &color)
Sets the color for the rubberband.
QgsMapToolZoom(QgsMapCanvas *canvas, bool zoomOut)
constructor
QSize outputSize() const
Return the size of the resulting map image.
QgsPointXY toMapCoordinates(int x, int y) const