1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-28 05:34:47 +01:00

Improved error handling

This commit is contained in:
Martin Tůma 2017-04-15 08:59:31 +02:00
parent ccf91bb29f
commit fd53e89ea5
4 changed files with 92 additions and 70 deletions

View File

@ -305,8 +305,8 @@ bool OfflineMap::getImageInfo(const QString &path)
} }
if (_imgPath.endsWith("ozf2")) { if (_imgPath.endsWith("ozf2")) {
_ozf.load(_imgPath); if (_ozf.load(_imgPath))
_size = _ozf.size(); _size = _ozf.size();
} else { } else {
QImageReader img(_imgPath); QImageReader img(_imgPath);
_size = img.size(); _size = img.size();
@ -540,74 +540,100 @@ void OfflineMap::unload()
} }
} }
void OfflineMap::draw(QPainter *painter, const QRectF &rect) void OfflineMap::drawTiled(QPainter *painter, const QRectF &rect)
{ {
if (_tileSize.isValid()) { QPoint tl = QPoint((int)floor(rect.left() / (qreal)_tileSize.width())
QPoint tl = QPoint((int)floor(rect.left() / (qreal)_tileSize.width()) * _tileSize.width(), (int)floor(rect.top() / _tileSize.height())
* _tileSize.width(), (int)floor(rect.top() / _tileSize.height()) * _tileSize.height());
* _tileSize.height());
QSizeF s(rect.right() - tl.x(), rect.bottom() - tl.y()); QSizeF s(rect.right() - tl.x(), rect.bottom() - tl.y());
for (int i = 0; i < ceil(s.width() / _tileSize.width()); i++) { for (int i = 0; i < ceil(s.width() / _tileSize.width()); i++) {
for (int j = 0; j < ceil(s.height() / _tileSize.height()); j++) { for (int j = 0; j < ceil(s.height() / _tileSize.height()); j++) {
int x = tl.x() + i * _tileSize.width(); int x = tl.x() + i * _tileSize.width();
int y = tl.y() + j * _tileSize.height(); int y = tl.y() + j * _tileSize.height();
QString tileName(_tileName.arg(QString::number(x),
QString::number(y)));
QPixmap pixmap;
if (_tar.isOpen()) { if (!QRectF(QPointF(x, y), _ozf.tileSize()).intersects(bounds())) {
QString key = _tar.fileName() + "/" + tileName; painter->fillRect(QRectF(QPoint(x, y), _tileSize), Qt::white);
if (!QPixmapCache::find(key, &pixmap)) { continue;
QByteArray ba = _tar.file(tileName);
pixmap = QPixmap::fromImage(QImage::fromData(ba));
if (!pixmap.isNull())
QPixmapCache::insert(key, pixmap);
}
} else
pixmap = QPixmap(tileName);
if (pixmap.isNull()) {
qWarning("%s: error loading tile image", qPrintable(
_tileName.arg(QString::number(x), QString::number(y))));
painter->fillRect(QRectF(QPoint(x, y), _tileSize),
Qt::white);
} else
painter->drawPixmap(QPoint(x, y), pixmap);
} }
}
} else if (_ozf.isOpen()) {
QPoint tl = QPoint((int)floor(rect.left()
/ (qreal)_ozf.tileSize().width()) * _ozf.tileSize().width(),
(int)floor(rect.top() / _ozf.tileSize().height())
* _ozf.tileSize().height());
QSizeF s(rect.right() - tl.x(), rect.bottom() - tl.y()); QString tileName(_tileName.arg(QString::number(x),
for (int i = 0; i < ceil(s.width() / _ozf.tileSize().width()); i++) { QString::number(y)));
for (int j = 0; j < ceil(s.height() / _ozf.tileSize().height()); QPixmap pixmap;
j++) {
int x = tl.x() + i * _ozf.tileSize().width();
int y = tl.y() + j * _ozf.tileSize().height();
QPixmap pixmap; if (_tar.isOpen()) {
QString key = _ozf.fileName() + "/" + QString::number(x) QString key = _tar.fileName() + "/" + tileName;
+ "_" + QString::number(y);
if (!QPixmapCache::find(key, &pixmap)) { if (!QPixmapCache::find(key, &pixmap)) {
pixmap = _ozf.tile(x, y); QByteArray ba = _tar.file(tileName);
pixmap = QPixmap::fromImage(QImage::fromData(ba));
if (!pixmap.isNull()) if (!pixmap.isNull())
QPixmapCache::insert(key, pixmap); QPixmapCache::insert(key, pixmap);
} }
} else
pixmap = QPixmap(tileName);
if (pixmap.isNull()) {
qWarning("%s: error loading tile image", qPrintable(
_tileName.arg(QString::number(x), QString::number(y))));
painter->fillRect(QRectF(QPoint(x, y), _tileSize), Qt::white);
} else
painter->drawPixmap(QPoint(x, y), pixmap); painter->drawPixmap(QPoint(x, y), pixmap);
}
}
} else {
if (!_img || _img->isNull())
painter->fillRect(rect, Qt::white);
else {
QPoint p = rect.topLeft().toPoint();
QImage crop = _img->copy(QRect(p, rect.size().toSize()));
painter->drawImage(rect.topLeft(), crop);
} }
} }
} }
void OfflineMap::drawOZF(QPainter *painter, const QRectF &rect)
{
QPoint tl = QPoint((int)floor(rect.left() / _ozf.tileSize().width())
* _ozf.tileSize().width(), (int)floor(rect.top()
/ _ozf.tileSize().height()) * _ozf.tileSize().height());
QSizeF s(rect.right() - tl.x(), rect.bottom() - tl.y());
for (int i = 0; i < ceil(s.width() / _ozf.tileSize().width()); i++) {
for (int j = 0; j < ceil(s.height() / _ozf.tileSize().height()); j++) {
int x = tl.x() + i * _ozf.tileSize().width();
int y = tl.y() + j * _ozf.tileSize().height();
if (!QRectF(QPointF(x, y), _ozf.tileSize()).intersects(bounds())) {
painter->fillRect(QRectF(QPoint(x, y), _tileSize), Qt::white);
continue;
}
QPixmap pixmap;
QString key = _ozf.fileName() + "/" + QString::number(x)
+ "_" + QString::number(y);
if (!QPixmapCache::find(key, &pixmap)) {
pixmap = _ozf.tile(x, y);
if (!pixmap.isNull())
QPixmapCache::insert(key, pixmap);
}
if (pixmap.isNull()) {
qWarning("%s: error loading tile image", qPrintable(key));
painter->fillRect(QRectF(QPoint(x, y), _tileSize), Qt::white);
} else
painter->drawPixmap(QPoint(x, y), pixmap);
}
}
}
void OfflineMap::drawImage(QPainter *painter, const QRectF &rect)
{
if (!_img || _img->isNull())
painter->fillRect(rect, Qt::white);
else {
QPoint p = rect.topLeft().toPoint();
QImage crop = _img->copy(QRect(p, rect.size().toSize()));
painter->drawImage(rect.topLeft(), crop);
}
}
void OfflineMap::draw(QPainter *painter, const QRectF &rect)
{
if (_ozf.isOpen())
drawOZF(painter, rect);
else if (_tileSize.isValid())
drawTiled(painter, rect);
else
drawImage(painter, rect);
}

View File

@ -79,6 +79,10 @@ private:
bool getTileInfo(const QStringList &tiles, const QString &path = QString()); bool getTileInfo(const QStringList &tiles, const QString &path = QString());
bool getImageInfo(const QString &path); bool getImageInfo(const QString &path);
void drawTiled(QPainter *painter, const QRectF &rect);
void drawOZF(QPainter *painter, const QRectF &rect);
void drawImage(QPainter *painter, const QRectF &rect);
QString _name; QString _name;
QSize _size; QSize _size;
Projection *_projection; Projection *_projection;

View File

@ -113,33 +113,26 @@ bool OZF::load(const QString &path)
return true; return true;
} }
QPixmap OZF::blankTile()
{
QPixmap p(tileSize());
p.fill();
return p;
}
QPixmap OZF::tile(int x, int y) QPixmap OZF::tile(int x, int y)
{ {
Q_ASSERT(_file.isOpen()); Q_ASSERT(_file.isOpen());
int i = (y/tileSize().height()) * _dim.width() + (x/tileSize().width()); int i = (y/tileSize().height()) * _dim.width() + (x/tileSize().width());
if (i >= _tiles.size() - 1 || i < 0) if (i >= _tiles.size() - 1 || i < 0)
return blankTile(); return QPixmap();
int size = _tiles.at(i+1) - _tiles.at(i); int size = _tiles.at(i+1) - _tiles.at(i);
if (!_file.seek(_tiles.at(i))) if (!_file.seek(_tiles.at(i)))
return blankTile(); return QPixmap();
QByteArray ba = _file.read(size); QByteArray ba = _file.read(size);
if (ba.size() != size) if (ba.size() != size)
return blankTile(); return QPixmap();
quint32 bes = qToBigEndian(tileSize().width() * tileSize().height()); quint32 bes = qToBigEndian(tileSize().width() * tileSize().height());
ba.prepend(QByteArray((char*)&bes, sizeof(bes))); ba.prepend(QByteArray((char*)&bes, sizeof(bes)));
QByteArray uba = qUncompress(ba); QByteArray uba = qUncompress(ba);
if (uba.size() != tileSize().width() * tileSize().height()) if (uba.size() != tileSize().width() * tileSize().height())
return blankTile(); return QPixmap();
QImage img((const uchar*)uba.constData(), tileSize().width(), QImage img((const uchar*)uba.constData(), tileSize().width(),
tileSize().height(), QImage::Format_Indexed8); tileSize().height(), QImage::Format_Indexed8);

View File

@ -24,7 +24,6 @@ private:
template<class T> bool readValue(T &val); template<class T> bool readValue(T &val);
bool readHeaders(); bool readHeaders();
bool readTileTable(); bool readTileTable();
QPixmap blankTile();
QSize _size; QSize _size;
QSize _dim; QSize _dim;