QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
qgsmaptool.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmaptool.cpp - base class for map canvas tools
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 "qgslogger.h"
17 #include "qgsmaptool.h"
18 #include "qgsmapcanvas.h"
19 #include "qgsmaptopixel.h"
20 #include "qgsrendercontext.h"
21 #include "qgssettings.h"
22 #include "qgsmapmouseevent.h"
23 
24 #include <QAction>
25 #include <QAbstractButton>
26 
28  : QObject( canvas )
29  , mCanvas( canvas )
30  , mCursor( Qt::CrossCursor )
31 {
32 }
33 
34 
36 {
37  if ( mCanvas )
38  mCanvas->unsetMapTool( this );
39 }
40 
42 {
44 }
45 
47 {
48  return mCanvas->mapSettings().layerToMapCoordinates( layer, point );
49 }
50 
52 {
53  QgsPointXY pt = toMapCoordinates( point );
54  return toLayerCoordinates( layer, pt );
55 }
56 
58 {
59  return mCanvas->mapSettings().mapToLayerCoordinates( layer, point );
60 }
61 
63 {
64  return mCanvas->mapSettings().layerToMapCoordinates( layer, point );
65 }
66 
68 {
69  return mCanvas->mapSettings().mapToLayerCoordinates( layer, rect );
70 }
71 
72 QPoint QgsMapTool::toCanvasCoordinates( const QgsPointXY &point ) const
73 {
74  qreal x = point.x(), y = point.y();
76  return QPoint( std::round( x ), std::round( y ) );
77 }
78 
79 
81 {
82  // make action and/or button active
83  if ( mAction )
84  mAction->setChecked( true );
85  if ( mButton )
86  mButton->setChecked( true );
87 
88  // set cursor (map tools usually set it in constructor)
89  mCanvas->setCursor( mCursor );
90  QgsDebugMsgLevel( QStringLiteral( "Cursor has been set" ), 4 );
91 
92  emit activated();
93 }
94 
95 
97 {
98  if ( mAction )
99  mAction->setChecked( false );
100  if ( mButton )
101  mButton->setChecked( false );
102 
103  emit deactivated();
104 }
105 
107 {
108 
109 }
110 
111 void QgsMapTool::setAction( QAction *action )
112 {
113  if ( mAction )
114  disconnect( mAction, &QObject::destroyed, this, &QgsMapTool::actionDestroyed );
115  mAction = action;
116  if ( mAction )
117  connect( mAction, &QObject::destroyed, this, &QgsMapTool::actionDestroyed );
118 }
119 
120 void QgsMapTool::actionDestroyed()
121 {
122  if ( mAction == sender() )
123  mAction = nullptr;
124 }
125 
127 {
128  return mAction;
129 }
130 
132 {
133  return mCanvas && mCanvas->mapTool() == this;
134 }
135 
136 void QgsMapTool::setButton( QAbstractButton *button )
137 {
138  mButton = button;
139 }
140 
141 QAbstractButton *QgsMapTool::button()
142 {
143  return mButton;
144 }
145 
146 void QgsMapTool::setCursor( const QCursor &cursor )
147 {
148  mCursor = cursor;
149  if ( isActive() )
150  mCanvas->setCursor( mCursor );
151 }
152 
153 
155 {
156  Q_UNUSED( e )
157 }
158 
160 {
161  Q_UNUSED( e )
162 }
163 
165 {
166  Q_UNUSED( e )
167 }
168 
170 {
171  Q_UNUSED( e )
172 }
173 
174 void QgsMapTool::wheelEvent( QWheelEvent *e )
175 {
176  e->ignore();
177 }
178 
179 void QgsMapTool::keyPressEvent( QKeyEvent *e )
180 {
181  Q_UNUSED( e )
182 }
183 
184 void QgsMapTool::keyReleaseEvent( QKeyEvent *e )
185 {
186  Q_UNUSED( e )
187 }
188 
189 bool QgsMapTool::gestureEvent( QGestureEvent *e )
190 {
191  Q_UNUSED( e )
192  return true;
193 }
194 
196 {
197  return mCanvas;
198 }
199 
201 {
202  QgsSettings settings;
203  double radius = settings.value( QStringLiteral( "Map/searchRadiusMM" ), Qgis::DEFAULT_SEARCH_RADIUS_MM ).toDouble();
204 
205  if ( radius > 0 )
206  {
207  return radius;
208  }
210 }
211 
213 {
214  return searchRadiusMM() * context.scaleFactor() * context.mapToPixel().mapUnitsPerPixel();
215 }
216 
218 {
219  if ( !canvas )
220  {
221  return 0;
222  }
223  QgsMapSettings mapSettings = canvas->mapSettings();
224  QgsRenderContext context = QgsRenderContext::fromMapSettings( mapSettings );
225  return searchRadiusMU( context );
226 }
227 
228 
230 {
231 
232 }
233 
234 
236 {
237  return false;
238 }
static const double DEFAULT_SEARCH_RADIUS_MM
Identify search radius in mm.
Definition: qgis.h:138
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:86
void unsetMapTool(QgsMapTool *mapTool)
Unset the current map tool or last non zoom tool.
QgsMapTool * mapTool()
Returns the currently active tool.
const QgsMapToPixel * getCoordinateTransform()
Gets the current coordinate transform.
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
Base class for all map layer types.
Definition: qgsmaplayer.h:85
A QgsMapMouseEvent is the result of a user interaction with the mouse on a QgsMapCanvas.
The QgsMapSettings class contains configuration for rendering of the map.
QgsPointXY layerToMapCoordinates(const QgsMapLayer *layer, QgsPointXY point) const
transform point coordinates from layer's CRS to output CRS
QgsPointXY mapToLayerCoordinates(const QgsMapLayer *layer, QgsPointXY point) const
transform point coordinates from output CRS to layer's CRS
double mapUnitsPerPixel() const
Returns current map units per pixel.
QgsPointXY toMapCoordinates(int x, int y) const
Transform device coordinates to map (world) coordinates.
void transformInPlace(double &x, double &y) const
Transforms device coordinates to map coordinates.
void deactivated()
signal emitted once the map tool is deactivated
QgsMapCanvas * mCanvas
The pointer to the map canvas.
Definition: qgsmaptool.h:288
static double searchRadiusMM()
Gets search radius in mm.
Definition: qgsmaptool.cpp:200
QgsMapCanvas * canvas() const
returns pointer to the tool's map canvas
Definition: qgsmaptool.cpp:195
QgsMapTool(QgsMapCanvas *canvas)
Constructor takes a map canvas as a parameter.
Definition: qgsmaptool.cpp:27
QAbstractButton * mButton
Optional pointer to a button that will be checked on map tool activation and unchecked on map tool de...
Definition: qgsmaptool.h:303
virtual void populateContextMenu(QMenu *menu)
Allows the tool to populate and customize the given menu, prior to showing it in response to a right-...
Definition: qgsmaptool.cpp:229
virtual void canvasDoubleClickEvent(QgsMapMouseEvent *e)
Mouse double-click event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:159
QgsPointXY toMapCoordinates(QPoint point)
Transforms a point from screen coordinates to map coordinates.
Definition: qgsmaptool.cpp:41
virtual void setCursor(const QCursor &cursor)
Sets a user defined cursor.
Definition: qgsmaptool.cpp:146
QgsPointXY toLayerCoordinates(const QgsMapLayer *layer, QPoint point)
Transforms a point from screen coordinates to layer coordinates.
Definition: qgsmaptool.cpp:51
QAbstractButton * button()
Returns associated button with map tool or nullptr if no button is associated.
Definition: qgsmaptool.cpp:141
void setButton(QAbstractButton *button)
Use this to associate a button to this maptool.
Definition: qgsmaptool.cpp:136
virtual bool populateContextMenuWithEvent(QMenu *menu, QgsMapMouseEvent *event)
Allows the tool to populate and customize the given menu, prior to showing it in response to a right-...
Definition: qgsmaptool.cpp:235
virtual void canvasPressEvent(QgsMapMouseEvent *e)
Mouse press event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:164
void activated()
signal emitted once the map tool is activated
void setAction(QAction *action)
Use this to associate a QAction to this maptool.
Definition: qgsmaptool.cpp:111
virtual void canvasMoveEvent(QgsMapMouseEvent *e)
Mouse move event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:154
QAction * mAction
Optional pointer to an action that will be checked on map tool activation and unchecked on map tool d...
Definition: qgsmaptool.h:297
virtual void keyPressEvent(QKeyEvent *e)
Key event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:179
static double searchRadiusMU(const QgsRenderContext &context)
Gets search radius in map units for given context.
Definition: qgsmaptool.cpp:212
QAction * action()
Returns associated action with map tool or nullptr if no action is associated.
Definition: qgsmaptool.cpp:126
virtual void keyReleaseEvent(QKeyEvent *e)
Key event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:184
~QgsMapTool() override
Definition: qgsmaptool.cpp:35
virtual void canvasReleaseEvent(QgsMapMouseEvent *e)
Mouse release event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:169
QPoint toCanvasCoordinates(const QgsPointXY &point) const
Transforms a point from map coordinates to screen coordinates.
Definition: qgsmaptool.cpp:72
virtual void wheelEvent(QWheelEvent *e)
Mouse wheel event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:174
bool isActive() const
Returns if the current map tool active on the map canvas.
Definition: qgsmaptool.cpp:131
virtual void clean()
convenient method to clean members
Definition: qgsmaptool.cpp:106
virtual void activate()
called when set as currently active map tool
Definition: qgsmaptool.cpp:80
virtual bool gestureEvent(QGestureEvent *e)
gesture event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:189
QCursor mCursor
The cursor used in the map tool.
Definition: qgsmaptool.h:291
virtual void deactivate()
called when map tool is being deactivated
Definition: qgsmaptool.cpp:96
A class to represent a 2D point.
Definition: qgspointxy.h:44
double y
Definition: qgspointxy.h:48
Q_GADGET double x
Definition: qgspointxy.h:47
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:38
A rectangle specified with double values.
Definition: qgsrectangle.h:42
Contains information about the context of a rendering operation.
double scaleFactor() const
Returns the scaling factor for the render to convert painter units to physical sizes.
const QgsMapToPixel & mapToPixel() const
Returns the context's map to pixel transform, which transforms between map coordinates and device coo...
static QgsRenderContext fromMapSettings(const QgsMapSettings &mapSettings)
create initialized QgsRenderContext instance from given QgsMapSettings
This class is a composition of two QSettings instances:
Definition: qgssettings.h:62
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39