QGIS API Documentation 4.1.0-Master (3b8ef1f72a3)
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;
45
46#ifdef SIP_RUN
47//%ModuleHeaderCode
48// fix to allow compilation with sip 4.7 that for some reason
49// doesn't add these includes to the file where the code from
50// ConvertToSubClassCode goes.
51#include <qgsmaptoolzoom.h>
52#include <qgsmaptoolpan.h>
53#include <qgsmaptoolemitpoint.h>
54#include <qgsmaptoolidentify.h>
56#include <qgsmaptoolextent.h>
58#include <qgsmaptoolcapture.h>
60#include <qgsmaptooledit.h>
61//%End
62#endif
63
72class GUI_EXPORT QgsMapTool : public QObject
73{
74#ifdef SIP_RUN
76 if ( dynamic_cast<QgsMapToolZoom *>( sipCpp ) != NULL )
77 sipType = sipType_QgsMapToolZoom;
78 else if ( dynamic_cast<QgsMapToolPan *>( sipCpp ) != NULL )
79 sipType = sipType_QgsMapToolPan;
80 else if ( dynamic_cast<QgsMapToolEmitPoint *>( sipCpp ) != NULL )
81 sipType = sipType_QgsMapToolEmitPoint;
82 else if ( dynamic_cast<QgsMapToolExtent *>( sipCpp ) != NULL )
83 sipType = sipType_QgsMapToolExtent;
84 else if ( dynamic_cast<QgsMapToolIdentifyFeature *>( sipCpp ) != NULL )
85 sipType = sipType_QgsMapToolIdentifyFeature;
86 else if ( dynamic_cast<QgsMapToolIdentify *>( sipCpp ) != NULL )
87 sipType = sipType_QgsMapToolIdentify;
88 else if ( dynamic_cast<QgsMapToolDigitizeFeature *>( sipCpp ) != NULL )
89 sipType = sipType_QgsMapToolDigitizeFeature;
90 else if ( dynamic_cast<QgsMapToolCapture *>( sipCpp ) != NULL )
91 sipType = sipType_QgsMapToolCapture;
92 else if ( dynamic_cast<QgsMapToolAdvancedDigitizing *>( sipCpp ) != NULL )
93 sipType = sipType_QgsMapToolAdvancedDigitizing;
94 else if ( dynamic_cast<QgsMapToolEdit *>( sipCpp ) != NULL )
95 sipType = sipType_QgsMapToolEdit;
96 else if ( sipCpp->inherits( "QgsMapTool" ) ) // e.g. map tools from QGIS app library, which aren't exposed to SIP
97 sipType = sipType_QgsMapTool;
98 else
99 sipType = nullptr;
100 SIP_END
101#endif
102
103 Q_OBJECT
104
105 public:
109 enum Flag SIP_ENUM_BASETYPE( IntFlag )
110 {
111 Transient = 1 << 1,
112 EditTool = 1 << 2,
113 AllowZoomRect = 1 << 3,
115 };
116 Q_DECLARE_FLAGS( Flags, Flag )
117
118
121 virtual Flags flags() const { return Flags(); }
122
123 ~QgsMapTool() override;
124
126 virtual void canvasMoveEvent( QgsMapMouseEvent *e );
127
129 virtual void canvasDoubleClickEvent( QgsMapMouseEvent *e );
130
132 virtual void canvasPressEvent( QgsMapMouseEvent *e );
133
135 virtual void canvasReleaseEvent( QgsMapMouseEvent *e );
136
138 virtual void wheelEvent( QWheelEvent *e );
139
141 virtual void keyPressEvent( QKeyEvent *e );
142
144 virtual void keyReleaseEvent( QKeyEvent *e );
145
147 virtual bool gestureEvent( QGestureEvent *e );
148
154 virtual bool canvasToolTipEvent( QHelpEvent *e );
155
163 virtual bool shortcutEvent( QKeyEvent *e );
164
171 void setAction( QAction *action );
172
174 QAction *action();
175
180 bool isActive() const;
181
186 void setButton( QAbstractButton *button );
187
189 QAbstractButton *button();
190
192 virtual void setCursor( const QCursor &cursor );
193
195 virtual void activate();
196
198 virtual void deactivate();
199
206 virtual void reactivate();
207
209 virtual void clean();
210
212 QgsMapCanvas *canvas() const;
213
218 QString toolName() { return mToolName; }
219
225 static double searchRadiusMM();
226
227#ifndef SIP_RUN
228
234
235#endif
236
241 static double searchRadiusMU( const QgsRenderContext &context );
242
247 static double searchRadiusMU( QgsMapCanvas *canvas );
248
263 virtual void populateContextMenu( QMenu *menu );
264
283 virtual bool populateContextMenuWithEvent( QMenu *menu, QgsMapMouseEvent *event );
284
286 QgsPointXY toMapCoordinates( QPoint point );
287
288 signals:
289
295 void messageEmitted( const QString &message, Qgis::MessageLevel level = Qgis::MessageLevel::Info );
296
303
309 void activated();
310
317
324
325 private slots:
327 void actionDestroyed();
328
329 protected:
332
337 QgsPoint toLayerCoordinates( const QgsMapLayer *layer, const QgsPoint &point ) SIP_PYNAME( toLayerCoordinatesV2 );
338
340 QgsPointXY toLayerCoordinates( const QgsMapLayer *layer, QPoint point );
341
344
347
352 QgsPoint toMapCoordinates( const QgsMapLayer *layer, const QgsPoint &point ) SIP_PYNAME( toMapCoordinatesV2 );
353
356
358 QPoint toCanvasCoordinates( const QgsPointXY &point ) const;
359
369 QgsMapLayer *layer( const QString &id );
370
377 void setToolName( const QString &name );
378
380 QPointer<QgsMapCanvas> mCanvas;
381
383 QCursor mCursor;
384
389 QAction *mAction = nullptr;
390
395 QAbstractButton *mButton = nullptr;
396
398 QString mToolName;
399
400 friend class QgsMapCanvas;
401 friend class TestQgsMapToolEdit;
402};
403
405
406#endif
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition qgis.h:160
@ Info
Information message.
Definition qgis.h:161
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:73
QString toolName()
Emit map tool changed with the old tool.
Definition qgsmaptool.h:218
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: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-...
void setToolName(const QString &name)
Sets the tool's name.
QFlags< Flag > Flags
Definition qgsmaptool.h:116
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:380
QString mToolName
The translated name of the map tool.
Definition qgsmaptool.h:398
friend class QgsMapCanvas
Definition qgsmaptool.h:400
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-...
friend class TestQgsMapToolEdit
Definition qgsmaptool.h:401
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:389
static double searchRadiusMU(const QgsRenderContext &context)
Gets search radius in map units for given context.
virtual Flags flags() const
Returns the flags for the map tool.
Definition qgsmaptool.h:121
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
Flag
Enumeration of flags that adjust the way the map tool operates.
Definition qgsmaptool.h:110
@ AllowZoomRect
Allow zooming by rectangle (by holding shift and dragging) while the tool is active.
Definition qgsmaptool.h:113
@ EditTool
Map tool is an edit tool, which can only be used when layer is editable.
Definition qgsmaptool.h:112
@ Transient
Deprecated since QGIS 3.36 – no longer used by QGIS and will be removed in QGIS 5....
Definition qgsmaptool.h:111
@ ShowContextMenu
Show a context menu when right-clicking with the tool (since QGIS 3.14). See populateContextMenu().
Definition qgsmaptool.h:114
QCursor mCursor
The cursor used in the map tool.
Definition qgsmaptool.h:383
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.
A double settings entry.
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition qgis_sip.h:198
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:52
#define SIP_ENUM_BASETYPE(type)
Definition qgis_sip.h:274
#define SIP_PYNAME(name)
Definition qgis_sip.h:88
#define SIP_END
Definition qgis_sip.h:215
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsProjectionSelectionWidget::CrsOptions)