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\n", qPrintable(file
));
17 TileLoader::TileLoader(const QString
&dir
, QObject
*parent
)
18 : QObject(parent
), _dir(dir
)
20 if (!QDir().mkpath(_dir
))
21 qWarning("%s: %s\n", 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
)
31 for (int i
= 0; i
< list
.size(); i
++) {
33 QString
file(tileFile(t
));
37 if (url
.isLocalFile())
38 loadTileFile(t
, url
.toLocalFile());
40 loadTileFile(t
, file
);
42 dl
.append(Download(tileUrl(t
), file
));
46 _downloader
->get(dl
, _authorization
);
49 void TileLoader::loadTilesSync(QList
<Tile
> &list
)
53 for (int i
= 0; i
< list
.size(); i
++) {
55 QString
file(tileFile(t
));
59 if (url
.isLocalFile())
60 loadTileFile(t
, url
.toLocalFile());
62 loadTileFile(t
, file
);
64 dl
.append(Download(tileUrl(t
), file
));
71 QObject::connect(_downloader
, SIGNAL(finished()), &wait
, SLOT(quit()));
72 if (_downloader
->get(dl
, _authorization
))
75 for (int i
= 0; i
< list
.size(); 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
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
);
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()));
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());