QGIS API Documentation 4.1.0-Master (60fea48833c)
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#include <QString>
28
29#include "moc_qgsmaptool.cpp"
30
31using namespace Qt::StringLiterals;
32
34 : QObject( canvas )
35 , mCanvas( canvas )
36 , mCursor( Qt::CrossCursor )
37{}
38
39
41{
42 if ( mCanvas )
43 mCanvas->unsetMapTool( this );
44}
45
47{
48 return mCanvas->getCoordinateTransform()->toMapCoordinates( point );
49}
50
52{
53 return mCanvas->mapSettings().layerToMapCoordinates( layer, point );
54}
55
57{
58 const QgsPointXY pt = toMapCoordinates( point );
59 return toLayerCoordinates( layer, pt );
60}
61
63{
64 return mCanvas->mapSettings().mapToLayerCoordinates( layer, point );
65}
66
68{
69 return mCanvas->mapSettings().mapToLayerCoordinates( layer, point );
70}
71
73{
74 return mCanvas->mapSettings().layerToMapCoordinates( layer, point );
75}
76
78{
79 return mCanvas->mapSettings().mapToLayerCoordinates( layer, rect );
80}
81
82QPoint QgsMapTool::toCanvasCoordinates( const QgsPointXY &point ) const
83{
84 qreal x = point.x(), y = point.y();
85 mCanvas->getCoordinateTransform()->transformInPlace( x, y );
86 return QPoint( std::round( x ), std::round( y ) );
87}
88
89QgsMapLayer *QgsMapTool::layer( const QString &id )
90{
91 return mCanvas->layer( id );
92}
93
94void QgsMapTool::setToolName( const QString &name )
95{
96 mToolName = name;
97}
98
100{
101 // make action and/or button active
102 if ( mAction )
103 mAction->setChecked( true );
104 if ( mButton )
105 mButton->setChecked( true );
106
107 // set cursor (map tools usually set it in constructor)
108 mCanvas->setCursor( mCursor );
109 QgsDebugMsgLevel( u"Cursor has been set"_s, 4 );
110
111 emit activated();
112}
113
114
116{
117 if ( mAction )
118 mAction->setChecked( false );
119 if ( mButton )
120 mButton->setChecked( false );
121
122 emit deactivated();
123}
124
125
127{
128 emit reactivated();
129}
130
132{}
133
135{
136 if ( mAction )
137 disconnect( mAction, &QObject::destroyed, this, &QgsMapTool::actionDestroyed );
138 mAction = action;
139 if ( mAction )
140 connect( mAction, &QObject::destroyed, this, &QgsMapTool::actionDestroyed );
141}
142
143void QgsMapTool::actionDestroyed()
144{
145 if ( mAction == sender() )
146 mAction = nullptr;
147}
148
150{
151 return mAction;
152}
153
155{
156 return mCanvas && mCanvas->mapTool() == this;
157}
158
159void QgsMapTool::setButton( QAbstractButton *button )
160{
161 mButton = button;
162}
163
164QAbstractButton *QgsMapTool::button()
165{
166 return mButton;
167}
168
169void QgsMapTool::setCursor( const QCursor &cursor )
170{
171 mCursor = cursor;
172 if ( isActive() )
173 mCanvas->setCursor( mCursor );
174}
175
176
178{
179 Q_UNUSED( e )
180}
181
183{
184 Q_UNUSED( e )
185}
186
188{
189 Q_UNUSED( e )
190}
191
193{
194 Q_UNUSED( e )
195}
196
197void QgsMapTool::wheelEvent( QWheelEvent *e )
198{
199 e->ignore();
200}
201
202void QgsMapTool::keyPressEvent( QKeyEvent *e )
203{
204 Q_UNUSED( e )
205}
206
207void QgsMapTool::keyReleaseEvent( QKeyEvent *e )
208{
209 Q_UNUSED( e )
210}
211
212bool QgsMapTool::gestureEvent( QGestureEvent *e )
213{
214 Q_UNUSED( e )
215 return true;
216}
217
219{
220 Q_UNUSED( e )
221 return false;
222}
223
224bool QgsMapTool::shortcutEvent( QKeyEvent *e )
225{
226 Q_UNUSED( e )
227 return false;
228}
229
231{
232 return mCanvas;
233}
234
236{
237 const QgsSettings settings;
238 const double radius = settings.value( u"Map/searchRadiusMM"_s, Qgis::DEFAULT_SEARCH_RADIUS_MM ).toDouble();
239
240 if ( radius > 0 )
241 {
242 return radius;
243 }
245}
246
251
253{
254 if ( !canvas )
255 {
256 return 0;
257 }
258 const QgsMapSettings mapSettings = canvas->mapSettings();
259 const QgsRenderContext context = QgsRenderContext::fromMapSettings( mapSettings );
260 return searchRadiusMU( context ) / mapSettings.magnificationFactor();
261}
262
263
266
267
269{
270 return false;
271}
@ Millimeters
Millimeters.
Definition qgis.h:5341
static const double DEFAULT_SEARCH_RADIUS_MM
Identify search radius in mm.
Definition qgis.h:6543
Base class for all map layer types.
Definition qgsmaplayer.h:83
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:384
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:369
QString mToolName
The translated name of the map tool.
Definition qgsmaptool.h:387
friend class QgsMapCanvas
Definition qgsmaptool.h:389
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:378
virtual void keyPressEvent(QKeyEvent *e)
Key event for overriding. Default implementation does nothing.
virtual bool shortcutEvent(QKeyEvent *e)
Shortcut events coming from the application for overriding.
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:372
virtual void deactivate()
called when map tool is being deactivated
Represents a 2D point.
Definition qgspointxy.h:62
double y
Definition qgspointxy.h:66
double x
Definition qgspointxy.h:65
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:53
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:68
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:63