-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtilesmap.cpp
More file actions
166 lines (145 loc) · 4.44 KB
/
tilesmap.cpp
File metadata and controls
166 lines (145 loc) · 4.44 KB
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
#include "tilesmap.h"
TilesMap::TilesMap(LatLon center, int zoom, bool online, QWidget *parent) :
QWidget(parent),
m_center(center),
m_zoom(zoom),
m_online(online)
{
m_emptyTile = QPixmap(TILE_SIZE, TILE_SIZE);
m_emptyTile.fill(Qt::lightGray);
m_sources["osm"] = new OSMTilesSource();
m_source = m_sources["osm"];
connect(&m_loaderThread, SIGNAL(tileLoaded(QPoint)), SLOT(updateTile(QPoint)));
connect(&m_loaderThread, SIGNAL(tilesLoading(int)), SIGNAL(tilesLoading(int)));
}
void TilesMap::setCenter(LatLon center)
{
m_center = center;
calcTilesRect();
}
int TilesMap::setZoom(int zoom)
{
if (zoom < 0) {
zoom = 0;
}
else if (zoom > m_source->maxZoom()) {
zoom = m_source->maxZoom();
}
if (zoom != m_zoom) {
m_zoom = zoom;
emit zoomChanged(m_zoom);
calcTilesRect();
}
return m_zoom;
}
void TilesMap::setSource(QString source)
{
if (m_sources.contains(source)) {
m_source = m_sources[source];
}
else {
m_source = m_sources["osm"];
}
loadTiles();
}
void TilesMap::setOnline(bool online)
{
m_online = online;
loadTiles();
}
void TilesMap::paintEvent(QPaintEvent *event)
{
QPainter p;
p.begin(this);
QPixmap tile;
QString key;
for (int x = m_tilesRect.left(); x <= m_tilesRect.right(); x++) {
for (int y = m_tilesRect.top(); y <= m_tilesRect.bottom(); y++) {
QPoint tp(x, y);
QRect box = tileRect(tp);
if (event->rect().intersects(box)) {
key.sprintf("tile.%d.%d.%d.%d", m_source->id(), m_zoom, x, y);
if (QPixmapCache::find(key, tile)) {
p.drawPixmap(box, tile);
}
else {
bool drawn = false;
for (int k = 2, z = m_zoom - 1; k <= 8; k *= 2, z--) {
key.sprintf("tile.%d.%d.%d.%d", m_source->id(), z, x / k, y / k);
if (QPixmapCache::find(key, tile)) {
int s = TILE_SIZE / k;
p.drawPixmap(box, tile, QRect((x % k) * s, (y % k) * s, s, s));
drawn = true;
break;
}
}
if (!drawn) {
p.drawPixmap(box, m_emptyTile);
}
}
}
}
}
p.end();
}
void TilesMap::resizeEvent(QResizeEvent *event)
{
m_width = width();
m_height = height();
calcTilesRect();
loadTiles();
}
void TilesMap::calcTilesRect()
{
QPointF ct = tileForCoordinate(m_center);
qreal tx = ct.x();
qreal ty = ct.y();
m_tileCenter = QPoint(floor(tx), floor(ty));
int xp = m_width / 2 - (tx - floor(tx)) * TILE_SIZE;
int yp = m_height / 2 - (ty - floor(ty)) * TILE_SIZE;
int xa = (xp + TILE_SIZE - 1) / TILE_SIZE;
int ya = (yp + TILE_SIZE - 1) / TILE_SIZE;
int xs = static_cast<int>(tx) - xa;
int ys = static_cast<int>(ty) - ya;
m_offset = QPoint(xp - xa * TILE_SIZE, yp - ya * TILE_SIZE);
int xe = static_cast<int>(tx) + (m_width - xp - 1) / TILE_SIZE;
int ye = static_cast<int>(ty) + (m_height - yp - 1) / TILE_SIZE;
m_tilesRect = QRect(xs, ys, xe - xs + 1, ye - ys + 1);
}
void TilesMap::loadTiles()
{
QRect rect = m_tilesRect.intersected(QRect(0, 0, 1 << m_zoom, 1 << m_zoom));
m_loaderThread.loadTiles(
m_source,
rect,
m_zoom,
m_online
);
}
void TilesMap::updateTile(QPoint tp)
{
emit updated(tileRect(tp));
}
QRect TilesMap::tileRect(const QPoint &tp)
{
QPoint t = tp - m_tilesRect.topLeft();
int x = t.x() * TILE_SIZE + m_offset.x();
int y = t.y() * TILE_SIZE + m_offset.y();
return QRect(x, y, TILE_SIZE, TILE_SIZE);
}
QPointF TilesMap::tileForCoordinate(LatLon coord)
{
qreal zn = static_cast<qreal>(1 << m_zoom);
qreal tx = (coord.lon() + 180.0) / 360.0;
qreal ty = (1.0 - log(tan(coord.lat() * M_PI / 180.0)
+ 1.0 / cos(coord.lat() * M_PI / 180.0)) / M_PI) / 2.0;
return QPointF(tx * zn, ty * zn);
}
LatLon TilesMap::coordinateForTile(const QPointF &tile)
{
qreal zn = static_cast<qreal>(1 << m_zoom);
qreal n = M_PI - 2 * M_PI * tile.y() / zn;
qreal lat = 180.0 / M_PI * atan(0.5 * (exp(n) - exp(-n)));
qreal lon = tile.x() / zn * 360.0 - 180.0;
return LatLon(lat, lon);
}