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 #include "qgsmapmouseevent.h"
23
24 #include <QAction>
25 #include <QAbstractButton>
26
QgsMapTool(QgsMapCanvas * canvas)27 QgsMapTool::QgsMapTool( QgsMapCanvas *canvas )
28 : QObject( canvas )
29 , mCanvas( canvas )
30 , mCursor( Qt::CrossCursor )
31 {
32 }
33
34
~QgsMapTool()35 QgsMapTool::~QgsMapTool()
36 {
37 if ( mCanvas )
38 mCanvas->unsetMapTool( this );
39 }
40
toMapCoordinates(QPoint point)41 QgsPointXY QgsMapTool::toMapCoordinates( QPoint point )
42 {
43 return mCanvas->getCoordinateTransform()->toMapCoordinates( point );
44 }
45
toMapCoordinates(const QgsMapLayer * layer,const QgsPoint & point)46 QgsPoint QgsMapTool::toMapCoordinates( const QgsMapLayer *layer, const QgsPoint &point )
47 {
48 return mCanvas->mapSettings().layerToMapCoordinates( layer, point );
49 }
50
toLayerCoordinates(const QgsMapLayer * layer,QPoint point)51 QgsPointXY QgsMapTool::toLayerCoordinates( const QgsMapLayer *layer, QPoint point )
52 {
53 QgsPointXY pt = toMapCoordinates( point );
54 return toLayerCoordinates( layer, pt );
55 }
56
toLayerCoordinates(const QgsMapLayer * layer,const QgsPointXY & point)57 QgsPointXY QgsMapTool::toLayerCoordinates( const QgsMapLayer *layer, const QgsPointXY &point )
58 {
59 return mCanvas->mapSettings().mapToLayerCoordinates( layer, point );
60 }
61
toMapCoordinates(const QgsMapLayer * layer,const QgsPointXY & point)62 QgsPointXY QgsMapTool::toMapCoordinates( const QgsMapLayer *layer, const QgsPointXY &point )
63 {
64 return mCanvas->mapSettings().layerToMapCoordinates( layer, point );
65 }
66
toLayerCoordinates(const QgsMapLayer * layer,const QgsRectangle & rect)67 QgsRectangle QgsMapTool::toLayerCoordinates( const QgsMapLayer *layer, const QgsRectangle &rect )
68 {
69 return mCanvas->mapSettings().mapToLayerCoordinates( layer, rect );
70 }
71
toCanvasCoordinates(const QgsPointXY & point) const72 QPoint QgsMapTool::toCanvasCoordinates( const QgsPointXY &point ) const
73 {
74 qreal x = point.x(), y = point.y();
75 mCanvas->getCoordinateTransform()->transformInPlace( x, y );
76 return QPoint( std::round( x ), std::round( y ) );
77 }
78
79
activate()80 void QgsMapTool::activate()
81 {
82 // make action and/or button active
83 if ( mAction )
84 mAction->setChecked( true );
85 if ( mButton )
86 mButton->setChecked( true );
87
88 // set cursor (map tools usually set it in constructor)
89 mCanvas->setCursor( mCursor );
90 QgsDebugMsgLevel( QStringLiteral( "Cursor has been set" ), 4 );
91
92 emit activated();
93 }
94
95
deactivate()96 void QgsMapTool::deactivate()
97 {
98 if ( mAction )
99 mAction->setChecked( false );
100 if ( mButton )
101 mButton->setChecked( false );
102
103 emit deactivated();
104 }
105
clean()106 void QgsMapTool::clean()
107 {
108
109 }
110
setAction(QAction * action)111 void QgsMapTool::setAction( QAction *action )
112 {
113 if ( mAction )
114 disconnect( mAction, &QObject::destroyed, this, &QgsMapTool::actionDestroyed );
115 mAction = action;
116 if ( mAction )
117 connect( mAction, &QObject::destroyed, this, &QgsMapTool::actionDestroyed );
118 }
119
actionDestroyed()120 void QgsMapTool::actionDestroyed()
121 {
122 if ( mAction == sender() )
123 mAction = nullptr;
124 }
125
action()126 QAction *QgsMapTool::action()
127 {
128 return mAction;
129 }
130
isActive() const131 bool QgsMapTool::isActive() const
132 {
133 return mCanvas && mCanvas->mapTool() == this;
134 }
135
setButton(QAbstractButton * button)136 void QgsMapTool::setButton( QAbstractButton *button )
137 {
138 mButton = button;
139 }
140
button()141 QAbstractButton *QgsMapTool::button()
142 {
143 return mButton;
144 }
145
setCursor(const QCursor & cursor)146 void QgsMapTool::setCursor( const QCursor &cursor )
147 {
148 mCursor = cursor;
149 if ( isActive() )
150 mCanvas->setCursor( mCursor );
151 }
152
153
canvasMoveEvent(QgsMapMouseEvent * e)154 void QgsMapTool::canvasMoveEvent( QgsMapMouseEvent *e )
155 {
156 Q_UNUSED( e )
157 }
158
canvasDoubleClickEvent(QgsMapMouseEvent * e)159 void QgsMapTool::canvasDoubleClickEvent( QgsMapMouseEvent *e )
160 {
161 Q_UNUSED( e )
162 }
163
canvasPressEvent(QgsMapMouseEvent * e)164 void QgsMapTool::canvasPressEvent( QgsMapMouseEvent *e )
165 {
166 Q_UNUSED( e )
167 }
168
canvasReleaseEvent(QgsMapMouseEvent * e)169 void QgsMapTool::canvasReleaseEvent( QgsMapMouseEvent *e )
170 {
171 Q_UNUSED( e )
172 }
173
wheelEvent(QWheelEvent * e)174 void QgsMapTool::wheelEvent( QWheelEvent *e )
175 {
176 e->ignore();
177 }
178
keyPressEvent(QKeyEvent * e)179 void QgsMapTool::keyPressEvent( QKeyEvent *e )
180 {
181 Q_UNUSED( e )
182 }
183
keyReleaseEvent(QKeyEvent * e)184 void QgsMapTool::keyReleaseEvent( QKeyEvent *e )
185 {
186 Q_UNUSED( e )
187 }
188
gestureEvent(QGestureEvent * e)189 bool QgsMapTool::gestureEvent( QGestureEvent *e )
190 {
191 Q_UNUSED( e )
192 return true;
193 }
194
canvas() const195 QgsMapCanvas *QgsMapTool::canvas() const
196 {
197 return mCanvas;
198 }
199
searchRadiusMM()200 double QgsMapTool::searchRadiusMM()
201 {
202 QgsSettings settings;
203 double radius = settings.value( QStringLiteral( "Map/searchRadiusMM" ), Qgis::DEFAULT_SEARCH_RADIUS_MM ).toDouble();
204
205 if ( radius > 0 )
206 {
207 return radius;
208 }
209 return Qgis::DEFAULT_SEARCH_RADIUS_MM;
210 }
211
searchRadiusMU(const QgsRenderContext & context)212 double QgsMapTool::searchRadiusMU( const QgsRenderContext &context )
213 {
214 return searchRadiusMM() * context.scaleFactor() * context.mapToPixel().mapUnitsPerPixel();
215 }
216
searchRadiusMU(QgsMapCanvas * canvas)217 double QgsMapTool::searchRadiusMU( QgsMapCanvas *canvas )
218 {
219 if ( !canvas )
220 {
221 return 0;
222 }
223 QgsMapSettings mapSettings = canvas->mapSettings();
224 QgsRenderContext context = QgsRenderContext::fromMapSettings( mapSettings );
225 return searchRadiusMU( context );
226 }
227
populateContextMenu(QMenu *)228 void QgsMapTool::populateContextMenu( QMenu * )
229 {
230
231 }
232