QGIS API Documentation  2.2.0-Valmiera
 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 <QAction>
22 #include <QAbstractButton>
23 
25  : QObject( canvas ), mCanvas( canvas ), mCursor( Qt::CrossCursor ), mAction( NULL ), mButton( NULL )
26 {
27 }
28 
29 
31 {
32  mCanvas->unsetMapTool( this );
33 }
34 
35 
36 QgsPoint QgsMapTool::toMapCoordinates( const QPoint& point )
37 {
39 }
40 
41 
42 QgsPoint QgsMapTool::toLayerCoordinates( QgsMapLayer* layer, const QPoint& point )
43 {
44  QgsPoint pt = toMapCoordinates( point );
45  return toLayerCoordinates( layer, pt );
46 }
47 
49 {
50  return mCanvas->mapRenderer()->mapToLayerCoordinates( layer, point );
51 }
52 
54 {
55  return mCanvas->mapRenderer()->layerToMapCoordinates( layer, point );
56 }
57 
59 {
60  return mCanvas->mapRenderer()->mapToLayerCoordinates( layer, rect );
61 }
62 
64 {
65  double x = point.x(), y = point.y();
67  return QPoint( qRound( x ), qRound( y ) );
68 }
69 
70 
72 {
73  // make action and/or button active
74  if ( mAction )
75  mAction->setChecked( true );
76  if ( mButton )
77  mButton->setChecked( true );
78 
79  // set cursor (map tools usually set it in constructor)
80  mCanvas->setCursor( mCursor );
81  QgsDebugMsg( "Cursor has been set" );
82 }
83 
84 
86 {
87  if ( mAction )
88  mAction->setChecked( false );
89  if ( mButton )
90  mButton->setChecked( false );
91 }
92 
93 void QgsMapTool::setAction( QAction* action )
94 {
95  mAction = action;
96 }
97 
99 {
100  return mAction;
101 }
102 
103 void QgsMapTool::setButton( QAbstractButton* button )
104 {
105  mButton = button;
106 }
107 
108 QAbstractButton* QgsMapTool::button()
109 {
110  return mButton;
111 }
112 
113 void QgsMapTool::setCursor( QCursor cursor )
114 {
115  mCursor = cursor;
116 }
117 
118 
119 void QgsMapTool::canvasMoveEvent( QMouseEvent *e )
120 {
121  Q_UNUSED( e );
122 }
123 
125 {
126  Q_UNUSED( e );
127 }
128 
129 void QgsMapTool::canvasPressEvent( QMouseEvent *e )
130 {
131  Q_UNUSED( e );
132 }
133 
134 void QgsMapTool::canvasReleaseEvent( QMouseEvent *e )
135 {
136  Q_UNUSED( e );
137 }
138 
139 void QgsMapTool::wheelEvent( QWheelEvent *e )
140 {
141  Q_UNUSED( e );
142 }
143 
144 void QgsMapTool::keyPressEvent( QKeyEvent *e )
145 {
146  Q_UNUSED( e );
147 }
148 
149 void QgsMapTool::keyReleaseEvent( QKeyEvent *e )
150 {
151  Q_UNUSED( e );
152 }
153 
154 #ifdef HAVE_TOUCH
155 bool QgsMapTool::gestureEvent( QGestureEvent *e )
156 {
157  Q_UNUSED( e );
158  return true;
159 }
160 #endif
161 
163 {
164 }
165 
167 {
168  return false;
169 }
170 
172 {
173  return false;
174 }
175 
177 {
178  return mCanvas;
179 }
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:45
QgsPoint toMapCoordinates(const QPoint &point)
transformation from screen coordinates to map coordinates
Definition: qgsmaptool.cpp:36
QAbstractButton * button()
Return associated button with map tool or NULL if no button is associated.
Definition: qgsmaptool.cpp:108
#define QgsDebugMsg(str)
Definition: qgslogger.h:36
virtual void canvasMoveEvent(QMouseEvent *e)
Mouse move event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:119
QAbstractButton * mButton
optionally map tool can have pointer to a button which will be used to set that action as active ...
Definition: qgsmaptool.h:158
virtual void canvasDoubleClickEvent(QMouseEvent *e)
Mouse double click event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:124
QgsPoint mapToLayerCoordinates(QgsMapLayer *theLayer, QgsPoint point)
transform point coordinates from output CRS to layer's CRS
virtual void canvasPressEvent(QMouseEvent *e)
Mouse press event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:129
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:99
double x() const
Definition: qgspoint.h:110
virtual void renderComplete()
Called when rendering has finished. Default implementation does nothing.
Definition: qgsmaptool.cpp:162
QgsPoint layerToMapCoordinates(QgsMapLayer *theLayer, QgsPoint point)
transform point coordinates from layer's CRS to output CRS
void setButton(QAbstractButton *button)
Use this to associate a button to this maptool.
Definition: qgsmaptool.cpp:103
virtual void activate()
called when set as currently active map tool
Definition: qgsmaptool.cpp:71
QgsMapCanvas * mCanvas
pointer to map canvas
Definition: qgsmaptool.h:147
virtual void keyReleaseEvent(QKeyEvent *e)
Key event for overriding.
Definition: qgsmaptool.cpp:149
QCursor mCursor
cursor used in map tool
Definition: qgsmaptool.h:150
void transformInPlace(double &x, double &y) const
QgsMapCanvas * canvas()
returns pointer to the tool's map canvas
Definition: qgsmaptool.cpp:176
void setAction(QAction *action)
Use this to associate a QAction to this maptool.
Definition: qgsmaptool.cpp:93
QgsPoint toLayerCoordinates(QgsMapLayer *layer, const QPoint &point)
transformation from screen coordinates to layer's coordinates
Definition: qgsmaptool.cpp:42
virtual void deactivate()
called when map tool is being deactivated
Definition: qgsmaptool.cpp:85
virtual bool isEditTool()
Check whether this MapTool performs an edit operation.
Definition: qgsmaptool.cpp:171
virtual void wheelEvent(QWheelEvent *e)
Mouse wheel event for overriding.
Definition: qgsmaptool.cpp:139
A class to represent a point geometry.
Definition: qgspoint.h:63
QAction * action()
Return associated action with map tool or NULL if no action is associated.
Definition: qgsmaptool.cpp:98
virtual void keyPressEvent(QKeyEvent *e)
Key event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:144
QgsPoint toMapCoordinates(int x, int y) const
virtual void canvasReleaseEvent(QMouseEvent *e)
Mouse release event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:134
QgsMapRenderer * mapRenderer()
virtual ~QgsMapTool()
virtual destructor
Definition: qgsmaptool.cpp:30
QPoint toCanvasCoordinates(const QgsPoint &point)
transformation from map coordinates to screen coordinates
Definition: qgsmaptool.cpp:63
QAction * mAction
optionally map tool can have pointer to action which will be used to set that action as active ...
Definition: qgsmaptool.h:154
const QgsMapToPixel * getCoordinateTransform()
Get the current coordinate transform.
virtual bool isTransient()
Check whether this MapTool performs a zoom or pan operation.
Definition: qgsmaptool.cpp:166
double y() const
Definition: qgspoint.h:118
virtual void setCursor(QCursor cursor)
Set a user defined cursor.
Definition: qgsmaptool.cpp:113
QgsMapTool(QgsMapCanvas *canvas)
constructor takes map canvas as a parameter
Definition: qgsmaptool.cpp:24