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