QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
137  void setAction( QAction *action );
138 
140  QAction *action();
141 
146  bool isActive() const;
147 
151  void setButton( QAbstractButton *button );
152 
154  QAbstractButton *button();
155 
157  virtual void setCursor( const QCursor &cursor );
158 
160  virtual void activate();
161 
163  virtual void deactivate();
164 
166  virtual void clean();
167 
169  QgsMapCanvas *canvas() const;
170 
175  QString toolName() { return mToolName; }
176 
182  static double searchRadiusMM();
183 
188  static double searchRadiusMU( const QgsRenderContext &context );
189 
194  static double searchRadiusMU( QgsMapCanvas *canvas );
195 
210  virtual void populateContextMenu( QMenu *menu );
211 
212  signals:
214  void messageEmitted( const QString &message, Qgis::MessageLevel = Qgis::Info );
215 
217  void messageDiscarded();
218 
220  void activated();
221 
223  void deactivated();
224 
225  private slots:
227  void actionDestroyed();
228 
229  protected:
230 
233 
235  QgsPointXY toMapCoordinates( QPoint point );
236 
238  QgsPointXY toLayerCoordinates( const QgsMapLayer *layer, QPoint point );
239 
241  QgsPointXY toLayerCoordinates( const QgsMapLayer *layer, const QgsPointXY &point );
242 
244  QgsPointXY toMapCoordinates( const QgsMapLayer *layer, const QgsPointXY &point );
245 
250  QgsPoint toMapCoordinates( const QgsMapLayer *layer, const QgsPoint &point ) SIP_PYNAME( toMapCoordinatesV2 );
251 
253  QgsRectangle toLayerCoordinates( const QgsMapLayer *layer, const QgsRectangle &rect );
254 
256  QPoint toCanvasCoordinates( const QgsPointXY &point ) const;
257 
259  QgsMapCanvas *mCanvas = nullptr;
260 
262  QCursor mCursor;
263 
268  QAction *mAction = nullptr;
269 
274  QAbstractButton *mButton = nullptr;
275 
277  QString mToolName;
278 
279 };
280 
281 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsMapTool::Flags )
282 
283 #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:37
QgsMapToolIdentify
Map tool for identifying features in layers.
Definition: qgsmaptoolidentify.h:47
QgsMapCanvas
Definition: qgsmapcanvas.h:83
qgis.h
QgsRenderContext
Definition: qgsrendercontext.h:57
QgsMapTool::toolName
QString toolName()
Emit map tool changed with the old tool.
Definition: qgsmaptool.h:175
QgsMapTool::Flag
Flag
Enumeration of flags that adjust the way the map tool operates.
Definition: qgsmaptool.h:89
QgsRectangle
Definition: qgsrectangle.h:41
QgsMapTool
Definition: qgsmaptool.h:63
qgsmaptoolzoom.h
SIP_CONVERT_TO_SUBCLASS_CODE
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:172
qgsmaptoolpan.h
Qgis::Info
@ Info
Definition: qgis.h:103
QgsMapToolZoom
Definition: qgsmaptoolzoom.h:30
QgsMapToolPan
Definition: qgsmaptoolpan.h:32
QgsPointXY
Definition: qgspointxy.h:43
QgsMapTool::mCursor
QCursor mCursor
cursor used in map tool
Definition: qgsmaptool.h:262
QgsMapToolEmitPoint
Definition: qgsmaptoolemitpoint.h:30
QgsMapMouseEvent
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:101
QgsMapLayer
Definition: qgsmaplayer.h:81
SIP_END
#define SIP_END
Definition: qgis_sip.h:189
SIP_TRANSFERTHIS
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
qgsmaptoolemitpoint.h
QgsMapTool::mToolName
QString mToolName
translated name of the map tool
Definition: qgsmaptool.h:277