QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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( NULL )
30  , mButton( NULL )
31  , mToolName( QString() )
32 {
33 }
34 
35 
37 {
38  mCanvas->unsetMapTool( this );
39 }
40 
41 
42 QgsPoint QgsMapTool::toMapCoordinates( const QPoint& point )
43 {
45 }
46 
47 
48 QgsPoint QgsMapTool::toLayerCoordinates( QgsMapLayer* layer, const QPoint& point )
49 {
50  QgsPoint pt = toMapCoordinates( point );
51  return toLayerCoordinates( layer, pt );
52 }
53 
55 {
56  return mCanvas->mapSettings().mapToLayerCoordinates( layer, point );
57 }
58 
60 {
61  return mCanvas->mapSettings().layerToMapCoordinates( layer, point );
62 }
63 
65 {
66  return mCanvas->mapSettings().mapToLayerCoordinates( layer, rect );
67 }
68 
70 {
71  double x = point.x(), y = point.y();
73  return QPoint( qRound( x ), qRound( y ) );
74 }
75 
76 
78 {
79  // make action and/or button active
80  if ( mAction )
81  mAction->setChecked( true );
82  if ( mButton )
83  mButton->setChecked( true );
84 
85  // set cursor (map tools usually set it in constructor)
86  mCanvas->setCursor( mCursor );
87  QgsDebugMsg( "Cursor has been set" );
88 }
89 
90 
92 {
93  if ( mAction )
94  mAction->setChecked( false );
95  if ( mButton )
96  mButton->setChecked( false );
97 }
98 
99 void QgsMapTool::setAction( QAction* action )
100 {
101  if ( mAction )
102  disconnect( mAction, SIGNAL( destroyed() ), this, SLOT( actionDestroyed() ) );
103  mAction = action;
104  connect( mAction, SIGNAL( destroyed() ), this, SLOT( actionDestroyed() ) );
105 }
106 
108 {
109  if ( mAction == sender() )
110  mAction = 0;
111 }
112 
114 {
115  return mAction;
116 }
117 
118 void QgsMapTool::setButton( QAbstractButton* button )
119 {
120  mButton = button;
121 }
122 
123 QAbstractButton* QgsMapTool::button()
124 {
125  return mButton;
126 }
127 
128 void QgsMapTool::setCursor( QCursor cursor )
129 {
130  mCursor = cursor;
131 }
132 
133 
134 void QgsMapTool::canvasMoveEvent( QMouseEvent *e )
135 {
136  Q_UNUSED( e );
137 }
138 
140 {
141  Q_UNUSED( e );
142 }
143 
144 void QgsMapTool::canvasPressEvent( QMouseEvent *e )
145 {
146  Q_UNUSED( e );
147 }
148 
149 void QgsMapTool::canvasReleaseEvent( QMouseEvent *e )
150 {
151  Q_UNUSED( e );
152 }
153 
154 void QgsMapTool::wheelEvent( QWheelEvent *e )
155 {
156  Q_UNUSED( e );
157 }
158 
159 void QgsMapTool::keyPressEvent( QKeyEvent *e )
160 {
161  Q_UNUSED( e );
162 }
163 
164 void QgsMapTool::keyReleaseEvent( QKeyEvent *e )
165 {
166  Q_UNUSED( e );
167 }
168 
169 #ifdef HAVE_TOUCH
170 bool QgsMapTool::gestureEvent( QGestureEvent *e )
171 {
172  Q_UNUSED( e );
173  return true;
174 }
175 #endif
176 
178 {
179 }
180 
182 {
183  return false;
184 }
185 
187 {
188  return false;
189 }
190 
192 {
193  return mCanvas;
194 }
195 
197 {
198  QSettings settings;
199  double radius = settings.value( "/Map/searchRadiusMM", QGis::DEFAULT_SEARCH_RADIUS_MM ).toDouble();
200 
201  if ( radius > 0 )
202  {
203  return radius;
204  }
206 }
207 
209 {
210  return searchRadiusMM() * context.scaleFactor() * context.mapToPixel().mapUnitsPerPixel();
211 }
212 
214 {
215  if ( !canvas )
216  {
217  return 0;
218  }
219  QgsMapSettings mapSettings = canvas->mapSettings();
220  QgsRenderContext context = QgsRenderContext::fromMapSettings( mapSettings );
221  return searchRadiusMU( context );
222 }
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:48
QgsPoint layerToMapCoordinates(QgsMapLayer *theLayer, QgsPoint point) const
transform point coordinates from layer's CRS to output CRS
static double searchRadiusMU(const QgsRenderContext &context)
Get search radius in map units for given context.
Definition: qgsmaptool.cpp:208
QgsPoint toMapCoordinates(const QPoint &point)
transformation from screen coordinates to map coordinates
Definition: qgsmaptool.cpp:42
QAbstractButton * button()
Return associated button with map tool or NULL if no button is associated.
Definition: qgsmaptool.cpp:123
#define QgsDebugMsg(str)
Definition: qgslogger.h:36
virtual void canvasMoveEvent(QMouseEvent *e)
Mouse move event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:134
QAbstractButton * mButton
optionally map tool can have pointer to a button which will be used to set that action as active ...
Definition: qgsmaptool.h:195
const QgsMapSettings & mapSettings() const
Get access to properties used for map rendering.
double scaleFactor() const
virtual void canvasDoubleClickEvent(QMouseEvent *e)
Mouse double click event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:139
virtual void canvasPressEvent(QMouseEvent *e)
Mouse press event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:144
QgsPoint mapToLayerCoordinates(QgsMapLayer *theLayer, QgsPoint point) const
transform point coordinates from output CRS to layer's CRS
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:104
double x() const
Definition: qgspoint.h:110
virtual Q_DECL_DEPRECATED void renderComplete()
Called when rendering has finished.
Definition: qgsmaptool.cpp:177
void setButton(QAbstractButton *button)
Use this to associate a button to this maptool.
Definition: qgsmaptool.cpp:118
The QgsMapSettings class contains configuration for rendering of the map.
virtual void activate()
called when set as currently active map tool
Definition: qgsmaptool.cpp:77
QgsMapCanvas * mCanvas
pointer to map canvas
Definition: qgsmaptool.h:184
virtual void keyReleaseEvent(QKeyEvent *e)
Key event for overriding.
Definition: qgsmaptool.cpp:164
QCursor mCursor
cursor used in map tool
Definition: qgsmaptool.h:187
void transformInPlace(double &x, double &y) const
QgsMapCanvas * canvas()
returns pointer to the tool's map canvas
Definition: qgsmaptool.cpp:191
void actionDestroyed()
clear pointer when action is destroyed
Definition: qgsmaptool.cpp:107
void setAction(QAction *action)
Use this to associate a QAction to this maptool.
Definition: qgsmaptool.cpp:99
QgsPoint toLayerCoordinates(QgsMapLayer *layer, const QPoint &point)
transformation from screen coordinates to layer's coordinates
Definition: qgsmaptool.cpp:48
double mapUnitsPerPixel() const
Return current map units per pixel.
virtual void deactivate()
called when map tool is being deactivated
Definition: qgsmaptool.cpp:91
virtual bool isEditTool()
Check whether this MapTool performs an edit operation.
Definition: qgsmaptool.cpp:186
virtual void wheelEvent(QWheelEvent *e)
Mouse wheel event for overriding.
Definition: qgsmaptool.cpp:154
A class to represent a point geometry.
Definition: qgspoint.h:63
static const double DEFAULT_SEARCH_RADIUS_MM
Identify search radius in mm.
Definition: qgis.h:273
QAction * action()
Return associated action with map tool or NULL if no action is associated.
Definition: qgsmaptool.cpp:113
virtual void keyPressEvent(QKeyEvent *e)
Key event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:159
QgsPoint toMapCoordinates(int x, int y) const
Contains information about the context of a rendering operation.
virtual void canvasReleaseEvent(QMouseEvent *e)
Mouse release 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:36
QPoint toCanvasCoordinates(const QgsPoint &point)
transformation from map coordinates to screen coordinates
Definition: qgsmaptool.cpp:69
QAction * mAction
optionally map tool can have pointer to action which will be used to set that action as active ...
Definition: qgsmaptool.h:191
const QgsMapToPixel * getCoordinateTransform()
Get the current coordinate transform.
const QgsMapToPixel & mapToPixel() const
virtual bool isTransient()
Check whether this MapTool performs a zoom or pan operation.
Definition: qgsmaptool.cpp:181
double y() const
Definition: qgspoint.h:118
virtual void setCursor(QCursor cursor)
Set a user defined cursor.
Definition: qgsmaptool.cpp:128
QgsMapTool(QgsMapCanvas *canvas)
constructor takes map canvas as a parameter
Definition: qgsmaptool.cpp:25
static double searchRadiusMM()
Get search radius in mm.
Definition: qgsmaptool.cpp:196