1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2025-06-27 11:39:16 +02:00

Map API refactoring

Including several map providers refactoring/rewrite
especial KML, JNX and TrekBuddy atlases.
This commit is contained in:
2023-05-04 09:38:35 +02:00
parent d593b073d5
commit 97e12d809f
60 changed files with 753 additions and 816 deletions

View File

@ -25,7 +25,6 @@ ToolTip AreaItem::info() const
AreaItem::AreaItem(const Area &area, Map *map, GraphicsItem *parent)
: PlaneItem(parent), _area(area)
{
_map = map;
_digitalZoom = 0;
_width = 2;
_opacity = 0.5;
@ -35,34 +34,34 @@ AreaItem::AreaItem(const Area &area, Map *map, GraphicsItem *parent)
_pen = QPen(strokeColor(), width());
_brush = QBrush(fillColor());
updatePainterPath();
updatePainterPath(map);
setCursor(Qt::ArrowCursor);
setAcceptHoverEvents(true);
}
QPainterPath AreaItem::painterPath(const Polygon &polygon)
QPainterPath AreaItem::painterPath(Map *map, const Polygon &polygon)
{
QPainterPath path;
for (int i = 0; i < polygon.size(); i++) {
const QVector<Coordinates> &subpath = polygon.at(i);
path.moveTo(_map->ll2xy(subpath.first()));
path.moveTo(map->ll2xy(subpath.first()));
for (int j = 1; j < subpath.size(); j++)
path.lineTo(_map->ll2xy(subpath.at(j)));
path.lineTo(map->ll2xy(subpath.at(j)));
path.closeSubpath();
}
return path;
}
void AreaItem::updatePainterPath()
void AreaItem::updatePainterPath(Map *map)
{
_painterPath = QPainterPath();
for (int i = 0; i < _area.polygons().size(); i++)
_painterPath.addPath(painterPath(_area.polygons().at(i)));
_painterPath.addPath(painterPath(map, _area.polygons().at(i)));
}
void AreaItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
@ -85,10 +84,7 @@ void AreaItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
void AreaItem::setMap(Map *map)
{
prepareGeometryChange();
_map = map;
updatePainterPath();
updatePainterPath(map);
}
const QColor &AreaItem::strokeColor() const

View File

@ -33,8 +33,8 @@ protected:
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
private:
QPainterPath painterPath(const Polygon &polygon);
void updatePainterPath();
QPainterPath painterPath(Map *map, const Polygon &polygon);
void updatePainterPath(Map *map);
void updateColor();
void updateWidth();
void updatePenStyle();
@ -45,8 +45,6 @@ private:
Area _area;
Map *_map;
qreal _width;
QColor _color;
qreal _opacity;

View File

@ -1721,8 +1721,7 @@ bool GUI::loadMapNode(const TreeNode<Map*> &node, MapAction *&action,
bool GUI::loadMap(const QString &fileName, MapAction *&action, bool silent)
{
TreeNode<Map*> maps(MapList::loadMaps(fileName,
CRS::projection(_options.inputProjection)));
TreeNode<Map*> maps(MapList::loadMaps(fileName));
QList<QAction*> existingActions(_mapsActionGroup->actions());
return loadMapNode(maps, action, silent, existingActions);
@ -1812,8 +1811,7 @@ void GUI::loadMapDir()
return;
QFileInfo fi(dir);
TreeNode<Map*> maps(MapList::loadMaps(dir,
CRS::projection(_options.inputProjection)));
TreeNode<Map*> maps(MapList::loadMaps(dir));
QList<QAction*> existingActions(_mapsActionGroup->actions());
QList<MapAction*> actions;
QMenu *menu = new QMenu(maps.name());
@ -2741,13 +2739,9 @@ void GUI::loadOptions()
_mapView->setPOIColor(_options.poiColor);
_mapView->setRenderHint(QPainter::Antialiasing, _options.pathAntiAliasing);
_mapView->setMarkerColor(_options.sliderColor);
if (_options.useOpenGL)
_mapView->useOpenGL(true);
_mapView->setDevicePixelRatio(devicePixelRatioF(),
_options.hidpiMap ? devicePixelRatioF() : 1.0);
_mapView->setOutputProjection(CRS::projection(4326,
_options.outputProjection));
_mapView->setInputProjection(CRS::projection(_options.inputProjection));
_mapView->useOpenGL(_options.useOpenGL);
_mapView->setMapConfig(CRS::projection(_options.inputProjection),
CRS::projection(4326, _options.outputProjection), _options.hidpiMap);
_mapView->setTimeZone(_options.timeZone.zone());
_mapView->setPositionSource(_positionSource);
@ -2856,14 +2850,12 @@ void GUI::updateOptions(const Options &options)
_positionSource = source;
}
if (options.hidpiMap != _options.hidpiMap)
_mapView->setDevicePixelRatio(devicePixelRatioF(),
options.hidpiMap ? devicePixelRatioF() : 1.0);
if (options.outputProjection != _options.outputProjection)
_mapView->setOutputProjection(CRS::projection(4326,
options.outputProjection));
if (options.inputProjection != _options.inputProjection)
_mapView->setInputProjection(CRS::projection(options.inputProjection));
if (options.hidpiMap != _options.hidpiMap
|| options.outputProjection != _options.outputProjection
|| options.inputProjection != _options.inputProjection)
_mapView->setMapConfig(CRS::projection(options.inputProjection),
CRS::projection(4326, options.outputProjection), options.hidpiMap);
if (options.timeZone != _options.timeZone) {
_mapView->setTimeZone(options.timeZone.zone());
_dateRange.first = _dateRange.first.toTimeZone(options.timeZone.zone());
@ -2941,8 +2933,7 @@ void GUI::loadInitialMaps(const QString &selected)
if (mapDir.isNull())
return;
TreeNode<Map*> maps(MapList::loadMaps(mapDir,
CRS::projection(_options.inputProjection)));
TreeNode<Map*> maps(MapList::loadMaps(mapDir));
createMapNodeMenu(createMapActionsNode(maps), _mapMenu, _mapsEnd);
// Select the active map according to the user settings
@ -3044,8 +3035,7 @@ void GUI::show()
void GUI::screenChanged(QScreen *screen)
{
_mapView->setDevicePixelRatio(devicePixelRatioF(),
_options.hidpiMap ? devicePixelRatioF() : 1.0);
_mapView->setDevicePixelRatio(devicePixelRatioF());
disconnect(SIGNAL(logicalDotsPerInchChanged(qreal)), this,
SLOT(logicalDotsPerInchChanged(qreal)));
@ -3057,6 +3047,5 @@ void GUI::logicalDotsPerInchChanged(qreal dpi)
{
Q_UNUSED(dpi)
_mapView->setDevicePixelRatio(devicePixelRatioF(),
_options.hidpiMap ? devicePixelRatioF() : 1.0);
_mapView->setDevicePixelRatio(devicePixelRatioF());
}

View File

@ -81,19 +81,18 @@ ToolTip MapItem::info() const
return tt;
}
MapItem::MapItem(MapAction *action, Map *map, GraphicsItem *parent)
: PlaneItem(parent)
MapItem::MapItem(MapAction *action, Map *map, const Projection &proj,
GraphicsItem *parent) : PlaneItem(parent)
{
Map *src = action->data().value<Map*>();
Q_ASSERT(map->isReady());
_name = src->name();
_fileName = src->path();
_bounds = src->llBounds();
_bounds = src->llBounds(proj);
connect(this, &MapItem::triggered, action, &MapAction::trigger);
_map = map;
_digitalZoom = 0;
_width = 2;
@ -101,23 +100,23 @@ MapItem::MapItem(MapAction *action, Map *map, GraphicsItem *parent)
QBrush brush(Qt::SolidPattern);
_pen = QPen(brush, _width);
updatePainterPath();
updatePainterPath(map);
setCursor(Qt::ArrowCursor);
setAcceptHoverEvents(true);
}
void MapItem::updatePainterPath()
void MapItem::updatePainterPath(Map *map)
{
_painterPath = QPainterPath();
QRectF r(bbox(_bounds, _map));
QRectF r(bbox(_bounds, map));
if (r.left() > r.right()) {
QRectF r1(bbox(RectC(_bounds.topLeft(),
Coordinates(180, _bounds.bottomRight().lat())), _map));
Coordinates(180, _bounds.bottomRight().lat())), map));
QRectF r2(bbox(RectC(Coordinates(-180, _bounds.topLeft().lat()),
_bounds.bottomRight()), _map));
_bounds.bottomRight()), map));
_painterPath.addRect(r1);
_painterPath.addRect(r2);
@ -143,10 +142,7 @@ void MapItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
void MapItem::setMap(Map *map)
{
prepareGeometryChange();
_map = map;
updatePainterPath();
updatePainterPath(map);
}
void MapItem::setColor(const QColor &color)

View File

@ -4,13 +4,15 @@
#include "planeitem.h"
class MapAction;
class Projection;
class MapItem : public QObject, public PlaneItem
{
Q_OBJECT
public:
MapItem(MapAction *action, Map *map, GraphicsItem *parent = 0);
MapItem(MapAction *action, Map *map, const Projection &proj,
GraphicsItem *parent = 0);
QPainterPath shape() const {return _painterPath;}
QRectF boundingRect() const {return _painterPath.boundingRect();}
@ -37,13 +39,12 @@ protected:
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
private:
void updatePainterPath();
void updatePainterPath(Map *map);
RectC _bounds;
QString _name;
QString _fileName;
Map *_map;
int _digitalZoom;
qreal _width;

View File

@ -60,12 +60,12 @@ MapView::MapView(Map *map, POI *poi, QWidget *parent) : QGraphicsView(parent)
_cursorCoordinates->setVisible(false);
_scene->addItem(_cursorCoordinates);
_deviceRatio = devicePixelRatioF();
_outputProjection = PCS::pcs(3857);
_inputProjection = GCS::gcs(4326);
_hidpi = true;
_map = map;
_map->load();
_map->setOutputProjection(_outputProjection);
_map->setInputProjection(_inputProjection);
_map->load(_inputProjection, _outputProjection, _deviceRatio, _hidpi);
connect(_map, &Map::tilesLoaded, this, &MapView::reloadMap);
_poi = poi;
@ -119,8 +119,6 @@ MapView::MapView(Map *map, POI *poi, QWidget *parent) : QGraphicsView(parent)
_showPositionCoordinates = false;
_showMotionInfo = false;
_deviceRatio = 1.0;
_mapRatio = 1.0;
_opengl = false;
_plot = false;
_digitalZoom = 0;
@ -247,7 +245,7 @@ void MapView::addWaypoints(const QVector<Waypoint> &waypoints)
MapItem *MapView::addMap(MapAction *map)
{
MapItem *mi = new MapItem(map, _map);
MapItem *mi = new MapItem(map, _map, _inputProjection);
mi->setColor(_palette.nextColor());
mi->setWidth(_areaWidth);
mi->setPenStyle(_areaStyle);
@ -328,7 +326,7 @@ int MapView::fitMapZoom() const
RectC br = _tr | _rr | _wr | _ar;
return _map->zoomFit(viewport()->size() - QSize(2*MARGIN, 2*MARGIN),
br.isNull() ? _map->llBounds() : br);
br.isNull() ? _map->llBounds(_inputProjection) : br);
}
QPointF MapView::contentCenter() const
@ -402,36 +400,18 @@ void MapView::setMap(Map *map)
.intersected(_map->bounds()));
RectC cr(_map->xy2ll(vr.topLeft()), _map->xy2ll(vr.bottomRight()));
_map->unload();
disconnect(_map, &Map::tilesLoaded, this, &MapView::reloadMap);
_map->unload();
_map = map;
_map->load();
_map->setOutputProjection(_outputProjection);
_map->setInputProjection(_inputProjection);
_map->setDevicePixelRatio(_deviceRatio, _mapRatio);
_map->load(_inputProjection, _outputProjection, _deviceRatio, _hidpi);
connect(_map, &Map::tilesLoaded, this, &MapView::reloadMap);
digitalZoom(0);
_map->zoomFit(viewport()->rect().size(), cr);
_scene->setSceneRect(_map->bounds());
for (int i = 0; i < _tracks.size(); i++)
_tracks.at(i)->setMap(_map);
for (int i = 0; i < _routes.size(); i++)
_routes.at(i)->setMap(_map);
for (int i = 0; i < _areas.size(); i++)
_areas.at(i)->setMap(_map);
for (int i = 0; i < _waypoints.size(); i++)
_waypoints.at(i)->setMap(_map);
for (POIHash::const_iterator it = _pois.constBegin();
it != _pois.constEnd(); it++)
it.value()->setMap(_map);
updatePOIVisibility();
_crosshair->setMap(_map);
rescale();
QPointF nc = QRectF(_map->ll2xy(cr.topLeft()),
_map->ll2xy(cr.bottomRight())).center();
@ -702,8 +682,9 @@ void MapView::plot(QPainter *painter, const QRectF &target, qreal scale,
PlotFlags flags)
{
QRect orig, adj;
qreal mapRatio, ratio, diff, q, p;
qreal ratio, diff, q, p;
QPointF scenePos, scalePos, posPos, motionPos;
bool hidpi = _hidpi && _deviceRatio > 1.0;
int zoom;
@ -728,8 +709,8 @@ void MapView::plot(QPainter *painter, const QRectF &target, qreal scale,
}
// Expand the view if plotting into a bitmap
mapRatio = _mapRatio;
setDevicePixelRatio(_deviceRatio, 1.0);
if (hidpi)
setHidpi(false);
if (flags & Expand) {
qreal xdiff = (target.width() - adj.width()) / 2.0;
@ -784,7 +765,8 @@ void MapView::plot(QPainter *painter, const QRectF &target, qreal scale,
centerOn(scenePos);
}
setDevicePixelRatio(_deviceRatio, mapRatio);
if (hidpi)
setHidpi(true);
_mapScale->setDigitalZoom(_digitalZoom);
_mapScale->setPos(scalePos);
@ -1236,6 +1218,9 @@ bool MapView::gestureEvent(QGestureEvent *event)
void MapView::useOpenGL(bool use)
{
if (_opengl == use)
return;
_opengl = use;
if (use)
@ -1286,60 +1271,21 @@ void MapView::reloadMap()
_scene->invalidate();
}
void MapView::setDevicePixelRatio(qreal deviceRatio, qreal mapRatio)
void MapView::setMapConfig(const Projection &in, const Projection &out,
bool hidpi)
{
if (_deviceRatio == deviceRatio && _mapRatio == mapRatio)
return;
_inputProjection = in;
_outputProjection = out;
_hidpi = hidpi;
_deviceRatio = deviceRatio;
_mapRatio = mapRatio;
QRectF vr(mapToScene(viewport()->rect()).boundingRect()
.intersected(_map->bounds()));
RectC cr(_map->xy2ll(vr.topLeft()), _map->xy2ll(vr.bottomRight()));
_map->setDevicePixelRatio(_deviceRatio, _mapRatio);
_scene->setSceneRect(_map->bounds());
for (int i = 0; i < _tracks.size(); i++)
_tracks.at(i)->setMap(_map);
for (int i = 0; i < _routes.size(); i++)
_routes.at(i)->setMap(_map);
for (int i = 0; i < _areas.size(); i++)
_areas.at(i)->setMap(_map);
for (int i = 0; i < _waypoints.size(); i++)
_waypoints.at(i)->setMap(_map);
for (POIHash::const_iterator it = _pois.constBegin();
it != _pois.constEnd(); it++)
it.value()->setMap(_map);
updatePOIVisibility();
_crosshair->setMap(_map);
QPointF nc = QRectF(_map->ll2xy(cr.topLeft()),
_map->ll2xy(cr.bottomRight())).center();
centerOn(nc);
reloadMap();
setMap(_map);
}
void MapView::setOutputProjection(const Projection &proj)
void MapView::setDevicePixelRatio(qreal ratio)
{
_outputProjection = proj;
Coordinates center = _map->xy2ll(mapToScene(viewport()->rect().center()));
_map->setOutputProjection(_outputProjection);
rescale();
centerOn(_map->ll2xy(center));
}
_deviceRatio = ratio;
void MapView::setInputProjection(const Projection &proj)
{
_inputProjection = proj;
Coordinates center = _map->xy2ll(mapToScene(viewport()->rect().center()));
_map->setInputProjection(_inputProjection);
rescale();
centerOn(_map->ll2xy(center));
setMap(_map);
}
void MapView::fitContentToSize()
@ -1436,3 +1382,20 @@ void MapView::drawInfoBackground(bool draw)
_positionCoordinates->drawBackground(draw);
_motionInfo->drawBackground(draw);
}
void MapView::setHidpi(bool hidpi)
{
QRectF vr(mapToScene(viewport()->rect()).boundingRect()
.intersected(_map->bounds()));
RectC cr(_map->xy2ll(vr.topLeft()), _map->xy2ll(vr.bottomRight()));
_map->unload();
_map->load(_inputProjection, _outputProjection, _deviceRatio, hidpi);
rescale();
QPointF nc = QRectF(_map->ll2xy(cr.topLeft()),
_map->ll2xy(cr.bottomRight())).center();
centerOn(nc);
reloadMap();
}

View File

@ -92,9 +92,8 @@ public:
void useAntiAliasing(bool use);
void setCoordinatesFormat(CoordinatesFormat format);
void setTimeZone(const QTimeZone &zone);
void setDevicePixelRatio(qreal deviceRatio, qreal mapRatio);
void setOutputProjection(const Projection &proj);
void setInputProjection(const Projection &proj);
void setMapConfig(const Projection &in, const Projection &out, bool hidpi);
void setDevicePixelRatio(qreal ratio);
void clearMapCache();
void fitContentToSize();
@ -156,6 +155,7 @@ private:
bool gestureEvent(QGestureEvent *event);
void pinchGesture(QPinchGesture *gesture);
void skipColor() {_palette.nextColor();}
void setHidpi(bool hidpi);
void mouseMoveEvent(QMouseEvent *event);
void mousePressEvent(QMouseEvent *event);
@ -209,7 +209,7 @@ private:
QCursor _cursor;
qreal _deviceRatio;
qreal _mapRatio;
bool _hidpi;
bool _opengl;
int _pinchZoom;