-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapwidget.cpp
278 lines (244 loc) · 8.5 KB
/
mapwidget.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include "mapwidget.h"
MapWidget::MapWidget(QWidget *parent) :
QWidget(parent),
m_center(55.755831, 37.617673),
m_zoom(10),
m_online(true),
m_lockWheel(false)
{
m_tiles = new TilesMap(m_center, m_zoom, m_online, this);
connect(m_tiles, SIGNAL(updated(QRect)), SLOT(updateMap(QRect)));
connect(m_tiles, SIGNAL(tilesLoading(int)), SIGNAL(tilesLoading(int)));
connect(m_tiles, SIGNAL(zoomChanged(int)), SIGNAL(zoomChanged(int)));
m_overlays = new Overlays(m_center, m_zoom, this);
m_pointDialog = new PointDialog(this);
connect(m_pointDialog, SIGNAL(accepted()), SLOT(pointDialogAccepted()));
QShortcut *sc;
sc = new QShortcut(QKeySequence("Left"), this);
connect(sc, SIGNAL(activated()), SLOT(panLeft()));
sc = new QShortcut(QKeySequence("Right"), this);
connect(sc, SIGNAL(activated()), SLOT(panRight()));
sc = new QShortcut(QKeySequence("Down"), this);
connect(sc, SIGNAL(activated()), SLOT(panDown()));
sc = new QShortcut(QKeySequence("Up"), this);
connect(sc, SIGNAL(activated()), SLOT(panUp()));
sc = new QShortcut(QKeySequence("PgUp"), this);
connect(sc, SIGNAL(activated()), SLOT(zoomIn()));
sc = new QShortcut(QKeySequence("PgDown"), this);
connect(sc, SIGNAL(activated()), SLOT(zoomOut()));
sc = new QShortcut(QKeySequence("Escape"), this);
connect(sc, SIGNAL(activated()), SLOT(hideAll()));
sc = new QShortcut(QKeySequence("Shift+Insert"), this);
connect(sc, SIGNAL(activated()), SLOT(openAddPointDialog()));
sc = new QShortcut(QKeySequence("E"), this);
connect(sc, SIGNAL(activated()), SLOT(openEditPointDialog()));
sc = new QShortcut(QKeySequence("Delete"), this);
connect(sc, SIGNAL(activated()), SLOT(openDeletePointDialog()));
sc = new QShortcut(QKeySequence("Alt+I"), this);
connect(sc, SIGNAL(activated()), SLOT(switchOnline()));
sc = new QShortcut(QKeySequence("Alt+O, Alt+O"), this);
connect(sc, SIGNAL(activated()), SLOT(openInOSM()));
sc = new QShortcut(QKeySequence("Alt+O, Alt+G"), this);
connect(sc, SIGNAL(activated()), SLOT(openInGoogleMaps()));
sc = new QShortcut(QKeySequence("Alt+O, Alt+Y"), this);
connect(sc, SIGNAL(activated()), SLOT(openInYandexMaps()));
}
void MapWidget::resizeEvent(QResizeEvent *event)
{
m_tiles->resize(size());
m_overlays->resize(size());
}
void MapWidget::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
m_pressPos = m_movePos = event->pos();
}
else if (event->button() == Qt::RightButton) {
QPoint delta = event->pos() - QPoint(width() / 2, height() / 2);
LatLon coord = pointToLatLon(latLonToPoint(m_center) + delta);
QLineEdit *edit;
edit = m_pointDialog->findChild<QLineEdit *>("latitudeEdit");
edit->setText(QString().setNum(coord.lat(), 'f', 6));
edit = m_pointDialog->findChild<QLineEdit *>("longitudeEdit");
edit->setText(QString().setNum(coord.lon(), 'f', 6));
edit = m_pointDialog->findChild<QLineEdit *>("nameEdit");
edit->setText("");
edit->setFocus();
m_pointDialog->setWindowTitle("Add Point");
m_pointDialog->open();
}
}
void MapWidget::mouseMoveEvent(QMouseEvent *event)
{
QPoint delta = event->pos() - m_movePos;
m_movePos = event->pos();
QPointF ct = latLonToPoint(m_center) - QPointF(delta);
m_center = pointToLatLon(ct);
m_tiles->setCenter(m_center);
m_overlays->setCenter(m_center);
update();
}
void MapWidget::mouseReleaseEvent(QMouseEvent *event)
{
if (event->pos() == m_pressPos) {
m_overlays->deselect();
}
update();
m_tiles->loadTiles();
}
void MapWidget::wheelEvent(QWheelEvent *event)
{
if (m_lockWheel) {
return;
}
int newZoom = m_tiles->setZoom(m_zoom + event->delta() / 120);
if (newZoom != m_zoom) {
QPoint delta = event->pos() - QPoint(width() / 2, height() / 2);
LatLon pos = pointToLatLon(latLonToPoint(m_center) + delta);
m_zoom = newZoom;
m_center = pointToLatLon(latLonToPoint(pos) - delta);
m_tiles->setCenter(m_center);
m_tiles->loadTiles();
m_overlays->setZoom(m_zoom);
m_overlays->setCenter(m_center);
update();
}
m_lockWheel = true;
QTimer::singleShot(100, this, SLOT(unlockWheel()));
}
void MapWidget::zoomIn()
{
int newZoom = m_tiles->setZoom(m_zoom + 1);
if (newZoom != m_zoom) {
m_zoom = newZoom;
m_tiles->loadTiles();
m_overlays->setZoom(m_zoom);
update();
}
}
void MapWidget::zoomOut()
{
int newZoom = m_tiles->setZoom(m_zoom - 1);
if (newZoom != m_zoom) {
m_zoom = newZoom;
m_tiles->loadTiles();
m_overlays->setZoom(m_zoom);
update();
}
}
void MapWidget::hideAll()
{
m_overlays->deselect();
}
void MapWidget::openAddPointDialog()
{
QClipboard *clipboard = QApplication::clipboard();
LatLon coord(clipboard->text());
QLineEdit *edit;
edit = m_pointDialog->findChild<QLineEdit *>("latitudeEdit");
edit->setText(QString().setNum(coord.lat(), 'f', 6));
edit = m_pointDialog->findChild<QLineEdit *>("longitudeEdit");
edit->setText(QString().setNum(coord.lon(), 'f', 6));
edit = m_pointDialog->findChild<QLineEdit *>("nameEdit");
edit->setText("");
edit->setFocus();
m_pointDialog->setWindowTitle("Add Point");
m_pointDialog->open();
}
void MapWidget::openEditPointDialog()
{
if (Point *p = m_overlays->selectedPoint()) {
QLineEdit *edit;
edit = m_pointDialog->findChild<QLineEdit *>("latitudeEdit");
edit->setText(QString().setNum(p->coord().lat(), 'f', 6));
edit = m_pointDialog->findChild<QLineEdit *>("longitudeEdit");
edit->setText(QString().setNum(p->coord().lon(), 'f', 6));
edit = m_pointDialog->findChild<QLineEdit *>("nameEdit");
edit->setText(p->label());
edit->selectAll();
edit->setFocus();
m_pointDialog->setWindowTitle("Edit Point");
m_pointDialog->open();
}
}
void MapWidget::openDeletePointDialog()
{
if (Point *p = m_overlays->selectedPoint()) {
if (QMessageBox::Ok == QMessageBox::warning(
this,
"Delete Point",
QString("Delete point \"%1\"?").arg(p->label()),
QMessageBox::Ok | QMessageBox::Cancel)) {
m_overlays->deleteSelectedPoint();
}
}
}
void MapWidget::pointDialogAccepted()
{
QLineEdit *edit;
edit = m_pointDialog->findChild<QLineEdit *>("latitudeEdit");
qreal lat = edit->text().toDouble();
edit = m_pointDialog->findChild<QLineEdit *>("longitudeEdit");
qreal lon = edit->text().toDouble();
edit = m_pointDialog->findChild<QLineEdit *>("nameEdit");
QString label = edit->text();
LatLon coord(lat, lon);
if (m_overlays->selectedPoint()) {
m_overlays->updateSelectedPoint(coord, label);
}
else {
m_overlays->addPoint(coord, label);
}
}
void MapWidget::switchOnline()
{
m_online = !m_online;
m_tiles->setOnline(m_online);
emit onlineSwitched(m_online);
}
void MapWidget::openInOSM()
{
QDesktopServices::openUrl(QUrl(
QString("http://www.openstreetmap.org/?lat=%1&lon=%2&zoom=%3&layers=M")
.arg(m_center.lat())
.arg(m_center.lon())
.arg(m_zoom)));
}
void MapWidget::openInGoogleMaps()
{
QDesktopServices::openUrl(QUrl(
QString("http://maps.google.com/?ll=%1,%2&z=%3&t=m")
.arg(m_center.lat())
.arg(m_center.lon())
.arg(m_zoom)));
}
void MapWidget::openInYandexMaps()
{
QDesktopServices::openUrl(QUrl(
QString("http://maps.yandex.ru/?ll=%2,%1&z=%3&l=map")
.arg(m_center.lat())
.arg(m_center.lon())
.arg(m_zoom)));
}
void MapWidget::pan(const QPoint &delta)
{
m_center = pointToLatLon(latLonToPoint(m_center) + delta);
m_tiles->setCenter(m_center);
m_tiles->loadTiles();
m_overlays->setCenter(m_center);
update();
}
QPointF MapWidget::latLonToPoint(LatLon latlon)
{
qreal x = (latlon.lon() + 180.0) / 360.0;
qreal y = (M_PI - log(tan(M_PI / 4 + latlon.lat() * (M_PI / 180.0) / 2))) / (2 * M_PI);
qreal z = static_cast<qreal>(1 << m_zoom);
return QPointF(x * z * 256, y * z * 256);
}
LatLon MapWidget::pointToLatLon(QPointF point)
{
qreal z = static_cast<qreal>(1 << m_zoom);
qreal lat = (2 * atan(exp((0.5 - point.y() / z / 256) * 2 * M_PI)) - M_PI / 2) * 180 / M_PI;
qreal lon = point.x() / z / 256 * 360.0 - 180.0;
return LatLon(lat, lon);
}