QGIS API Documentation 3.43.0-Master (3ee7834ace6)
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#ifdef SIP_RUN
74 if ( dynamic_cast<QgsMapToolZoom *>( sipCpp ) != NULL )
75 sipType = sipType_QgsMapToolZoom;
76 else if ( dynamic_cast<QgsMapToolPan *>( sipCpp ) != NULL )
77 sipType = sipType_QgsMapToolPan;
78 else if ( dynamic_cast<QgsMapToolEmitPoint *>( sipCpp ) != NULL )
79 sipType = sipType_QgsMapToolEmitPoint;
80 else if ( dynamic_cast<QgsMapToolExtent *>( sipCpp ) != NULL )
81 sipType = sipType_QgsMapToolExtent;
82 else if ( dynamic_cast<QgsMapToolIdentifyFeature *>( sipCpp ) != NULL )
83 sipType = sipType_QgsMapToolIdentifyFeature;
84 else if ( dynamic_cast<QgsMapToolIdentify *>( sipCpp ) != NULL )
85 sipType = sipType_QgsMapToolIdentify;
86 else if ( dynamic_cast<QgsMapToolDigitizeFeature *>( sipCpp ) != NULL )
87 sipType = sipType_QgsMapToolDigitizeFeature;
88 else if ( dynamic_cast<QgsMapToolCapture *>( sipCpp ) != NULL )
89 sipType = sipType_QgsMapToolCapture;
90 else if ( dynamic_cast<QgsMapToolAdvancedDigitizing *>( sipCpp ) != NULL )
91 sipType = sipType_QgsMapToolAdvancedDigitizing;
92 else if ( dynamic_cast<QgsMapToolEdit *>( sipCpp ) != NULL )
93 sipType = sipType_QgsMapToolEdit;
94 else if ( sipCpp->inherits( "QgsMapTool" ) ) // e.g. map tools from QGIS app library, which aren't exposed to SIP
95 sipType = sipType_QgsMapTool;
96 else
97 sipType = nullptr;
99#endif
100
101 Q_OBJECT
102
103 public:
107 enum Flag SIP_ENUM_BASETYPE( IntFlag )
108 {
109 Transient = 1 << 1,
110 EditTool = 1 << 2,
111 AllowZoomRect = 1 << 3,
112 ShowContextMenu = 1 << 4,
113 };
114 Q_DECLARE_FLAGS( Flags, Flag )
115
116
119 virtual Flags flags() const { return Flags(); }
120
121 ~QgsMapTool() override;
122
124 virtual void canvasMoveEvent( QgsMapMouseEvent *e );
125
127 virtual void canvasDoubleClickEvent( QgsMapMouseEvent *e );
128
130 virtual void canvasPressEvent( QgsMapMouseEvent *e );
131
133 virtual void canvasReleaseEvent( QgsMapMouseEvent *e );
134
136 virtual void wheelEvent( QWheelEvent *e );
137
139 virtual void keyPressEvent( QKeyEvent *e );
140
142 virtual void keyReleaseEvent( QKeyEvent *e );
143
145 virtual bool gestureEvent( QGestureEvent *e );
146
152 virtual bool canvasToolTipEvent( QHelpEvent *e );
153
160 void setAction( QAction *action );
161
163 QAction *action();
164
169 bool isActive() const;
170
175 void setButton( QAbstractButton *button );
176
178 QAbstractButton *button();
179
181 virtual void setCursor( const QCursor &cursor );
182
184 virtual void activate();
185
187 virtual void deactivate();
188
195 virtual void reactivate();
196
198 virtual void clean();
199
201 QgsMapCanvas *canvas() const;
202
207 QString toolName() { return mToolName; }
208
214 static double searchRadiusMM();
215
220 static double searchRadiusMU( const QgsRenderContext &context );
221
226 static double searchRadiusMU( QgsMapCanvas *canvas );
227
242 virtual void populateContextMenu( QMenu *menu );
243
262 virtual bool populateContextMenuWithEvent( QMenu *menu, QgsMapMouseEvent *event );
263
265 QgsPointXY toMapCoordinates( QPoint point );
266
267 signals:
268
274 void messageEmitted( const QString &message, Qgis::MessageLevel level = Qgis::MessageLevel::Info );
275
282
288 void activated();
289
296
303
304 private slots:
306 void actionDestroyed();
307
308 protected:
311
316 QgsPoint toLayerCoordinates( const QgsMapLayer *layer, const QgsPoint &point ) SIP_PYNAME( toLayerCoordinatesV2 );
317
319 QgsPointXY toLayerCoordinates( const QgsMapLayer *layer, QPoint point );
320
322 QgsPointXY toLayerCoordinates( const QgsMapLayer *layer, const QgsPointXY &point );
323
325 QgsPointXY toMapCoordinates( const QgsMapLayer *layer, const QgsPointXY &point );
326
331 QgsPoint toMapCoordinates( const QgsMapLayer *layer, const QgsPoint &point ) SIP_PYNAME( toMapCoordinatesV2 );
332
334 QgsRectangle toLayerCoordinates( const QgsMapLayer *layer, const QgsRectangle &rect );
335
337 QPoint toCanvasCoordinates( const QgsPointXY &point ) const;
338
348 QgsMapLayer *layer( const QString &id );
349
356 void setToolName( const QString &name );
357
359 QPointer<QgsMapCanvas> mCanvas;
360
362 QCursor mCursor;
363
368 QAction *mAction = nullptr;
369
374 QAbstractButton *mButton = nullptr;
375
377 QString mToolName;
378
379 friend class QgsMapCanvas;
380 friend class TestQgsMapToolEdit;
381};
382
384
385#endif
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition qgis.h:154
@ Info
Information message.
Definition qgis.h:155
Map canvas is a class for displaying all GIS data types on a canvas.
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 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.
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:207
void deactivated()
Emitted when the map tool is deactivated.
void messageDiscarded()
Emitted when the previous message from the tool should be cleared from the application message bar.
QFlags< Flag > Flags
Definition qgsmaptool.h:114
QPointer< QgsMapCanvas > mCanvas
The pointer to the map canvas.
Definition qgsmaptool.h:359
QString mToolName
The translated name of the map tool.
Definition qgsmaptool.h:377
void messageEmitted(const QString &message, Qgis::MessageLevel level=Qgis::MessageLevel::Info)
Emitted when a message should be shown to the user in the application message bar.
void activated()
Emitted when the map tool is activated.
void reactivated()
Emitted when the map tool is activated, while it is already active.
Flag
Enumeration of flags that adjust the way the map tool operates.
Definition qgsmaptool.h:108
QCursor mCursor
The cursor used in the map tool.
Definition qgsmaptool.h:362
A class to represent a 2D point.
Definition qgspointxy.h:60
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.
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition qgis_sip.h:191
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:53
#define SIP_ENUM_BASETYPE(type)
Definition qgis_sip.h:278
#define SIP_PYNAME(name)
Definition qgis_sip.h:81
#define SIP_END
Definition qgis_sip.h:208
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsTextRendererUtils::CurvedTextFlags)