QGIS API Documentation  3.21.0-Master (402cf1129e)
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 
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 % End
56 #endif
57 
65 class GUI_EXPORT QgsMapTool : public QObject
66 {
67 
68 #ifdef SIP_RUN
70  if ( dynamic_cast<QgsMapToolZoom *>( sipCpp ) != NULL )
71  sipType = sipType_QgsMapToolZoom;
72  else if ( dynamic_cast<QgsMapToolPan *>( sipCpp ) != NULL )
73  sipType = sipType_QgsMapToolPan;
74  else if ( dynamic_cast<QgsMapToolEmitPoint *>( sipCpp ) != NULL )
75  sipType = sipType_QgsMapToolEmitPoint;
76  else if ( dynamic_cast<QgsMapToolIdentify *>( sipCpp ) != NULL )
77  sipType = sipType_QgsMapToolIdentify;
78  else if ( dynamic_cast<QgsMapToolDigitizeFeature *>( sipCpp ) != NULL )
79  sipType = sipType_QgsMapToolDigitizeFeature;
80  else
81  sipType = NULL;
82  SIP_END
83 #endif
84 
85  Q_OBJECT
86 
87  public:
88 
93  enum Flag
94  {
95  Transient = 1 << 1,
98  EditTool = 1 << 2,
99  AllowZoomRect = 1 << 3,
100  ShowContextMenu = 1 << 4,
101  };
102  Q_DECLARE_FLAGS( Flags, Flag )
103 
104 
108  virtual Flags flags() const { return Flags(); }
109 
110  ~QgsMapTool() override;
111 
113  virtual void canvasMoveEvent( QgsMapMouseEvent *e );
114 
116  virtual void canvasDoubleClickEvent( QgsMapMouseEvent *e );
117 
119  virtual void canvasPressEvent( QgsMapMouseEvent *e );
120 
122  virtual void canvasReleaseEvent( QgsMapMouseEvent *e );
123 
125  virtual void wheelEvent( QWheelEvent *e );
126 
128  virtual void keyPressEvent( QKeyEvent *e );
129 
131  virtual void keyReleaseEvent( QKeyEvent *e );
132 
134  virtual bool gestureEvent( QGestureEvent *e );
135 
141  virtual bool canvasToolTipEvent( QHelpEvent *e );
142 
149  void setAction( QAction *action );
150 
152  QAction *action();
153 
158  bool isActive() const;
159 
164  void setButton( QAbstractButton *button );
165 
167  QAbstractButton *button();
168 
170  virtual void setCursor( const QCursor &cursor );
171 
173  virtual void activate();
174 
176  virtual void deactivate();
177 
179  virtual void clean();
180 
182  QgsMapCanvas *canvas() const;
183 
189  QString toolName() { return mToolName; }
190 
197  static double searchRadiusMM();
198 
204  static double searchRadiusMU( const QgsRenderContext &context );
205 
211  static double searchRadiusMU( QgsMapCanvas *canvas );
212 
227  virtual void populateContextMenu( QMenu *menu );
228 
247  virtual bool populateContextMenuWithEvent( QMenu *menu, QgsMapMouseEvent *event );
248 
249  signals:
251  void messageEmitted( const QString &message, Qgis::MessageLevel = Qgis::MessageLevel::Info );
252 
255 
257  void activated();
258 
260  void deactivated();
261 
262  private slots:
264  void actionDestroyed();
265 
266  protected:
267 
270 
272  QgsPointXY toMapCoordinates( QPoint point );
273 
275  QgsPointXY toLayerCoordinates( const QgsMapLayer *layer, QPoint point );
276 
278  QgsPointXY toLayerCoordinates( const QgsMapLayer *layer, const QgsPointXY &point );
279 
281  QgsPointXY toMapCoordinates( const QgsMapLayer *layer, const QgsPointXY &point );
282 
287  QgsPoint toMapCoordinates( const QgsMapLayer *layer, const QgsPoint &point ) SIP_PYNAME( toMapCoordinatesV2 );
288 
290  QgsRectangle toLayerCoordinates( const QgsMapLayer *layer, const QgsRectangle &rect );
291 
293  QPoint toCanvasCoordinates( const QgsPointXY &point ) const;
294 
301  void setToolName( const QString &name );
302 
304  QgsMapCanvas *mCanvas = nullptr;
305 
307  QCursor mCursor;
308 
313  QAction *mAction = nullptr;
314 
319  QAbstractButton *mButton = nullptr;
320 
322  QString mToolName;
323 
324 };
325 
326 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsMapTool::Flags )
327 
328 #endif
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:106
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:86
Base class for all map layer types.
Definition: qgsmaplayer.h:71
A QgsMapMouseEvent is the result of a user interaction with the mouse on a QgsMapCanvas.
This tool digitizes geometry of new point/line/polygon features on already existing vector layers Onc...
A map tool that simply emits a point when clicking on the map.
Map tool for identifying features in layers.
A map tool for panning the map.
Definition: qgsmaptoolpan.h:33
A map tool for zooming into the map.
Abstract base class for all map tools.
Definition: qgsmaptool.h:66
QString toolName()
Emit map tool changed with the old tool.
Definition: qgsmaptool.h:189
void deactivated()
signal emitted once the map tool is deactivated
void messageDiscarded()
emit signal to clear previous message
QString mToolName
The translated name of the map tool.
Definition: qgsmaptool.h:322
void messageEmitted(const QString &message, Qgis::MessageLevel=Qgis::MessageLevel::Info)
emit a message
void activated()
signal emitted once the map tool is activated
Flag
Enumeration of flags that adjust the way the map tool operates.
Definition: qgsmaptool.h:94
QCursor mCursor
The cursor used in the map tool.
Definition: qgsmaptool.h:307
A class to represent a 2D point.
Definition: qgspointxy.h:59
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:49
A rectangle specified with double values.
Definition: qgsrectangle.h:42
Contains information about the context of a rendering operation.
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:177
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81
#define SIP_END
Definition: qgis_sip.h:194
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.