QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 class QgsMapLayer;
30 class QgsMapCanvas;
31 class QgsRenderContext;
32 class QKeyEvent;
33 class QMouseEvent;
34 class QWheelEvent;
35 class QgsPoint;
36 class QgsPointXY;
37 class QgsRectangle;
38 class QPoint;
39 class QAction;
40 class QAbstractButton;
41 class QgsMapMouseEvent;
42 class QMenu;
43 
44 #ifdef SIP_RUN
45 % ModuleHeaderCode
46 // fix to allow compilation with sip 4.7 that for some reason
47 // doesn't add these includes to the file where the code from
48 // ConvertToSubClassCode goes.
49 #include <qgsmaptoolzoom.h>
50 #include <qgsmaptoolpan.h>
51 #include <qgsmaptoolemitpoint.h>
52 #include <qgsmaptoolidentify.h>
53 % End
54 #endif
55 
63 class GUI_EXPORT QgsMapTool : public QObject
64 {
65 
66 #ifdef SIP_RUN
68  if ( dynamic_cast<QgsMapToolZoom *>( sipCpp ) != NULL )
69  sipType = sipType_QgsMapToolZoom;
70  else if ( dynamic_cast<QgsMapToolPan *>( sipCpp ) != NULL )
71  sipType = sipType_QgsMapToolPan;
72  else if ( dynamic_cast<QgsMapToolEmitPoint *>( sipCpp ) != NULL )
73  sipType = sipType_QgsMapToolEmitPoint;
74  else if ( dynamic_cast<QgsMapToolIdentify *>( sipCpp ) != NULL )
75  sipType = sipType_QgsMapToolIdentify;
76  else
77  sipType = NULL;
78  SIP_END
79 #endif
80 
81  Q_OBJECT
82 
83  public:
84 
89  enum Flag
90  {
91  Transient = 1 << 1,
94  EditTool = 1 << 2,
95  AllowZoomRect = 1 << 3,
96  ShowContextMenu = 1 << 4,
97  };
98  Q_DECLARE_FLAGS( Flags, Flag )
99 
100 
104  virtual Flags flags() const { return Flags(); }
105 
106  ~QgsMapTool() override;
107 
109  virtual void canvasMoveEvent( QgsMapMouseEvent *e );
110 
112  virtual void canvasDoubleClickEvent( QgsMapMouseEvent *e );
113 
115  virtual void canvasPressEvent( QgsMapMouseEvent *e );
116 
118  virtual void canvasReleaseEvent( QgsMapMouseEvent *e );
119 
121  virtual void wheelEvent( QWheelEvent *e );
122 
124  virtual void keyPressEvent( QKeyEvent *e );
125 
127  virtual void keyReleaseEvent( QKeyEvent *e );
128 
130  virtual bool gestureEvent( QGestureEvent *e );
131 
138  void setAction( QAction *action );
139 
141  QAction *action();
142 
147  bool isActive() const;
148 
153  void setButton( QAbstractButton *button );
154 
156  QAbstractButton *button();
157 
159  virtual void setCursor( const QCursor &cursor );
160 
162  virtual void activate();
163 
165  virtual void deactivate();
166 
168  virtual void clean();
169 
171  QgsMapCanvas *canvas() const;
172 
177  QString toolName() { return mToolName; }
178 
185  static double searchRadiusMM();
186 
192  static double searchRadiusMU( const QgsRenderContext &context );
193 
199  static double searchRadiusMU( QgsMapCanvas *canvas );
200 
215  virtual void populateContextMenu( QMenu *menu );
216 
217  signals:
219  void messageEmitted( const QString &message, Qgis::MessageLevel = Qgis::Info );
220 
223 
225  void activated();
226 
228  void deactivated();
229 
230  private slots:
232  void actionDestroyed();
233 
234  protected:
235 
238 
240  QgsPointXY toMapCoordinates( QPoint point );
241 
243  QgsPointXY toLayerCoordinates( const QgsMapLayer *layer, QPoint point );
244 
246  QgsPointXY toLayerCoordinates( const QgsMapLayer *layer, const QgsPointXY &point );
247 
249  QgsPointXY toMapCoordinates( const QgsMapLayer *layer, const QgsPointXY &point );
250 
255  QgsPoint toMapCoordinates( const QgsMapLayer *layer, const QgsPoint &point ) SIP_PYNAME( toMapCoordinatesV2 );
256 
258  QgsRectangle toLayerCoordinates( const QgsMapLayer *layer, const QgsRectangle &rect );
259 
261  QPoint toCanvasCoordinates( const QgsPointXY &point ) const;
262 
264  QgsMapCanvas *mCanvas = nullptr;
265 
267  QCursor mCursor;
268 
273  QAction *mAction = nullptr;
274 
279  QAbstractButton *mButton = nullptr;
280 
282  QString mToolName;
283 
284 };
285 
286 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsMapTool::Flags )
287 
288 #endif
SIP_PYNAME
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81
qgsmaptoolidentify.h
QgsPoint
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:38
QgsMapToolIdentify
Map tool for identifying features in layers.
Definition: qgsmaptoolidentify.h:49
QgsMapCanvas
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:85
qgis.h
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:58
QgsMapTool::toolName
QString toolName()
Emit map tool changed with the old tool.
Definition: qgsmaptool.h:177
QgsMapTool::Flag
Flag
Enumeration of flags that adjust the way the map tool operates.
Definition: qgsmaptool.h:90
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:42
QgsMapTool
Abstract base class for all map tools.
Definition: qgsmaptool.h:64
qgsmaptoolzoom.h
SIP_CONVERT_TO_SUBCLASS_CODE
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:177
qgsmaptoolpan.h
QgsMapTool::messageEmitted
void messageEmitted(const QString &message, Qgis::MessageLevel=Qgis::Info)
emit a message
Qgis::Info
@ Info
Definition: qgis.h:90
QgsMapToolZoom
A map tool for zooming into the map.
Definition: qgsmaptoolzoom.h:31
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.
QgsMapTool::messageDiscarded
void messageDiscarded()
emit signal to clear previous message
QgsMapToolPan
A map tool for panning the map.
Definition: qgsmaptoolpan.h:33
QgsPointXY
A class to represent a 2D point.
Definition: qgspointxy.h:44
QgsMapTool::mCursor
QCursor mCursor
cursor used in map tool
Definition: qgsmaptool.h:267
QgsMapToolEmitPoint
A map tool that simply emits a point when clicking on the map.
Definition: qgsmaptoolemitpoint.h:31
QgsMapMouseEvent
A QgsMapMouseEvent is the result of a user interaction with the mouse on a QgsMapCanvas.
Definition: qgsmapmouseevent.h:36
Qgis::MessageLevel
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:89
QgsMapLayer
Base class for all map layer types.
Definition: qgsmaplayer.h:83
SIP_END
#define SIP_END
Definition: qgis_sip.h:194
SIP_TRANSFERTHIS
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
QgsMapTool::activated
void activated()
signal emitted once the map tool is activated
qgsmaptoolemitpoint.h
QgsMapTool::deactivated
void deactivated()
signal emitted once the map tool is deactivated
QgsMapTool::mToolName
QString mToolName
translated name of the map tool
Definition: qgsmaptool.h:282