QGIS API Documentation  3.37.0-Master (a5b4d9743e8)
Public Types | Signals | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Friends | List of all members
QgsMapTool Class Reference

Abstract base class for all map tools. More...

#include <qgsmaptool.h>

Inheritance diagram for QgsMapTool:
Inheritance graph
[legend]

Public Types

enum  Flag { Transient = 1 << 1 , EditTool = 1 << 2 , AllowZoomRect = 1 << 3 , ShowContextMenu = 1 << 4 }
 Enumeration of flags that adjust the way the map tool operates. More...
 
typedef QFlags< FlagFlags
 

Signals

void activated ()
 signal emitted once the map tool is activated More...
 
void deactivated ()
 signal emitted once the map tool is deactivated More...
 
void messageDiscarded ()
 emit signal to clear previous message More...
 
void messageEmitted (const QString &message, Qgis::MessageLevel=Qgis::MessageLevel::Info)
 emit a message More...
 
void reactivated ()
 

Public Member Functions

 ~QgsMapTool () override
 
QAction * action ()
 Returns associated action with map tool or nullptr if no action is associated. More...
 
virtual void activate ()
 called when set as currently active map tool More...
 
QAbstractButton * button ()
 Returns associated button with map tool or nullptr if no button is associated. More...
 
QgsMapCanvascanvas () const
 returns pointer to the tool's map canvas More...
 
virtual void canvasDoubleClickEvent (QgsMapMouseEvent *e)
 Mouse double-click event for overriding. Default implementation does nothing. More...
 
virtual void canvasMoveEvent (QgsMapMouseEvent *e)
 Mouse move event for overriding. Default implementation does nothing. More...
 
virtual void canvasPressEvent (QgsMapMouseEvent *e)
 Mouse press event for overriding. Default implementation does nothing. More...
 
virtual void canvasReleaseEvent (QgsMapMouseEvent *e)
 Mouse release event for overriding. Default implementation does nothing. More...
 
virtual bool canvasToolTipEvent (QHelpEvent *e)
 Tooltip event for overriding. More...
 
virtual void clean ()
 convenient method to clean members More...
 
virtual void deactivate ()
 called when map tool is being deactivated More...
 
virtual Flags flags () const
 Returns the flags for the map tool. More...
 
virtual bool gestureEvent (QGestureEvent *e)
 gesture event for overriding. Default implementation does nothing. More...
 
bool isActive () const
 Returns if the current map tool active on the map canvas. More...
 
virtual void keyPressEvent (QKeyEvent *e)
 Key event for overriding. Default implementation does nothing. More...
 
virtual void keyReleaseEvent (QKeyEvent *e)
 Key event for overriding. Default implementation does nothing. More...
 
virtual void populateContextMenu (QMenu *menu)
 Allows the tool to populate and customize the given menu, prior to showing it in response to a right-mouse button click. More...
 
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-mouse button click. More...
 
virtual void reactivate ()
 Called when the map tool is being activated while it is already active. More...
 
void setAction (QAction *action)
 Use this to associate a QAction to this maptool. More...
 
void setButton (QAbstractButton *button)
 Use this to associate a button to this maptool. More...
 
virtual void setCursor (const QCursor &cursor)
 Sets a user defined cursor. More...
 
QgsPointXY toMapCoordinates (QPoint point)
 Transforms a point from screen coordinates to map coordinates. More...
 
QString toolName ()
 Emit map tool changed with the old tool. More...
 
virtual void wheelEvent (QWheelEvent *e)
 Mouse wheel event for overriding. Default implementation does nothing. More...
 

Static Public Member Functions

static double searchRadiusMM ()
 Gets search radius in mm. More...
 
static double searchRadiusMU (const QgsRenderContext &context)
 Gets search radius in map units for given context. More...
 
static double searchRadiusMU (QgsMapCanvas *canvas)
 Gets search radius in map units for given canvas. More...
 

Protected Member Functions

 QgsMapTool (QgsMapCanvas *canvas)
 Constructor takes a map canvas as a parameter. More...
 
QgsMapLayerlayer (const QString &id)
 Returns the map layer with the matching ID, or nullptr if no layers could be found. More...
 
void setToolName (const QString &name)
 Sets the tool's name. More...
 
QPoint toCanvasCoordinates (const QgsPointXY &point) const
 Transforms a point from map coordinates to screen coordinates. More...
 
QgsPoint toLayerCoordinates (const QgsMapLayer *layer, const QgsPoint &point)
 Transforms a point from map coordinates to layer coordinates. More...
 
QgsPointXY toLayerCoordinates (const QgsMapLayer *layer, const QgsPointXY &point)
 Transforms a point from map coordinates to layer coordinates. More...
 
QgsRectangle toLayerCoordinates (const QgsMapLayer *layer, const QgsRectangle &rect)
 Transforms a rect from map coordinates to layer coordinates. More...
 
QgsPointXY toLayerCoordinates (const QgsMapLayer *layer, QPoint point)
 Transforms a point from screen coordinates to layer coordinates. More...
 
QgsPoint toMapCoordinates (const QgsMapLayer *layer, const QgsPoint &point)
 Transforms a point from layer coordinates to map coordinates (which is different in case reprojection is used). More...
 
QgsPointXY toMapCoordinates (const QgsMapLayer *layer, const QgsPointXY &point)
 Transforms a point from layer coordinates to map coordinates (which is different in case reprojection is used). More...
 

Protected Attributes

QAction * mAction = nullptr
 Optional pointer to an action that will be checked on map tool activation and unchecked on map tool deactivation. More...
 
QAbstractButton * mButton = nullptr
 Optional pointer to a button that will be checked on map tool activation and unchecked on map tool deactivation. More...
 
QPointer< QgsMapCanvasmCanvas
 The pointer to the map canvas. More...
 
QCursor mCursor
 The cursor used in the map tool. More...
 
QString mToolName
 The translated name of the map tool. More...
 

Friends

class QgsMapCanvas
 
class TestQgsMapToolEdit
 

Detailed Description

Abstract base class for all map tools.

Map tools are user interactive tools for manipulating the map canvas. For example map pan and zoom features are implemented as map tools.

Definition at line 70 of file qgsmaptool.h.

Member Typedef Documentation

◆ Flags

typedef QFlags< Flag > QgsMapTool::Flags

Definition at line 116 of file qgsmaptool.h.

Member Enumeration Documentation

◆ Flag

Enumeration of flags that adjust the way the map tool operates.

Enumerator
Transient 

Deprecated since QGIS 3.36 – no longer used by QGIS and will be removed in QGIS 4.0.

EditTool 

Map tool is an edit tool, which can only be used when layer is editable.

AllowZoomRect 

Allow zooming by rectangle (by holding shift and dragging) while the tool is active.

ShowContextMenu 

Show a context menu when right-clicking with the tool (since QGIS 3.14). See populateContextMenu().

Definition at line 109 of file qgsmaptool.h.

Constructor & Destructor Documentation

◆ ~QgsMapTool()

QgsMapTool::~QgsMapTool ( )
override

Definition at line 35 of file qgsmaptool.cpp.

◆ QgsMapTool()

QgsMapTool::QgsMapTool ( QgsMapCanvas canvas)
protected

Constructor takes a map canvas as a parameter.

Definition at line 27 of file qgsmaptool.cpp.

Member Function Documentation

◆ action()

QAction * QgsMapTool::action ( )

Returns associated action with map tool or nullptr if no action is associated.

Definition at line 146 of file qgsmaptool.cpp.

◆ activate()

void QgsMapTool::activate ( )
virtual

called when set as currently active map tool

Reimplemented in QgsMapToolPan, QgsMapToolIdentify, QgsMapToolExtent, QgsMapToolDigitizeFeature, QgsMapToolCapture, and QgsMapToolAdvancedDigitizing.

Definition at line 94 of file qgsmaptool.cpp.

◆ activated

void QgsMapTool::activated ( )
signal

signal emitted once the map tool is activated

◆ button()

QAbstractButton * QgsMapTool::button ( )

Returns associated button with map tool or nullptr if no button is associated.

Definition at line 161 of file qgsmaptool.cpp.

◆ canvas()

QgsMapCanvas * QgsMapTool::canvas ( ) const

returns pointer to the tool's map canvas

Definition at line 221 of file qgsmaptool.cpp.

◆ canvasDoubleClickEvent()

void QgsMapTool::canvasDoubleClickEvent ( QgsMapMouseEvent e)
virtual

Mouse double-click event for overriding. Default implementation does nothing.

Reimplemented in QgsMapToolModifyAnnotation, and QgsMapToolPan.

Definition at line 179 of file qgsmaptool.cpp.

◆ canvasMoveEvent()

void QgsMapTool::canvasMoveEvent ( QgsMapMouseEvent e)
virtual

Mouse move event for overriding. Default implementation does nothing.

Reimplemented in QgsMapToolZoom, QgsMapToolPan, QgsMapToolIdentify, QgsMapToolExtent, QgsMapToolEmitPoint, and QgsMapToolAdvancedDigitizing.

Definition at line 174 of file qgsmaptool.cpp.

◆ canvasPressEvent()

void QgsMapTool::canvasPressEvent ( QgsMapMouseEvent e)
virtual

Mouse press event for overriding. Default implementation does nothing.

Reimplemented in QgsMapToolZoom, QgsMapToolPan, QgsMapToolIdentify, QgsMapToolExtent, QgsMapToolEmitPoint, and QgsMapToolAdvancedDigitizing.

Definition at line 184 of file qgsmaptool.cpp.

◆ canvasReleaseEvent()

void QgsMapTool::canvasReleaseEvent ( QgsMapMouseEvent e)
virtual

Mouse release event for overriding. Default implementation does nothing.

Reimplemented in QgsMapToolZoom, QgsMapToolPan, QgsMapToolIdentifyFeature, QgsMapToolIdentify, QgsMapToolExtent, QgsMapToolEmitPoint, and QgsMapToolAdvancedDigitizing.

Definition at line 189 of file qgsmaptool.cpp.

◆ canvasToolTipEvent()

bool QgsMapTool::canvasToolTipEvent ( QHelpEvent *  e)
virtual

Tooltip event for overriding.

Default implementation does nothing. Returns whether the event was handled by the tool and should not be propagated further.

Since
QGIS 3.22

Definition at line 215 of file qgsmaptool.cpp.

◆ clean()

void QgsMapTool::clean ( )
virtual

convenient method to clean members

Reimplemented in QgsMapToolCapture.

Definition at line 126 of file qgsmaptool.cpp.

◆ deactivate()

void QgsMapTool::deactivate ( )
virtual

◆ deactivated

void QgsMapTool::deactivated ( )
signal

signal emitted once the map tool is deactivated

◆ flags()

virtual Flags QgsMapTool::flags ( ) const
inlinevirtual

Returns the flags for the map tool.

Reimplemented in QgsMapToolZoom, QgsMapToolPan, QgsMapToolIdentify, QgsMapToolExtent, QgsMapToolEmitPoint, and QgsMapToolEdit.

Definition at line 121 of file qgsmaptool.h.

◆ gestureEvent()

bool QgsMapTool::gestureEvent ( QGestureEvent *  e)
virtual

gesture event for overriding. Default implementation does nothing.

Reimplemented in QgsMapToolPan.

Definition at line 209 of file qgsmaptool.cpp.

◆ isActive()

bool QgsMapTool::isActive ( ) const

Returns if the current map tool active on the map canvas.

Since
QGIS 3.4

Definition at line 151 of file qgsmaptool.cpp.

◆ keyPressEvent()

void QgsMapTool::keyPressEvent ( QKeyEvent *  e)
virtual

Key event for overriding. Default implementation does nothing.

Reimplemented in QgsMapToolModifyAnnotation, QgsMapToolZoom, QgsMapToolIdentifyFeature, QgsMapToolDigitizeFeature, and QgsMapToolCapture.

Definition at line 199 of file qgsmaptool.cpp.

◆ keyReleaseEvent()

void QgsMapTool::keyReleaseEvent ( QKeyEvent *  e)
virtual

Key event for overriding. Default implementation does nothing.

Reimplemented in QgsMapToolZoom.

Definition at line 204 of file qgsmaptool.cpp.

◆ layer()

QgsMapLayer * QgsMapTool::layer ( const QString &  id)
protected

Returns the map layer with the matching ID, or nullptr if no layers could be found.

This method searches both layers associated with the map canvas (see QgsMapCanvas::layers()) and layers from the QgsProject associated with the canvas. It can be used to resolve layer IDs to layers which may be visible in the canvas, but not associated with a QgsProject.

Since
QGIS 3.22

Definition at line 84 of file qgsmaptool.cpp.

◆ messageDiscarded

void QgsMapTool::messageDiscarded ( )
signal

emit signal to clear previous message

◆ messageEmitted

void QgsMapTool::messageEmitted ( const QString &  message,
Qgis::MessageLevel  = Qgis::MessageLevel::Info 
)
signal

emit a message

◆ populateContextMenu()

void QgsMapTool::populateContextMenu ( QMenu *  menu)
virtual

Allows the tool to populate and customize the given menu, prior to showing it in response to a right-mouse button click.

menu will be initially populated with a set of default, generic actions. Any new actions added to the menu should be correctly parented to menu.

The default implementation does nothing.

Note
The context menu is only shown when the ShowContextMenu flag is present in flags().
Since
QGIS 3.14

Definition at line 255 of file qgsmaptool.cpp.

◆ populateContextMenuWithEvent()

bool QgsMapTool::populateContextMenuWithEvent ( QMenu *  menu,
QgsMapMouseEvent event 
)
virtual

Allows the tool to populate and customize the given menu, prior to showing it in response to a right-mouse button click.

menu will be initially populated with a set of default, generic actions. Any new actions added to the menu should be correctly parented to menu.

A pointer to the map mouse event can be provided to allow particular behavior depending on the map tool.

This method can return true to inform the caller that the menu was effectively populated.

The default implementation does nothing and returns false.

Note
The context menu is only shown when the ShowContextMenu flag is present in flags().
Since
QGIS 3.18

Definition at line 261 of file qgsmaptool.cpp.

◆ reactivate()

void QgsMapTool::reactivate ( )
virtual

Called when the map tool is being activated while it is already active.

The default implementation emits the reactivated () signal.

Since
QGIS 3.32

Definition at line 121 of file qgsmaptool.cpp.

◆ reactivated

void QgsMapTool::reactivated ( )
signal
Since
QGIS 3.32 signal emitted when the map tool is activated while it is already active

◆ searchRadiusMM()

double QgsMapTool::searchRadiusMM ( )
static

Gets search radius in mm.

Used by identify, tip etc. The values is currently set in identify tool options (move somewhere else?) and defaults to Qgis::DEFAULT_SEARCH_RADIUS_MM.

Definition at line 226 of file qgsmaptool.cpp.

◆ searchRadiusMU() [1/2]

double QgsMapTool::searchRadiusMU ( const QgsRenderContext context)
static

Gets search radius in map units for given context.

Used by identify, tip etc. The values is calculated from searchRadiusMM().

Definition at line 238 of file qgsmaptool.cpp.

◆ searchRadiusMU() [2/2]

double QgsMapTool::searchRadiusMU ( QgsMapCanvas canvas)
static

Gets search radius in map units for given canvas.

Used by identify, tip etc. The values is calculated from searchRadiusMM().

Definition at line 243 of file qgsmaptool.cpp.

◆ setAction()

void QgsMapTool::setAction ( QAction *  action)

Use this to associate a QAction to this maptool.

Then when the setMapTool method of mapcanvas is called the action state will be set to on. Usually this will cause e.g. a toolbutton to appear pressed in and the previously used toolbutton to pop out.

Definition at line 131 of file qgsmaptool.cpp.

◆ setButton()

void QgsMapTool::setButton ( QAbstractButton *  button)

Use this to associate a button to this maptool.

It has the same meaning as setAction() function except it works with a button instead of an QAction.

Definition at line 156 of file qgsmaptool.cpp.

◆ setCursor()

void QgsMapTool::setCursor ( const QCursor &  cursor)
virtual

Sets a user defined cursor.

Definition at line 166 of file qgsmaptool.cpp.

◆ setToolName()

void QgsMapTool::setToolName ( const QString &  name)
protected

Sets the tool's name.

See also
toolName()
Since
QGIS 3.20

Definition at line 89 of file qgsmaptool.cpp.

◆ toCanvasCoordinates()

QPoint QgsMapTool::toCanvasCoordinates ( const QgsPointXY point) const
protected

Transforms a point from map coordinates to screen coordinates.

Definition at line 77 of file qgsmaptool.cpp.

◆ toLayerCoordinates() [1/4]

QgsPoint QgsMapTool::toLayerCoordinates ( const QgsMapLayer layer,
const QgsPoint point 
)
protected

Transforms a point from map coordinates to layer coordinates.

Note
This method is available in the Python bindings as toLayerCoordinatesV2.

Definition at line 62 of file qgsmaptool.cpp.

◆ toLayerCoordinates() [2/4]

QgsPointXY QgsMapTool::toLayerCoordinates ( const QgsMapLayer layer,
const QgsPointXY point 
)
protected

Transforms a point from map coordinates to layer coordinates.

Definition at line 57 of file qgsmaptool.cpp.

◆ toLayerCoordinates() [3/4]

QgsRectangle QgsMapTool::toLayerCoordinates ( const QgsMapLayer layer,
const QgsRectangle rect 
)
protected

Transforms a rect from map coordinates to layer coordinates.

Definition at line 72 of file qgsmaptool.cpp.

◆ toLayerCoordinates() [4/4]

QgsPointXY QgsMapTool::toLayerCoordinates ( const QgsMapLayer layer,
QPoint  point 
)
protected

Transforms a point from screen coordinates to layer coordinates.

Definition at line 51 of file qgsmaptool.cpp.

◆ toMapCoordinates() [1/3]

QgsPoint QgsMapTool::toMapCoordinates ( const QgsMapLayer layer,
const QgsPoint point 
)
protected

Transforms a point from layer coordinates to map coordinates (which is different in case reprojection is used).

Note
This method is available in the Python bindings as toMapCoordinatesV2.

Definition at line 46 of file qgsmaptool.cpp.

◆ toMapCoordinates() [2/3]

QgsPointXY QgsMapTool::toMapCoordinates ( const QgsMapLayer layer,
const QgsPointXY point 
)
protected

Transforms a point from layer coordinates to map coordinates (which is different in case reprojection is used).

Definition at line 67 of file qgsmaptool.cpp.

◆ toMapCoordinates() [3/3]

QgsPointXY QgsMapTool::toMapCoordinates ( QPoint  point)

Transforms a point from screen coordinates to map coordinates.

Definition at line 41 of file qgsmaptool.cpp.

◆ toolName()

QString QgsMapTool::toolName ( )
inline

Emit map tool changed with the old tool.

See also
setToolName()

Definition at line 209 of file qgsmaptool.h.

◆ wheelEvent()

void QgsMapTool::wheelEvent ( QWheelEvent *  e)
virtual

Mouse wheel event for overriding. Default implementation does nothing.

Definition at line 194 of file qgsmaptool.cpp.

Friends And Related Function Documentation

◆ QgsMapCanvas

friend class QgsMapCanvas
friend

Definition at line 361 of file qgsmaptool.h.

◆ TestQgsMapToolEdit

friend class TestQgsMapToolEdit
friend

Definition at line 362 of file qgsmaptool.h.

Member Data Documentation

◆ mAction

QAction* QgsMapTool::mAction = nullptr
protected

Optional pointer to an action that will be checked on map tool activation and unchecked on map tool deactivation.

Definition at line 350 of file qgsmaptool.h.

◆ mButton

QAbstractButton* QgsMapTool::mButton = nullptr
protected

Optional pointer to a button that will be checked on map tool activation and unchecked on map tool deactivation.

Definition at line 356 of file qgsmaptool.h.

◆ mCanvas

QPointer< QgsMapCanvas > QgsMapTool::mCanvas
protected

The pointer to the map canvas.

Definition at line 341 of file qgsmaptool.h.

◆ mCursor

QCursor QgsMapTool::mCursor
protected

The cursor used in the map tool.

Definition at line 344 of file qgsmaptool.h.

◆ mToolName

QString QgsMapTool::mToolName
protected

The translated name of the map tool.

Definition at line 359 of file qgsmaptool.h.


The documentation for this class was generated from the following files: