QGIS API Documentation 3.39.0-Master (47f7b3a4989)
Loading...
Searching...
No Matches
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
77QPoint 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
84QgsMapLayer *QgsMapTool::layer( const QString &id )
85{
86 return mCanvas->layer( id );
87}
88
89void 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
120
122{
123 emit reactivated();
124}
125
127{
128
129}
130
131void QgsMapTool::setAction( QAction *action )
132{
133 if ( mAction )
134 disconnect( mAction, &QObject::destroyed, this, &QgsMapTool::actionDestroyed );
135 mAction = action;
136 if ( mAction )
137 connect( mAction, &QObject::destroyed, this, &QgsMapTool::actionDestroyed );
138}
139
140void QgsMapTool::actionDestroyed()
141{
142 if ( mAction == sender() )
143 mAction = nullptr;
144}
145
147{
148 return mAction;
149}
150
152{
153 return mCanvas && mCanvas->mapTool() == this;
154}
155
156void QgsMapTool::setButton( QAbstractButton *button )
157{
158 mButton = button;
159}
160
161QAbstractButton *QgsMapTool::button()
162{
163 return mButton;
164}
165
166void QgsMapTool::setCursor( const QCursor &cursor )
167{
168 mCursor = cursor;
169 if ( isActive() )
170 mCanvas->setCursor( mCursor );
171}
172
173
175{
176 Q_UNUSED( e )
177}
178
180{
181 Q_UNUSED( e )
182}
183
185{
186 Q_UNUSED( e )
187}
188
190{
191 Q_UNUSED( e )
192}
193
194void QgsMapTool::wheelEvent( QWheelEvent *e )
195{
196 e->ignore();
197}
198
199void QgsMapTool::keyPressEvent( QKeyEvent *e )
200{
201 Q_UNUSED( e )
202}
203
204void QgsMapTool::keyReleaseEvent( QKeyEvent *e )
205{
206 Q_UNUSED( e )
207}
208
209bool QgsMapTool::gestureEvent( QGestureEvent *e )
210{
211 Q_UNUSED( e )
212 return true;
213}
214
216{
217 Q_UNUSED( e )
218 return false;
219}
220
222{
223 return mCanvas;
224}
225
227{
228 const QgsSettings settings;
229 const double radius = settings.value( QStringLiteral( "Map/searchRadiusMM" ), Qgis::DEFAULT_SEARCH_RADIUS_MM ).toDouble();
230
231 if ( radius > 0 )
232 {
233 return radius;
234 }
236}
237
239{
240 return searchRadiusMM() * context.scaleFactor() * context.mapToPixel().mapUnitsPerPixel();
241}
242
244{
245 if ( !canvas )
246 {
247 return 0;
248 }
249 const QgsMapSettings mapSettings = canvas->mapSettings();
250 const QgsRenderContext context = QgsRenderContext::fromMapSettings( mapSettings );
251 return searchRadiusMU( context );
252}
253
254
256{
257
258}
259
260
262{
263 return false;
264}
static const double DEFAULT_SEARCH_RADIUS_MM
Identify search radius in mm.
Definition qgis.h:5136
Map canvas is a class for displaying all GIS data types on a canvas.
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
Base class for all map layer types.
Definition qgsmaplayer.h:75
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.
void deactivated()
signal emitted once the map tool is deactivated
QPointer< QgsMapCanvas > mCanvas
The pointer to the map canvas.
Definition qgsmaptool.h:341
static double searchRadiusMM()
Gets search radius in mm.
QgsMapLayer * layer(const QString &id)
Returns the map layer with the matching ID, or nullptr if no layers could be found.
QgsMapCanvas * canvas() const
returns pointer to the tool's map canvas
QgsMapTool(QgsMapCanvas *canvas)
Constructor takes a map canvas as a parameter.
QAbstractButton * mButton
Optional pointer to a button that will be checked on map tool activation and unchecked on map tool de...
Definition qgsmaptool.h:356
virtual void populateContextMenu(QMenu *menu)
Allows the tool to populate and customize the given menu, prior to showing it in response to a right-...
virtual bool canvasToolTipEvent(QHelpEvent *e)
Tooltip event for overriding.
void setToolName(const QString &name)
Sets the tool's name.
virtual void canvasDoubleClickEvent(QgsMapMouseEvent *e)
Mouse double-click event for overriding. Default implementation does nothing.
QgsPointXY toMapCoordinates(QPoint point)
Transforms a point from screen coordinates to map coordinates.
virtual void setCursor(const QCursor &cursor)
Sets a user defined cursor.
QString mToolName
The translated name of the map tool.
Definition qgsmaptool.h:359
QAbstractButton * button()
Returns associated button with map tool or nullptr if no button is associated.
void setButton(QAbstractButton *button)
Use this to associate a button to this maptool.
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-...
virtual void canvasPressEvent(QgsMapMouseEvent *e)
Mouse press event for overriding. Default implementation does nothing.
void activated()
signal emitted once the map tool is activated
void setAction(QAction *action)
Use this to associate a QAction to this maptool.
virtual void canvasMoveEvent(QgsMapMouseEvent *e)
Mouse move event for overriding. Default implementation does nothing.
QAction * mAction
Optional pointer to an action that will be checked on map tool activation and unchecked on map tool d...
Definition qgsmaptool.h:350
virtual void keyPressEvent(QKeyEvent *e)
Key event for overriding. Default implementation does nothing.
static double searchRadiusMU(const QgsRenderContext &context)
Gets search radius in map units for given context.
QAction * action()
Returns associated action with map tool or nullptr if no action is associated.
virtual void keyReleaseEvent(QKeyEvent *e)
Key event for overriding. Default implementation does nothing.
~QgsMapTool() override
virtual void canvasReleaseEvent(QgsMapMouseEvent *e)
Mouse release event for overriding. Default implementation does nothing.
QPoint toCanvasCoordinates(const QgsPointXY &point) const
Transforms a point from map coordinates to screen coordinates.
void reactivated()
virtual void wheelEvent(QWheelEvent *e)
Mouse wheel event for overriding. Default implementation does nothing.
bool isActive() const
Returns if the current map tool active on the map canvas.
virtual void reactivate()
Called when the map tool is being activated while it is already active.
virtual void clean()
convenient method to clean members
virtual void activate()
called when set as currently active map tool
virtual bool gestureEvent(QGestureEvent *e)
gesture event for overriding. Default implementation does nothing.
QCursor mCursor
The cursor used in the map tool.
Definition qgsmaptool.h:344
virtual void deactivate()
called when map tool is being deactivated
A class to represent a 2D point.
Definition qgspointxy.h:60
double y
Definition qgspointxy.h:64
double x
Definition qgspointxy.h:63
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:49
A rectangle specified with double values.
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:64
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