QGIS API Documentation  3.13.0-Master (788156190c)
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 
41 
43 {
45 }
46 
48 {
49  QgsPointXY result = mCanvas->mapSettings().layerToMapCoordinates( layer, QgsPointXY( point.x(), point.y() ) );
50  return QgsPoint( result );
51 }
52 
53 
55 {
56  QgsPointXY pt = toMapCoordinates( point );
57  return toLayerCoordinates( layer, pt );
58 }
59 
61 {
62  return mCanvas->mapSettings().mapToLayerCoordinates( layer, point );
63 }
64 
66 {
67  return mCanvas->mapSettings().layerToMapCoordinates( layer, point );
68 }
69 
71 {
72  return mCanvas->mapSettings().mapToLayerCoordinates( layer, rect );
73 }
74 
75 QPoint QgsMapTool::toCanvasCoordinates( const QgsPointXY &point ) const
76 {
77  qreal x = point.x(), y = point.y();
79  return QPoint( std::round( x ), std::round( y ) );
80 }
81 
82 
84 {
85  // make action and/or button active
86  if ( mAction )
87  mAction->setChecked( true );
88  if ( mButton )
89  mButton->setChecked( true );
90 
91  // set cursor (map tools usually set it in constructor)
92  mCanvas->setCursor( mCursor );
93  QgsDebugMsgLevel( QStringLiteral( "Cursor has been set" ), 4 );
94 
95  emit activated();
96 }
97 
98 
100 {
101  if ( mAction )
102  mAction->setChecked( false );
103  if ( mButton )
104  mButton->setChecked( false );
105 
106  emit deactivated();
107 }
108 
110 {
111 
112 }
113 
114 void QgsMapTool::setAction( QAction *action )
115 {
116  if ( mAction )
117  disconnect( mAction, &QObject::destroyed, this, &QgsMapTool::actionDestroyed );
118  mAction = action;
119  if ( mAction )
120  connect( mAction, &QObject::destroyed, this, &QgsMapTool::actionDestroyed );
121 }
122 
123 void QgsMapTool::actionDestroyed()
124 {
125  if ( mAction == sender() )
126  mAction = nullptr;
127 }
128 
130 {
131  return mAction;
132 }
133 
135 {
136  return mCanvas && mCanvas->mapTool() == this;
137 }
138 
139 void QgsMapTool::setButton( QAbstractButton *button )
140 {
141  mButton = button;
142 }
143 
144 QAbstractButton *QgsMapTool::button()
145 {
146  return mButton;
147 }
148 
149 void QgsMapTool::setCursor( const QCursor &cursor )
150 {
151  mCursor = cursor;
152  if ( isActive() )
153  mCanvas->setCursor( mCursor );
154 }
155 
156 
158 {
159  Q_UNUSED( e )
160 }
161 
163 {
164  Q_UNUSED( e )
165 }
166 
168 {
169  Q_UNUSED( e )
170 }
171 
173 {
174  Q_UNUSED( e )
175 }
176 
177 void QgsMapTool::wheelEvent( QWheelEvent *e )
178 {
179  e->ignore();
180 }
181 
182 void QgsMapTool::keyPressEvent( QKeyEvent *e )
183 {
184  Q_UNUSED( e )
185 }
186 
187 void QgsMapTool::keyReleaseEvent( QKeyEvent *e )
188 {
189  Q_UNUSED( e )
190 }
191 
192 bool QgsMapTool::gestureEvent( QGestureEvent *e )
193 {
194  Q_UNUSED( e )
195  return true;
196 }
197 
199 {
200  return mCanvas;
201 }
202 
204 {
205  QgsSettings settings;
206  double radius = settings.value( QStringLiteral( "Map/searchRadiusMM" ), Qgis::DEFAULT_SEARCH_RADIUS_MM ).toDouble();
207 
208  if ( radius > 0 )
209  {
210  return radius;
211  }
213 }
214 
216 {
217  return searchRadiusMM() * context.scaleFactor() * context.mapToPixel().mapUnitsPerPixel();
218 }
219 
221 {
222  if ( !canvas )
223  {
224  return 0;
225  }
226  QgsMapSettings mapSettings = canvas->mapSettings();
227  QgsRenderContext context = QgsRenderContext::fromMapSettings( mapSettings );
228  return searchRadiusMU( context );
229 }
QgsMapTool::mCanvas
QgsMapCanvas * mCanvas
pointer to map canvas
Definition: qgsmaptool.h:241
QgsMapTool::keyReleaseEvent
virtual void keyReleaseEvent(QKeyEvent *e)
Key event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:187
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:307
QgsMapSettings::mapToLayerCoordinates
QgsPointXY mapToLayerCoordinates(const QgsMapLayer *layer, QgsPointXY point) const
transform point coordinates from output CRS to layer's CRS
Definition: qgsmapsettings.cpp:515
QgsMapTool::canvasDoubleClickEvent
virtual void canvasDoubleClickEvent(QgsMapMouseEvent *e)
Mouse double-click event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:162
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
qgsmapcanvas.h
QgsRenderContext::fromMapSettings
static QgsRenderContext fromMapSettings(const QgsMapSettings &mapSettings)
create initialized QgsRenderContext instance from given QgsMapSettings
Definition: qgsrendercontext.cpp:168
QgsMapCanvas::mapTool
QgsMapTool * mapTool()
Returns the currently active tool.
Definition: qgsmapcanvas.cpp:2056
QgsPoint
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
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:380
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:149
QgsMapCanvas
Definition: qgsmapcanvas.h:78
QgsMapTool::deactivate
virtual void deactivate()
called when map tool is being deactivated
Definition: qgsmaptool.cpp:99
QgsRenderContext
Definition: qgsrendercontext.h:56
QgsSettings
Definition: qgssettings.h:61
QgsMapTool::canvas
QgsMapCanvas * canvas() const
returns pointer to the tool's map canvas
Definition: qgsmaptool.cpp:198
QgsRenderContext::scaleFactor
double scaleFactor() const
Returns the scaling factor for the render to convert painter units to physical sizes.
Definition: qgsrendercontext.h:315
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:129
QgsMapTool::clean
virtual void clean()
convenient method to clean members
Definition: qgsmaptool.cpp:109
QgsMapTool::keyPressEvent
virtual void keyPressEvent(QKeyEvent *e)
Key event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:182
QgsRectangle
Definition: qgsrectangle.h:41
QgsMapTool::toLayerCoordinates
QgsPointXY toLayerCoordinates(const QgsMapLayer *layer, QPoint point)
transformation from screen coordinates to layer's coordinates
Definition: qgsmaptool.cpp:54
QgsPoint::y
double y
Definition: qgspoint.h:59
QgsMapTool::toCanvasCoordinates
QPoint toCanvasCoordinates(const QgsPointXY &point) const
transformation from map coordinates to screen coordinates
Definition: qgsmaptool.cpp:75
QgsMapTool::button
QAbstractButton * button()
Returns associated button with map tool or nullptr if no button is associated.
Definition: qgsmaptool.cpp:144
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:250
QgsMapTool::gestureEvent
virtual bool gestureEvent(QGestureEvent *e)
gesture event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:192
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:256
QgsMapTool::setButton
void setButton(QAbstractButton *button)
Use this to associate a button to this maptool.
Definition: qgsmaptool.cpp:139
QgsMapCanvas::getCoordinateTransform
const QgsMapToPixel * getCoordinateTransform()
Gets the current coordinate transform.
Definition: qgsmapcanvas.cpp:324
QgsMapTool::activate
virtual void activate()
called when set as currently active map tool
Definition: qgsmaptool.cpp:83
QgsPointXY
Definition: qgspointxy.h:43
QgsMapTool::mCursor
QCursor mCursor
cursor used in map tool
Definition: qgsmaptool.h:244
QgsMapTool::canvasPressEvent
virtual void canvasPressEvent(QgsMapMouseEvent *e)
Mouse press event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:167
QgsMapToPixel::transformInPlace
void transformInPlace(double &x, double &y) const
Transform device coordinates to map coordinates.
Definition: qgsmaptopixel.cpp:233
QgsMapMouseEvent
Definition: qgsmapmouseevent.h:35
QgsMapTool::canvasMoveEvent
virtual void canvasMoveEvent(QgsMapMouseEvent *e)
Mouse move event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:157
QgsMapTool::setAction
void setAction(QAction *action)
Use this to associate a QAction to this maptool.
Definition: qgsmaptool.cpp:114
QgsMapTool::searchRadiusMM
static double searchRadiusMM()
Gets search radius in mm.
Definition: qgsmaptool.cpp:203
QgsMapTool::canvasReleaseEvent
virtual void canvasReleaseEvent(QgsMapMouseEvent *e)
Mouse release event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:172
QgsMapLayer
Definition: qgsmaplayer.h:80
QgsPointXY::x
double x
Definition: qgspointxy.h:47
Qgis::DEFAULT_SEARCH_RADIUS_MM
static const double DEFAULT_SEARCH_RADIUS_MM
Identify search radius in mm.
Definition: qgis.h:150
qgssettings.h
QgsMapCanvas::unsetMapTool
void unsetMapTool(QgsMapTool *mapTool)
Unset the current map tool or last non zoom tool.
Definition: qgsmapcanvas.cpp:1826
QgsMapTool::searchRadiusMU
static double searchRadiusMU(const QgsRenderContext &context)
Gets search radius in map units for given context.
Definition: qgsmaptool.cpp:215
QgsMapTool::wheelEvent
virtual void wheelEvent(QWheelEvent *e)
Mouse wheel event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:177
qgslogger.h
QgsMapTool::isActive
bool isActive() const
Returns if the current map tool active on the map canvas.
Definition: qgsmaptool.cpp:134
QgsMapSettings
Definition: qgsmapsettings.h:86
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:481
QgsMapTool::~QgsMapTool
~QgsMapTool() override
Definition: qgsmaptool.cpp:35
QgsPoint::x
double x
Definition: qgspoint.h:58
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:42
QgsMapTool::deactivated
void deactivated()
signal emitted once the map tool is deactivated