QGIS API Documentation 4.1.0-Master (3b8ef1f72a3)
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"
25#include "qgssettingstree.h"
26
27#include <QAbstractButton>
28#include <QAction>
29#include <QString>
30
31#include "moc_qgsmaptool.cpp"
32
33using namespace Qt::StringLiterals;
34
36 = new QgsSettingsEntryDouble( u"search-radius-mm"_s, QgsSettingsTree::sTreeMap, Qgis::DEFAULT_SEARCH_RADIUS_MM, u"Search/identify radius in millimeters"_s );
37
39 : QObject( canvas )
40 , mCanvas( canvas )
41 , mCursor( Qt::CrossCursor )
42{}
43
44
46{
47 if ( mCanvas )
48 mCanvas->unsetMapTool( this );
49}
50
52{
53 return mCanvas->getCoordinateTransform()->toMapCoordinates( point );
54}
55
57{
58 return mCanvas->mapSettings().layerToMapCoordinates( layer, point );
59}
60
62{
63 const QgsPointXY pt = toMapCoordinates( point );
64 return toLayerCoordinates( layer, pt );
65}
66
68{
69 return mCanvas->mapSettings().mapToLayerCoordinates( layer, point );
70}
71
73{
74 return mCanvas->mapSettings().mapToLayerCoordinates( layer, point );
75}
76
78{
79 return mCanvas->mapSettings().layerToMapCoordinates( layer, point );
80}
81
83{
84 return mCanvas->mapSettings().mapToLayerCoordinates( layer, rect );
85}
86
87QPoint QgsMapTool::toCanvasCoordinates( const QgsPointXY &point ) const
88{
89 qreal x = point.x(), y = point.y();
90 mCanvas->getCoordinateTransform()->transformInPlace( x, y );
91 return QPoint( std::round( x ), std::round( y ) );
92}
93
94QgsMapLayer *QgsMapTool::layer( const QString &id )
95{
96 return mCanvas->layer( id );
97}
98
99void QgsMapTool::setToolName( const QString &name )
100{
101 mToolName = name;
102}
103
105{
106 // make action and/or button active
107 if ( mAction )
108 mAction->setChecked( true );
109 if ( mButton )
110 mButton->setChecked( true );
111
112 // set cursor (map tools usually set it in constructor)
113 mCanvas->setCursor( mCursor );
114 QgsDebugMsgLevel( u"Cursor has been set"_s, 4 );
115
116 emit activated();
117}
118
119
121{
122 if ( mAction )
123 mAction->setChecked( false );
124 if ( mButton )
125 mButton->setChecked( false );
126
127 emit deactivated();
128}
129
130
132{
133 emit reactivated();
134}
135
137{}
138
140{
141 if ( mAction )
142 disconnect( mAction, &QObject::destroyed, this, &QgsMapTool::actionDestroyed );
143 mAction = action;
144 if ( mAction )
145 connect( mAction, &QObject::destroyed, this, &QgsMapTool::actionDestroyed );
146}
147
148void QgsMapTool::actionDestroyed()
149{
150 if ( mAction == sender() )
151 mAction = nullptr;
152}
153
155{
156 return mAction;
157}
158
160{
161 return mCanvas && mCanvas->mapTool() == this;
162}
163
164void QgsMapTool::setButton( QAbstractButton *button )
165{
166 mButton = button;
167}
168
169QAbstractButton *QgsMapTool::button()
170{
171 return mButton;
172}
173
174void QgsMapTool::setCursor( const QCursor &cursor )
175{
176 mCursor = cursor;
177 if ( isActive() )
178 mCanvas->setCursor( mCursor );
179}
180
181
183{
184 Q_UNUSED( e )
185}
186
188{
189 Q_UNUSED( e )
190}
191
193{
194 Q_UNUSED( e )
195}
196
198{
199 Q_UNUSED( e )
200}
201
202void QgsMapTool::wheelEvent( QWheelEvent *e )
203{
204 e->ignore();
205}
206
207void QgsMapTool::keyPressEvent( QKeyEvent *e )
208{
209 Q_UNUSED( e )
210}
211
212void QgsMapTool::keyReleaseEvent( QKeyEvent *e )
213{
214 Q_UNUSED( e )
215}
216
217bool QgsMapTool::gestureEvent( QGestureEvent *e )
218{
219 Q_UNUSED( e )
220 return true;
221}
222
224{
225 Q_UNUSED( e )
226 return false;
227}
228
229bool QgsMapTool::shortcutEvent( QKeyEvent *e )
230{
231 Q_UNUSED( e )
232 return false;
233}
234
236{
237 return mCanvas;
238}
239
241{
242 const double radius = settingSearchRadiusMM->value();
243
244 if ( radius > 0 )
245 {
246 return radius;
247 }
249}
250
255
257{
258 if ( !canvas )
259 {
260 return 0;
261 }
262 const QgsMapSettings mapSettings = canvas->mapSettings();
263 const QgsRenderContext context = QgsRenderContext::fromMapSettings( mapSettings );
264 return searchRadiusMU( context ) / mapSettings.magnificationFactor();
265}
266
267
270
271
273{
274 return false;
275}
@ Millimeters
Millimeters.
Definition qgis.h:5440
static const double DEFAULT_SEARCH_RADIUS_MM
Identify search radius in mm.
Definition qgis.h:6642
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:395
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:380
QString mToolName
The translated name of the map tool.
Definition qgsmaptool.h:398
friend class QgsMapCanvas
Definition qgsmaptool.h:400
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:389
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.
static const QgsSettingsEntryDouble * settingSearchRadiusMM
Settings entry for the search/identify radius in mm.
Definition qgsmaptool.h:233
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:383
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
A double settings entry.
static QgsSettingsTreeNode * sTreeMap
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:63