QGIS API Documentation 4.1.0-Master (4aad578bf8d)
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"
24
25#include <QCursor>
26#include <QGestureEvent>
27#include <QObject>
28#include <QPointer>
29#include <QString>
30
31class QgsMapLayer;
32class QgsMapCanvas;
34class QKeyEvent;
35class QMouseEvent;
36class QWheelEvent;
37class QgsPoint;
38class QgsPointXY;
39class QgsRectangle;
40class QPoint;
41class QAction;
42class QAbstractButton;
44class QMenu;
46
47#ifdef SIP_RUN
48//%ModuleHeaderCode
49// fix to allow compilation with sip 4.7 that for some reason
50// doesn't add these includes to the file where the code from
51// ConvertToSubClassCode goes.
52#include <qgsmaptoolzoom.h>
53#include <qgsmaptoolpan.h>
54#include <qgsmaptoolemitpoint.h>
55#include <qgsmaptoolidentify.h>
57#include <qgsmaptoolextent.h>
59#include <qgsmaptoolcapture.h>
61#include <qgsmaptooledit.h>
62//%End
63#endif
64
73class GUI_EXPORT QgsMapTool : public QObject
74{
75#ifdef SIP_RUN
77 if ( dynamic_cast<QgsMapToolZoom *>( sipCpp ) != NULL )
78 sipType = sipType_QgsMapToolZoom;
79 else if ( dynamic_cast<QgsMapToolPan *>( sipCpp ) != NULL )
80 sipType = sipType_QgsMapToolPan;
81 else if ( dynamic_cast<QgsMapToolEmitPoint *>( sipCpp ) != NULL )
82 sipType = sipType_QgsMapToolEmitPoint;
83 else if ( dynamic_cast<QgsMapToolExtent *>( sipCpp ) != NULL )
84 sipType = sipType_QgsMapToolExtent;
85 else if ( dynamic_cast<QgsMapToolIdentifyFeature *>( sipCpp ) != NULL )
86 sipType = sipType_QgsMapToolIdentifyFeature;
87 else if ( dynamic_cast<QgsMapToolIdentify *>( sipCpp ) != NULL )
88 sipType = sipType_QgsMapToolIdentify;
89 else if ( dynamic_cast<QgsMapToolDigitizeFeature *>( sipCpp ) != NULL )
90 sipType = sipType_QgsMapToolDigitizeFeature;
91 else if ( dynamic_cast<QgsMapToolCapture *>( sipCpp ) != NULL )
92 sipType = sipType_QgsMapToolCapture;
93 else if ( dynamic_cast<QgsMapToolAdvancedDigitizing *>( sipCpp ) != NULL )
94 sipType = sipType_QgsMapToolAdvancedDigitizing;
95 else if ( dynamic_cast<QgsMapToolEdit *>( sipCpp ) != NULL )
96 sipType = sipType_QgsMapToolEdit;
97 else if ( sipCpp->inherits( "QgsMapTool" ) ) // e.g. map tools from QGIS app library, which aren't exposed to SIP
98 sipType = sipType_QgsMapTool;
99 else
100 sipType = nullptr;
101 SIP_END
102#endif
103
104 Q_OBJECT
105
106 public:
110 enum Flag SIP_ENUM_BASETYPE( IntFlag )
111 {
112 Transient = 1 << 1,
113 EditTool = 1 << 2,
114 AllowZoomRect = 1 << 3,
116 };
117 Q_DECLARE_FLAGS( Flags, Flag )
118
119
122 virtual Flags flags() const { return Flags(); }
123
124 ~QgsMapTool() override;
125
127 virtual void canvasMoveEvent( QgsMapMouseEvent *e );
128
130 virtual void canvasDoubleClickEvent( QgsMapMouseEvent *e );
131
133 virtual void canvasPressEvent( QgsMapMouseEvent *e );
134
136 virtual void canvasReleaseEvent( QgsMapMouseEvent *e );
137
139 virtual void wheelEvent( QWheelEvent *e );
140
142 virtual void keyPressEvent( QKeyEvent *e );
143
145 virtual void keyReleaseEvent( QKeyEvent *e );
146
148 virtual bool gestureEvent( QGestureEvent *e );
149
155 virtual bool canvasToolTipEvent( QHelpEvent *e );
156
164 virtual bool shortcutEvent( QKeyEvent *e );
165
172 void setAction( QAction *action );
173
175 QAction *action();
176
181 bool isActive() const;
182
187 void setButton( QAbstractButton *button );
188
190 QAbstractButton *button();
191
193 virtual void setCursor( const QCursor &cursor );
194
196 virtual void activate();
197
199 virtual void deactivate();
200
207 virtual void reactivate();
208
210 virtual void clean();
211
213 QgsMapCanvas *canvas() const;
214
219 QString toolName() { return mToolName; }
220
226 static double searchRadiusMM();
227
228#ifndef SIP_RUN
229
235
236#endif
237
242 static double searchRadiusMU( const QgsRenderContext &context );
243
248 static double searchRadiusMU( QgsMapCanvas *canvas );
249
264 virtual void populateContextMenu( QMenu *menu );
265
284 virtual bool populateContextMenuWithEvent( QMenu *menu, QgsMapMouseEvent *event );
285
287 QgsPointXY toMapCoordinates( QPoint point );
288
292 enum class PropertyStatus
293 {
294 Valid,
295 DoesNotExist,
296 CurrentExpressionInvalid
297 };
298
303 QString dataDefinedColumnName( int propertyKey, const QgsPropertyCollection &properties, const QgsVectorLayer *layer, PropertyStatus &status ) const;
304
308 int dataDefinedColumnIndex( int propertyKey, const QgsPropertyCollection &properties, const QgsVectorLayer *vlayer ) const;
309
310
311 signals:
312
318 void messageEmitted( const QString &message, Qgis::MessageLevel level = Qgis::MessageLevel::Info );
319
326
332 void activated();
333
340
347
348 private slots:
350 void actionDestroyed();
351
352 protected:
355
360 QgsPoint toLayerCoordinates( const QgsMapLayer *layer, const QgsPoint &point ) SIP_PYNAME( toLayerCoordinatesV2 );
361
363 QgsPointXY toLayerCoordinates( const QgsMapLayer *layer, QPoint point );
364
367
370
375 QgsPoint toMapCoordinates( const QgsMapLayer *layer, const QgsPoint &point ) SIP_PYNAME( toMapCoordinatesV2 );
376
379
381 QPoint toCanvasCoordinates( const QgsPointXY &point ) const;
382
392 QgsMapLayer *layer( const QString &id );
393
400 void setToolName( const QString &name );
401
403 QPointer<QgsMapCanvas> mCanvas;
404
406 QCursor mCursor;
407
412 QAction *mAction = nullptr;
413
418 QAbstractButton *mButton = nullptr;
419
421 QString mToolName;
422
423 friend class QgsMapCanvas;
424 friend class TestQgsMapToolEdit;
425};
426
428
429#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:74
QString toolName()
Emit map tool changed with the old tool.
Definition qgsmaptool.h:219
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:418
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:117
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:403
QString mToolName
The translated name of the map tool.
Definition qgsmaptool.h:421
friend class QgsMapCanvas
Definition qgsmaptool.h:423
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:424
PropertyStatus
Property status used in method dealing with property.
Definition qgsmaptool.h:293
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:412
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:122
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:234
Flag
Enumeration of flags that adjust the way the map tool operates.
Definition qgsmaptool.h:111
@ AllowZoomRect
Allow zooming by rectangle (by holding shift and dragging) while the tool is active.
Definition qgsmaptool.h:114
@ EditTool
Map tool is an edit tool, which can only be used when layer is editable.
Definition qgsmaptool.h:113
@ Transient
Deprecated since QGIS 3.36 – no longer used by QGIS and will be removed in QGIS 5....
Definition qgsmaptool.h:112
@ ShowContextMenu
Show a context menu when right-clicking with the tool (since QGIS 3.14). See populateContextMenu().
Definition qgsmaptool.h:115
QCursor mCursor
The cursor used in the map tool.
Definition qgsmaptool.h:406
Represents a 2D point.
Definition qgspointxy.h:62
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:53
A grouped map of multiple QgsProperty objects, each referenced by an integer key value.
A rectangle specified with double values.
Contains information about the context of a rendering operation.
A double settings entry.
Represents a vector layer which manages a vector based dataset.
#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)