1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-10-06 06:43:22 +02:00

Added proper tile download for PDF exports/printing

This commit is contained in:
Martin Tůma 2016-05-19 01:10:40 +02:00
parent f326c7e002
commit ffcba53b91
7 changed files with 124 additions and 51 deletions

View File

@ -23,12 +23,12 @@ Downloader::Downloader()
SLOT(downloadFinished(QNetworkReply*)));
}
void Downloader::doDownload(const Download &dl)
bool Downloader::doDownload(const Download &dl)
{
QUrl url(dl.url());
if (_errorDownloads.contains(url))
return;
return false;
QNetworkRequest request(url);
request.setAttribute(QNetworkRequest::User, QVariant(dl.file()));
@ -36,6 +36,8 @@ void Downloader::doDownload(const Download &dl)
QNetworkReply *reply = _manager.get(request);
_currentDownloads.append(reply);
return true;
}
bool Downloader::saveToDisk(const QString &filename, QIODevice *data)
@ -74,8 +76,12 @@ void Downloader::downloadFinished(QNetworkReply *reply)
emit finished();
}
void Downloader::get(const QList<Download> &list)
bool Downloader::get(const QList<Download> &list)
{
bool finishEmitted = false;
for (int i = 0; i < list.count(); i++)
doDownload(list.at(i));
finishEmitted |= doDownload(list.at(i));
return finishEmitted;
}

View File

@ -30,7 +30,7 @@ class Downloader : public QObject
public:
static Downloader& instance()
{static Downloader i; return i;}
void get(const QList<Download> &list);
bool get(const QList<Download> &list);
signals:
void finished();
@ -43,7 +43,7 @@ private:
Downloader(Downloader const&);
void operator=(Downloader const&);
void doDownload(const Download &dl);
bool doDownload(const Download &dl);
bool saveToDisk(const QString &filename, QIODevice *data);
QNetworkAccessManager _manager;

View File

@ -25,29 +25,26 @@ void Map::emitLoaded()
emit loaded();
}
void Map::loadTiles(QList<Tile> &list)
void Map::loadTiles(QList<Tile> &list, bool block)
{
if (block)
loadTilesSync(list);
else
loadTilesAsync(list);
}
void Map::loadTilesAsync(QList<Tile> &list)
{
QList<Download> dl;
for (int i = 0; i < list.size(); ++i) {
for (int i = 0; i < list.size(); i++) {
Tile &t = list[i];
QString file = TILES_DIR + QString("/%1/%2-%3-%4")
.arg(_name).arg(t.zoom()).arg(t.xy().rx()).arg(t.xy().ry());
QString file = tileFile(t);
QFileInfo fi(file);
if (fi.exists()) {
if (!t.pixmap().load(file))
fprintf(stderr, "Error loading map tile: %s\n",
qPrintable(file));
} else {
t.pixmap() = QPixmap(TILE_SIZE, TILE_SIZE);
t.pixmap().fill();
QString url(_url);
url.replace("$z", QString::number(t.zoom()));
url.replace("$x", QString::number(t.xy().x()));
url.replace("$y", QString::number(t.xy().y()));
dl.append(Download(url, file));
if (!(fi.exists() && loadTileFile(t, file))) {
fillTile(t);
dl.append(Download(tileUrl(t), file));
}
}
@ -55,6 +52,75 @@ void Map::loadTiles(QList<Tile> &list)
Downloader::instance().get(dl);
}
void Map::loadTilesSync(QList<Tile> &list)
{
QList<Download> dl;
for (int i = 0; i < list.size(); i++) {
Tile &t = list[i];
QString file = tileFile(t);
QFileInfo fi(file);
if (!(fi.exists() && loadTileFile(t, file)))
dl.append(Download(tileUrl(t), file));
}
if (dl.empty())
return;
QEventLoop wait;
connect(&Downloader::instance(), SIGNAL(finished()), &wait, SLOT(quit()));
if (Downloader::instance().get(dl))
wait.exec();
for (int i = 0; i < list.size(); i++) {
Tile &t = list[i];
if (t.pixmap().isNull()) {
QString file = tileFile(t);
QFileInfo fi(file);
if (!(fi.exists() && loadTileFile(t, file)))
fillTile(t);
}
}
}
void Map::fillTile(Tile &tile)
{
tile.pixmap() = QPixmap(TILE_SIZE, TILE_SIZE);
tile.pixmap().fill();
}
bool Map::loadTileFile(Tile &tile, const QString &file)
{
if (!tile.pixmap().load(file)) {
fprintf(stderr, "%s: error loading tile file\n", qPrintable(file));
return false;
}
return true;
}
QString Map::tileUrl(const Tile &tile)
{
QString url(_url);
url.replace("$z", QString::number(tile.zoom()));
url.replace("$x", QString::number(tile.xy().x()));
url.replace("$y", QString::number(tile.xy().y()));
return url;
}
QString Map::tileFile(const Tile &tile)
{
QString file = TILES_DIR + QString("/%1/%2-%3-%4").arg(_name)
.arg(tile.zoom()).arg(tile.xy().x()).arg(tile.xy().y());
return file;
}
void Map::clearCache()
{
QString path = TILES_DIR + QString("/") + _name;

View File

@ -10,7 +10,7 @@ public:
{_xy = xy; _zoom = zoom;}
int zoom() const {return _zoom;}
QPoint& xy() {return _xy;}
const QPoint& xy() const {return _xy;}
QPixmap& pixmap() {return _pixmap;}
private:
@ -29,7 +29,7 @@ public:
const QString &url = QString());
const QString &name() const {return _name;}
void loadTiles(QList<Tile> &list);
void loadTiles(QList<Tile> &list, bool block);
void clearCache();
signals:
@ -39,6 +39,14 @@ private slots:
void emitLoaded();
private:
QString tileUrl(const Tile &tile);
QString tileFile(const Tile &tile);
bool loadTileFile(Tile &tile, const QString &file);
void fillTile(Tile &tile);
void loadTilesAsync(QList<Tile> &list);
void loadTilesSync(QList<Tile> &list);
QString _name;
QString _url;
};

View File

@ -67,14 +67,12 @@ void ScaleItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
for (int i = 0; i < SEGMENTS; i += 2)
painter->fillRect(QRectF(i * _width, br.height() + PADDING, _width,
SCALE_HEIGHT), Qt::black);
if (aa)
painter->setRenderHint(QPainter::Antialiasing, true);
/*
painter->setPen(Qt::red);
painter->drawRect(boundingRect());
*/
if (aa)
painter->setRenderHint(QPainter::Antialiasing, true);
}
QString ScaleItem::units() const

View File

@ -36,6 +36,8 @@ TrackView::TrackView(QWidget *parent)
_map = 0;
_maxPath = 0;
_maxDistance = 0;
_plot = false;
}
TrackView::~TrackView()
@ -295,7 +297,8 @@ void TrackView::setMap(Map *map)
{
_map = map;
if (_map)
connect(_map, SIGNAL(loaded()), this, SLOT(redraw()));
connect(_map, SIGNAL(loaded()), this, SLOT(redraw()),
Qt::UniqueConnection);
resetCachedContent();
}
@ -343,18 +346,9 @@ void TrackView::wheelEvent(QWheelEvent *event)
resetCachedContent();
}
void TrackView::setTrackLineWidth(qreal width)
{
for (int i = 0; i < _paths.size(); i++) {
QPen pen(_paths.at(i)->pen());
pen.setWidthF(width);
_paths.at(i)->setPen(pen);
}
}
void TrackView::plot(QPainter *painter, const QRectF &target)
{
QRectF orig, adj;
QRect orig, adj;
qreal ratio, diff;
orig = viewport()->rect();
@ -369,11 +363,13 @@ void TrackView::plot(QPainter *painter, const QRectF &target)
adj = orig.adjusted(0, -diff/2, 0, diff/2);
}
_mapScale->setPos(mapToScene(QPointF(adj.bottomRight()
_mapScale->setPos(mapToScene(QPoint(adj.bottomRight()
+ QPoint(-_mapScale->boundingRect().width(),
-_mapScale->boundingRect().height())).toPoint()));
-_mapScale->boundingRect().height()))));
render(painter, target, adj.toRect());
_plot = true;
render(painter, target, adj);
_plot = false;
}
enum QPrinter::Orientation TrackView::orientation() const
@ -435,7 +431,7 @@ void TrackView::movePositionMarker(qreal val)
void TrackView::drawBackground(QPainter *painter, const QRectF &rect)
{
if ((_paths.isEmpty() && _locations.isEmpty())|| !_map) {
if ((_paths.isEmpty() && _locations.isEmpty()) || !_map) {
painter->fillRect(rect, Qt::white);
return;
}
@ -454,12 +450,12 @@ void TrackView::drawBackground(QPainter *painter, const QRectF &rect)
}
}
_map->loadTiles(tiles);
_map->loadTiles(tiles, _plot);
for (int i = 0; i < tiles.count(); i++) {
Tile &t = tiles[i];
QPoint tp(tl.x() + (t.xy().rx() - tile.rx()) * TILE_SIZE,
tl.y() + (t.xy().ry() - tile.ry()) * TILE_SIZE);
QPoint tp(tl.x() + (t.xy().x() - tile.x()) * TILE_SIZE,
tl.y() + (t.xy().y() - tile.y()) * TILE_SIZE);
painter->drawPixmap(tp, t.pixmap());
}
}
@ -493,7 +489,7 @@ void TrackView::paintEvent(QPaintEvent *e)
{
QPointF scenePos = mapToScene(rect().bottomLeft() + QPoint(SCALE_OFFSET,
-(SCALE_OFFSET + _mapScale->boundingRect().height())));
if (_mapScale->pos() != scenePos)
if (_mapScale->pos() != scenePos && !_plot)
_mapScale->setPos(scenePos);
QGraphicsView::paintEvent(e);

View File

@ -57,9 +57,6 @@ private:
void rescale(qreal scale);
void rescale();
void showMarkers(bool show);
void setTrackLineWidth(qreal width);
void wheelEvent(QWheelEvent *event);
void drawBackground(QPainter *painter, const QRectF &rect);
void resizeEvent(QResizeEvent *e);
@ -81,6 +78,8 @@ private:
qreal _scale;
int _zoom;
bool _plot;
};
#endif // TRACKVIEW_H