QGIS API Documentation  3.0.2-Girona (307d082)
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 
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  };
97  Q_DECLARE_FLAGS( Flags, Flag )
98 
99 
103  virtual Flags flags() const { return Flags(); }
104 
105  ~QgsMapTool() override;
106 
108  virtual void canvasMoveEvent( QgsMapMouseEvent *e );
109 
111  virtual void canvasDoubleClickEvent( QgsMapMouseEvent *e );
112 
114  virtual void canvasPressEvent( QgsMapMouseEvent *e );
115 
117  virtual void canvasReleaseEvent( QgsMapMouseEvent *e );
118 
120  virtual void wheelEvent( QWheelEvent *e );
121 
123  virtual void keyPressEvent( QKeyEvent *e );
124 
126  virtual void keyReleaseEvent( QKeyEvent *e );
127 
129  virtual bool gestureEvent( QGestureEvent *e );
130 
136  void setAction( QAction *action );
137 
139  QAction *action();
140 
144  void setButton( QAbstractButton *button );
145 
147  QAbstractButton *button();
148 
150  virtual void setCursor( const QCursor &cursor );
151 
153  virtual void activate();
154 
156  virtual void deactivate();
157 
159  virtual void clean();
160 
162  QgsMapCanvas *canvas();
163 
168  QString toolName() { return mToolName; }
169 
175  static double searchRadiusMM();
176 
181  static double searchRadiusMU( const QgsRenderContext &context );
182 
187  static double searchRadiusMU( QgsMapCanvas *canvas );
188 
189  signals:
191  void messageEmitted( const QString &message, Qgis::MessageLevel = Qgis::Info );
192 
194  void messageDiscarded();
195 
197  void activated();
198 
200  void deactivated();
201 
202  private slots:
204  void actionDestroyed();
205 
206  protected:
207 
210 
212  QgsPointXY toMapCoordinates( QPoint point );
213 
215  QgsPointXY toLayerCoordinates( const QgsMapLayer *layer, QPoint point );
216 
218  QgsPointXY toLayerCoordinates( const QgsMapLayer *layer, const QgsPointXY &point );
219 
221  QgsPointXY toMapCoordinates( const QgsMapLayer *layer, const QgsPointXY &point );
222 
227  QgsPoint toMapCoordinates( const QgsMapLayer *layer, const QgsPoint &point ) SIP_PYNAME( toMapCoordinatesV2 );
228 
230  QgsRectangle toLayerCoordinates( const QgsMapLayer *layer, const QgsRectangle &rect );
231 
233  QPoint toCanvasCoordinates( const QgsPointXY &point );
234 
236  QgsMapCanvas *mCanvas = nullptr;
237 
239  QCursor mCursor;
240 
245  QAction *mAction = nullptr;
246 
251  QAbstractButton *mButton = nullptr;
252 
254  QString mToolName;
255 
256 };
257 
258 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsMapTool::Flags )
259 
260 #endif
A rectangle specified with double values.
Definition: qgsrectangle.h:39
Base class for all map layer types.
Definition: qgsmaplayer.h:56
#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:78
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:74
QString mToolName
translated name of the map tool
Definition: qgsmaptool.h:254
QCursor mCursor
cursor used in map tool
Definition: qgsmaptool.h:239
QString toolName()
Emit map tool changed with the old tool.
Definition: qgsmaptool.h:168
#define SIP_END
Definition: qgis_sip.h:175
Flag
Enumeration of flags that adjust the way the map tool operates.
Definition: qgsmaptool.h:89
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:63
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