qWarning() mesage formating cleanup
[GPXSee.git] / src / map / tileloader.cpp
blobb0acd87d551dde8cf59157c5c43d8657068a50b9
1 #include <QDir>
2 #include <QFileInfo>
3 #include <QEventLoop>
4 #include "tileloader.h"
7 static bool loadTileFile(Tile &tile, const QString &file)
9 if (!tile.pixmap().load(file)) {
10 qWarning("%s: error loading tile file", qPrintable(file));
11 return false;
14 return true;
17 TileLoader::TileLoader(const QString &dir, QObject *parent)
18 : QObject(parent), _dir(dir)
20 if (!QDir().mkpath(_dir))
21 qWarning("%s: %s", qPrintable(_dir), "Error creating tiles directory");
23 _downloader = new Downloader(this);
24 connect(_downloader, SIGNAL(finished()), this, SIGNAL(finished()));
27 void TileLoader::loadTilesAsync(QList<Tile> &list)
29 QList<Download> dl;
31 for (int i = 0; i < list.size(); i++) {
32 Tile &t = list[i];
33 QString file(tileFile(t));
34 QFileInfo fi(file);
35 QUrl url(tileUrl(t));
37 if (url.isLocalFile())
38 loadTileFile(t, url.toLocalFile());
39 else if (fi.exists())
40 loadTileFile(t, file);
41 else
42 dl.append(Download(tileUrl(t), file));
45 if (!dl.empty())
46 _downloader->get(dl, _authorization);
49 void TileLoader::loadTilesSync(QList<Tile> &list)
51 QList<Download> dl;
53 for (int i = 0; i < list.size(); i++) {
54 Tile &t = list[i];
55 QString file(tileFile(t));
56 QFileInfo fi(file);
57 QUrl url(tileUrl(t));
59 if (url.isLocalFile())
60 loadTileFile(t, url.toLocalFile());
61 else if (fi.exists())
62 loadTileFile(t, file);
63 else
64 dl.append(Download(tileUrl(t), file));
67 if (dl.empty())
68 return;
70 QEventLoop wait;
71 QObject::connect(_downloader, SIGNAL(finished()), &wait, SLOT(quit()));
72 if (_downloader->get(dl, _authorization))
73 wait.exec();
75 for (int i = 0; i < list.size(); i++) {
76 Tile &t = list[i];
78 if (t.pixmap().isNull()) {
79 QString file = tileFile(t);
80 if (QFileInfo(file).exists())
81 loadTileFile(t, file);
86 void TileLoader::clearCache()
88 QDir dir = QDir(_dir);
89 QStringList list = dir.entryList();
91 for (int i = 0; i < list.count(); i++)
92 dir.remove(list.at(i));
94 _downloader->clearErrors();
97 QUrl TileLoader::tileUrl(const Tile &tile) const
99 QString url(_url);
101 if (!tile.bbox().isNull()) {
102 QString bbox = QString("%1,%2,%3,%4").arg(
103 QString::number(tile.bbox().left(), 'f', 6),
104 QString::number(tile.bbox().bottom(), 'f', 6),
105 QString::number(tile.bbox().right(), 'f', 6),
106 QString::number(tile.bbox().top(), 'f', 6));
107 url.replace("$bbox", bbox);
108 } else {
109 url.replace("$z", tile.zoom().toString());
110 url.replace("$x", QString::number(tile.xy().x()));
111 url.replace("$y", QString::number(tile.xy().y()));
114 return QUrl(url);
117 QString TileLoader::tileFile(const Tile &tile) const
119 QString file = _dir + QString("/%1-%2-%3").arg(tile.zoom().toString())
120 .arg(tile.xy().x()).arg(tile.xy().y());
122 return file;