QGIS API Documentation  2.99.0-Master (7d4f81d)
qgsmaptool.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmaptool.cpp - 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 #include "qgslogger.h"
17 #include "qgsmaptool.h"
18 #include "qgsmapcanvas.h"
19 #include "qgsmaptopixel.h"
20 #include "qgsrendercontext.h"
21 #include "qgssettings.h"
22 
23 #include <QAction>
24 #include <QAbstractButton>
25 
27  : QObject( canvas )
28  , mCanvas( canvas )
29  , mCursor( Qt::CrossCursor )
30  , mAction( nullptr )
31  , mButton( nullptr )
32  , mToolName( QString() )
33 {
34 }
35 
36 
38 {
39  mCanvas->unsetMapTool( this );
40 }
41 
42 
44 {
46 }
47 
49 {
50  QgsPointXY result = mCanvas->mapSettings().layerToMapCoordinates( layer, QgsPointXY( point.x(), point.y() ) );
51  return QgsPoint( result.x(), result.y() );
52 }
53 
54 
56 {
57  QgsPointXY pt = toMapCoordinates( point );
58  return toLayerCoordinates( layer, pt );
59 }
60 
62 {
63  return mCanvas->mapSettings().mapToLayerCoordinates( layer, point );
64 }
65 
67 {
68  return mCanvas->mapSettings().layerToMapCoordinates( layer, point );
69 }
70 
72 {
73  return mCanvas->mapSettings().mapToLayerCoordinates( layer, rect );
74 }
75 
77 {
78  qreal x = point.x(), y = point.y();
80  return QPoint( qRound( x ), qRound( y ) );
81 }
82 
83 
85 {
86  // make action and/or button active
87  if ( mAction )
88  mAction->setChecked( true );
89  if ( mButton )
90  mButton->setChecked( true );
91 
92  // set cursor (map tools usually set it in constructor)
93  mCanvas->setCursor( mCursor );
94  QgsDebugMsg( "Cursor has been set" );
95 
96  emit activated();
97 }
98 
99 
101 {
102  if ( mAction )
103  mAction->setChecked( false );
104  if ( mButton )
105  mButton->setChecked( false );
106 
107  emit deactivated();
108 }
109 
111 {
112  if ( mAction )
113  disconnect( mAction, &QObject::destroyed, this, &QgsMapTool::actionDestroyed );
114  mAction = action;
115  if ( mAction )
116  connect( mAction, &QObject::destroyed, this, &QgsMapTool::actionDestroyed );
117 }
118 
119 void QgsMapTool::actionDestroyed()
120 {
121  if ( mAction == sender() )
122  mAction = nullptr;
123 }
124 
126 {
127  return mAction;
128 }
129 
130 void QgsMapTool::setButton( QAbstractButton *button )
131 {
132  mButton = button;
133 }
134 
135 QAbstractButton *QgsMapTool::button()
136 {
137  return mButton;
138 }
139 
140 void QgsMapTool::setCursor( const QCursor &cursor )
141 {
142  mCursor = cursor;
143 }
144 
145 
147 {
148  Q_UNUSED( e );
149 }
150 
152 {
153  Q_UNUSED( e );
154 }
155 
157 {
158  Q_UNUSED( e );
159 }
160 
162 {
163  Q_UNUSED( e );
164 }
165 
166 void QgsMapTool::wheelEvent( QWheelEvent *e )
167 {
168  e->ignore();
169 }
170 
171 void QgsMapTool::keyPressEvent( QKeyEvent *e )
172 {
173  Q_UNUSED( e );
174 }
175 
176 void QgsMapTool::keyReleaseEvent( QKeyEvent *e )
177 {
178  Q_UNUSED( e );
179 }
180 
181 bool QgsMapTool::gestureEvent( QGestureEvent *e )
182 {
183  Q_UNUSED( e );
184  return true;
185 }
186 
188 {
189  return mCanvas;
190 }
191 
193 {
194  QgsSettings settings;
195  double radius = settings.value( QStringLiteral( "Map/searchRadiusMM" ), Qgis::DEFAULT_SEARCH_RADIUS_MM ).toDouble();
196 
197  if ( radius > 0 )
198  {
199  return radius;
200  }
202 }
203 
205 {
206  return searchRadiusMM() * context.scaleFactor() * context.mapToPixel().mapUnitsPerPixel();
207 }
208 
210 {
211  if ( !canvas )
212  {
213  return 0;
214  }
215  QgsMapSettings mapSettings = canvas->mapSettings();
216  QgsRenderContext context = QgsRenderContext::fromMapSettings( mapSettings );
217  return searchRadiusMU( context );
218 }
void unsetMapTool(QgsMapTool *mapTool)
Unset the current map tool or last non zoom tool.
A rectangle specified with double values.
Definition: qgsrectangle.h:38
Base class for all map layer types.
Definition: qgsmaplayer.h:54
double y
Definition: qgspoint.h:41
virtual void canvasMoveEvent(QgsMapMouseEvent *e)
Mouse move event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:146
static double searchRadiusMU(const QgsRenderContext &context)
Get search radius in map units for given context.
Definition: qgsmaptool.cpp:204
QAbstractButton * button()
Return associated button with map tool or NULL if no button is associated.
Definition: qgsmaptool.cpp:135
This class is a composition of two QSettings instances:
Definition: qgssettings.h:54
#define QgsDebugMsg(str)
Definition: qgslogger.h:37
double y
Definition: qgspointxy.h:47
A class to represent a 2D point.
Definition: qgspointxy.h:42
QAbstractButton * mButton
optionally map tool can have pointer to a button which will be used to set that action as active ...
Definition: qgsmaptool.h:231
A QgsMapMouseEvent is the result of a user interaction with the mouse on a QgsMapCanvas.
QgsPointXY toMapCoordinates(QPoint point)
transformation from screen coordinates to map coordinates
Definition: qgsmaptool.cpp:43
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:73
virtual void canvasPressEvent(QgsMapMouseEvent *e)
Mouse press event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:156
void setButton(QAbstractButton *button)
Use this to associate a button to this maptool.
Definition: qgsmaptool.cpp:130
The QgsMapSettings class contains configuration for rendering of the map.
void transformInPlace(double &x, double &y) const
Transform device coordinates to map coordinates.
virtual void activate()
called when set as currently active map tool
Definition: qgsmaptool.cpp:84
QgsMapCanvas * mCanvas
pointer to map canvas
Definition: qgsmaptool.h:220
virtual void keyReleaseEvent(QKeyEvent *e)
Key event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:176
QCursor mCursor
cursor used in map tool
Definition: qgsmaptool.h:223
QgsMapCanvas * canvas()
returns pointer to the tool&#39;s map canvas
Definition: qgsmaptool.cpp:187
void setAction(QAction *action)
Use this to associate a QAction to this maptool.
Definition: qgsmaptool.cpp:110
static const double DEFAULT_SEARCH_RADIUS_MM
Identify search radius in mm.
Definition: qgis.h:94
virtual void deactivate()
called when map tool is being deactivated
Definition: qgsmaptool.cpp:100
double mapUnitsPerPixel() const
Return current map units per pixel.
virtual void wheelEvent(QWheelEvent *e)
Mouse wheel event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:166
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:36
void activated()
signal emitted once the map tool is activated
double x
Definition: qgspointxy.h:46
QAction * action()
Return associated action with map tool or NULL if no action is associated.
Definition: qgsmaptool.cpp:125
virtual void keyPressEvent(QKeyEvent *e)
Key event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:171
const QgsMapSettings & mapSettings() const
Get access to properties used for map rendering.
Contains information about the context of a rendering operation.
QgsPointXY toLayerCoordinates(const QgsMapLayer *layer, QPoint point)
transformation from screen coordinates to layer&#39;s coordinates
Definition: qgsmaptool.cpp:55
void deactivated()
signal emitted once the map tool is deactivated
const QgsMapToPixel & mapToPixel() const
virtual bool gestureEvent(QGestureEvent *e)
gesture event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:181
virtual void canvasDoubleClickEvent(QgsMapMouseEvent *e)
Mouse double-click event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:151
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), const Section section=NoSection) const
Returns the value for setting key.
QgsPointXY layerToMapCoordinates(const QgsMapLayer *layer, QgsPointXY point) const
transform point coordinates from layer&#39;s CRS to output CRS
static QgsRenderContext fromMapSettings(const QgsMapSettings &mapSettings)
create initialized QgsRenderContext instance from given QgsMapSettings
virtual ~QgsMapTool()
Definition: qgsmaptool.cpp:37
QAction * mAction
optionally map tool can have pointer to action which will be used to set that action as active ...
Definition: qgsmaptool.h:227
const QgsMapToPixel * getCoordinateTransform()
Get the current coordinate transform.
virtual void setCursor(const QCursor &cursor)
Set a user defined cursor.
Definition: qgsmaptool.cpp:140
QgsMapTool(QgsMapCanvas *canvas)
constructor takes map canvas as a parameter
Definition: qgsmaptool.cpp:26
QgsPointXY mapToLayerCoordinates(const QgsMapLayer *layer, QgsPointXY point) const
transform point coordinates from output CRS to layer&#39;s CRS
double scaleFactor() const
Returns the scaling factor for the render to convert painter units to physical sizes.
QPoint toCanvasCoordinates(const QgsPointXY &point)
transformation from map coordinates to screen coordinates
Definition: qgsmaptool.cpp:76
QgsPointXY toMapCoordinates(int x, int y) const
static double searchRadiusMM()
Get search radius in mm.
Definition: qgsmaptool.cpp:192
double x
Definition: qgspoint.h:40
virtual void canvasReleaseEvent(QgsMapMouseEvent *e)
Mouse release event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:161