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

Fixed broken print/PDF export

(wrong map scale/waypoints size)
This commit is contained in:
Martin Tůma 2017-05-01 12:59:56 +02:00
parent 713e331b2a
commit 1c0a0fd0b3
7 changed files with 87 additions and 54 deletions

View File

@ -12,20 +12,21 @@ PathItem::PathItem(const Path &path, Map *map, QGraphicsItem *parent)
: QGraphicsObject(parent) : QGraphicsObject(parent)
{ {
Q_ASSERT(path.count() >= 2); Q_ASSERT(path.count() >= 2);
_path = path; _path = path;
_map = map; _map = map;
_digitalZoom = 0;
updatePainterPath(map);
updateShape();
_width = 3; _width = 3;
QBrush brush(Qt::SolidPattern); QBrush brush(Qt::SolidPattern);
_pen = QPen(brush, _width); _pen = QPen(brush, _width);
updatePainterPath(map);
updateShape();
_marker = new MarkerItem(this); _marker = new MarkerItem(this);
_marker->setPos(position(_path.at(0).distance())); _marker->setPos(position(_path.at(0).distance()));
_marker->setFlag(QGraphicsItem::ItemIgnoresTransformations); _markerDistance = _path.at(0).distance();
_md = _path.at(0).distance();
setCursor(Qt::ArrowCursor); setCursor(Qt::ArrowCursor);
setAcceptHoverEvents(true); setAcceptHoverEvents(true);
@ -34,7 +35,7 @@ PathItem::PathItem(const Path &path, Map *map, QGraphicsItem *parent)
void PathItem::updateShape() void PathItem::updateShape()
{ {
QPainterPathStroker s; QPainterPathStroker s;
s.setWidth((_width + 1) * 1.0/scale()); s.setWidth((_width + 1) * pow(2, -_digitalZoom));
_shape = s.createStroke(_painterPath); _shape = s.createStroke(_painterPath);
} }
@ -71,7 +72,7 @@ void PathItem::setMap(Map *map)
updatePainterPath(map); updatePainterPath(map);
updateShape(); updateShape();
_marker->setPos(position(_md)); _marker->setPos(position(_markerDistance));
} }
void PathItem::setColor(const QColor &color) void PathItem::setColor(const QColor &color)
@ -85,7 +86,7 @@ void PathItem::setWidth(qreal width)
prepareGeometryChange(); prepareGeometryChange();
_width = width; _width = width;
_pen.setWidthF(_width * 1.0/scale()); _pen.setWidthF(_width * pow(2, -_digitalZoom));
updateShape(); updateShape();
} }
@ -96,6 +97,17 @@ void PathItem::setStyle(Qt::PenStyle style)
update(); update();
} }
void PathItem::setDigitalZoom(int zoom)
{
prepareGeometryChange();
_digitalZoom = zoom;
_pen.setWidthF(_width * pow(2, -_digitalZoom));
_marker->setScale(pow(2, -_digitalZoom));
updateShape();
}
QPointF PathItem::position(qreal x) const QPointF PathItem::position(qreal x) const
{ {
int low = 0; int low = 0;
@ -137,7 +149,7 @@ void PathItem::moveMarker(qreal distance)
&& distance <= _path.last().distance()) { && distance <= _path.last().distance()) {
_marker->setVisible(true); _marker->setVisible(true);
_marker->setPos(position(distance)); _marker->setPos(position(distance));
_md = distance; _markerDistance = distance;
} else } else
_marker->setVisible(false); _marker->setVisible(false);
} }
@ -146,7 +158,7 @@ void PathItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{ {
Q_UNUSED(event); Q_UNUSED(event);
_pen.setWidthF((_width + 1) * 1.0/scale()); _pen.setWidthF((_width + 1) * pow(2, -_digitalZoom));
setZValue(zValue() + 1.0); setZValue(zValue() + 1.0);
update(); update();
@ -157,7 +169,7 @@ void PathItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{ {
Q_UNUSED(event); Q_UNUSED(event);
_pen.setWidthF(_width * 1.0/scale()); _pen.setWidthF(_width * pow(2, -_digitalZoom));
setZValue(zValue() - 1.0); setZValue(zValue() - 1.0);
update(); update();

View File

@ -28,8 +28,7 @@ public:
void setColor(const QColor &color); void setColor(const QColor &color);
void setWidth(qreal width); void setWidth(qreal width);
void setStyle(Qt::PenStyle style); void setStyle(Qt::PenStyle style);
void setDigitalZoom(int zoom);
void showMarker(bool show) {_marker->setVisible(show);}
public slots: public slots:
void moveMarker(qreal distance); void moveMarker(qreal distance);
@ -50,7 +49,8 @@ private:
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event); void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
Map *_map; Map *_map;
qreal _md; qreal _markerDistance;
int _digitalZoom;
qreal _width; qreal _width;
QPen _pen; QPen _pen;

View File

@ -50,7 +50,6 @@ PathView::PathView(Map *map, POI *poi, QWidget *parent)
_mapScale = new ScaleItem(); _mapScale = new ScaleItem();
_mapScale->setZValue(2.0); _mapScale->setZValue(2.0);
_mapScale->setFlag(QGraphicsItem::ItemIgnoresTransformations);
_map = map; _map = map;
_poi = poi; _poi = poi;
@ -97,9 +96,10 @@ PathItem *PathView::addTrack(const Track &track)
_tracks.append(ti); _tracks.append(ti);
_tr |= ti->path().boundingRect(); _tr |= ti->path().boundingRect();
ti->setColor(_palette.nextColor()); ti->setColor(_palette.nextColor());
ti->setWidth(_trackWidth * pow(2, -_digitalZoom)); ti->setWidth(_trackWidth);
ti->setStyle(_trackStyle); ti->setStyle(_trackStyle);
ti->setVisible(_showTracks); ti->setVisible(_showTracks);
ti->setDigitalZoom(_digitalZoom);
_scene->addItem(ti); _scene->addItem(ti);
addPOI(_poi->points(ti->path())); addPOI(_poi->points(ti->path()));
@ -118,11 +118,12 @@ PathItem *PathView::addRoute(const Route &route)
_routes.append(ri); _routes.append(ri);
_rr |= ri->path().boundingRect(); _rr |= ri->path().boundingRect();
ri->setColor(_palette.nextColor()); ri->setColor(_palette.nextColor());
ri->setWidth(_routeWidth * pow(2, -_digitalZoom)); ri->setWidth(_routeWidth);
ri->setStyle(_routeStyle); ri->setStyle(_routeStyle);
ri->setVisible(_showRoutes); ri->setVisible(_showRoutes);
ri->showWaypoints(_showRouteWaypoints); ri->showWaypoints(_showRouteWaypoints);
ri->showWaypointLabels(_showWaypointLabels); ri->showWaypointLabels(_showWaypointLabels);
ri->setDigitalZoom(_digitalZoom);
_scene->addItem(ri); _scene->addItem(ri);
addPOI(_poi->points(ri->path())); addPOI(_poi->points(ri->path()));
@ -142,7 +143,7 @@ void PathView::addWaypoints(const QList<Waypoint> &waypoints)
wi->setZValue(1); wi->setZValue(1);
wi->showLabel(_showWaypointLabels); wi->showLabel(_showWaypointLabels);
wi->setVisible(_showWaypoints); wi->setVisible(_showWaypoints);
wi->setFlag(QGraphicsItem::ItemIgnoresTransformations); wi->setDigitalZoom(_digitalZoom);
_scene->addItem(wi); _scene->addItem(wi);
} }
@ -340,7 +341,7 @@ void PathView::addPOI(const QVector<Waypoint> &waypoints)
pi->setZValue(1); pi->setZValue(1);
pi->showLabel(_showPOILabels); pi->showLabel(_showPOILabels);
pi->setVisible(_showPOI); pi->setVisible(_showPOI);
pi->setFlag(QGraphicsItem::ItemIgnoresTransformations); pi->setDigitalZoom(_digitalZoom);
_scene->addItem(pi); _scene->addItem(pi);
_pois.insert(w, pi); _pois.insert(w, pi);
@ -372,23 +373,40 @@ void PathView::redraw()
void PathView::resetDigitalZoom() void PathView::resetDigitalZoom()
{ {
_digitalZoom = 0; QHash<Waypoint, WaypointItem*>::const_iterator it;
_digitalZoom = 0;
resetTransform(); resetTransform();
setTrackWidth(_trackWidth); for (int i = 0; i < _tracks.size(); i++)
setRouteWidth(_routeWidth); _tracks.at(i)->setDigitalZoom(0);
for (int i = 0; i < _routes.size(); i++)
_routes.at(i)->setDigitalZoom(0);
for (int i = 0; i < _waypoints.size(); i++)
_waypoints.at(i)->setDigitalZoom(0);
for (it = _pois.constBegin(); it != _pois.constEnd(); it++)
it.value()->setDigitalZoom(0);
_mapScale->setDigitalZoom(0);
} }
void PathView::digitalZoom(int zoom) void PathView::digitalZoom(int zoom)
{ {
QHash<Waypoint, WaypointItem*>::const_iterator it;
_digitalZoom += zoom; _digitalZoom += zoom;
scale(pow(2, zoom), pow(2, zoom)); scale(pow(2, zoom), pow(2, zoom));
setTrackWidth(_trackWidth); for (int i = 0; i < _tracks.size(); i++)
setRouteWidth(_routeWidth); _tracks.at(i)->setDigitalZoom(_digitalZoom);
for (int i = 0; i < _routes.size(); i++)
_routes.at(i)->setDigitalZoom(_digitalZoom);
for (int i = 0; i < _waypoints.size(); i++)
_waypoints.at(i)->setDigitalZoom(_digitalZoom);
for (it = _pois.constBegin(); it != _pois.constEnd(); it++)
it.value()->setDigitalZoom(_digitalZoom);
_mapScale->setResolution(_res * pow(2, -_digitalZoom)); _mapScale->setDigitalZoom(_digitalZoom);
} }
void PathView::zoom(int zoom, const QPoint &pos, const Coordinates &c) void PathView::zoom(int zoom, const QPoint &pos, const Coordinates &c)
@ -519,9 +537,7 @@ void PathView::clear()
_tr = QRectF(); _rr = QRectF(); _wr = QRectF(); _tr = QRectF(); _rr = QRectF(); _wr = QRectF();
_wp = QPointF(); _wp = QPointF();
_digitalZoom = 0; resetDigitalZoom();
resetTransform();
resetCachedContent(); resetCachedContent();
} }
@ -608,7 +624,7 @@ void PathView::setTrackWidth(int width)
_trackWidth = width; _trackWidth = width;
for (int i = 0; i < _tracks.count(); i++) for (int i = 0; i < _tracks.count(); i++)
_tracks.at(i)->setWidth(width * pow(2, -_digitalZoom)); _tracks.at(i)->setWidth(width);
} }
void PathView::setRouteWidth(int width) void PathView::setRouteWidth(int width)
@ -616,7 +632,7 @@ void PathView::setRouteWidth(int width)
_routeWidth = width; _routeWidth = width;
for (int i = 0; i < _routes.count(); i++) for (int i = 0; i < _routes.count(); i++)
_routes.at(i)->setWidth(width * pow(2, -_digitalZoom)); _routes.at(i)->setWidth(width);
} }
void PathView::setTrackStyle(Qt::PenStyle style) void PathView::setTrackStyle(Qt::PenStyle style)

View File

@ -1,3 +1,4 @@
#include <cmath>
#include <QPainter> #include <QPainter>
#include "config.h" #include "config.h"
#include "misc.h" #include "misc.h"
@ -15,6 +16,7 @@ ScaleItem::ScaleItem(QGraphicsItem *parent) : QGraphicsItem(parent)
{ {
_units = Metric; _units = Metric;
_res = 1.0; _res = 1.0;
_digitalZoom = 0;
#ifndef Q_OS_MAC #ifndef Q_OS_MAC
setCacheMode(QGraphicsItem::DeviceCoordinateCache); setCacheMode(QGraphicsItem::DeviceCoordinateCache);
@ -87,24 +89,26 @@ QString ScaleItem::units() const
void ScaleItem::computeScale() void ScaleItem::computeScale()
{ {
qreal res = _res * pow(2, -_digitalZoom);
if (_units == Imperial) { if (_units == Imperial) {
_length = niceNum((_res * M2FT * SCALE_WIDTH) / SEGMENTS, 1); _length = niceNum((res * M2FT * SCALE_WIDTH) / SEGMENTS, 1);
if (_length >= MIINFT) { if (_length >= MIINFT) {
_length = niceNum((_res * M2FT * FT2MI * SCALE_WIDTH) / SEGMENTS, 1); _length = niceNum((res * M2FT * FT2MI * SCALE_WIDTH) / SEGMENTS, 1);
_width = (_length / (_res * M2FT * FT2MI)); _width = (_length / (res * M2FT * FT2MI));
_scale = true; _scale = true;
} else { } else {
_width = (_length / (_res * M2FT)); _width = (_length / (res * M2FT));
_scale = false; _scale = false;
} }
} else { } else {
_length = niceNum((_res * SCALE_WIDTH) / SEGMENTS, 1); _length = niceNum((res * SCALE_WIDTH) / SEGMENTS, 1);
if (_length >= KMINM) { if (_length >= KMINM) {
_length *= M2KM; _length *= M2KM;
_width = (_length / (_res * M2KM)); _width = (_length / (res * M2KM));
_scale = true; _scale = true;
} else { } else {
_width = (_length / _res); _width = (_length / res);
_scale = false; _scale = false;
} }
} }
@ -127,3 +131,14 @@ void ScaleItem::setUnits(enum Units units)
updateBoundingRect(); updateBoundingRect();
update(); update();
} }
void ScaleItem::setDigitalZoom(int zoom)
{
prepareGeometryChange();
_digitalZoom = zoom;
computeScale();
updateBoundingRect();
update();
setScale(pow(2, -_digitalZoom));
}

View File

@ -15,6 +15,7 @@ public:
void setResolution(qreal res); void setResolution(qreal res);
void setUnits(enum Units units); void setUnits(enum Units units);
void setDigitalZoom(int zoom);
private: private:
void updateBoundingRect(); void updateBoundingRect();
@ -27,6 +28,8 @@ private:
Units _units; Units _units;
bool _scale; bool _scale;
int _digitalZoom;
QRectF _boundingRect; QRectF _boundingRect;
}; };

View File

@ -3,7 +3,6 @@
#include "config.h" #include "config.h"
#include "format.h" #include "format.h"
#include "tooltip.h" #include "tooltip.h"
#include "map.h"
#include "waypointitem.h" #include "waypointitem.h"
@ -46,11 +45,6 @@ WaypointItem::WaypointItem(const Waypoint &waypoint, Map *map,
setAcceptHoverEvents(true); setAcceptHoverEvents(true);
} }
void WaypointItem::setMap(Map *map)
{
setPos(map->ll2xy(_waypoint.coordinates()));
}
void WaypointItem::updateShape() void WaypointItem::updateShape()
{ {
QPainterPath p; QPainterPath p;
@ -106,14 +100,6 @@ void WaypointItem::paint(QPainter *painter,
*/ */
} }
/*
void WaypointItem::setScale(qreal scale)
{
QPointF p = _map->ll2xy(_waypoint.coordinates());
setPos(QPointF(p.x(), -p.y()) * scale);
}
*/
void WaypointItem::setUnits(enum Units units) void WaypointItem::setUnits(enum Units units)
{ {
setToolTip(toolTip(units)); setToolTip(toolTip(units));

View File

@ -1,11 +1,11 @@
#ifndef WAYPOINTITEM_H #ifndef WAYPOINTITEM_H
#define WAYPOINTITEM_H #define WAYPOINTITEM_H
#include <cmath>
#include <QGraphicsItem> #include <QGraphicsItem>
#include "waypoint.h" #include "waypoint.h"
#include "units.h" #include "units.h"
#include "map.h"
class Map;
class WaypointItem : public QGraphicsItem class WaypointItem : public QGraphicsItem
{ {
@ -14,9 +14,10 @@ public:
const Waypoint &waypoint() const {return _waypoint;} const Waypoint &waypoint() const {return _waypoint;}
void setMap(Map *map); void setMap(Map *map) {setPos(map->ll2xy(_waypoint.coordinates()));}
void setUnits(Units units); void setUnits(Units units);
void showLabel(bool show); void showLabel(bool show);
void setDigitalZoom(int zoom) {setScale(pow(2, -zoom));}
QPainterPath shape() const {return _shape;} QPainterPath shape() const {return _shape;}
QRectF boundingRect() const {return _shape.boundingRect();} QRectF boundingRect() const {return _shape.boundingRect();}