QGIS API Documentation  2.8.2-Wien
 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  emit activated();
90 }
91 
92 
94 {
95  if ( mAction )
96  mAction->setChecked( false );
97  if ( mButton )
98  mButton->setChecked( false );
99 
100  emit deactivated();
101 }
102 
103 void QgsMapTool::setAction( QAction* action )
104 {
105  if ( mAction )
106  disconnect( mAction, SIGNAL( destroyed() ), this, SLOT( actionDestroyed() ) );
107  mAction = action;
108  connect( mAction, SIGNAL( destroyed() ), this, SLOT( actionDestroyed() ) );
109 }
110 
111 void QgsMapTool::actionDestroyed()
112 {
113  if ( mAction == sender() )
114  mAction = 0;
115 }
116 
118 {
119  return mAction;
120 }
121 
122 void QgsMapTool::setButton( QAbstractButton* button )
123 {
124  mButton = button;
125 }
126 
127 QAbstractButton* QgsMapTool::button()
128 {
129  return mButton;
130 }
131 
132 void QgsMapTool::setCursor( QCursor cursor )
133 {
134  mCursor = cursor;
135 }
136 
137 
138 void QgsMapTool::canvasMoveEvent( QMouseEvent *e )
139 {
140  Q_UNUSED( e );
141 }
142 
144 {
145  Q_UNUSED( e );
146 }
147 
148 void QgsMapTool::canvasPressEvent( QMouseEvent *e )
149 {
150  Q_UNUSED( e );
151 }
152 
153 void QgsMapTool::canvasReleaseEvent( QMouseEvent *e )
154 {
155  Q_UNUSED( e );
156 }
157 
158 void QgsMapTool::wheelEvent( QWheelEvent *e )
159 {
160  Q_UNUSED( e );
161 }
162 
163 void QgsMapTool::keyPressEvent( QKeyEvent *e )
164 {
165  Q_UNUSED( e );
166 }
167 
168 void QgsMapTool::keyReleaseEvent( QKeyEvent *e )
169 {
170  Q_UNUSED( e );
171 }
172 
173 #ifdef HAVE_TOUCH
174 bool QgsMapTool::gestureEvent( QGestureEvent *e )
175 {
176  Q_UNUSED( e );
177  return true;
178 }
179 #endif
180 
182 {
183 }
184 
186 {
187  return false;
188 }
189 
191 {
192  return false;
193 }
194 
196 {
197  return mCanvas;
198 }
199 
201 {
202  QSettings settings;
203  double radius = settings.value( "/Map/searchRadiusMM", QGis::DEFAULT_SEARCH_RADIUS_MM ).toDouble();
204 
205  if ( radius > 0 )
206  {
207  return radius;
208  }
210 }
211 
213 {
214  return searchRadiusMM() * context.scaleFactor() * context.mapToPixel().mapUnitsPerPixel();
215 }
216 
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 }