-
Notifications
You must be signed in to change notification settings - Fork 3
/
map.cpp
414 lines (372 loc) · 13.9 KB
/
map.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/************************************************************************************
* QSlippyMap - Tile based slippery map *
* Copyright (C) 2017 Michael Carpenter (malcom2073@gmail.com) *
* *
* This file is a part of QSlippyMap *
* *
* QSlippyMap is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation, version *
* 2.1 of the License. *
* *
* QSlippyMap is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this program; if not, write to the Free Software *
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
************************************************************************************/
#include "map.h"
#define M_PI 3.14159265358979323846
#include "functions.h"
#include <QMouseEvent>
#include <QPen>
#include <QStandardPaths>
#include <QDir>
Map::Map(QWidget *parent) : QGraphicsView(parent)
{
m_currentPosition=0;
m_scene = new QGraphicsScene(this);
this->setScene(m_scene);
//setDragMode(ScrollHandDrag);
m_tileCache = new TileCache();
connect(m_tileCache,SIGNAL(tileRecv(int,int,int,QImage)),this,SLOT(tileRecv(int,int,int,QImage)));
connect(m_tileCache,SIGNAL(localTileUpdate(int)),this,SIGNAL(localTileUpdate(int)));
connect(m_tileCache,SIGNAL(networkTileUpdate(int)),this,SIGNAL(networkTileUpdate(int)));
//setAlignment(Qt::NoAlignment);
//double lat = 39.359194;
//double lon = -75.069349;
//double lat = 36.561288;
//double lon = -79.207576;
double lat = 39.155955;
double lon = -76.535755;
setCenter(lat,lon,18);
m_mouseIsDown = false;
this->setTransformationAnchor(QGraphicsView::NoAnchor);
//this->setResizeAnchor(QGraphicsView::AnchorViewCenter);
this->setHorizontalScrollBarPolicy ( Qt::ScrollBarAlwaysOff );
this->setVerticalScrollBarPolicy ( Qt::ScrollBarAlwaysOff );
setMouseTracking(true);
}
void Map::SetGoogle()
{
m_tileCache->SetGoogle();
int tilex = m_currentTileCoords.x();
int tiley = m_currentTileCoords.y();
for (int x=-5;x<5;x++)
{
for (int y=-5;y<5;y++)
{
m_tileCache->getTile(tilex+x,tiley+y,m_zoomLevel);
}
}
}
void Map::SetMapBox()
{
m_tileCache->SetMapbox();
int tilex = m_currentTileCoords.x();
int tiley = m_currentTileCoords.y();
for (int x=-5;x<5;x++)
{
for (int y=-5;y<5;y++)
{
m_tileCache->getTile(tilex+x,tiley+y,m_zoomLevel);
}
}
}
void Map::setCenter(double lat, double lon,int zoom)
{
if (zoom != m_zoomLevel)
{
m_tileCache->zoomLevelChanged();
}
m_zoomLevel = zoom;
m_currentLatLon = QPointF(lon,lat);
m_scene->clear();
m_currentPosition = 0;
int tilex = long2tilex(lon,m_zoomLevel);
int tiley = lat2tiley(lat,m_zoomLevel);
m_scene->setSceneRect((tilex-5) * 256,(tiley-5)*256,10*256,10*256);
//QPointF whmap = mapToScene(width(),height());
//this->setSceneRect((tilex-5) * 256,(tiley-5)*256,10*256,10*256);
this->centerOn(lat,lon);
qDebug() << m_scene->sceneRect();
qDebug() << this->sceneRect();
//m_scene->setSceneRect((tilex-5) * 256,(tiley-5)*256,whmap.x(),whmap.y());
//m_scene->setSceneRect();
m_currentTileCoords.setX(tilex);
m_currentTileCoords.setY(tiley);
for (int x=-5;x<5;x++)
{
for (int y=-5;y<5;y++)
{
m_tileCache->getTile(tilex+x,tiley+y,m_zoomLevel);
/*if (m_tileCache->contains(tilex+x,tiley+y,m_zoomLevel))
{
m_tileCache->getTile(tilex+x,tiley+y,m_zoomLevel);
}
else
{
QNetworkRequest req;
QString url = "https://mt0.google.com/vt/";
QString loc = "lyrs=s&x=%1&y=%2&z=%3";
req.setUrl(url + loc.arg(tilex+x).arg(tiley+y).arg(m_zoomLevel));
QNetworkReply *reply = m_nam->get(req);
m_tileIdList[reply] = QPair<QPair<int,int> , int>(QPair<int,int>(tilex+x,tiley+y),m_zoomLevel);
connect(reply,SIGNAL(finished()),this,SLOT(networkFinished()));
emit tileUpdates(m_tileIdList.keys().count());
}*/
}
}
QGraphicsLineItem *item = new QGraphicsLineItem();
m_scene->addItem(item);
m_cursorCircle = new QGraphicsEllipseItem();
m_scene->addItem(m_cursorCircle);
m_cursorCircle->setZValue(1);
QPen pen = m_cursorCircle->pen();
pen.setWidth(5);
m_cursorCircle->setPen(pen);
for (int i=0;i<m_waypoints.size();i++)
{
QPointF scenecoords = mapToSceneCoords(QPointF(m_waypoints.at(i).first,m_waypoints.at(i).second));
if (i == 0)
{
m_lastLinePoint = scenecoords;
}
QGraphicsEllipseItem *wpitem = new QGraphicsEllipseItem(scenecoords.x()-2,scenecoords.y()-2,4,4);
QPen pen = wpitem->pen();
pen.setWidth(2);
wpitem->setPen(pen);
m_scene->addItem(wpitem);
wpitem->setZValue(2);
QGraphicsLineItem *wpline = new QGraphicsLineItem(m_lastLinePoint.x(),m_lastLinePoint.y(),scenecoords.x(),scenecoords.y());
pen.setColor(QColor::fromRgb(0,0,0));
wpline->setPen(pen);
m_scene->addItem(wpline);
wpline->setZValue(1);
m_lastLinePoint.setX(scenecoords.x());
m_lastLinePoint.setY(scenecoords.y());
}
}
void Map::setCurrentPosition(double lat, double lon)
{
QPointF scenecoords = mapToSceneCoords(QPointF(lon,lat));
if (!m_currentPosition)
{
m_currentPosition = new QGraphicsEllipseItem(scenecoords.x()-3,scenecoords.y()-3,6,6);
QPen pen = m_currentPosition->pen();
pen.setColor(QColor::fromRgb(0,255,0));
pen.setWidth(2);
m_currentPosition->setPen(pen);
m_currentPosition->setZValue(3);
m_scene->addItem(m_currentPosition);
return;
}
m_currentPosition->setRect(scenecoords.x()-3,scenecoords.y()-3,6,6);
}
void Map::centerOn(double lat,double lon)
{
QPointF scenecoords = mapToSceneCoords(QPointF(lon,lat));
QGraphicsView::centerOn(scenecoords);
}
void Map::addWaypoint(double lat,double lon,double accel)
{
QPointF scenecoords = mapToSceneCoords(QPointF(lon,lat));
QGraphicsEllipseItem *wpitem = new QGraphicsEllipseItem(scenecoords.x()-2,scenecoords.y()-2,4,4);
QPen pen = wpitem->pen();
if (accel > 0)
{
pen.setColor(QColor::fromRgb(0,((accel) / 0.15) * 255 ,0));
}
else
{
pen.setColor(QColor::fromRgb(((accel + 0.20) / 0.15) * 255 ,0,0));
}
pen.setWidth(2);
wpitem->setPen(pen);
m_scene->addItem(wpitem);
wpitem->setZValue(2);
QGraphicsLineItem *wpline = new QGraphicsLineItem(m_lastLinePoint.x(),m_lastLinePoint.y(),scenecoords.x(),scenecoords.y());
m_waypoints.append(QPair<double,double>(lon,lat));
pen.setColor(QColor::fromRgb(0,0,0));
wpline->setPen(pen);
m_scene->addItem(wpline);
wpline->setZValue(1);
m_lastLinePoint.setX(scenecoords.x());
m_lastLinePoint.setY(scenecoords.y());
}
QPointF Map::sceneToMapCoords(QPointF scenecoords)
{
//Convert scene coords to lat/lon
double currtilex = ((int)(scenecoords.x()/256)) * 256;
double nexttilex = ((int)(scenecoords.x()/256)+1) * 256;
double currtiley = ((int)(scenecoords.y()/256)) * 256;
double nexttiley = ((int)(scenecoords.y()/256)+1) * 256;
double lat = tiley2lat(scenecoords.y()/256,m_zoomLevel);
double lon = tilex2long(scenecoords.x()/256,m_zoomLevel);
double latnext = tiley2lat((scenecoords.y()/256)+1,m_zoomLevel);
double lonnext = tilex2long((scenecoords.x()/256)+1,m_zoomLevel);
double newlat = lat + ((latnext - lat) * ((scenecoords.y() - currtiley) / (nexttiley - currtiley)));
double newlon = lon + ((lonnext - lon) * ((scenecoords.x() - currtilex) / (nexttilex - currtilex)));
return QPointF(newlon,newlat);
}
QPointF Map::mapToSceneCoords(QPointF latlon)
{
//Convert lat/lon to scene coords
double lat = latlon.y();
double lon = latlon.x();
int currtiley = lat2tiley(lat,m_zoomLevel);
int currtilex = long2tilex(lat,m_zoomLevel);
double currlat = tiley2lat(currtiley,m_zoomLevel);
double currlon = tilex2long(currtilex,m_zoomLevel);
double nextlat = tiley2lat(currtiley+1,m_zoomLevel);
double nextlon = tilex2long(currtilex+1,m_zoomLevel);
double sceney = (currtiley * 256) + (256 * ((lat-currlat) / (nextlat - currlat)));
double scenex = (currtilex * 256) + (256 * ((lon-currlon) / (nextlon - currlon)));
return QPointF(scenex,sceney);
}
void Map::mouseMoveEvent(QMouseEvent *evt)
{
QPointF scenecoords = mapToScene(evt->x(),evt->y());
QPointF realcentercoords = mapToScene(width()/2,height()/2);
QPointF realcenterlatlon = sceneToMapCoords(realcentercoords);
QPointF latlon = sceneToMapCoords(scenecoords);
emit mouseMoved(latlon.y(),latlon.x());
QPointF newscene = mapToSceneCoords(latlon);
m_cursorCircle->setRect(newscene.x()-10,newscene.y()-10,20,20);
if (m_mouseIsDown)
{
//Recalculate which tiles should be shown, and possibly remove old ones?
/*int tilex = long2tilex(latlon.x(),m_zoomLevel);
int tiley = lat2tiley(latlon.y(),m_zoomLevel);
for (int x=-5;x<5;x++)
{
for (int y=-5;y<5;y++)
{
m_tileCache->getTile(tilex+x,tiley+y,m_zoomLevel);
}
}*/
int tilex = long2tilex(realcenterlatlon.x(),m_zoomLevel);
int tiley = lat2tiley(realcenterlatlon.y(),m_zoomLevel);
if (tilex < m_currentTileCoords.x())
{
//Center has shifted left/right, remove one layer of images and add another.
for (int y=-5;y<5;y++)
{
QList<QGraphicsItem*> items = m_scene->items(QPointF((m_currentTileCoords.x()+5)*256,(m_currentTileCoords.y()+y)*256));
if (items.count() > 0)
{
m_scene->removeItem(items.at(0));
delete items.at(0);
}
m_tileCache->getTile(m_currentTileCoords.x()-5,m_currentTileCoords.y()+y,m_zoomLevel);
}
}
else if (tilex > m_currentTileCoords.x())
{
for (int y=-5;y<5;y++)
{
QList<QGraphicsItem*> items = m_scene->items(QPointF((m_currentTileCoords.x()-5)*256,(m_currentTileCoords.y()+y)*256));
if (items.count() > 0)
{
m_scene->removeItem(items.at(0));
delete items.at(0);
}
m_tileCache->getTile(m_currentTileCoords.x()+5,m_currentTileCoords.y()+y,m_zoomLevel);
}
}
if (tiley < m_currentTileCoords.y())
{
//Center has shifted left/right, remove one layer of images and add another.
for (int x=-5;x<5;x++)
{
QList<QGraphicsItem*> items = m_scene->items(QPointF((m_currentTileCoords.x()+x)*256,(m_currentTileCoords.y()+5)*256));
if (items.count() > 0)
{
m_scene->removeItem(items.at(0));
delete items.at(0);
}
m_tileCache->getTile(m_currentTileCoords.x()+x,m_currentTileCoords.y()-5,m_zoomLevel);
}
}
else if (tiley > m_currentTileCoords.y())
{
for (int x=-5;x<5;x++)
{
QList<QGraphicsItem*> items = m_scene->items(QPointF((m_currentTileCoords.x()+x)*256,(m_currentTileCoords.y()-5)*256));
if (items.count() > 0)
{
m_scene->removeItem(items.at(0));
delete items.at(0);
}
m_tileCache->getTile(m_currentTileCoords.x()+x,m_currentTileCoords.y()+5,m_zoomLevel);
}
}
m_currentTileCoords.setX(tilex);
m_currentTileCoords.setY(tiley);
//qDebug() << "Center tile:" << tilex << tiley;
this->translate(scenecoords.x() - m_lastMousePos.x(),scenecoords.y() - m_lastMousePos.y());
//qreal scenex = sceneRect().x() + (scenecoords.x() - m_lastMousePos.x());
//qreal sceney = sceneRect().y() + (scenecoords.y() - m_lastMousePos.y());
//this->setSceneRect(scenex,sceney,sceneRect().width(),sceneRect().height());
//qDebug() << scenex << sceney;
//qDebug() << scenecoords.x() - m_lastMousePos.x() << scenecoords.y() - m_lastMousePos.y();
//qDebug() << this->transform();
m_lastMousePos = mapToScene(evt->x(),evt->y());
update();
//qDebug() << QString::number(sceneRect().x(),'f') << QString::number(m_scene->sceneRect().x(),'f');
}
QGraphicsView::mouseMoveEvent(evt);
}
void Map::mousePressEvent(QMouseEvent *evt)
{
m_mouseIsDown = true;
m_lastMousePos = mapToScene(evt->x(),evt->y());
// setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
QGraphicsView::mousePressEvent(evt);
}
void Map::mouseReleaseEvent(QMouseEvent *evt)
{
QPointF scenecoords = this->mapToScene(evt->x(),evt->y());
QPointF latlon = sceneToMapCoords(scenecoords);
emit mouseReleased(latlon.y(),latlon.x());
m_mouseIsDown = false;
//setTransformationAnchor(QGraphicsView::AnchorViewCenter);
QGraphicsView::mouseReleaseEvent(evt);
}
void Map::wheelEvent(QWheelEvent *evt)
{
qDebug() << evt->x() << evt->y();
QPointF latlon = sceneToMapCoords(mapToScene(evt->x(),evt->y()));
if (evt->delta() > 0)
{
setCenter(latlon.y(),latlon.x(),m_zoomLevel+1);
}
else
{
setCenter(latlon.y(),latlon.x(),m_zoomLevel-1);
}
}
void Map::tileRecv(int x,int y, int z, QImage tile)
{
if (z != m_zoomLevel)
{
return;
}
Tile *t = new Tile();
t->setFlag(QGraphicsItem::ItemIsMovable,false);
t->setFlag(QGraphicsItem::ItemIsSelectable,false);
t->setImage(tile,x,y);
m_scene->addItem(t);
m_scene->setSceneRect(QRectF());
m_scene->update();
//this->update();
//qDebug() << "Adding tile at:" << x*256 << y*256;
//qDebug() << "New scene:" << m_scene->sceneRect();
}
void Map::setZoom(int zoom)
{
setCenter(m_currentLatLon.y(),m_currentLatLon.x(),zoom);
}