mirror of
https://github.com/tumic0/GPXSee.git
synced 2025-01-18 11:52:08 +01:00
Fixed the 1px content offset issue on zoom/map change
This commit is contained in:
parent
2cd35b139e
commit
7bc83603ca
@ -3,6 +3,7 @@
|
|||||||
#include <QWheelEvent>
|
#include <QWheelEvent>
|
||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
#include <QPixmapCache>
|
#include <QPixmapCache>
|
||||||
|
#include <QScrollBar>
|
||||||
#include "opengl.h"
|
#include "opengl.h"
|
||||||
#include "misc.h"
|
#include "misc.h"
|
||||||
#include "poi.h"
|
#include "poi.h"
|
||||||
@ -39,6 +40,7 @@ PathView::PathView(Map *map, POI *poi, QWidget *parent)
|
|||||||
|
|
||||||
_mapScale = new ScaleItem();
|
_mapScale = new ScaleItem();
|
||||||
_mapScale->setZValue(2.0);
|
_mapScale->setZValue(2.0);
|
||||||
|
_scene->addItem(_mapScale);
|
||||||
|
|
||||||
_map = map;
|
_map = map;
|
||||||
_poi = poi;
|
_poi = poi;
|
||||||
@ -72,13 +74,26 @@ PathView::PathView(Map *map, POI *poi, QWidget *parent)
|
|||||||
|
|
||||||
_map->setBackgroundColor(_backgroundColor);
|
_map->setBackgroundColor(_backgroundColor);
|
||||||
_scene->setSceneRect(_map->bounds());
|
_scene->setSceneRect(_map->bounds());
|
||||||
_res = _map->resolution(_scene->sceneRect().center());
|
|
||||||
|
centerOn(_scene->sceneRect().center());
|
||||||
}
|
}
|
||||||
|
|
||||||
PathView::~PathView()
|
void PathView::centerOn(const QPointF &pos)
|
||||||
{
|
{
|
||||||
if (_mapScale->scene() != _scene)
|
QGraphicsView::centerOn(pos);
|
||||||
delete _mapScale;
|
|
||||||
|
/* Fix the offset caused by QGraphicsView::centerOn() approximation */
|
||||||
|
QPointF center = mapToScene(viewport()->rect().center());
|
||||||
|
QPoint offset((int)(pos.x() - center.x()), (int)(pos.y() - center.y()));
|
||||||
|
if (qAbs(offset.x()) == 1)
|
||||||
|
horizontalScrollBar()->setValue(horizontalScrollBar()->value()
|
||||||
|
+ offset.x());
|
||||||
|
if (qAbs(offset.y()) == 1)
|
||||||
|
verticalScrollBar()->setValue(verticalScrollBar()->value()
|
||||||
|
+ offset.y());
|
||||||
|
|
||||||
|
_res = _map->resolution(pos);
|
||||||
|
_mapScale->setResolution(_res);
|
||||||
}
|
}
|
||||||
|
|
||||||
PathItem *PathView::addTrack(const Track &track)
|
PathItem *PathView::addTrack(const Track &track)
|
||||||
@ -169,13 +184,7 @@ QList<PathItem *> PathView::loadData(const Data &data)
|
|||||||
else
|
else
|
||||||
updatePOIVisibility();
|
updatePOIVisibility();
|
||||||
|
|
||||||
QPointF center = contentCenter();
|
centerOn(contentCenter());
|
||||||
centerOn(center);
|
|
||||||
|
|
||||||
_res = _map->resolution(center);
|
|
||||||
_mapScale->setResolution(_res);
|
|
||||||
if (_mapScale->scene() != _scene)
|
|
||||||
_scene->addItem(_mapScale);
|
|
||||||
|
|
||||||
return paths;
|
return paths;
|
||||||
}
|
}
|
||||||
@ -283,11 +292,7 @@ void PathView::setMap(Map *map)
|
|||||||
it.value()->setMap(_map);
|
it.value()->setMap(_map);
|
||||||
updatePOIVisibility();
|
updatePOIVisibility();
|
||||||
|
|
||||||
pos = _map->ll2xy(center);
|
centerOn(_map->ll2xy(center));
|
||||||
centerOn(pos);
|
|
||||||
|
|
||||||
_res = _map->resolution(pos);
|
|
||||||
_mapScale->setResolution(_res);
|
|
||||||
|
|
||||||
resetCachedContent();
|
resetCachedContent();
|
||||||
QPixmapCache::clear();
|
QPixmapCache::clear();
|
||||||
@ -421,15 +426,8 @@ void PathView::zoom(int zoom, const QPoint &pos, const Coordinates &c)
|
|||||||
ns = (zoom > 0) ? _map->zoomIn() : _map->zoomOut();
|
ns = (zoom > 0) ? _map->zoomIn() : _map->zoomOut();
|
||||||
|
|
||||||
if (ns != os) {
|
if (ns != os) {
|
||||||
QPoint offset = pos - viewport()->rect().center();
|
|
||||||
|
|
||||||
rescale();
|
rescale();
|
||||||
|
centerOn(_map->ll2xy(c) - (pos - viewport()->rect().center()));
|
||||||
QPointF center = _map->ll2xy(c) - offset;
|
|
||||||
centerOn(center);
|
|
||||||
|
|
||||||
_res = _map->resolution(center);
|
|
||||||
_mapScale->setResolution(_res);
|
|
||||||
} else {
|
} else {
|
||||||
if (shift)
|
if (shift)
|
||||||
digitalZoom(zoom);
|
digitalZoom(zoom);
|
||||||
@ -523,11 +521,11 @@ void PathView::plot(QPainter *painter, const QRectF &target, qreal scale,
|
|||||||
adj = QRect(0, 0, adj.width() * s.x(), adj.height() * s.y());
|
adj = QRect(0, 0, adj.width() * s.x(), adj.height() * s.y());
|
||||||
_map->zoomFit(adj.size(), _tr | _rr | _wr);
|
_map->zoomFit(adj.size(), _tr | _rr | _wr);
|
||||||
rescale();
|
rescale();
|
||||||
|
|
||||||
QPointF center = contentCenter();
|
QPointF center = contentCenter();
|
||||||
centerOn(center);
|
centerOn(center);
|
||||||
adj.moveCenter(mapFromScene(center));
|
adj.moveCenter(mapFromScene(center));
|
||||||
|
|
||||||
_mapScale->setResolution(_map->resolution(_map->ll2xy(origLL)));
|
|
||||||
_mapScale->setDigitalZoom(-log2(s.x() / q));
|
_mapScale->setDigitalZoom(-log2(s.x() / q));
|
||||||
_mapScale->setPos(mapToScene(QPoint(adj.bottomRight() + QPoint(
|
_mapScale->setPos(mapToScene(QPoint(adj.bottomRight() + QPoint(
|
||||||
-(SCALE_OFFSET + _mapScale->boundingRect().width()) * (s.x() / q),
|
-(SCALE_OFFSET + _mapScale->boundingRect().width()) * (s.x() / q),
|
||||||
@ -547,7 +545,6 @@ void PathView::plot(QPainter *painter, const QRectF &target, qreal scale,
|
|||||||
_map->zoomFit(origRes, origLL);
|
_map->zoomFit(origRes, origLL);
|
||||||
rescale();
|
rescale();
|
||||||
centerOn(origScene);
|
centerOn(origScene);
|
||||||
_mapScale->setResolution(origRes);
|
|
||||||
}
|
}
|
||||||
_mapScale->setDigitalZoom(0);
|
_mapScale->setDigitalZoom(0);
|
||||||
_mapScale->setPos(origPos);
|
_mapScale->setPos(origPos);
|
||||||
@ -560,14 +557,15 @@ void PathView::plot(QPainter *painter, const QRectF &target, qreal scale,
|
|||||||
|
|
||||||
void PathView::clear()
|
void PathView::clear()
|
||||||
{
|
{
|
||||||
if (_mapScale->scene() == _scene)
|
|
||||||
_scene->removeItem(_mapScale);
|
|
||||||
|
|
||||||
_pois.clear();
|
_pois.clear();
|
||||||
_tracks.clear();
|
_tracks.clear();
|
||||||
_routes.clear();
|
_routes.clear();
|
||||||
_waypoints.clear();
|
_waypoints.clear();
|
||||||
|
|
||||||
|
_scene->removeItem(_mapScale);
|
||||||
_scene->clear();
|
_scene->clear();
|
||||||
|
_scene->addItem(_mapScale);
|
||||||
|
|
||||||
_palette.reset();
|
_palette.reset();
|
||||||
|
|
||||||
_tr = RectC();
|
_tr = RectC();
|
||||||
@ -756,11 +754,7 @@ void PathView::resizeEvent(QResizeEvent *event)
|
|||||||
if (mapZoom() != zoom)
|
if (mapZoom() != zoom)
|
||||||
rescale();
|
rescale();
|
||||||
|
|
||||||
QPointF center = contentCenter();
|
centerOn(contentCenter());
|
||||||
centerOn(center);
|
|
||||||
|
|
||||||
_res = _map->resolution(center);
|
|
||||||
_mapScale->setResolution(_res);
|
|
||||||
|
|
||||||
QGraphicsView::resizeEvent(event);
|
QGraphicsView::resizeEvent(event);
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,6 @@ class PathView : public QGraphicsView
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
PathView(Map *map, POI *poi, QWidget *parent = 0);
|
PathView(Map *map, POI *poi, QWidget *parent = 0);
|
||||||
~PathView();
|
|
||||||
|
|
||||||
QList<PathItem*> loadData(const Data &data);
|
QList<PathItem*> loadData(const Data &data);
|
||||||
|
|
||||||
@ -85,6 +84,7 @@ private:
|
|||||||
qreal mapZoom() const;
|
qreal mapZoom() const;
|
||||||
QPointF contentCenter() const;
|
QPointF contentCenter() const;
|
||||||
void rescale();
|
void rescale();
|
||||||
|
void centerOn(const QPointF &pos);
|
||||||
void zoom(int zoom, const QPoint &pos, const Coordinates &c);
|
void zoom(int zoom, const QPoint &pos, const Coordinates &c);
|
||||||
void digitalZoom(int zoom);
|
void digitalZoom(int zoom);
|
||||||
void resetDigitalZoom();
|
void resetDigitalZoom();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user