QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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 "qgsmaptool.h"
17
18#include "qgslogger.h"
19#include "qgsmapcanvas.h"
20#include "qgsmapmouseevent.h"
21#include "qgsmaptopixel.h"
22#include "qgsrendercontext.h"
23#include "qgssettings.h"
24
25#include <QAbstractButton>
26#include <QAction>
27
28#include "moc_qgsmaptool.cpp"
29
31 : QObject( canvas )
32 , mCanvas( canvas )
33 , mCursor( Qt::CrossCursor )
34{
35}
36
37
39{
40 if ( mCanvas )
41 mCanvas->unsetMapTool( this );
42}
43
45{
46 return mCanvas->getCoordinateTransform()->toMapCoordinates( point );
47}
48
50{
51 return mCanvas->mapSettings().layerToMapCoordinates( layer, point );
52}
53
55{
56 const 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().mapToLayerCoordinates( layer, point );
68}
69
71{
72 return mCanvas->mapSettings().layerToMapCoordinates( layer, point );
73}
74
76{
77 return mCanvas->mapSettings().mapToLayerCoordinates( layer, rect );
78}
79
80QPoint QgsMapTool::toCanvasCoordinates( const QgsPointXY &point ) const
81{
82 qreal x = point.x(), y = point.y();
83 mCanvas->getCoordinateTransform()->transformInPlace( x, y );
84 return QPoint( std::round( x ), std::round( y ) );
85}
86
87QgsMapLayer *QgsMapTool::layer( const QString &id )
88{
89 return mCanvas->layer( id );
90}
91
92void QgsMapTool::setToolName( const QString &name )
93{
94 mToolName = name;
95}
96
98{
99 // make action and/or button active
100 if ( mAction )
101 mAction->setChecked( true );
102 if ( mButton )
103 mButton->setChecked( true );
104
105 // set cursor (map tools usually set it in constructor)
106 mCanvas->setCursor( mCursor );
107 QgsDebugMsgLevel( QStringLiteral( "Cursor has been set" ), 4 );
108
109 emit activated();
110}
111
112
114{
115 if ( mAction )
116 mAction->setChecked( false );
117 if ( mButton )
118 mButton->setChecked( false );
119
120 emit deactivated();
121}
122
123
125{
126 emit reactivated();
127}
128
130{
131}
132
134{
135 if ( mAction )
136 disconnect( mAction, &QObject::destroyed, this, &QgsMapTool::actionDestroyed );
137 mAction = action;
138 if ( mAction )
139 connect( mAction, &QObject::destroyed, this, &QgsMapTool::actionDestroyed );
140}
141
142void QgsMapTool::actionDestroyed()
143{
144 if ( mAction == sender() )
145 mAction = nullptr;
146}
147
149{
150 return mAction;
151}
152
154{
155 return mCanvas && mCanvas->mapTool() == this;
156}
157
158void QgsMapTool::setButton( QAbstractButton *button )
159{
160 mButton = button;
161}
162
163QAbstractButton *QgsMapTool::button()
164{
165 return mButton;
166}
167
168void QgsMapTool::setCursor( const QCursor &cursor )
169{
170 mCursor = cursor;
171 if ( isActive() )
172 mCanvas->setCursor( mCursor );
173}
174
175
177{
178 Q_UNUSED( e )
179}
180
182{
183 Q_UNUSED( e )
184}
185
187{
188 Q_UNUSED( e )
189}
190
192{
193 Q_UNUSED( e )
194}
195
196void QgsMapTool::wheelEvent( QWheelEvent *e )
197{
198 e->ignore();
199}
200
201void QgsMapTool::keyPressEvent( QKeyEvent *e )
202{
203 Q_UNUSED( e )
204}
205
206void QgsMapTool::keyReleaseEvent( QKeyEvent *e )
207{
208 Q_UNUSED( e )
209}
210
211bool QgsMapTool::gestureEvent( QGestureEvent *e )
212{
213 Q_UNUSED( e )
214 return true;
215}
216
218{
219 Q_UNUSED( e )
220 return false;
221}
222
224{
225 return mCanvas;
226}
227
229{
230 const QgsSettings settings;
231 const double radius = settings.value( QStringLiteral( "Map/searchRadiusMM" ), Qgis::DEFAULT_SEARCH_RADIUS_MM ).toDouble();
232
233 if ( radius > 0 )
234 {
235 return radius;
236 }
238}
239
244
246{
247 if ( !canvas )
248 {
249 return 0;
250 }
251 const QgsMapSettings mapSettings = canvas->mapSettings();
252 const QgsRenderContext context = QgsRenderContext::fromMapSettings( mapSettings );
253 return searchRadiusMU( context ) / mapSettings.magnificationFactor();
254}
255
256
258{
259}
260
261
263{
264 return false;
265}
@ Millimeters
Millimeters.
Definition qgis.h:5184
static const double DEFAULT_SEARCH_RADIUS_MM
Identify search radius in mm.
Definition qgis.h:6174
Base class for all map layer types.
Definition qgsmaplayer.h:80
A mouse event which is the result of a user interaction with a QgsMapCanvas.
Contains configuration for rendering maps.
double magnificationFactor() const
Returns the magnification factor.
QgsPoint toLayerCoordinates(const QgsMapLayer *layer, const QgsPoint &point)
Transforms a point from map coordinates to layer coordinates.
void deactivated()
Emitted when the map tool is deactivated.
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:375
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.
QPointer< QgsMapCanvas > mCanvas
The pointer to the map canvas.
Definition qgsmaptool.h:360
QString mToolName
The translated name of the map tool.
Definition qgsmaptool.h:378
friend class QgsMapCanvas
Definition qgsmaptool.h:380
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()
Emitted when 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:369
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()
Emitted when the map tool is activated, while it is already active.
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:363
virtual void deactivate()
called when map tool is being deactivated
Represents 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 convertToMapUnits(double size, Qgis::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale()) const
Converts a size from the specified units to map units.
static QgsRenderContext fromMapSettings(const QgsMapSettings &mapSettings)
create initialized QgsRenderContext instance from given QgsMapSettings
Stores settings for use within QGIS.
Definition qgssettings.h:65
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:61