QGIS API Documentation 3.99.0-Master (1d785854362)
Loading...
Searching...
No Matches
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
21#include "qgis.h"
22#include "qgis_gui.h"
23
24#include <QCursor>
25#include <QGestureEvent>
26#include <QObject>
27#include <QPointer>
28#include <QString>
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
71class GUI_EXPORT QgsMapTool : public QObject
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:
108 enum Flag SIP_ENUM_BASETYPE( IntFlag )
109 {
110 Transient = 1 << 1,
111 EditTool = 1 << 2,
112 AllowZoomRect = 1 << 3,
114 };
115 Q_DECLARE_FLAGS( Flags, Flag )
116
117
120 virtual Flags flags() const { return Flags(); }
121
122 ~QgsMapTool() override;
123
125 virtual void canvasMoveEvent( QgsMapMouseEvent *e );
126
128 virtual void canvasDoubleClickEvent( QgsMapMouseEvent *e );
129
131 virtual void canvasPressEvent( QgsMapMouseEvent *e );
132
134 virtual void canvasReleaseEvent( QgsMapMouseEvent *e );
135
137 virtual void wheelEvent( QWheelEvent *e );
138
140 virtual void keyPressEvent( QKeyEvent *e );
141
143 virtual void keyReleaseEvent( QKeyEvent *e );
144
146 virtual bool gestureEvent( QGestureEvent *e );
147
153 virtual bool canvasToolTipEvent( QHelpEvent *e );
154
162 virtual bool shortcutEvent( QKeyEvent *e );
163
170 void setAction( QAction *action );
171
173 QAction *action();
174
179 bool isActive() const;
180
185 void setButton( QAbstractButton *button );
186
188 QAbstractButton *button();
189
191 virtual void setCursor( const QCursor &cursor );
192
194 virtual void activate();
195
197 virtual void deactivate();
198
205 virtual void reactivate();
206
208 virtual void clean();
209
211 QgsMapCanvas *canvas() const;
212
217 QString toolName() { return mToolName; }
218
224 static double searchRadiusMM();
225
230 static double searchRadiusMU( const QgsRenderContext &context );
231
236 static double searchRadiusMU( QgsMapCanvas *canvas );
237
252 virtual void populateContextMenu( QMenu *menu );
253
272 virtual bool populateContextMenuWithEvent( QMenu *menu, QgsMapMouseEvent *event );
273
275 QgsPointXY toMapCoordinates( QPoint point );
276
277 signals:
278
284 void messageEmitted( const QString &message, Qgis::MessageLevel level = Qgis::MessageLevel::Info );
285
292
298 void activated();
299
306
313
314 private slots:
316 void actionDestroyed();
317
318 protected:
321
326 QgsPoint toLayerCoordinates( const QgsMapLayer *layer, const QgsPoint &point ) SIP_PYNAME( toLayerCoordinatesV2 );
327
329 QgsPointXY toLayerCoordinates( const QgsMapLayer *layer, QPoint point );
330
333
336
341 QgsPoint toMapCoordinates( const QgsMapLayer *layer, const QgsPoint &point ) SIP_PYNAME( toMapCoordinatesV2 );
342
345
347 QPoint toCanvasCoordinates( const QgsPointXY &point ) const;
348
358 QgsMapLayer *layer( const QString &id );
359
366 void setToolName( const QString &name );
367
369 QPointer<QgsMapCanvas> mCanvas;
370
372 QCursor mCursor;
373
378 QAction *mAction = nullptr;
379
384 QAbstractButton *mButton = nullptr;
385
387 QString mToolName;
388
389 friend class QgsMapCanvas;
390 friend class TestQgsMapToolEdit;
391};
392
394
395#endif
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition qgis.h:159
@ Info
Information message.
Definition qgis.h:160
Map canvas is a class for displaying all GIS data types on a canvas.
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.
A QgsMapTool which gives events directly in map coordinates and allows filtering of events.
Base class for map tools capable of capturing point, lines and polygons.
This tool digitizes geometry of new point/line/polygon features on already existing vector layers.
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.
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:72
QString toolName()
Emit map tool changed with the old tool.
Definition qgsmaptool.h:217
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.
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.
void messageDiscarded()
Emitted when the previous message from the tool should be cleared from the application message bar.
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
void setToolName(const QString &name)
Sets the tool's name.
QFlags< Flag > Flags
Definition qgsmaptool.h:115
QgsPointXY toMapCoordinates(QPoint point)
Transforms a point from screen coordinates to map coordinates.
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
friend class TestQgsMapToolEdit
Definition qgsmaptool.h:390
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.
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 Flags flags() const
Returns the flags for the map tool.
Definition qgsmaptool.h:120
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.
Flag
Enumeration of flags that adjust the way the map tool operates.
Definition qgsmaptool.h:109
@ AllowZoomRect
Allow zooming by rectangle (by holding shift and dragging) while the tool is active.
Definition qgsmaptool.h:112
@ EditTool
Map tool is an edit tool, which can only be used when layer is editable.
Definition qgsmaptool.h:111
@ Transient
Deprecated since QGIS 3.36 – no longer used by QGIS and will be removed in QGIS 5....
Definition qgsmaptool.h:110
@ ShowContextMenu
Show a context menu when right-clicking with the tool (since QGIS 3.14). See populateContextMenu().
Definition qgsmaptool.h:113
QCursor mCursor
The cursor used in the map tool.
Definition qgsmaptool.h:372
Represents a 2D point.
Definition qgspointxy.h:62
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.
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition qgis_sip.h:199
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:53
#define SIP_ENUM_BASETYPE(type)
Definition qgis_sip.h:275
#define SIP_PYNAME(name)
Definition qgis_sip.h:89
#define SIP_END
Definition qgis_sip.h:216
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsProjectionSelectionWidget::CrsOptions)