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:
parent
ccf91bb29f
commit
fd53e89ea5
@ -305,7 +305,7 @@ 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);
|
||||||
@ -540,9 +540,8 @@ 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());
|
||||||
@ -552,6 +551,12 @@ void OfflineMap::draw(QPainter *painter, const QRectF &rect)
|
|||||||
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();
|
||||||
|
|
||||||
|
if (!QRectF(QPointF(x, y), _ozf.tileSize()).intersects(bounds())) {
|
||||||
|
painter->fillRect(QRectF(QPoint(x, y), _tileSize), Qt::white);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
QString tileName(_tileName.arg(QString::number(x),
|
QString tileName(_tileName.arg(QString::number(x),
|
||||||
QString::number(y)));
|
QString::number(y)));
|
||||||
QPixmap pixmap;
|
QPixmap pixmap;
|
||||||
@ -570,25 +575,30 @@ void OfflineMap::draw(QPainter *painter, const QRectF &rect)
|
|||||||
if (pixmap.isNull()) {
|
if (pixmap.isNull()) {
|
||||||
qWarning("%s: error loading tile image", qPrintable(
|
qWarning("%s: error loading tile image", qPrintable(
|
||||||
_tileName.arg(QString::number(x), QString::number(y))));
|
_tileName.arg(QString::number(x), QString::number(y))));
|
||||||
painter->fillRect(QRectF(QPoint(x, y), _tileSize),
|
painter->fillRect(QRectF(QPoint(x, y), _tileSize), Qt::white);
|
||||||
Qt::white);
|
|
||||||
} else
|
} else
|
||||||
painter->drawPixmap(QPoint(x, y), pixmap);
|
painter->drawPixmap(QPoint(x, y), pixmap);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (_ozf.isOpen()) {
|
}
|
||||||
QPoint tl = QPoint((int)floor(rect.left()
|
|
||||||
/ (qreal)_ozf.tileSize().width()) * _ozf.tileSize().width(),
|
void OfflineMap::drawOZF(QPainter *painter, const QRectF &rect)
|
||||||
(int)floor(rect.top() / _ozf.tileSize().height())
|
{
|
||||||
* _ozf.tileSize().height());
|
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());
|
QSizeF s(rect.right() - tl.x(), rect.bottom() - tl.y());
|
||||||
for (int i = 0; i < ceil(s.width() / _ozf.tileSize().width()); i++) {
|
for (int i = 0; i < ceil(s.width() / _ozf.tileSize().width()); i++) {
|
||||||
for (int j = 0; j < ceil(s.height() / _ozf.tileSize().height());
|
for (int j = 0; j < ceil(s.height() / _ozf.tileSize().height()); j++) {
|
||||||
j++) {
|
|
||||||
int x = tl.x() + i * _ozf.tileSize().width();
|
int x = tl.x() + i * _ozf.tileSize().width();
|
||||||
int y = tl.y() + j * _ozf.tileSize().height();
|
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;
|
QPixmap pixmap;
|
||||||
QString key = _ozf.fileName() + "/" + QString::number(x)
|
QString key = _ozf.fileName() + "/" + QString::number(x)
|
||||||
+ "_" + QString::number(y);
|
+ "_" + QString::number(y);
|
||||||
@ -598,10 +608,17 @@ void OfflineMap::draw(QPainter *painter, const QRectF &rect)
|
|||||||
QPixmapCache::insert(key, pixmap);
|
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);
|
painter->drawPixmap(QPoint(x, y), pixmap);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
|
||||||
|
void OfflineMap::drawImage(QPainter *painter, const QRectF &rect)
|
||||||
|
{
|
||||||
if (!_img || _img->isNull())
|
if (!_img || _img->isNull())
|
||||||
painter->fillRect(rect, Qt::white);
|
painter->fillRect(rect, Qt::white);
|
||||||
else {
|
else {
|
||||||
@ -609,5 +626,14 @@ void OfflineMap::draw(QPainter *painter, const QRectF &rect)
|
|||||||
QImage crop = _img->copy(QRect(p, rect.size().toSize()));
|
QImage crop = _img->copy(QRect(p, rect.size().toSize()));
|
||||||
painter->drawImage(rect.topLeft(), crop);
|
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);
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
15
src/ozf.cpp
15
src/ozf.cpp
@ -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);
|
||||||
|
Loading…
Reference in New Issue
Block a user