QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
30 class QgsMapLayer;
31 class QgsMapCanvas;
32 class QgsRenderContext;
33 class QKeyEvent;
34 class QMouseEvent;
35 class QWheelEvent;
36 class QgsPoint;
37 class QgsPointXY;
38 class QgsRectangle;
39 class QPoint;
40 class QAction;
41 class QAbstractButton;
42 class QgsMapMouseEvent;
43 class 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 
70 class GUI_EXPORT QgsMapTool : public QObject
71 {
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
96  sipType = NULL;
97  SIP_END
98 #endif
99 
100  Q_OBJECT
101 
102  public:
103 
108  enum Flag
109  {
110  Transient = 1 << 1,
113  EditTool = 1 << 2,
114  AllowZoomRect = 1 << 3,
115  ShowContextMenu = 1 << 4,
116  };
117  Q_DECLARE_FLAGS( Flags, Flag )
118 
119 
123  virtual Flags flags() const { return Flags(); }
124 
125  ~QgsMapTool() override;
126 
128  virtual void canvasMoveEvent( QgsMapMouseEvent *e );
129 
131  virtual void canvasDoubleClickEvent( QgsMapMouseEvent *e );
132 
134  virtual void canvasPressEvent( QgsMapMouseEvent *e );
135 
137  virtual void canvasReleaseEvent( QgsMapMouseEvent *e );
138 
140  virtual void wheelEvent( QWheelEvent *e );
141 
143  virtual void keyPressEvent( QKeyEvent *e );
144 
146  virtual void keyReleaseEvent( QKeyEvent *e );
147 
149  virtual bool gestureEvent( QGestureEvent *e );
150 
156  virtual bool canvasToolTipEvent( QHelpEvent *e );
157 
164  void setAction( QAction *action );
165 
167  QAction *action();
168 
173  bool isActive() const;
174 
179  void setButton( QAbstractButton *button );
180 
182  QAbstractButton *button();
183 
185  virtual void setCursor( const QCursor &cursor );
186 
188  virtual void activate();
189 
191  virtual void deactivate();
192 
194  virtual void clean();
195 
197  QgsMapCanvas *canvas() const;
198 
204  QString toolName() { return mToolName; }
205 
212  static double searchRadiusMM();
213 
219  static double searchRadiusMU( const QgsRenderContext &context );
220 
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:
269  void messageEmitted( const QString &message, Qgis::MessageLevel = Qgis::MessageLevel::Info );
270 
272  void messageDiscarded();
273 
275  void activated();
276 
278  void deactivated();
279 
280  private slots:
282  void actionDestroyed();
283 
284  protected:
285 
288 
293  QgsPoint toLayerCoordinates( const QgsMapLayer *layer, const QgsPoint &point ) SIP_PYNAME( toLayerCoordinatesV2 );
294 
296  QgsPointXY toLayerCoordinates( const QgsMapLayer *layer, QPoint point );
297 
299  QgsPointXY toLayerCoordinates( const QgsMapLayer *layer, const QgsPointXY &point );
300 
302  QgsPointXY toMapCoordinates( const QgsMapLayer *layer, const QgsPointXY &point );
303 
308  QgsPoint toMapCoordinates( const QgsMapLayer *layer, const QgsPoint &point ) SIP_PYNAME( toMapCoordinatesV2 );
309 
311  QgsRectangle toLayerCoordinates( const QgsMapLayer *layer, const QgsRectangle &rect );
312 
314  QPoint toCanvasCoordinates( const QgsPointXY &point ) const;
315 
325  QgsMapLayer *layer( const QString &id );
326 
333  void setToolName( const QString &name );
334 
336  QPointer< QgsMapCanvas > mCanvas;
337 
339  QCursor mCursor;
340 
345  QAction *mAction = nullptr;
346 
351  QAbstractButton *mButton = nullptr;
352 
354  QString mToolName;
355 
356  friend class TestQgsMapToolEdit;
357 
358 };
359 
360 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsMapTool::Flags )
361 
362 #endif
qgsmaptoolidentifyfeature.h
SIP_PYNAME
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81
qgsmaptoolidentify.h
qgsmaptooladvanceddigitizing.h
QgsPoint
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:48
QgsMapToolIdentify
Map tool for identifying features in layers.
Definition: qgsmaptoolidentify.h:53
QgsMapCanvas
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:89
qgis.h
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:59
QgsMapTool::toolName
QString toolName()
Emit map tool changed with the old tool.
Definition: qgsmaptool.h:204
QgsMapTool::mCanvas
QPointer< QgsMapCanvas > mCanvas
The pointer to the map canvas.
Definition: qgsmaptool.h:336
QgsMapToolIdentifyFeature
The QgsMapToolIdentifyFeature class is a map tool to identify a feature on a chosen layer....
Definition: qgsmaptoolidentifyfeature.h:28
QgsMapTool::Flag
Flag
Enumeration of flags that adjust the way the map tool operates.
Definition: qgsmaptool.h:108
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QgsMapTool
Abstract base class for all map tools. Map tools are user interactive tools for manipulating the map ...
Definition: qgsmaptool.h:70
qgsmaptoolzoom.h
qgsmaptooldigitizefeature.h
SIP_CONVERT_TO_SUBCLASS_CODE
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:186
qgsmaptoolpan.h
QgsMapToolZoom
A map tool for zooming into the map.
Definition: qgsmaptoolzoom.h:30
Q_DECLARE_OPERATORS_FOR_FLAGS
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsField::ConfigurationFlags) CORE_EXPORT QDataStream &operator<<(QDataStream &out
Writes the field to stream out. QGIS version compatibility is not guaranteed.
QgsMapToolDigitizeFeature
This tool digitizes geometry of new point/line/polygon features on already existing vector layers Onc...
Definition: qgsmaptooldigitizefeature.h:31
QgsMapToolPan
A map tool for panning the map.
Definition: qgsmaptoolpan.h:32
QgsPointXY
A class to represent a 2D point.
Definition: qgspointxy.h:58
QgsMapTool::mCursor
QCursor mCursor
The cursor used in the map tool.
Definition: qgsmaptool.h:339
QgsMapToolEmitPoint
A map tool that simply emits a point when clicking on the map. Connecting a slot to its canvasClicked...
Definition: qgsmaptoolemitpoint.h:30
QgsMapMouseEvent
A QgsMapMouseEvent is the result of a user interaction with the mouse on a QgsMapCanvas....
Definition: qgsmapmouseevent.h:35
Qgis::MessageLevel
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:114
QgsMapToolEdit
Base class for map tools that edit vector geometry.
Definition: qgsmaptooledit.h:32
QgsMapLayer
Base class for all map layer types. This is the base class for all map layer types (vector,...
Definition: qgsmaplayer.h:72
qgsmaptooledit.h
QgsMapToolExtent
A map tool that emits an extent from a rectangle drawn onto the map canvas.
Definition: qgsmaptoolextent.h:33
qgsmaptoolcapture.h
qgsmaptoolextent.h
QgsMapToolCapture
Definition: qgsmaptoolcapture.h:49
QgsMapToolAdvancedDigitizing
The QgsMapToolAdvancedDigitizing class is a QgsMapTool which gives event directly in map coordinates ...
Definition: qgsmaptooladvanceddigitizing.h:36
SIP_END
#define SIP_END
Definition: qgis_sip.h:203
SIP_TRANSFERTHIS
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
qgsmaptoolemitpoint.h
QgsMapTool::mToolName
QString mToolName
The translated name of the map tool.
Definition: qgsmaptool.h:354