QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 
229 {
230 
231 }
QgsMapTool::mCanvas
QgsMapCanvas * mCanvas
pointer to map canvas
Definition: qgsmaptool.h:264
QgsMapTool::keyReleaseEvent
virtual void keyReleaseEvent(QKeyEvent *e)
Key event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:184
QgsPointXY::y
double y
Definition: qgspointxy.h:48
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:325
QgsMapSettings::mapToLayerCoordinates
QgsPointXY mapToLayerCoordinates(const QgsMapLayer *layer, QgsPointXY point) const
transform point coordinates from output CRS to layer's CRS
Definition: qgsmapsettings.cpp:537
QgsMapTool::canvasDoubleClickEvent
virtual void canvasDoubleClickEvent(QgsMapMouseEvent *e)
Mouse double-click event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:159
QgsMapToPixel::mapUnitsPerPixel
double mapUnitsPerPixel() const
Returns current map units per pixel.
Definition: qgsmaptopixel.cpp:128
QgsSettings::value
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
Definition: qgssettings.cpp:174
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:228
qgsmapcanvas.h
QgsRenderContext::fromMapSettings
static QgsRenderContext fromMapSettings(const QgsMapSettings &mapSettings)
create initialized QgsRenderContext instance from given QgsMapSettings
Definition: qgsrendercontext.cpp:197
QgsMapCanvas::mapTool
QgsMapTool * mapTool()
Returns the currently active tool.
Definition: qgsmapcanvas.cpp:2281
QgsPoint
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:38
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:391
QgsPointXY::x
Q_GADGET double x
Definition: qgspointxy.h:47
QgsMapTool::QgsMapTool
QgsMapTool(QgsMapCanvas *canvas)
constructor takes 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:146
QgsMapCanvas
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:85
QgsMapTool::deactivate
virtual void deactivate()
called when map tool is being deactivated
Definition: qgsmaptool.cpp:96
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:58
QgsSettings
This class is a composition of two QSettings instances:
Definition: qgssettings.h:62
QgsMapTool::canvas
QgsMapCanvas * canvas() const
returns pointer to the tool's map canvas
Definition: qgsmaptool.cpp:195
QgsRenderContext::scaleFactor
double scaleFactor() const
Returns the scaling factor for the render to convert painter units to physical sizes.
Definition: qgsrendercontext.h:333
QgsMapToPixel::toMapCoordinates
QgsPointXY toMapCoordinates(int x, int y) const
Transform device coordinates to map (world) coordinates.
Definition: qgsmaptopixel.cpp:108
QgsMapTool::action
QAction * action()
Returns associated action with map tool or nullptr if no action is associated.
Definition: qgsmaptool.cpp:126
QgsMapTool::clean
virtual void clean()
convenient method to clean members
Definition: qgsmaptool.cpp:106
QgsMapTool::keyPressEvent
virtual void keyPressEvent(QKeyEvent *e)
Key event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:179
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:42
QgsMapTool::toLayerCoordinates
QgsPointXY toLayerCoordinates(const QgsMapLayer *layer, QPoint point)
transformation from screen coordinates to layer's coordinates
Definition: qgsmaptool.cpp:51
QgsMapTool::toCanvasCoordinates
QPoint toCanvasCoordinates(const QgsPointXY &point) const
transformation from map coordinates to screen coordinates
Definition: qgsmaptool.cpp:72
QgsMapTool::button
QAbstractButton * button()
Returns associated button with map tool or nullptr if no button is associated.
Definition: qgsmaptool.cpp:141
QgsMapTool::mAction
QAction * mAction
optionally map tool can have pointer to action which will be used to set that action as active
Definition: qgsmaptool.h:273
QgsMapTool::gestureEvent
virtual bool gestureEvent(QGestureEvent *e)
gesture event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:189
qgsmaptool.h
qgsrendercontext.h
QgsMapTool::mButton
QAbstractButton * mButton
optionally map tool can have pointer to a button which will be used to set that action as active
Definition: qgsmaptool.h:279
QgsMapTool::setButton
void setButton(QAbstractButton *button)
Use this to associate a button to this maptool.
Definition: qgsmaptool.cpp:136
QgsMapCanvas::getCoordinateTransform
const QgsMapToPixel * getCoordinateTransform()
Gets the current coordinate transform.
Definition: qgsmapcanvas.cpp:335
QgsMapTool::activate
virtual void activate()
called when set as currently active map tool
Definition: qgsmaptool.cpp:80
QgsPointXY
A class to represent a 2D point.
Definition: qgspointxy.h:44
QgsMapTool::mCursor
QCursor mCursor
cursor used in map tool
Definition: qgsmaptool.h:267
QgsMapTool::canvasPressEvent
virtual void canvasPressEvent(QgsMapMouseEvent *e)
Mouse press event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:164
QgsMapToPixel::transformInPlace
void transformInPlace(double &x, double &y) const
Transform device coordinates to map coordinates.
Definition: qgsmaptopixel.cpp:233
QgsMapMouseEvent
A QgsMapMouseEvent is the result of a user interaction with the mouse on a QgsMapCanvas.
Definition: qgsmapmouseevent.h:36
QgsMapTool::canvasMoveEvent
virtual void canvasMoveEvent(QgsMapMouseEvent *e)
Mouse move event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:154
QgsMapTool::setAction
void setAction(QAction *action)
Use this to associate a QAction to this maptool.
Definition: qgsmaptool.cpp:111
QgsMapTool::searchRadiusMM
static double searchRadiusMM()
Gets search radius in mm.
Definition: qgsmaptool.cpp:200
QgsMapTool::canvasReleaseEvent
virtual void canvasReleaseEvent(QgsMapMouseEvent *e)
Mouse release event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:169
QgsMapLayer
Base class for all map layer types.
Definition: qgsmaplayer.h:83
Qgis::DEFAULT_SEARCH_RADIUS_MM
static const double DEFAULT_SEARCH_RADIUS_MM
Identify search radius in mm.
Definition: qgis.h:137
qgssettings.h
QgsMapCanvas::unsetMapTool
void unsetMapTool(QgsMapTool *mapTool)
Unset the current map tool or last non zoom tool.
Definition: qgsmapcanvas.cpp:2046
QgsMapTool::searchRadiusMU
static double searchRadiusMU(const QgsRenderContext &context)
Gets search radius in map units for given context.
Definition: qgsmaptool.cpp:212
QgsMapTool::wheelEvent
virtual void wheelEvent(QWheelEvent *e)
Mouse wheel event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:174
qgslogger.h
QgsMapTool::isActive
bool isActive() const
Returns if the current map tool active on the map canvas.
Definition: qgsmaptool.cpp:131
QgsMapSettings
The QgsMapSettings class contains configuration for rendering of the map.
Definition: qgsmapsettings.h:88
qgsmapmouseevent.h
QgsMapSettings::layerToMapCoordinates
QgsPointXY layerToMapCoordinates(const QgsMapLayer *layer, QgsPointXY point) const
transform point coordinates from layer's CRS to output CRS
Definition: qgsmapsettings.cpp:483
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)
transformation from screen coordinates to map coordinates
Definition: qgsmaptool.cpp:41
QgsMapTool::deactivated
void deactivated()
signal emitted once the map tool is deactivated