QGIS API Documentation  3.21.0-Master (5b68dc587e)
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  const 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 void QgsMapTool::setToolName( const QString &name )
80 {
81  mToolName = name;
82 }
83 
85 {
86  // make action and/or button active
87  if ( mAction )
88  mAction->setChecked( true );
89  if ( mButton )
90  mButton->setChecked( true );
91 
92  // set cursor (map tools usually set it in constructor)
93  mCanvas->setCursor( mCursor );
94  QgsDebugMsgLevel( QStringLiteral( "Cursor has been set" ), 4 );
95 
96  emit activated();
97 }
98 
99 
101 {
102  if ( mAction )
103  mAction->setChecked( false );
104  if ( mButton )
105  mButton->setChecked( false );
106 
107  emit deactivated();
108 }
109 
111 {
112 
113 }
114 
115 void QgsMapTool::setAction( QAction *action )
116 {
117  if ( mAction )
118  disconnect( mAction, &QObject::destroyed, this, &QgsMapTool::actionDestroyed );
119  mAction = action;
120  if ( mAction )
121  connect( mAction, &QObject::destroyed, this, &QgsMapTool::actionDestroyed );
122 }
123 
124 void QgsMapTool::actionDestroyed()
125 {
126  if ( mAction == sender() )
127  mAction = nullptr;
128 }
129 
131 {
132  return mAction;
133 }
134 
136 {
137  return mCanvas && mCanvas->mapTool() == this;
138 }
139 
140 void QgsMapTool::setButton( QAbstractButton *button )
141 {
142  mButton = button;
143 }
144 
145 QAbstractButton *QgsMapTool::button()
146 {
147  return mButton;
148 }
149 
150 void QgsMapTool::setCursor( const QCursor &cursor )
151 {
152  mCursor = cursor;
153  if ( isActive() )
154  mCanvas->setCursor( mCursor );
155 }
156 
157 
159 {
160  Q_UNUSED( e )
161 }
162 
164 {
165  Q_UNUSED( e )
166 }
167 
169 {
170  Q_UNUSED( e )
171 }
172 
174 {
175  Q_UNUSED( e )
176 }
177 
178 void QgsMapTool::wheelEvent( QWheelEvent *e )
179 {
180  e->ignore();
181 }
182 
183 void QgsMapTool::keyPressEvent( QKeyEvent *e )
184 {
185  Q_UNUSED( e )
186 }
187 
188 void QgsMapTool::keyReleaseEvent( QKeyEvent *e )
189 {
190  Q_UNUSED( e )
191 }
192 
193 bool QgsMapTool::gestureEvent( QGestureEvent *e )
194 {
195  Q_UNUSED( e )
196  return true;
197 }
198 
199 bool QgsMapTool::canvasToolTipEvent( QHelpEvent *e )
200 {
201  Q_UNUSED( e )
202  return false;
203 }
204 
206 {
207  return mCanvas;
208 }
209 
211 {
212  const QgsSettings settings;
213  const double radius = settings.value( QStringLiteral( "Map/searchRadiusMM" ), Qgis::DEFAULT_SEARCH_RADIUS_MM ).toDouble();
214 
215  if ( radius > 0 )
216  {
217  return radius;
218  }
220 }
221 
223 {
224  return searchRadiusMM() * context.scaleFactor() * context.mapToPixel().mapUnitsPerPixel();
225 }
226 
228 {
229  if ( !canvas )
230  {
231  return 0;
232  }
233  const QgsMapSettings mapSettings = canvas->mapSettings();
234  const QgsRenderContext context = QgsRenderContext::fromMapSettings( mapSettings );
235  return searchRadiusMU( context );
236 }
237 
238 
240 {
241 
242 }
243 
244 
246 {
247  return false;
248 }
static const double DEFAULT_SEARCH_RADIUS_MM
Identify search radius in mm.
Definition: qgis.h:988
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:88
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:73
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 the current map units per pixel.
QgsPointXY toMapCoordinates(int x, int y) const
Transforms 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:304
static double searchRadiusMM()
Gets search radius in mm.
Definition: qgsmaptool.cpp:210
QgsMapCanvas * canvas() const
returns pointer to the tool's map canvas
Definition: qgsmaptool.cpp:205
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:319
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:239
virtual bool canvasToolTipEvent(QHelpEvent *e)
Tooltip event for overriding.
Definition: qgsmaptool.cpp:199
void setToolName(const QString &name)
Sets the tool's name.
Definition: qgsmaptool.cpp:79
virtual void canvasDoubleClickEvent(QgsMapMouseEvent *e)
Mouse double-click event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:163
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:150
QString mToolName
The translated name of the map tool.
Definition: qgsmaptool.h:322
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:145
void setButton(QAbstractButton *button)
Use this to associate a button to this maptool.
Definition: qgsmaptool.cpp:140
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:245
virtual void canvasPressEvent(QgsMapMouseEvent *e)
Mouse press event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:168
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:115
virtual void canvasMoveEvent(QgsMapMouseEvent *e)
Mouse move event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:158
QAction * mAction
Optional pointer to an action that will be checked on map tool activation and unchecked on map tool d...
Definition: qgsmaptool.h:313
virtual void keyPressEvent(QKeyEvent *e)
Key event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:183
static double searchRadiusMU(const QgsRenderContext &context)
Gets search radius in map units for given context.
Definition: qgsmaptool.cpp:222
QAction * action()
Returns associated action with map tool or nullptr if no action is associated.
Definition: qgsmaptool.cpp:130
virtual void keyReleaseEvent(QKeyEvent *e)
Key event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:188
~QgsMapTool() override
Definition: qgsmaptool.cpp:35
virtual void canvasReleaseEvent(QgsMapMouseEvent *e)
Mouse release event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:173
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:178
bool isActive() const
Returns if the current map tool active on the map canvas.
Definition: qgsmaptool.cpp:135
virtual void clean()
convenient method to clean members
Definition: qgsmaptool.cpp:110
virtual void activate()
called when set as currently active map tool
Definition: qgsmaptool.cpp:84
virtual bool gestureEvent(QGestureEvent *e)
gesture event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:193
QCursor mCursor
The cursor used in the map tool.
Definition: qgsmaptool.h:307
virtual void deactivate()
called when map tool is being deactivated
Definition: qgsmaptool.cpp:100
A class to represent a 2D point.
Definition: qgspointxy.h:59
double y
Definition: qgspointxy.h:63
Q_GADGET double x
Definition: qgspointxy.h:62
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:49
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
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39