QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 }
QgsMapTool::keyReleaseEvent
virtual void keyReleaseEvent(QKeyEvent *e)
Key event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:198
QgsPointXY::y
double y
Definition: qgspointxy.h:63
QgsMapTool::toLayerCoordinates
QgsPoint toLayerCoordinates(const QgsMapLayer *layer, const QgsPoint &point)
Transforms a point from map coordinates to layer coordinates.
Definition: qgsmaptool.cpp:62
QgsRenderContext::mapToPixel
const QgsMapToPixel & mapToPixel() const
Returns the context's map to pixel transform, which transforms between map coordinates and device coo...
Definition: qgsrendercontext.h:258
QgsMapTool::canvasDoubleClickEvent
virtual void canvasDoubleClickEvent(QgsMapMouseEvent *e)
Mouse double-click event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:173
QgsMapToPixel::mapUnitsPerPixel
double mapUnitsPerPixel() const
Returns the current map units per pixel.
Definition: qgsmaptopixel.h:229
QgsSettings::value
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
Definition: qgssettings.cpp:161
QgsMapTool::populateContextMenu
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
qgsmapcanvas.h
QgsRenderContext::fromMapSettings
static QgsRenderContext fromMapSettings(const QgsMapSettings &mapSettings)
create initialized QgsRenderContext instance from given QgsMapSettings
Definition: qgsrendercontext.cpp:234
QgsPoint
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:48
QgsDebugMsgLevel
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39
qgsmaptopixel.h
QgsMapCanvas::mapSettings
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
Definition: qgsmapcanvas.cpp:437
QgsMapTool::QgsMapTool
QgsMapTool(QgsMapCanvas *canvas)
Constructor takes a map canvas as a parameter.
Definition: qgsmaptool.cpp:27
QgsMapTool::setCursor
virtual void setCursor(const QCursor &cursor)
Sets a user defined cursor.
Definition: qgsmaptool.cpp:160
QgsMapCanvas
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:89
QgsMapTool::deactivate
virtual void deactivate()
called when map tool is being deactivated
Definition: qgsmaptool.cpp:110
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:59
QgsSettings
This class is a composition of two QSettings instances:
Definition: qgssettings.h:61
QgsMapTool::canvas
QgsMapCanvas * canvas() const
returns pointer to the tool's map canvas
Definition: qgsmaptool.cpp:215
QgsRenderContext::scaleFactor
double scaleFactor() const
Returns the scaling factor for the render to convert painter units to physical sizes.
Definition: qgsrendercontext.h:266
QgsMapTool::mCanvas
QPointer< QgsMapCanvas > mCanvas
The pointer to the map canvas.
Definition: qgsmaptool.h:336
QgsMapTool::action
QAction * action()
Returns associated action with map tool or nullptr if no action is associated.
Definition: qgsmaptool.cpp:140
QgsMapTool::clean
virtual void clean()
convenient method to clean members
Definition: qgsmaptool.cpp:120
QgsMapTool::keyPressEvent
virtual void keyPressEvent(QKeyEvent *e)
Key event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:193
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QgsMapTool::toCanvasCoordinates
QPoint toCanvasCoordinates(const QgsPointXY &point) const
Transforms a point from map coordinates to screen coordinates.
Definition: qgsmaptool.cpp:77
QgsMapTool::layer
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
QgsMapTool::button
QAbstractButton * button()
Returns associated button with map tool or nullptr if no button is associated.
Definition: qgsmaptool.cpp:155
QgsMapTool::mAction
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
QgsMapTool::gestureEvent
virtual bool gestureEvent(QGestureEvent *e)
gesture event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:203
qgsmaptool.h
QgsMapTool::setToolName
void setToolName(const QString &name)
Sets the tool's name.
Definition: qgsmaptool.cpp:89
qgsrendercontext.h
QgsMapTool::mButton
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
QgsMapTool::setButton
void setButton(QAbstractButton *button)
Use this to associate a button to this maptool.
Definition: qgsmaptool.cpp:150
QgsMapTool::activate
virtual void activate()
called when set as currently active map tool
Definition: qgsmaptool.cpp:94
QgsMapTool::canvasToolTipEvent
virtual bool canvasToolTipEvent(QHelpEvent *e)
Tooltip event for overriding.
Definition: qgsmaptool.cpp:209
QgsPointXY
A class to represent a 2D point.
Definition: qgspointxy.h:58
QgsMapTool::mCursor
QCursor mCursor
The cursor used in the map tool.
Definition: qgsmaptool.h:339
QgsMapTool::canvasPressEvent
virtual void canvasPressEvent(QgsMapMouseEvent *e)
Mouse press event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:178
QgsMapMouseEvent
A QgsMapMouseEvent is the result of a user interaction with the mouse on a QgsMapCanvas....
Definition: qgsmapmouseevent.h:35
QgsMapTool::canvasMoveEvent
virtual void canvasMoveEvent(QgsMapMouseEvent *e)
Mouse move event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:168
QgsMapTool::setAction
void setAction(QAction *action)
Use this to associate a QAction to this maptool.
Definition: qgsmaptool.cpp:125
QgsMapTool::searchRadiusMM
static double searchRadiusMM()
Gets search radius in mm.
Definition: qgsmaptool.cpp:220
QgsMapTool::canvasReleaseEvent
virtual void canvasReleaseEvent(QgsMapMouseEvent *e)
Mouse release event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:183
QgsMapLayer
Base class for all map layer types. This is the base class for all map layer types (vector,...
Definition: qgsmaplayer.h:72
QgsPointXY::x
double x
Definition: qgspointxy.h:62
Qgis::DEFAULT_SEARCH_RADIUS_MM
static const double DEFAULT_SEARCH_RADIUS_MM
Flags which control project capabilities.
Definition: qgis.h:1991
qgssettings.h
QgsMapTool::searchRadiusMU
static double searchRadiusMU(const QgsRenderContext &context)
Gets search radius in map units for given context.
Definition: qgsmaptool.cpp:232
QgsMapTool::wheelEvent
virtual void wheelEvent(QWheelEvent *e)
Mouse wheel event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:188
qgslogger.h
QgsMapTool::isActive
bool isActive() const
Returns if the current map tool active on the map canvas.
Definition: qgsmaptool.cpp:145
QgsMapSettings
The QgsMapSettings class contains configuration for rendering of the map. The rendering itself is don...
Definition: qgsmapsettings.h:88
qgsmapmouseevent.h
QgsMapTool::~QgsMapTool
~QgsMapTool() override
Definition: qgsmaptool.cpp:35
QgsMapTool::activated
void activated()
signal emitted once the map tool is activated
QgsMapTool::toMapCoordinates
QgsPointXY toMapCoordinates(QPoint point)
Transforms a point from screen coordinates to map coordinates.
Definition: qgsmaptool.cpp:41
QgsMapTool::deactivated
void deactivated()
signal emitted once the map tool is deactivated
QgsMapTool::mToolName
QString mToolName
The translated name of the map tool.
Definition: qgsmaptool.h:354
QgsMapTool::populateContextMenuWithEvent
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