QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 "qgsmaprenderer.h"
21 #include "qgsrendercontext.h"
22 #include <QAction>
23 #include <QAbstractButton>
24 
26  : QObject( canvas )
27  , mCanvas( canvas )
28  , mCursor( Qt::CrossCursor )
29  , mAction( nullptr )
30  , mButton( nullptr )
31  , mToolName( QString() )
32 {
33 }
34 
35 
37 {
38  mCanvas->unsetMapTool( this );
39 }
40 
41 
43 {
45 }
46 
48 {
49  QgsPoint result = mCanvas->mapSettings().layerToMapCoordinates( layer, QgsPoint( point.x(), point.y() ) );
50  return QgsPointV2( result.x(), result.y() );
51 }
52 
53 
55 {
56  QgsPoint pt = toMapCoordinates( point );
57  return toLayerCoordinates( layer, pt );
58 }
59 
61 {
62  return mCanvas->mapSettings().mapToLayerCoordinates( layer, point );
63 }
64 
66 {
67  return mCanvas->mapSettings().layerToMapCoordinates( layer, point );
68 }
69 
71 {
72  return mCanvas->mapSettings().mapToLayerCoordinates( layer, rect );
73 }
74 
76 {
77  qreal x = point.x(), y = point.y();
79  return QPoint( qRound( x ), qRound( y ) );
80 }
81 
82 
84 {
85  // make action and/or button active
86  if ( mAction )
87  mAction->setChecked( true );
88  if ( mButton )
89  mButton->setChecked( true );
90 
91  // set cursor (map tools usually set it in constructor)
93  QgsDebugMsg( "Cursor has been set" );
94 
95  emit activated();
96 }
97 
98 
100 {
101  if ( mAction )
102  mAction->setChecked( false );
103  if ( mButton )
104  mButton->setChecked( false );
105 
106  emit deactivated();
107 }
108 
110 {
111  if ( mAction )
112  disconnect( mAction, SIGNAL( destroyed() ), this, SLOT( actionDestroyed() ) );
113  mAction = action;
114  if ( mAction )
115  connect( mAction, SIGNAL( destroyed() ), this, SLOT( actionDestroyed() ) );
116 }
117 
118 void QgsMapTool::actionDestroyed()
119 {
120  if ( mAction == sender() )
121  mAction = nullptr;
122 }
123 
125 {
126  return mAction;
127 }
128 
130 {
131  mButton = button;
132 }
133 
135 {
136  return mButton;
137 }
138 
139 void QgsMapTool::setCursor( const QCursor& cursor )
140 {
141  mCursor = cursor;
142 }
143 
144 
146 {
147  Q_UNUSED( e );
148 }
149 
151 {
152  Q_UNUSED( e );
153 }
154 
156 {
157  Q_UNUSED( e );
158 }
159 
161 {
162  Q_UNUSED( e );
163 }
164 
166 {
167  e->ignore();
168 }
169 
171 {
172  Q_UNUSED( e );
173 }
174 
176 {
177  Q_UNUSED( e );
178 }
179 
180 #ifdef HAVE_TOUCH
181 bool QgsMapTool::gestureEvent( QGestureEvent *e )
182 {
183  Q_UNUSED( e );
184  return true;
185 }
186 #endif
187 
189 {
190 }
191 
193 {
194  return flags() & Transient;
195 }
196 
198 {
199  return flags() & EditTool;
200 }
201 
203 {
204  return mCanvas;
205 }
206 
208 {
209  QSettings settings;
210  double radius = settings.value( "/Map/searchRadiusMM", QGis::DEFAULT_SEARCH_RADIUS_MM ).toDouble();
211 
212  if ( radius > 0 )
213  {
214  return radius;
215  }
217 }
218 
220 {
221  return searchRadiusMM() * context.scaleFactor() * context.mapToPixel().mapUnitsPerPixel();
222 }
223 
225 {
226  if ( !canvas )
227  {
228  return 0;
229  }
230  QgsMapSettings mapSettings = canvas->mapSettings();
231  QgsRenderContext context = QgsRenderContext::fromMapSettings( mapSettings );
232  return searchRadiusMU( context );
233 }
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
virtual void canvasMoveEvent(QgsMapMouseEvent *e)
Mouse move event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:145
static double searchRadiusMU(const QgsRenderContext &context)
Get search radius in map units for given context.
Definition: qgsmaptool.cpp:219
void setCursor(const QCursor &)
QAbstractButton * button()
Return associated button with map tool or NULL if no button is associated.
Definition: qgsmaptool.cpp:134
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
QObject * sender() const
void setChecked(bool)
virtual Q_DECL_DEPRECATED bool isTransient() const
Check whether this MapTool performs a zoom or pan operation.
Definition: qgsmaptool.cpp:192
QAbstractButton * mButton
optionally map tool can have pointer to a button which will be used to set that action as active ...
Definition: qgsmaptool.h:224
A QgsMapMouseEvent is the result of a user interaction with the mouse on a QgsMapCanvas.
QgsPoint toMapCoordinates(int x, int y) const
double y() const
Returns the point&#39;s y-coordinate.
Definition: qgspointv2.h:74
virtual Flags flags() const
Returns the flags for the map tool.
Definition: qgsmaptool.h:72
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:109
virtual Q_DECL_DEPRECATED void renderComplete()
Called when rendering has finished.
Definition: qgsmaptool.cpp:188
virtual void canvasPressEvent(QgsMapMouseEvent *e)
Mouse press event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:155
double y() const
Get the y value of the point.
Definition: qgspoint.h:193
void setButton(QAbstractButton *button)
Use this to associate a button to this maptool.
Definition: qgsmaptool.cpp:129
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:83
QgsMapCanvas * mCanvas
pointer to map canvas
Definition: qgsmaptool.h:213
virtual void keyReleaseEvent(QKeyEvent *e)
Key event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:175
QCursor mCursor
cursor used in map tool
Definition: qgsmaptool.h:216
virtual Q_DECL_DEPRECATED bool isEditTool() const
Check whether this MapTool performs an edit operation.
Definition: qgsmaptool.cpp:197
QgsMapCanvas * canvas()
returns pointer to the tool&#39;s map canvas
Definition: qgsmaptool.cpp:202
void ignore()
QgsPoint toLayerCoordinates(QgsMapLayer *layer, QPoint point)
transformation from screen coordinates to layer&#39;s coordinates
Definition: qgsmaptool.cpp:54
void setAction(QAction *action)
Use this to associate a QAction to this maptool.
Definition: qgsmaptool.cpp:109
Point geometry type, with support for z-dimension and m-values.
Definition: qgspointv2.h:34
double x() const
Returns the point&#39;s x-coordinate.
Definition: qgspointv2.h:68
virtual void deactivate()
called when map tool is being deactivated
Definition: qgsmaptool.cpp:99
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:165
A class to represent a point.
Definition: qgspoint.h:117
void activated()
signal emitted once the map tool is activated
static const double DEFAULT_SEARCH_RADIUS_MM
Identify search radius in mm.
Definition: qgis.h:235
QAction * action()
Return associated action with map tool or NULL if no action is associated.
Definition: qgsmaptool.cpp:124
virtual void keyPressEvent(QKeyEvent *e)
Key event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:170
const QgsMapSettings & mapSettings() const
Get access to properties used for map rendering.
void setChecked(bool)
QVariant value(const QString &key, const QVariant &defaultValue) const
Contains information about the context of a rendering operation.
void deactivated()
signal emitted once the map tool is deactivated
QgsPoint layerToMapCoordinates(QgsMapLayer *theLayer, QgsPoint point) const
transform point coordinates from layer&#39;s CRS to output CRS
const QgsMapToPixel & mapToPixel() const
QgsPoint mapToLayerCoordinates(QgsMapLayer *theLayer, QgsPoint point) const
transform point coordinates from output CRS to layer&#39;s CRS
virtual void canvasDoubleClickEvent(QgsMapMouseEvent *e)
Mouse double click event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:150
static QgsRenderContext fromMapSettings(const QgsMapSettings &mapSettings)
create initialized QgsRenderContext instance from given QgsMapSettings
virtual ~QgsMapTool()
virtual destructor
Definition: qgsmaptool.cpp:36
QPoint toCanvasCoordinates(const QgsPoint &point)
transformation from map coordinates to screen coordinates
Definition: qgsmaptool.cpp:75
QAction * mAction
optionally map tool can have pointer to action which will be used to set that action as active ...
Definition: qgsmaptool.h:220
const QgsMapToPixel * getCoordinateTransform()
Get the current coordinate transform.
virtual void setCursor(const QCursor &cursor)
Set a user defined cursor.
Definition: qgsmaptool.cpp:139
double toDouble(bool *ok) const
QgsPoint toMapCoordinates(QPoint point)
transformation from screen coordinates to map coordinates
Definition: qgsmaptool.cpp:42
QgsMapTool(QgsMapCanvas *canvas)
constructor takes map canvas as a parameter
Definition: qgsmaptool.cpp:25
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
double scaleFactor() const
void destroyed(QObject *obj)
double x() const
Get the x value of the point.
Definition: qgspoint.h:185
static double searchRadiusMM()
Get search radius in mm.
Definition: qgsmaptool.cpp:207
virtual void canvasReleaseEvent(QgsMapMouseEvent *e)
Mouse release event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:160