QGIS API Documentation 3.29.0-Master (006c3c0232)
qgsmaptool.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsmaptool.h - 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#ifndef QGSMAPTOOL_H
17#define QGSMAPTOOL_H
18
19#include "qgsconfig.h"
20#include "qgis.h"
21
22#include <QCursor>
23#include <QString>
24#include <QObject>
25#include <QPointer>
26#include <QGestureEvent>
27#include "qgis_gui.h"
28
29
30class QgsMapLayer;
31class QgsMapCanvas;
33class QKeyEvent;
34class QMouseEvent;
35class QWheelEvent;
36class QgsPoint;
37class QgsPointXY;
38class QgsRectangle;
39class QPoint;
40class QAction;
41class QAbstractButton;
43class QMenu;
44
45#ifdef SIP_RUN
46% ModuleHeaderCode
47// fix to allow compilation with sip 4.7 that for some reason
48// doesn't add these includes to the file where the code from
49// ConvertToSubClassCode goes.
50#include <qgsmaptoolzoom.h>
51#include <qgsmaptoolpan.h>
52#include <qgsmaptoolemitpoint.h>
53#include <qgsmaptoolidentify.h>
55#include <qgsmaptoolextent.h>
57#include <qgsmaptoolcapture.h>
59#include <qgsmaptooledit.h>
60% End
61#endif
62
70class GUI_EXPORT QgsMapTool : public QObject
71{
72
73#ifdef SIP_RUN
75 if ( dynamic_cast<QgsMapToolZoom *>( sipCpp ) != NULL )
76 sipType = sipType_QgsMapToolZoom;
77 else if ( dynamic_cast<QgsMapToolPan *>( sipCpp ) != NULL )
78 sipType = sipType_QgsMapToolPan;
79 else if ( dynamic_cast<QgsMapToolEmitPoint *>( sipCpp ) != NULL )
80 sipType = sipType_QgsMapToolEmitPoint;
81 else if ( dynamic_cast<QgsMapToolExtent *>( sipCpp ) != NULL )
82 sipType = sipType_QgsMapToolExtent;
83 else if ( dynamic_cast<QgsMapToolIdentifyFeature *>( sipCpp ) != NULL )
84 sipType = sipType_QgsMapToolIdentifyFeature;
85 else if ( dynamic_cast<QgsMapToolIdentify *>( sipCpp ) != NULL )
86 sipType = sipType_QgsMapToolIdentify;
87 else if ( dynamic_cast<QgsMapToolDigitizeFeature *>( sipCpp ) != NULL )
88 sipType = sipType_QgsMapToolDigitizeFeature;
89 else if ( dynamic_cast<QgsMapToolCapture *>( sipCpp ) != NULL )
90 sipType = sipType_QgsMapToolCapture;
91 else if ( dynamic_cast<QgsMapToolAdvancedDigitizing *>( sipCpp ) != NULL )
92 sipType = sipType_QgsMapToolAdvancedDigitizing;
93 else if ( dynamic_cast<QgsMapToolEdit *>( sipCpp ) != NULL )
94 sipType = sipType_QgsMapToolEdit;
95 else if ( sipCpp->inherits( "QgsMapTool" ) ) // e.g. map tools from QGIS app library, which aren't exposed to SIP
96 sipType = sipType_QgsMapTool;
97 else
98 sipType = nullptr;
100#endif
101
102 Q_OBJECT
103
104 public:
105
110 enum Flag
111 {
112 Transient = 1 << 1,
115 EditTool = 1 << 2,
116 AllowZoomRect = 1 << 3,
117 ShowContextMenu = 1 << 4,
118 };
119 Q_DECLARE_FLAGS( Flags, Flag )
120
121
125 virtual Flags flags() const { return Flags(); }
126
127 ~QgsMapTool() override;
128
130 virtual void canvasMoveEvent( QgsMapMouseEvent *e );
131
133 virtual void canvasDoubleClickEvent( QgsMapMouseEvent *e );
134
136 virtual void canvasPressEvent( QgsMapMouseEvent *e );
137
139 virtual void canvasReleaseEvent( QgsMapMouseEvent *e );
140
142 virtual void wheelEvent( QWheelEvent *e );
143
145 virtual void keyPressEvent( QKeyEvent *e );
146
148 virtual void keyReleaseEvent( QKeyEvent *e );
149
151 virtual bool gestureEvent( QGestureEvent *e );
152
158 virtual bool canvasToolTipEvent( QHelpEvent *e );
159
166 void setAction( QAction *action );
167
169 QAction *action();
170
175 bool isActive() const;
176
181 void setButton( QAbstractButton *button );
182
184 QAbstractButton *button();
185
187 virtual void setCursor( const QCursor &cursor );
188
190 virtual void activate();
191
193 virtual void deactivate();
194
196 virtual void clean();
197
199 QgsMapCanvas *canvas() const;
200
206 QString toolName() { return mToolName; }
207
214 static double searchRadiusMM();
215
221 static double searchRadiusMU( const QgsRenderContext &context );
222
228 static double searchRadiusMU( QgsMapCanvas *canvas );
229
244 virtual void populateContextMenu( QMenu *menu );
245
264 virtual bool populateContextMenuWithEvent( QMenu *menu, QgsMapMouseEvent *event );
265
267 QgsPointXY toMapCoordinates( QPoint point );
268
269 signals:
271 void messageEmitted( const QString &message, Qgis::MessageLevel = Qgis::MessageLevel::Info );
272
275
277 void activated();
278
281
282 private slots:
284 void actionDestroyed();
285
286 protected:
287
290
295 QgsPoint toLayerCoordinates( const QgsMapLayer *layer, const QgsPoint &point ) SIP_PYNAME( toLayerCoordinatesV2 );
296
298 QgsPointXY toLayerCoordinates( const QgsMapLayer *layer, QPoint point );
299
301 QgsPointXY toLayerCoordinates( const QgsMapLayer *layer, const QgsPointXY &point );
302
304 QgsPointXY toMapCoordinates( const QgsMapLayer *layer, const QgsPointXY &point );
305
310 QgsPoint toMapCoordinates( const QgsMapLayer *layer, const QgsPoint &point ) SIP_PYNAME( toMapCoordinatesV2 );
311
313 QgsRectangle toLayerCoordinates( const QgsMapLayer *layer, const QgsRectangle &rect );
314
316 QPoint toCanvasCoordinates( const QgsPointXY &point ) const;
317
327 QgsMapLayer *layer( const QString &id );
328
335 void setToolName( const QString &name );
336
338 QPointer< QgsMapCanvas > mCanvas;
339
341 QCursor mCursor;
342
347 QAction *mAction = nullptr;
348
353 QAbstractButton *mButton = nullptr;
354
356 QString mToolName;
357
358 friend class QgsMapCanvas;
359 friend class TestQgsMapToolEdit;
360
361};
362
363Q_DECLARE_OPERATORS_FOR_FLAGS( QgsMapTool::Flags )
364
365#endif
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:115
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:90
Base class for all map layer types.
Definition: qgsmaplayer.h:73
A QgsMapMouseEvent is the result of a user interaction with the mouse on a QgsMapCanvas.
The QgsMapToolAdvancedDigitizing class is a QgsMapTool which gives event directly in map coordinates ...
QgsMapToolCapture is a base class capable of capturing point, lines and polygons.
This tool digitizes geometry of new point/line/polygon features on already existing vector layers Onc...
Base class for map tools that edit vector geometry.
A map tool that simply emits a point when clicking on the map.
A map tool that emits an extent from a rectangle drawn onto the map canvas.
The QgsMapToolIdentifyFeature class is a map tool to identify a feature on a chosen layer.
Map tool for identifying features in layers.
A map tool for panning the map.
Definition: qgsmaptoolpan.h:33
A map tool for zooming into the map.
Abstract base class for all map tools.
Definition: qgsmaptool.h:71
QString toolName()
Emit map tool changed with the old tool.
Definition: qgsmaptool.h:206
void deactivated()
signal emitted once the map tool is deactivated
QPointer< QgsMapCanvas > mCanvas
The pointer to the map canvas.
Definition: qgsmaptool.h:338
void messageDiscarded()
emit signal to clear previous message
QString mToolName
The translated name of the map tool.
Definition: qgsmaptool.h:356
void messageEmitted(const QString &message, Qgis::MessageLevel=Qgis::MessageLevel::Info)
emit a message
void activated()
signal emitted once the map tool is activated
Flag
Enumeration of flags that adjust the way the map tool operates.
Definition: qgsmaptool.h:111
QCursor mCursor
The cursor used in the map tool.
Definition: qgsmaptool.h:341
A class to represent a 2D point.
Definition: qgspointxy.h:59
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:49
A rectangle specified with double values.
Definition: qgsrectangle.h:42
Contains information about the context of a rendering operation.
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:186
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81
#define SIP_END
Definition: qgis_sip.h:203
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsField::ConfigurationFlags) CORE_EXPORT QDataStream &operator<<(QDataStream &out
Writes the field to stream out. QGIS version compatibility is not guaranteed.