QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
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 "moc_qgsmaptool.cpp"
19#include "qgsmapcanvas.h"
20#include "qgsmaptopixel.h"
21#include "qgsrendercontext.h"
22#include "qgssettings.h"
23#include "qgsmapmouseevent.h"
24
25#include <QAction>
26#include <QAbstractButton>
27
29 : QObject( canvas )
30 , mCanvas( canvas )
31 , mCursor( Qt::CrossCursor )
32{
33}
34
35
37{
38 if ( mCanvas )
39 mCanvas->unsetMapTool( this );
40}
41
43{
44 return mCanvas->getCoordinateTransform()->toMapCoordinates( point );
45}
46
48{
49 return mCanvas->mapSettings().layerToMapCoordinates( layer, point );
50}
51
53{
54 const QgsPointXY pt = toMapCoordinates( point );
55 return toLayerCoordinates( layer, pt );
56}
57
59{
60 return mCanvas->mapSettings().mapToLayerCoordinates( layer, point );
61}
62
64{
65 return mCanvas->mapSettings().mapToLayerCoordinates( layer, point );
66}
67
69{
70 return mCanvas->mapSettings().layerToMapCoordinates( layer, point );
71}
72
74{
75 return mCanvas->mapSettings().mapToLayerCoordinates( layer, rect );
76}
77
78QPoint QgsMapTool::toCanvasCoordinates( const QgsPointXY &point ) const
79{
80 qreal x = point.x(), y = point.y();
81 mCanvas->getCoordinateTransform()->transformInPlace( x, y );
82 return QPoint( std::round( x ), std::round( y ) );
83}
84
85QgsMapLayer *QgsMapTool::layer( const QString &id )
86{
87 return mCanvas->layer( id );
88}
89
90void QgsMapTool::setToolName( const QString &name )
91{
92 mToolName = name;
93}
94
96{
97 // make action and/or button active
98 if ( mAction )
99 mAction->setChecked( true );
100 if ( mButton )
101 mButton->setChecked( true );
102
103 // set cursor (map tools usually set it in constructor)
104 mCanvas->setCursor( mCursor );
105 QgsDebugMsgLevel( QStringLiteral( "Cursor has been set" ), 4 );
106
107 emit activated();
108}
109
110
112{
113 if ( mAction )
114 mAction->setChecked( false );
115 if ( mButton )
116 mButton->setChecked( false );
117
118 emit deactivated();
119}
120
121
123{
124 emit reactivated();
125}
126
128{
129
130}
131
132void QgsMapTool::setAction( QAction *action )
133{
134 if ( mAction )
135 disconnect( mAction, &QObject::destroyed, this, &QgsMapTool::actionDestroyed );
136 mAction = action;
137 if ( mAction )
138 connect( mAction, &QObject::destroyed, this, &QgsMapTool::actionDestroyed );
139}
140
141void QgsMapTool::actionDestroyed()
142{
143 if ( mAction == sender() )
144 mAction = nullptr;
145}
146
148{
149 return mAction;
150}
151
153{
154 return mCanvas && mCanvas->mapTool() == this;
155}
156
157void QgsMapTool::setButton( QAbstractButton *button )
158{
159 mButton = button;
160}
161
162QAbstractButton *QgsMapTool::button()
163{
164 return mButton;
165}
166
167void QgsMapTool::setCursor( const QCursor &cursor )
168{
169 mCursor = cursor;
170 if ( isActive() )
171 mCanvas->setCursor( mCursor );
172}
173
174
176{
177 Q_UNUSED( e )
178}
179
181{
182 Q_UNUSED( e )
183}
184
186{
187 Q_UNUSED( e )
188}
189
191{
192 Q_UNUSED( e )
193}
194
195void QgsMapTool::wheelEvent( QWheelEvent *e )
196{
197 e->ignore();
198}
199
200void QgsMapTool::keyPressEvent( QKeyEvent *e )
201{
202 Q_UNUSED( e )
203}
204
205void QgsMapTool::keyReleaseEvent( QKeyEvent *e )
206{
207 Q_UNUSED( e )
208}
209
210bool QgsMapTool::gestureEvent( QGestureEvent *e )
211{
212 Q_UNUSED( e )
213 return true;
214}
215
217{
218 Q_UNUSED( e )
219 return false;
220}
221
223{
224 return mCanvas;
225}
226
228{
229 const QgsSettings settings;
230 const double radius = settings.value( QStringLiteral( "Map/searchRadiusMM" ), Qgis::DEFAULT_SEARCH_RADIUS_MM ).toDouble();
231
232 if ( radius > 0 )
233 {
234 return radius;
235 }
237}
238
240{
241 return searchRadiusMM() * context.scaleFactor() * context.mapToPixel().mapUnitsPerPixel();
242}
243
245{
246 if ( !canvas )
247 {
248 return 0;
249 }
250 const QgsMapSettings mapSettings = canvas->mapSettings();
251 const QgsRenderContext context = QgsRenderContext::fromMapSettings( mapSettings );
252 return searchRadiusMU( context );
253}
254
255
257{
258
259}
260
261
263{
264 return false;
265}
static const double DEFAULT_SEARCH_RADIUS_MM
Identify search radius in mm.
Definition qgis.h:5579
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:76
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