QGIS API Documentation  2.99.0-Master (9caa722)
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 #include "qgsmessagebar.h"
22 #include "qgspoint.h"
23 #include "qgsmapmouseevent.h"
24 
25 #include <QCursor>
26 #include <QString>
27 #include <QObject>
28 
29 #include <QGestureEvent>
30 #include "qgis_gui.h"
31 
32 class QgsMapLayer;
33 class QgsMapCanvas;
34 class QgsRenderContext;
35 class QKeyEvent;
36 class QMouseEvent;
37 class QWheelEvent;
38 class QgsPointXY;
39 class QgsRectangle;
40 class QPoint;
41 class QAction;
42 class QAbstractButton;
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 
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 
86  enum Flag
87  {
88  Transient = 1 << 1,
91  EditTool = 1 << 2,
92  AllowZoomRect = 1 << 3,
93  };
94  Q_DECLARE_FLAGS( Flags, Flag )
95 
96 
99  virtual Flags flags() const { return Flags(); }
100 
101  virtual ~QgsMapTool();
102 
104  virtual void canvasMoveEvent( QgsMapMouseEvent *e );
105 
107  virtual void canvasDoubleClickEvent( QgsMapMouseEvent *e );
108 
110  virtual void canvasPressEvent( QgsMapMouseEvent *e );
111 
113  virtual void canvasReleaseEvent( QgsMapMouseEvent *e );
114 
116  virtual void wheelEvent( QWheelEvent *e );
117 
119  virtual void keyPressEvent( QKeyEvent *e );
120 
122  virtual void keyReleaseEvent( QKeyEvent *e );
123 
125  virtual bool gestureEvent( QGestureEvent *e );
126 
131  void setAction( QAction *action );
132 
134  QAction *action();
135 
138  void setButton( QAbstractButton *button );
139 
141  QAbstractButton *button();
142 
144  virtual void setCursor( const QCursor &cursor );
145 
147  virtual void activate();
148 
150  virtual void deactivate();
151 
153  QgsMapCanvas *canvas();
154 
157  QString toolName() { return mToolName; }
158 
163  static double searchRadiusMM();
164 
168  static double searchRadiusMU( const QgsRenderContext &context );
169 
173  static double searchRadiusMU( QgsMapCanvas *canvas );
174 
175  signals:
177  void messageEmitted( const QString &message, QgsMessageBar::MessageLevel = QgsMessageBar::INFO );
178 
180  void messageDiscarded();
181 
183  void activated();
184 
186  void deactivated();
187 
188  private slots:
190  void actionDestroyed();
191 
192  protected:
193 
196 
198  QgsPointXY toMapCoordinates( QPoint point );
199 
201  QgsPointXY toLayerCoordinates( const QgsMapLayer *layer, QPoint point );
202 
204  QgsPointXY toLayerCoordinates( const QgsMapLayer *layer, const QgsPointXY &point );
205 
207  QgsPointXY toMapCoordinates( const QgsMapLayer *layer, const QgsPointXY &point );
208 
211  QgsPoint toMapCoordinates( const QgsMapLayer *layer, const QgsPoint &point ) SIP_PYNAME( toMapCoordinatesV2 );
212 
214  QgsRectangle toLayerCoordinates( const QgsMapLayer *layer, const QgsRectangle &rect );
215 
217  QPoint toCanvasCoordinates( const QgsPointXY &point );
218 
220  QgsMapCanvas *mCanvas = nullptr;
221 
223  QCursor mCursor;
224 
227  QAction *mAction = nullptr;
228 
231  QAbstractButton *mButton = nullptr;
232 
234  QString mToolName;
235 
236 };
237 
238 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsMapTool::Flags )
239 
240 #endif
A rectangle specified with double values.
Definition: qgsrectangle.h:38
Base class for all map layer types.
Definition: qgsmaplayer.h:54
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
A class to represent a 2D point.
Definition: qgspointxy.h:42
A QgsMapMouseEvent is the result of a user interaction with the mouse on a QgsMapCanvas.
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:234
QCursor mCursor
cursor used in map tool
Definition: qgsmaptool.h:223
QString toolName()
Emit map tool changed with the old tool.
Definition: qgsmaptool.h:157
#define SIP_END
Definition: qgis_sip.h:175
Flag
Enumeration of flags that adjust the way the map tool operates.
Definition: qgsmaptool.h:86
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:36
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