QGIS API Documentation  3.27.0-Master (0e23467727)
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 {
43  return mCanvas->getCoordinateTransform()->toMapCoordinates( point );
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().mapToLayerCoordinates( layer, point );
65 }
66 
68 {
69  return mCanvas->mapSettings().layerToMapCoordinates( layer, point );
70 }
71 
73 {
74  return mCanvas->mapSettings().mapToLayerCoordinates( layer, rect );
75 }
76 
77 QPoint QgsMapTool::toCanvasCoordinates( const QgsPointXY &point ) const
78 {
79  qreal x = point.x(), y = point.y();
80  mCanvas->getCoordinateTransform()->transformInPlace( x, y );
81  return QPoint( std::round( x ), std::round( y ) );
82 }
83 
84 QgsMapLayer *QgsMapTool::layer( const QString &id )
85 {
86  return mCanvas->layer( id );
87 }
88 
89 void QgsMapTool::setToolName( const QString &name )
90 {
91  mToolName = name;
92 }
93 
95 {
96  // make action and/or button active
97  if ( mAction )
98  mAction->setChecked( true );
99  if ( mButton )
100  mButton->setChecked( true );
101 
102  // set cursor (map tools usually set it in constructor)
103  mCanvas->setCursor( mCursor );
104  QgsDebugMsgLevel( QStringLiteral( "Cursor has been set" ), 4 );
105 
106  emit activated();
107 }
108 
109 
111 {
112  if ( mAction )
113  mAction->setChecked( false );
114  if ( mButton )
115  mButton->setChecked( false );
116 
117  emit deactivated();
118 }
119 
121 {
122 
123 }
124 
125 void QgsMapTool::setAction( QAction *action )
126 {
127  if ( mAction )
128  disconnect( mAction, &QObject::destroyed, this, &QgsMapTool::actionDestroyed );
129  mAction = action;
130  if ( mAction )
131  connect( mAction, &QObject::destroyed, this, &QgsMapTool::actionDestroyed );
132 }
133 
134 void QgsMapTool::actionDestroyed()
135 {
136  if ( mAction == sender() )
137  mAction = nullptr;
138 }
139 
141 {
142  return mAction;
143 }
144 
146 {
147  return mCanvas && mCanvas->mapTool() == this;
148 }
149 
150 void QgsMapTool::setButton( QAbstractButton *button )
151 {
152  mButton = button;
153 }
154 
155 QAbstractButton *QgsMapTool::button()
156 {
157  return mButton;
158 }
159 
160 void QgsMapTool::setCursor( const QCursor &cursor )
161 {
162  mCursor = cursor;
163  if ( isActive() )
164  mCanvas->setCursor( mCursor );
165 }
166 
167 
169 {
170  Q_UNUSED( e )
171 }
172 
174 {
175  Q_UNUSED( e )
176 }
177 
179 {
180  Q_UNUSED( e )
181 }
182 
184 {
185  Q_UNUSED( e )
186 }
187 
188 void QgsMapTool::wheelEvent( QWheelEvent *e )
189 {
190  e->ignore();
191 }
192 
193 void QgsMapTool::keyPressEvent( QKeyEvent *e )
194 {
195  Q_UNUSED( e )
196 }
197 
198 void QgsMapTool::keyReleaseEvent( QKeyEvent *e )
199 {
200  Q_UNUSED( e )
201 }
202 
203 bool QgsMapTool::gestureEvent( QGestureEvent *e )
204 {
205  Q_UNUSED( e )
206  return true;
207 }
208 
209 bool QgsMapTool::canvasToolTipEvent( QHelpEvent *e )
210 {
211  Q_UNUSED( e )
212  return false;
213 }
214 
216 {
217  return mCanvas;
218 }
219 
221 {
222  const QgsSettings settings;
223  const double radius = settings.value( QStringLiteral( "Map/searchRadiusMM" ), Qgis::DEFAULT_SEARCH_RADIUS_MM ).toDouble();
224 
225  if ( radius > 0 )
226  {
227  return radius;
228  }
230 }
231 
233 {
234  return searchRadiusMM() * context.scaleFactor() * context.mapToPixel().mapUnitsPerPixel();
235 }
236 
238 {
239  if ( !canvas )
240  {
241  return 0;
242  }
243  const QgsMapSettings mapSettings = canvas->mapSettings();
244  const QgsRenderContext context = QgsRenderContext::fromMapSettings( mapSettings );
245  return searchRadiusMU( context );
246 }
247 
248 
250 {
251 
252 }
253 
254 
256 {
257  return false;
258 }
static ReadFlags const double DEFAULT_SEARCH_RADIUS_MM
Identify search radius in mm.
Definition: qgis.h:1987
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:90
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.
double mapUnitsPerPixel() const
Returns the current map units per pixel.
QgsPoint toLayerCoordinates(const QgsMapLayer *layer, const QgsPoint &point)
Transforms a point from map coordinates to layer coordinates.
Definition: qgsmaptool.cpp:62
void deactivated()
signal emitted once the map tool is deactivated
QPointer< QgsMapCanvas > mCanvas
The pointer to the map canvas.
Definition: qgsmaptool.h:336
static double searchRadiusMM()
Gets search radius in mm.
Definition: qgsmaptool.cpp:220
QgsMapLayer * layer(const QString &id)
Returns the map layer with the matching ID, or nullptr if no layers could be found.
Definition: qgsmaptool.cpp:84
QgsMapCanvas * canvas() const
returns pointer to the tool's map canvas
Definition: qgsmaptool.cpp:215
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:351
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:249
virtual bool canvasToolTipEvent(QHelpEvent *e)
Tooltip event for overriding.
Definition: qgsmaptool.cpp:209
void setToolName(const QString &name)
Sets the tool's name.
Definition: qgsmaptool.cpp:89
virtual void canvasDoubleClickEvent(QgsMapMouseEvent *e)
Mouse double-click event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:173
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:160
QString mToolName
The translated name of the map tool.
Definition: qgsmaptool.h:354
QAbstractButton * button()
Returns associated button with map tool or nullptr if no button is associated.
Definition: qgsmaptool.cpp:155
void setButton(QAbstractButton *button)
Use this to associate a button to this maptool.
Definition: qgsmaptool.cpp:150
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:255
virtual void canvasPressEvent(QgsMapMouseEvent *e)
Mouse press event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:178
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:125
virtual void canvasMoveEvent(QgsMapMouseEvent *e)
Mouse move event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:168
QAction * mAction
Optional pointer to an action that will be checked on map tool activation and unchecked on map tool d...
Definition: qgsmaptool.h:345
virtual void keyPressEvent(QKeyEvent *e)
Key event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:193
static double searchRadiusMU(const QgsRenderContext &context)
Gets search radius in map units for given context.
Definition: qgsmaptool.cpp:232
QAction * action()
Returns associated action with map tool or nullptr if no action is associated.
Definition: qgsmaptool.cpp:140
virtual void keyReleaseEvent(QKeyEvent *e)
Key event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:198
~QgsMapTool() override
Definition: qgsmaptool.cpp:35
virtual void canvasReleaseEvent(QgsMapMouseEvent *e)
Mouse release event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:183
QPoint toCanvasCoordinates(const QgsPointXY &point) const
Transforms a point from map coordinates to screen coordinates.
Definition: qgsmaptool.cpp:77
virtual void wheelEvent(QWheelEvent *e)
Mouse wheel event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:188
bool isActive() const
Returns if the current map tool active on the map canvas.
Definition: qgsmaptool.cpp:145
virtual void clean()
convenient method to clean members
Definition: qgsmaptool.cpp:120
virtual void activate()
called when set as currently active map tool
Definition: qgsmaptool.cpp:94
virtual bool gestureEvent(QGestureEvent *e)
gesture event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:203
QCursor mCursor
The cursor used in the map tool.
Definition: qgsmaptool.h:339
virtual void deactivate()
called when map tool is being deactivated
Definition: qgsmaptool.cpp:110
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
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