-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtilesloaderthread.cpp
More file actions
184 lines (161 loc) · 5.33 KB
/
Copy pathtilesloaderthread.cpp
File metadata and controls
184 lines (161 loc) · 5.33 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "tilesloaderthread.h"
TilesLoaderThread::TilesLoaderThread(QObject *parent) :
QThread(parent),
m_abort(false)
{
QSettings settings(QCoreApplication::applicationDirPath() + "/xtrip.ini", QSettings::IniFormat);
m_tilesPath = settings.value("TilesPath").toString();
m_tileLifetime = settings.value("TileLifetime").toInt();
start();
}
TilesLoaderThread::~TilesLoaderThread()
{
m_mutex.lock();
m_abort = true;
m_mutex.unlock();
quit();
wait();
}
void TilesLoaderThread::loadTiles(TilesSource *source, const QRect &bounds, int zoom, bool online)
{
m_mutex.lock();
m_source = source;
m_bounds = bounds;
m_zoom = zoom;
m_online = online;
m_tilesQueue.clear();
m_restart = true;
m_mutex.unlock();
quit();
}
void TilesLoaderThread::run()
{
QSettings settings(QCoreApplication::applicationDirPath() + "/xtrip.ini", QSettings::IniFormat);
QPixmapCache::setCacheLimit(settings.value("CacheLimit").toInt());
m_manager = new QNetworkAccessManager();
connect(m_manager,
SIGNAL(finished(QNetworkReply *)),
SLOT(handleNetworkData(QNetworkReply *)),
Qt::DirectConnection);
while (!m_abort) {
m_mutex.lock();
TilesSource *source = m_source;
QRect bounds = m_bounds;
int zoom = m_zoom;
bool online = m_online;
m_restart = false;
m_mutex.unlock();
QMap<int, QPoint> tiles;
QPoint center = bounds.center();
QString key;
for (int x = bounds.left(); x <= bounds.right(); x++) {
for (int y = bounds.top(); y <= bounds.bottom(); y++) {
QPoint tp(x, y);
key.sprintf("tile.%d.%d.%d.%d", source->id(), zoom, x, y);
if (!QPixmapCache::find(key)) {
QPoint p = tp - center;
tiles.insertMulti(p.x() * p.x() + p.y() * p.y(), tp);
}
}
}
if (m_restart) {
continue;
}
QDateTime expiration = QDateTime::currentDateTime().addDays(-m_tileLifetime);
foreach (QPoint tp, tiles) {
if (m_restart) {
break;
}
QFile file(tilePath(source->dirname(), zoom, tp, source->format()));
if (file.open(QIODevice::ReadOnly)) {
QByteArray buf = file.readAll();
file.close();
QPixmap tile;
if (tile.loadFromData(buf)) {
key.sprintf("tile.%d.%d.%d.%d", source->id(), zoom, tp.x(), tp.y());
if (QPixmapCache::insert(key, tile)) {
emit tileLoaded(tp);
}
QFileInfo info(file);
if (info.lastModified() >= expiration) {
continue;
}
}
}
if (online) {
QUrl url = QUrl(source->tileUrl(tp.x(), tp.y(), zoom));
QNetworkRequest request;
request.setUrl(url);
QHash<QString, QVariant> data;
data["tp"] = tp;
data["key"] = key.sprintf("tile.%d.%d.%d.%d", source->id(), zoom, tp.x(), tp.y());
data["dirname"] = dirPath(source->dirname(), zoom, tp);
data["filename"] = tilePath(source->dirname(), zoom, tp, source->format());
request.setAttribute(QNetworkRequest::User, QVariant(data));
m_mutex.lock();
m_tilesQueue.enqueue(request);
m_mutex.unlock();
}
}
if (m_restart) {
continue;
}
if (online) {
emit tilesLoading(m_tilesQueue.count());
loadNextTile();
}
exec();
}
}
void TilesLoaderThread::handleNetworkData(QNetworkReply *reply)
{
QPixmap tile;
QHash<QString, QVariant> data = reply->request().attribute(QNetworkRequest::User).toHash();
QPoint tp = data["tp"].toPoint();
QString key = data["key"].toString();
QString dirname = data["dirname"].toString();
QString filename = data["filename"].toString();
if (!reply->error()) {
QDir dir;
dir.mkpath(dirname);
QFile file(filename);
file.open(QFile::WriteOnly);
QByteArray buf = reply->readAll();
file.write(buf);
file.close();
if (tile.loadFromData(buf)) {
QPixmapCache::insert(key, tile);
emit tileLoaded(tp);
}
}
else {
qDebug() << reply->errorString();
}
reply->deleteLater();
emit tilesLoading(m_tilesQueue.count());
loadNextTile();
}
void TilesLoaderThread::loadNextTile()
{
m_mutex.lock();
if (m_tilesQueue.isEmpty()) {
m_mutex.unlock();
return;
}
QNetworkRequest request = m_tilesQueue.dequeue();
m_mutex.unlock();
m_manager->get(request);
}
QString TilesLoaderThread::dirPath(const QString &source, int zoom, const QPoint &tp)
{
return (m_tilesPath + "\\" + source + "\\z%1\\%2\\x%3\\%4")
.arg(zoom + 1)
.arg(tp.x() / 1024)
.arg(tp.x())
.arg(tp.y() / 1024);
}
QString TilesLoaderThread::tilePath(const QString &source, int zoom, const QPoint &tp, const QString &format)
{
return (dirPath(source, zoom, tp) + "\\y%1." + format)
.arg(tp.y());
}