/
qgsmaptool.cpp
152 lines (121 loc) · 3.37 KB
/
qgsmaptool.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/***************************************************************************
qgsmaptool.cpp - base class for map canvas tools
----------------------
begin : January 2006
copyright : (C) 2006 by Martin Dobias
email : wonder.sk at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
/* $Id$ */
#include "qgslogger.h"
#include "qgsmaptool.h"
#include "qgsmapcanvas.h"
#include "qgsmaptopixel.h"
#include "qgsmaprenderer.h"
#include <QAction>
#include <QAbstractButton>
QgsMapTool::QgsMapTool(QgsMapCanvas* canvas)
: QObject(canvas), mCanvas(canvas), mCursor(Qt::CrossCursor), mAction(NULL), mButton(NULL)
{
}
QgsMapTool::~QgsMapTool()
{
mCanvas->unsetMapTool(this);
}
QgsPoint QgsMapTool::toMapCoords(const QPoint& point)
{
return mCanvas->getCoordinateTransform()->toMapCoordinates(point);
}
QgsPoint QgsMapTool::toLayerCoords(QgsMapLayer* layer, const QPoint& point)
{
QgsPoint pt = toMapCoords(point);
return toLayerCoords(layer, pt);
}
QgsPoint QgsMapTool::toLayerCoords(QgsMapLayer* layer, const QgsPoint& point)
{
return mCanvas->mapRenderer()->outputCoordsToLayerCoords(layer, point);
}
QgsPoint QgsMapTool::toMapCoords(QgsMapLayer* layer, const QgsPoint& point)
{
return mCanvas->mapRenderer()->layerCoordsToOutputCoords(layer, point);
}
QgsRect QgsMapTool::toLayerCoords(QgsMapLayer* layer, const QgsRect& rect)
{
return mCanvas->mapRenderer()->outputCoordsToLayerCoords(layer, rect);
}
QPoint QgsMapTool::toCanvasCoords(const QgsPoint& point)
{
double x = point.x(), y = point.y();
mCanvas->getCoordinateTransform()->transformInPlace(x,y);
return QPoint((int)(x+0.5), (int)(y+0.5)); // round the values
}
void QgsMapTool::activate()
{
// make action and/or button active
if (mAction)
mAction->setChecked(true);
if (mButton)
mButton->setChecked(true);
// set cursor (map tools usually set it in constructor)
mCanvas->setCursor(mCursor);
QgsDebugMsg("Cursor has been set");
}
void QgsMapTool::deactivate()
{
if (mAction)
mAction->setChecked(false);
if (mButton)
mButton->setChecked(false);
}
void QgsMapTool::setAction(QAction* action)
{
mAction = action;
}
QAction* QgsMapTool::action()
{
return mAction;
}
void QgsMapTool::setButton(QAbstractButton* button)
{
mButton = button;
}
QAbstractButton* QgsMapTool::button()
{
return mButton;
}
void QgsMapTool::canvasMoveEvent(QMouseEvent *)
{
}
void QgsMapTool::canvasDoubleClickEvent(QMouseEvent *)
{
}
void QgsMapTool::canvasPressEvent(QMouseEvent *)
{
}
void QgsMapTool::canvasReleaseEvent(QMouseEvent *)
{
}
void QgsMapTool::keyPressEvent(QKeyEvent* e)
{
}
void QgsMapTool::renderComplete()
{
}
bool QgsMapTool::isZoomTool()
{
return false;
}
bool QgsMapTool::isEditTool()
{
return false;
}
QgsMapCanvas* QgsMapTool::canvas()
{
return mCanvas;
}