1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-24 11:45:53 +01: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*))); SLOT(downloadFinished(QNetworkReply*)));
} }
void Downloader::doDownload(const Download &dl) bool Downloader::doDownload(const Download &dl)
{ {
QUrl url(dl.url()); QUrl url(dl.url());
if (_errorDownloads.contains(url)) if (_errorDownloads.contains(url))
return; return false;
QNetworkRequest request(url); QNetworkRequest request(url);
request.setAttribute(QNetworkRequest::User, QVariant(dl.file())); request.setAttribute(QNetworkRequest::User, QVariant(dl.file()));
@ -36,6 +36,8 @@ void Downloader::doDownload(const Download &dl)
QNetworkReply *reply = _manager.get(request); QNetworkReply *reply = _manager.get(request);
_currentDownloads.append(reply); _currentDownloads.append(reply);
return true;
} }
bool Downloader::saveToDisk(const QString &filename, QIODevice *data) bool Downloader::saveToDisk(const QString &filename, QIODevice *data)
@ -74,8 +76,12 @@ void Downloader::downloadFinished(QNetworkReply *reply)
emit finished(); 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++) 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: public:
static Downloader& instance() static Downloader& instance()
{static Downloader i; return i;} {static Downloader i; return i;}
void get(const QList<Download> &list); bool get(const QList<Download> &list);
signals: signals:
void finished(); void finished();
@ -43,7 +43,7 @@ private:
Downloader(Downloader const&); Downloader(Downloader const&);
void operator=(Downloader const&); void operator=(Downloader const&);
void doDownload(const Download &dl); bool doDownload(const Download &dl);
bool saveToDisk(const QString &filename, QIODevice *data); bool saveToDisk(const QString &filename, QIODevice *data);
QNetworkAccessManager _manager; QNetworkAccessManager _manager;

View File

@ -25,29 +25,26 @@ void Map::emitLoaded()
emit loaded(); 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; QList<Download> dl;
for (int i = 0; i < list.size(); ++i) { for (int i = 0; i < list.size(); i++) {
Tile &t = list[i]; Tile &t = list[i];
QString file = TILES_DIR + QString("/%1/%2-%3-%4") QString file = tileFile(t);
.arg(_name).arg(t.zoom()).arg(t.xy().rx()).arg(t.xy().ry());
QFileInfo fi(file); QFileInfo fi(file);
if (fi.exists()) { if (!(fi.exists() && loadTileFile(t, file))) {
if (!t.pixmap().load(file)) fillTile(t);
fprintf(stderr, "Error loading map tile: %s\n", dl.append(Download(tileUrl(t), file));
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));
} }
} }
@ -55,6 +52,75 @@ void Map::loadTiles(QList<Tile> &list)
Downloader::instance().get(dl); 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() void Map::clearCache()
{ {
QString path = TILES_DIR + QString("/") + _name; QString path = TILES_DIR + QString("/") + _name;

View File

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

View File

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

View File

@ -36,6 +36,8 @@ TrackView::TrackView(QWidget *parent)
_map = 0; _map = 0;
_maxPath = 0; _maxPath = 0;
_maxDistance = 0; _maxDistance = 0;
_plot = false;
} }
TrackView::~TrackView() TrackView::~TrackView()
@ -295,7 +297,8 @@ void TrackView::setMap(Map *map)
{ {
_map = map; _map = map;
if (_map) if (_map)
connect(_map, SIGNAL(loaded()), this, SLOT(redraw())); connect(_map, SIGNAL(loaded()), this, SLOT(redraw()),
Qt::UniqueConnection);
resetCachedContent(); resetCachedContent();
} }
@ -343,18 +346,9 @@ void TrackView::wheelEvent(QWheelEvent *event)
resetCachedContent(); 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) void TrackView::plot(QPainter *painter, const QRectF &target)
{ {
QRectF orig, adj; QRect orig, adj;
qreal ratio, diff; qreal ratio, diff;
orig = viewport()->rect(); orig = viewport()->rect();
@ -369,11 +363,13 @@ void TrackView::plot(QPainter *painter, const QRectF &target)
adj = orig.adjusted(0, -diff/2, 0, diff/2); 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(), + 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 enum QPrinter::Orientation TrackView::orientation() const
@ -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++) { for (int i = 0; i < tiles.count(); i++) {
Tile &t = tiles[i]; Tile &t = tiles[i];
QPoint tp(tl.x() + (t.xy().rx() - tile.rx()) * TILE_SIZE, QPoint tp(tl.x() + (t.xy().x() - tile.x()) * TILE_SIZE,
tl.y() + (t.xy().ry() - tile.ry()) * TILE_SIZE); tl.y() + (t.xy().y() - tile.y()) * TILE_SIZE);
painter->drawPixmap(tp, t.pixmap()); painter->drawPixmap(tp, t.pixmap());
} }
} }
@ -493,7 +489,7 @@ void TrackView::paintEvent(QPaintEvent *e)
{ {
QPointF scenePos = mapToScene(rect().bottomLeft() + QPoint(SCALE_OFFSET, QPointF scenePos = mapToScene(rect().bottomLeft() + QPoint(SCALE_OFFSET,
-(SCALE_OFFSET + _mapScale->boundingRect().height()))); -(SCALE_OFFSET + _mapScale->boundingRect().height())));
if (_mapScale->pos() != scenePos) if (_mapScale->pos() != scenePos && !_plot)
_mapScale->setPos(scenePos); _mapScale->setPos(scenePos);
QGraphicsView::paintEvent(e); QGraphicsView::paintEvent(e);

View File

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