1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-24 03:35:53 +01:00

Code/API cleanup

This commit is contained in:
Martin Tůma 2018-08-23 20:26:10 +02:00
parent 27886ea96a
commit 1f71b3a3b2
21 changed files with 168 additions and 128 deletions

View File

@ -140,7 +140,8 @@ HEADERS += src/config.h \
src/GUI/kv.h \
src/data/locparser.h \
src/data/slfparser.h \
src/map/geotiffmap.h
src/map/geotiffmap.h \
src/map/image.h
SOURCES += src/main.cpp \
src/common/coordinates.cpp \
src/common/rectc.cpp \
@ -244,7 +245,8 @@ SOURCES += src/main.cpp \
src/map/map.cpp \
src/data/locparser.cpp \
src/data/slfparser.cpp \
src/map/geotiffmap.cpp
src/map/geotiffmap.cpp \
src/map/image.cpp
RESOURCES += gpxsee.qrc
TRANSLATIONS = lang/gpxsee_cs.ts \

View File

@ -27,7 +27,6 @@ MapView::MapView(Map *map, POI *poi, QWidget *parent)
Q_ASSERT(map != 0);
Q_ASSERT(poi != 0);
_opengl = false;
_scene = new QGraphicsScene(this);
setScene(_scene);
setDragMode(QGraphicsView::ScrollHandDrag);
@ -42,11 +41,11 @@ MapView::MapView(Map *map, POI *poi, QWidget *parent)
_scene->addItem(_mapScale);
_map = map;
_map->load();
#ifdef ENABLE_HIDPI
_ratio = devicePixelRatioF();
_map->setDevicePixelRatio(_ratio);
#endif // ENABLE_HIDPI
_map->load();
connect(_map, SIGNAL(loaded()), this, SLOT(reloadMap()));
_poi = poi;
@ -76,6 +75,7 @@ MapView::MapView(Map *map, POI *poi, QWidget *parent)
_poiSize = 8;
_poiColor = Qt::black;
_opengl = false;
_plot = false;
_digitalZoom = 0;
@ -269,11 +269,10 @@ void MapView::setMap(Map *map)
disconnect(_map, SIGNAL(loaded()), this, SLOT(reloadMap()));
_map = map;
_map->load();
#ifdef ENABLE_HIDPI
_map->setDevicePixelRatio(_ratio);
#endif // ENABLE_HIDPI
_map->setOpenGLEnabled(_opengl);
_map->load();
connect(_map, SIGNAL(loaded()), this, SLOT(reloadMap()));
digitalZoom(0);
@ -509,7 +508,6 @@ void MapView::plot(QPainter *painter, const QRectF &target, qreal scale,
#ifdef ENABLE_HIDPI
_map->setDevicePixelRatio(1.0);
#endif // ENABLE_HIDPI
_map->setOpenGLEnabled(false);
// Compute sizes & ratios
orig = viewport()->rect();
@ -571,7 +569,6 @@ void MapView::plot(QPainter *painter, const QRectF &target, qreal scale,
#ifdef ENABLE_HIDPI
_map->setDevicePixelRatio(_ratio);
#endif // ENABLE_HIDPI
_map->setOpenGLEnabled(_opengl);
_plot = false;
setUpdatesEnabled(true);
}
@ -769,9 +766,17 @@ void MapView::drawBackground(QPainter *painter, const QRectF &rect)
if (_showMap) {
QRectF ir = rect.intersected(_map->bounds());
Map::Flags flags = Map::NoFlags;
if (_opacity < 1.0)
painter->setOpacity(_opacity);
_map->draw(painter, ir, _plot);
if (_plot)
flags = Map::Block;
else if (_opengl)
flags = Map::OpenGL;
_map->draw(painter, ir, flags);
}
}
@ -818,8 +823,6 @@ void MapView::useOpenGL(bool use)
setViewport(new OPENGL_WIDGET);
else
setViewport(new QWidget);
_map->setOpenGLEnabled(_opengl);
}
void MapView::useAntiAliasing(bool use)

View File

@ -255,14 +255,12 @@ Coordinates Atlas::xy2ll(const QPointF &p)
return _maps.at(idx)->xy2ll(p2);
}
void Atlas::draw(QPainter *painter, const QRectF &rect, bool block)
void Atlas::draw(QPainter *painter, const QRectF &rect, Flags flags)
{
Q_UNUSED(block);
// All in one map
for (int i = _zooms.at(_zoom).first; i <= _zooms.at(_zoom).last; i++) {
if (_bounds.at(i).xy.contains(rect)) {
draw(painter, rect, i);
draw(painter, rect, i, flags);
return;
}
}
@ -271,11 +269,12 @@ void Atlas::draw(QPainter *painter, const QRectF &rect, bool block)
for (int i = _zooms.at(_zoom).first; i <= _zooms.at(_zoom).last; i++) {
QRectF ir = rect.intersected(_bounds.at(i).xy);
if (!ir.isNull())
draw(painter, ir, i);
draw(painter, ir, i, flags);
}
}
void Atlas::draw(QPainter *painter, const QRectF &rect, int mapIndex)
void Atlas::draw(QPainter *painter, const QRectF &rect, int mapIndex,
Flags flags)
{
OziMap *map = _maps.at(mapIndex);
const QPointF offset = _bounds.at(mapIndex).xy.topLeft();
@ -284,7 +283,7 @@ void Atlas::draw(QPainter *painter, const QRectF &rect, int mapIndex)
map->load();
painter->translate(offset);
map->draw(painter, pr, true);
map->draw(painter, pr, flags);
painter->translate(-offset);
}

View File

@ -26,7 +26,7 @@ public:
QPointF ll2xy(const Coordinates &c);
Coordinates xy2ll(const QPointF &p);
void draw(QPainter *painter, const QRectF &rect, bool block);
void draw(QPainter *painter, const QRectF &rect, Flags flags);
void setDevicePixelRatio(qreal ratio);
void unload();
@ -53,7 +53,7 @@ private:
Bounds(const RectD &pp, const QRectF &xy) : pp(pp), xy(xy) {}
};
void draw(QPainter *painter, const QRectF &rect, int mapIndex);
void draw(QPainter *painter, const QRectF &rect, int mapIndex, Flags flags);
void computeZooms();
void computeBounds();

View File

@ -85,11 +85,11 @@ int EmptyMap::zoomOut()
return _zoom;
}
void EmptyMap::draw(QPainter *painter, const QRectF &rect, bool block)
void EmptyMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
{
Q_UNUSED(painter);
Q_UNUSED(rect);
Q_UNUSED(block);
Q_UNUSED(flags);
}
QPointF EmptyMap::ll2xy(const Coordinates &c)

View File

@ -24,7 +24,7 @@ public:
QPointF ll2xy(const Coordinates &c);
Coordinates xy2ll(const QPointF &p);
void draw(QPainter *painter, const QRectF &rect, bool block);
void draw(QPainter *painter, const QRectF &rect, Flags flags);
private:
int _zoom;

View File

@ -3,29 +3,29 @@
#include <QImageReader>
#include "config.h"
#include "geotiff.h"
#include "image.h"
#include "geotiffmap.h"
GeoTIFFMap::GeoTIFFMap(const QString &fileName, QObject *parent)
: Map(parent), _img(0), _ratio(1.0), _opengl(false), _valid(false)
: Map(parent), _fileName(fileName), _img(0), _ratio(1.0), _valid(false)
{
QImageReader ir(fileName);
if (!ir.canRead()) {
_errorString = "Unsupported/invalid image file";
return;
}
_size = ir.size();
GeoTIFF gt(fileName);
if (!gt.isValid()) {
_errorString = gt.errorString();
return;
} else {
_path = fileName;
_projection = gt.projection();
_transform = gt.transform();
}
QImageReader img(_path);
_size = img.size();
if (!_size.isValid()) {
_errorString = QString("%1: Invalid image file").arg(_path);
return;
}
_valid = true;
}
@ -36,30 +36,10 @@ GeoTIFFMap::~GeoTIFFMap()
QString GeoTIFFMap::name() const
{
QFileInfo fi(_path);
QFileInfo fi(_fileName);
return fi.fileName();
}
void GeoTIFFMap::load()
{
if (!_img) {
_img = new QImage(_path);
if (!_img || _img->isNull()) {
qWarning("%s: error loading map image", qPrintable(_path));
return;
}
#ifdef ENABLE_HIDPI
_img->setDevicePixelRatio(_ratio);
#endif // ENABLE_HIDPI
}
}
void GeoTIFFMap::unload()
{
delete _img;
_img = 0;
}
QPointF GeoTIFFMap::ll2xy(const Coordinates &c)
{
return QPointF(_transform.proj2img(_projection.ll2xy(c))) / _ratio;
@ -75,16 +55,27 @@ QRectF GeoTIFFMap::bounds()
return QRectF(QPointF(0, 0), _size / _ratio);
}
void GeoTIFFMap::draw(QPainter *painter, const QRectF &rect, bool block)
void GeoTIFFMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
{
Q_UNUSED(block)
if (_img && !_img->isNull()) {
QRectF sr(rect.topLeft() * _ratio, rect.size() * _ratio);
if (_opengl) {
QImage img(_img->copy(sr.toRect()));
painter->drawImage(rect.topLeft(), img);
} else
painter->drawImage(rect.topLeft(), *_img, sr);
}
if (_img)
_img->draw(painter, rect, flags);
}
void GeoTIFFMap::setDevicePixelRatio(qreal ratio)
{
_ratio = ratio;
if (_img)
_img->setDevicePixelRatio(_ratio);
}
void GeoTIFFMap::load()
{
if (!_img)
_img = new Image(_fileName);
}
void GeoTIFFMap::unload()
{
delete _img;
_img = 0;
}

View File

@ -5,6 +5,8 @@
#include "projection.h"
#include "map.h"
class Image;
class GeoTIFFMap : public Map
{
Q_OBJECT
@ -26,24 +28,22 @@ public:
QPointF ll2xy(const Coordinates &c);
Coordinates xy2ll(const QPointF &p);
void draw(QPainter *painter, const QRectF &rect, bool block);
void draw(QPainter *painter, const QRectF &rect, Flags flags);
void setDevicePixelRatio(qreal ratio) {_ratio = ratio;}
void setOpenGLEnabled(bool enabled) {_opengl = enabled;}
void load();
void unload();
void setDevicePixelRatio(qreal ratio);
bool isValid() const {return _valid;}
QString errorString() const {return _errorString;}
private:
QString _path;
QString _fileName;
Projection _projection;
Transform _transform;
QImage *_img;
Image *_img;
QSize _size;
qreal _ratio;
bool _opengl;
bool _valid;
QString _errorString;

29
src/map/image.cpp Normal file
View File

@ -0,0 +1,29 @@
#include <QPainter>
#include "config.h"
#include "image.h"
#define TILE_SIZE 256
Image::Image(const QString &fileName) : _img(fileName), _ratio(1.0)
{
}
void Image::draw(QPainter *painter, const QRectF &rect, Map::Flags flags)
{
QRectF sr(rect.topLeft() * _ratio, rect.size() * _ratio);
if (flags & Map::OpenGL) {
QImage img(_img.copy(sr.toRect()));
painter->drawImage(rect.topLeft(), img);
} else
painter->drawImage(rect.topLeft(), _img, sr);
}
void Image::setDevicePixelRatio(qreal ratio)
{
#ifdef ENABLE_HIDPI
_ratio = ratio;
_img.setDevicePixelRatio(_ratio);
#endif // ENABLE_HIDPI
}

23
src/map/image.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef IMAGE_H
#define IMAGE_H
#include <QImage>
#include "map.h"
class QPainter;
class Image
{
public:
Image(const QString &fileName);
QSize size() const {return _img.size();}
void draw(QPainter *painter, const QRectF &rect, Map::Flags flags);
void setDevicePixelRatio(qreal ratio);
private:
QImage _img;
qreal _ratio;
};
#endif // IMAGE_H

View File

@ -245,9 +245,9 @@ bool JNXMap::cb(Tile *tile, void *context)
return true;
}
void JNXMap::draw(QPainter *painter, const QRectF &rect, bool block)
void JNXMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
{
Q_UNUSED(block);
Q_UNUSED(flags);
const RTree<Tile*, qreal, 2> &tree = _zooms.at(_zoom).tree;
Ctx ctx(painter, &_file, _ratio);
QRectF rr(rect.topLeft() * _ratio, rect.size() * _ratio);

View File

@ -30,7 +30,7 @@ public:
QPointF ll2xy(const Coordinates &c);
Coordinates xy2ll(const QPointF &p);
void draw(QPainter *painter, const QRectF &rect, bool block);
void draw(QPainter *painter, const QRectF &rect, Flags flags);
void setDevicePixelRatio(qreal ratio) {_ratio = ratio;}

View File

@ -4,6 +4,7 @@
#include <QObject>
#include <QString>
#include <QRectF>
#include <QFlags>
#include "common/coordinates.h"
class QPainter;
@ -14,6 +15,13 @@ class Map : public QObject
Q_OBJECT
public:
enum Flag {
NoFlags = 0,
Block = 1,
OpenGL = 2
};
Q_DECLARE_FLAGS(Flags, Flag)
Map(QObject *parent = 0) : QObject(parent) {}
virtual ~Map() {}
@ -31,13 +39,12 @@ public:
virtual QPointF ll2xy(const Coordinates &c) = 0;
virtual Coordinates xy2ll(const QPointF &p) = 0;
virtual void draw(QPainter *painter, const QRectF &rect, bool block) = 0;
virtual void draw(QPainter *painter, const QRectF &rect, Flags flags) = 0;
virtual void clearCache() {}
virtual void load() {}
virtual void unload() {}
virtual void setDevicePixelRatio(qreal ratio) {Q_UNUSED(ratio);}
virtual void setOpenGLEnabled(bool enabled) {Q_UNUSED(enabled);}
virtual bool isValid() const {return true;}
virtual QString errorString() const {return QString();}
@ -46,4 +53,6 @@ signals:
void loaded();
};
Q_DECLARE_OPERATORS_FOR_FLAGS(Map::Flags)
#endif // MAP_H

View File

@ -126,7 +126,7 @@ qreal OnlineMap::tileSize() const
return (TILE_SIZE / coordinatesRatio());
}
void OnlineMap::draw(QPainter *painter, const QRectF &rect, bool block)
void OnlineMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
{
qreal scale = zoom2scale(_zoom);
@ -141,7 +141,7 @@ void OnlineMap::draw(QPainter *painter, const QRectF &rect, bool block)
for (int j = 0; j < ceil(s.height() / tileSize()); j++)
tiles.append(Tile(QPoint(tile.x() + i, tile.y() + j), _zoom));
if (block)
if (flags & Map::Block)
_tileLoader->loadTilesSync(tiles);
else
_tileLoader->loadTilesAsync(tiles);

View File

@ -29,7 +29,7 @@ public:
QPointF ll2xy(const Coordinates &c);
Coordinates xy2ll(const QPointF &p);
void draw(QPainter *painter, const QRectF &rect, bool block);
void draw(QPainter *painter, const QRectF &rect, Flags flags);
void setDevicePixelRatio(qreal ratio) {_deviceRatio = ratio;}
void clearCache() {_tileLoader->clearCache();}

View File

@ -3,21 +3,20 @@
#include <QMap>
#include <QDir>
#include <QBuffer>
#include <QImage>
#include <QImageReader>
#include <QPixmapCache>
#include "common/coordinates.h"
#include "common/rectc.h"
#include "tar.h"
#include "ozf.h"
#include "image.h"
#include "mapfile.h"
#include "config.h"
#include "ozimap.h"
OziMap::OziMap(const QString &fileName, QObject *parent)
: Map(parent), _img(0), _tar(0), _ozf(0), _zoom(0), _ratio(1.0),
_opengl(false), _valid(false)
: Map(parent), _img(0), _tar(0), _ozf(0), _zoom(0), _ratio(1.0), _valid(false)
{
QFileInfo fi(fileName);
QString suffix = fi.suffix().toLower();
@ -79,8 +78,7 @@ OziMap::OziMap(const QString &fileName, QObject *parent)
}
OziMap::OziMap(const QString &fileName, Tar &tar, QObject *parent)
: Map(parent), _img(0), _tar(0), _ozf(0), _zoom(0), _ratio(1.0),
_opengl(false), _valid(false)
: Map(parent), _img(0), _tar(0), _ozf(0), _zoom(0), _ratio(1.0), _valid(false)
{
QFileInfo fi(fileName);
QFileInfo map(fi.absolutePath());
@ -140,20 +138,19 @@ bool OziMap::setImageInfo(const QString &path)
if (OZF::isOZF(_map.path)) {
_ozf = new OZF(_map.path);
if (!_ozf->open()) {
_errorString = QString("%1: Error loading OZF file")
.arg(_ozf->fileName());
if (!_ozf || !_ozf->open()) {
_errorString = QString("%1: Error loading OZF file").arg(_map.path);
return false;
}
_scale = _ozf->scale(_zoom);
} else {
QImageReader img(_map.path);
_map.size = img.size();
if (!_map.size.isValid()) {
_errorString = QString("%1: Error reading map image")
.arg(QFileInfo(_map.path).fileName());
QImageReader ir(_map.path);
if (!ir.canRead()) {
_errorString = QString("%1: Unsupported/invalid image file")
.arg(_map.path);
return false;
}
_map.size = ir.size();
}
return true;
@ -208,16 +205,8 @@ void OziMap::load()
return;
}
if (!_ozf && !_img && _map.isValid()) {
_img = new QImage(_map.path);
if (!_img || _img->isNull()) {
qWarning("%s: error loading map image", qPrintable(_map.path));
return;
}
#ifdef ENABLE_HIDPI
_img->setDevicePixelRatio(_ratio);
#endif // ENABLE_HIDPI
}
if (!_tile.isValid() && !_ozf && !_img)
_img = new Image(_map.path);
}
void OziMap::unload()
@ -302,26 +291,16 @@ void OziMap::drawOZF(QPainter *painter, const QRectF &rect) const
}
}
void OziMap::drawImage(QPainter *painter, const QRectF &rect) const
void OziMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
{
QRectF sr(rect.topLeft() * _ratio, rect.size() * _ratio);
if (_opengl) {
QImage img(_img->copy(sr.toRect()));
painter->drawImage(rect.topLeft(), img);
} else
painter->drawImage(rect.topLeft(), *_img, sr);
}
void OziMap::draw(QPainter *painter, const QRectF &rect, bool block)
{
Q_UNUSED(block);
Q_UNUSED(flags);
if (_ozf)
drawOZF(painter, rect);
else if (_img)
_img->draw(painter, rect, flags);
else if (_tile.isValid())
drawTiled(painter, rect);
else if (_img && !_img->isNull())
drawImage(painter, rect);
}
QPointF OziMap::ll2xy(const Coordinates &c)
@ -391,3 +370,10 @@ void OziMap::rescale(int zoom)
_zoom = zoom;
_scale = _ozf->scale(zoom);
}
void OziMap::setDevicePixelRatio(qreal ratio)
{
_ratio = ratio;
if (_img)
_img->setDevicePixelRatio(_ratio);
}

View File

@ -7,7 +7,7 @@
class Tar;
class OZF;
class QImage;
class Image;
class OziMap : public Map
{
@ -31,10 +31,9 @@ public:
QPointF ll2xy(const Coordinates &c);
Coordinates xy2ll(const QPointF &p);
void draw(QPainter *painter, const QRectF &rect, bool block);
void draw(QPainter *painter, const QRectF &rect, Flags flags);
void setDevicePixelRatio(qreal ratio) {_ratio = ratio;}
void setOpenGLEnabled(bool enabled) {_opengl = enabled;}
void setDevicePixelRatio(qreal ratio);
void load();
void unload();
@ -61,21 +60,20 @@ private:
void drawTiled(QPainter *painter, const QRectF &rect) const;
void drawOZF(QPainter *painter, const QRectF &rect) const;
void drawImage(QPainter *painter, const QRectF &rect) const;
void drawImage(QPainter *painter, const QRectF &rect, Flags flags) const;
void rescale(int zoom);
QString _name;
Projection _projection;
Transform _transform;
QImage *_img;
Image *_img;
Tar *_tar;
OZF *_ozf;
ImageInfo _map, _tile;
int _zoom;
QPointF _scale;
qreal _ratio;
bool _opengl;
bool _valid;
QString _errorString;

View File

@ -192,7 +192,7 @@ qreal WMSMap::tileSize() const
return (TILE_SIZE / _ratio);
}
void WMSMap::draw(QPainter *painter, const QRectF &rect, bool block)
void WMSMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
{
QPoint tl = QPoint((int)floor(rect.left() / tileSize()),
(int)floor(rect.top() / tileSize()));
@ -214,7 +214,7 @@ void WMSMap::draw(QPainter *painter, const QRectF &rect, bool block)
}
}
if (block)
if (flags & Map::Block)
_tileLoader->loadTilesSync(tiles);
else
_tileLoader->loadTilesAsync(tiles);

View File

@ -29,7 +29,7 @@ public:
QPointF ll2xy(const Coordinates &c);
Coordinates xy2ll(const QPointF &p);
void draw(QPainter *painter, const QRectF &rect, bool block);
void draw(QPainter *painter, const QRectF &rect, Flags flags);
void setDevicePixelRatio(qreal ratio) {_ratio = ratio;}
void clearCache();

View File

@ -169,7 +169,7 @@ QSizeF WMTSMap::tileSize(const WMTS::Zoom &zoom) const
zoom.tile().height() / coordinatesRatio());
}
void WMTSMap::draw(QPainter *painter, const QRectF &rect, bool block)
void WMTSMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
{
const WMTS::Zoom &z = _zooms.at(_zoom);
QSizeF ts(tileSize(z));
@ -184,7 +184,7 @@ void WMTSMap::draw(QPainter *painter, const QRectF &rect, bool block)
for (int j = tl.y(); j < br.y(); j++)
tiles.append(Tile(QPoint(i, j), z.id()));
if (block)
if (flags & Map::Block)
_tileLoader->loadTilesSync(tiles);
else
_tileLoader->loadTilesAsync(tiles);

View File

@ -29,7 +29,7 @@ public:
QPointF ll2xy(const Coordinates &c);
Coordinates xy2ll(const QPointF &p);
void draw(QPainter *painter, const QRectF &rect, bool block);
void draw(QPainter *painter, const QRectF &rect, Flags flags);
void setDevicePixelRatio(qreal ratio) {_deviceRatio = ratio;}
void clearCache();