QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 
43 #ifdef SIP_RUN
44 % ModuleHeaderCode
45 // fix to allow compilation with sip 4.7 that for some reason
46 // doesn't add these includes to the file where the code from
47 // ConvertToSubClassCode goes.
48 #include <qgsmaptoolzoom.h>
49 #include <qgsmaptoolpan.h>
50 #include <qgsmaptoolemitpoint.h>
51 #include <qgsmaptoolidentify.h>
52 % End
53 #endif
54 
62 class GUI_EXPORT QgsMapTool : public QObject
63 {
64 
65 #ifdef SIP_RUN
67  if ( dynamic_cast<QgsMapToolZoom *>( sipCpp ) != NULL )
68  sipType = sipType_QgsMapToolZoom;
69  else if ( dynamic_cast<QgsMapToolPan *>( sipCpp ) != NULL )
70  sipType = sipType_QgsMapToolPan;
71  else if ( dynamic_cast<QgsMapToolEmitPoint *>( sipCpp ) != NULL )
72  sipType = sipType_QgsMapToolEmitPoint;
73  else if ( dynamic_cast<QgsMapToolIdentify *>( sipCpp ) != NULL )
74  sipType = sipType_QgsMapToolIdentify;
75  else
76  sipType = NULL;
77  SIP_END
78 #endif
79 
80  Q_OBJECT
81 
82  public:
83 
88  enum Flag
89  {
90  Transient = 1 << 1,
93  EditTool = 1 << 2,
94  AllowZoomRect = 1 << 3,
95  };
96  Q_DECLARE_FLAGS( Flags, Flag )
97 
98 
102  virtual Flags flags() const { return Flags(); }
103 
104  ~QgsMapTool() override;
105 
107  virtual void canvasMoveEvent( QgsMapMouseEvent *e );
108 
110  virtual void canvasDoubleClickEvent( QgsMapMouseEvent *e );
111 
113  virtual void canvasPressEvent( QgsMapMouseEvent *e );
114 
116  virtual void canvasReleaseEvent( QgsMapMouseEvent *e );
117 
119  virtual void wheelEvent( QWheelEvent *e );
120 
122  virtual void keyPressEvent( QKeyEvent *e );
123 
125  virtual void keyReleaseEvent( QKeyEvent *e );
126 
128  virtual bool gestureEvent( QGestureEvent *e );
129 
135  void setAction( QAction *action );
136 
138  QAction *action();
139 
144  bool isActive() const;
145 
149  void setButton( QAbstractButton *button );
150 
152  QAbstractButton *button();
153 
155  virtual void setCursor( const QCursor &cursor );
156 
158  virtual void activate();
159 
161  virtual void deactivate();
162 
164  virtual void clean();
165 
167  QgsMapCanvas *canvas();
168 
173  QString toolName() { return mToolName; }
174 
180  static double searchRadiusMM();
181 
186  static double searchRadiusMU( const QgsRenderContext &context );
187 
192  static double searchRadiusMU( QgsMapCanvas *canvas );
193 
194  signals:
196  void messageEmitted( const QString &message, Qgis::MessageLevel = Qgis::Info );
197 
199  void messageDiscarded();
200 
202  void activated();
203 
205  void deactivated();
206 
207  private slots:
209  void actionDestroyed();
210 
211  protected:
212 
215 
217  QgsPointXY toMapCoordinates( QPoint point );
218 
220  QgsPointXY toLayerCoordinates( const QgsMapLayer *layer, QPoint point );
221 
223  QgsPointXY toLayerCoordinates( const QgsMapLayer *layer, const QgsPointXY &point );
224 
226  QgsPointXY toMapCoordinates( const QgsMapLayer *layer, const QgsPointXY &point );
227 
232  QgsPoint toMapCoordinates( const QgsMapLayer *layer, const QgsPoint &point ) SIP_PYNAME( toMapCoordinatesV2 );
233 
235  QgsRectangle toLayerCoordinates( const QgsMapLayer *layer, const QgsRectangle &rect );
236 
238  QPoint toCanvasCoordinates( const QgsPointXY &point );
239 
241  QgsMapCanvas *mCanvas = nullptr;
242 
244  QCursor mCursor;
245 
250  QAction *mAction = nullptr;
251 
256  QAbstractButton *mButton = nullptr;
257 
259  QString mToolName;
260 
261 };
262 
263 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsMapTool::Flags )
264 
265 #endif
A rectangle specified with double values.
Definition: qgsrectangle.h:41
Base class for all map layer types.
Definition: qgsmaplayer.h:78
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
A class to represent a 2D point.
Definition: qgspointxy.h:43
A QgsMapMouseEvent is the result of a user interaction with the mouse on a QgsMapCanvas.
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:66
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:73
QString mToolName
translated name of the map tool
Definition: qgsmaptool.h:259
QCursor mCursor
cursor used in map tool
Definition: qgsmaptool.h:244
QString toolName()
Emit map tool changed with the old tool.
Definition: qgsmaptool.h:173
#define SIP_END
Definition: qgis_sip.h:182
Flag
Enumeration of flags that adjust the way the map tool operates.
Definition: qgsmaptool.h:88
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
Abstract base class for all map tools.
Definition: qgsmaptool.h:62
Contains information about the context of a rendering operation.
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:165
#define SIP_PYNAME(name)
Definition: qgis_sip.h:74