1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-10-06 23:03:22 +02:00
GPXSee/src/GUI/pathitem.cpp

381 lines
7.8 KiB
C++
Raw Normal View History

2016-11-18 18:02:40 +01:00
#include <cmath>
2016-09-26 21:01:58 +02:00
#include <QCursor>
#include <QPainter>
#include "common/greatcircle.h"
2017-11-26 18:54:03 +01:00
#include "map/map.h"
#include "pathtickitem.h"
2016-09-26 21:01:58 +02:00
#include "pathitem.h"
2018-09-15 08:40:37 +02:00
#define GEOGRAPHICAL_MILE 1855.3248
2019-02-16 12:39:23 +01:00
static inline bool isValid(const QPointF &p)
2019-02-11 23:28:08 +01:00
{
2019-02-16 12:39:23 +01:00
return (!std::isnan(p.x()) && !std::isnan(p.y()));
2019-02-11 23:28:08 +01:00
}
static inline unsigned segments(qreal distance)
{
return ceil(distance / GEOGRAPHICAL_MILE);
}
PathItem::PathItem(const Path &path, Map *map, QGraphicsItem *parent)
2019-02-11 23:28:08 +01:00
: QGraphicsObject(parent), _path(path), _map(map)
2016-09-26 21:01:58 +02:00
{
2019-02-11 23:28:08 +01:00
Q_ASSERT(_path.isValid());
_units = Metric;
_digitalZoom = 0;
_width = 3;
2016-09-26 21:01:58 +02:00
QBrush brush(Qt::SolidPattern);
2016-12-06 01:48:26 +01:00
_pen = QPen(brush, _width);
2019-02-16 12:39:23 +01:00
_showMarker = true;
_showTicks = false;
2016-09-26 21:01:58 +02:00
2018-09-15 13:40:21 +02:00
updatePainterPath();
updateShape();
updateTicks();
2019-02-11 23:28:08 +01:00
_markerDistance = _path.first().first().distance();
2019-02-16 12:39:23 +01:00
_marker = new MarkerItem(this);
_marker->setPos(position(_markerDistance));
2016-09-26 21:01:58 +02:00
setCursor(Qt::ArrowCursor);
setAcceptHoverEvents(true);
}
void PathItem::updateShape()
{
QPainterPathStroker s;
s.setWidth((_width + 1) * pow(2, -_digitalZoom));
_shape = s.createStroke(_painterPath);
}
2018-09-15 13:40:21 +02:00
void PathItem::addSegment(const Coordinates &c1, const Coordinates &c2)
{
if (fabs(c1.lon() - c2.lon()) > 180.0) {
2018-10-11 18:19:35 +02:00
// Split segment on date line crossing
QPointF p;
if (c2.lon() < 0) {
QLineF l(QPointF(c1.lon(), c1.lat()), QPointF(c2.lon() + 360,
c2.lat()));
QLineF dl(QPointF(180, -90), QPointF(180, 90));
l.intersect(dl, &p);
_painterPath.lineTo(_map->ll2xy(Coordinates(180, p.y())));
_painterPath.moveTo(_map->ll2xy(Coordinates(-180, p.y())));
} else {
QLineF l(QPointF(c1.lon(), c1.lat()), QPointF(c2.lon() - 360,
c2.lat()));
QLineF dl(QPointF(-180, -90), QPointF(-180, 90));
l.intersect(dl, &p);
_painterPath.lineTo(_map->ll2xy(Coordinates(-180, p.y())));
_painterPath.moveTo(_map->ll2xy(Coordinates(180, p.y())));
}
_painterPath.lineTo(_map->ll2xy(c2));
} else
2018-09-15 13:40:21 +02:00
_painterPath.lineTo(_map->ll2xy(c2));
}
void PathItem::updatePainterPath()
{
_painterPath = QPainterPath();
2019-02-11 23:28:08 +01:00
for (int i = 0; i < _path.size(); i++) {
const PathSegment &segment = _path.at(i);
_painterPath.moveTo(_map->ll2xy(segment.first().coordinates()));
for (int j = 1; j < segment.size(); j++) {
const PathPoint &p1 = segment.at(j-1);
const PathPoint &p2 = segment.at(j);
unsigned n = segments(p2.distance() - p1.distance());
if (n > 1) {
GreatCircle gc(p1.coordinates(), p2.coordinates());
Coordinates last = p1.coordinates();
for (unsigned k = 1; k <= n; k++) {
Coordinates c(gc.pointAt(k/(double)n));
addSegment(last, c);
last = c;
}
} else
addSegment(p1.coordinates(), p2.coordinates());
}
}
}
2016-09-26 21:01:58 +02:00
void PathItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
painter->setPen(_pen);
painter->drawPath(_painterPath);
2016-09-26 21:01:58 +02:00
/*
painter->setPen(Qt::red);
2016-09-26 21:01:58 +02:00
painter->drawRect(boundingRect());
*/
}
void PathItem::setMap(Map *map)
2016-09-26 21:01:58 +02:00
{
prepareGeometryChange();
2017-10-04 23:15:39 +02:00
_map = map;
2018-09-15 13:40:21 +02:00
updatePainterPath();
2016-09-26 21:01:58 +02:00
updateShape();
updateTicks();
2019-02-16 12:39:23 +01:00
QPointF pos = position(_markerDistance);
if (isValid(pos))
_marker->setPos(pos);
2016-09-26 21:01:58 +02:00
}
void PathItem::setColor(const QColor &color)
{
2017-10-04 23:15:39 +02:00
if (_pen.color() == color)
return;
2016-09-26 21:01:58 +02:00
_pen.setColor(color);
update();
}
2017-04-05 22:53:25 +02:00
void PathItem::setWidth(qreal width)
2016-12-06 01:48:26 +01:00
{
2017-10-04 23:15:39 +02:00
if (_width == width)
return;
2016-12-06 01:48:26 +01:00
prepareGeometryChange();
_width = width;
_pen.setWidthF(_width * pow(2, -_digitalZoom));
2016-12-06 01:48:26 +01:00
updateShape();
}
void PathItem::setStyle(Qt::PenStyle style)
{
2017-10-04 23:15:39 +02:00
if (_pen.style() == style)
return;
2016-12-06 01:48:26 +01:00
_pen.setStyle(style);
update();
}
void PathItem::setDigitalZoom(int zoom)
{
2017-10-04 23:15:39 +02:00
if (_digitalZoom == zoom)
return;
prepareGeometryChange();
_digitalZoom = zoom;
_pen.setWidthF(_width * pow(2, -_digitalZoom));
_marker->setScale(pow(2, -_digitalZoom));
updateShape();
}
2019-02-11 23:28:08 +01:00
const PathSegment *PathItem::segment(qreal x) const
{
for (int i = 0; i < _path.size(); i++)
if (x <= _path.at(i).last().distance())
return &(_path.at(i));
return 0;
}
2016-11-14 22:12:43 +01:00
QPointF PathItem::position(qreal x) const
{
2019-02-11 23:28:08 +01:00
const PathSegment *seg = segment(x);
if (!seg)
return QPointF(NAN, NAN);
2016-11-14 22:12:43 +01:00
int low = 0;
2019-02-11 23:28:08 +01:00
int high = seg->count() - 1;
2016-11-14 22:12:43 +01:00
int mid = 0;
2019-02-11 23:28:08 +01:00
if (!(x >= seg->first().distance() && x <= seg->last().distance()))
return QPointF(NAN, NAN);
2016-11-14 22:12:43 +01:00
while (low <= high) {
mid = low + ((high - low) / 2);
2019-02-11 23:28:08 +01:00
qreal val = seg->at(mid).distance();
2016-11-14 22:12:43 +01:00
if (val > x)
high = mid - 1;
else if (val < x)
low = mid + 1;
else
2019-02-11 23:28:08 +01:00
return _map->ll2xy(seg->at(mid).coordinates());
2016-11-14 22:12:43 +01:00
}
Coordinates c1, c2;
2016-11-14 22:12:43 +01:00
qreal p1, p2;
2019-02-11 23:28:08 +01:00
if (seg->at(mid).distance() < x) {
c1 = seg->at(mid).coordinates(); c2 = seg->at(mid+1).coordinates();
p1 = seg->at(mid).distance(); p2 = seg->at(mid+1).distance();
2016-11-14 22:12:43 +01:00
} else {
2019-02-11 23:28:08 +01:00
c1 = seg->at(mid-1).coordinates(); c2 = seg->at(mid).coordinates();
p1 = seg->at(mid-1).distance(); p2 = seg->at(mid).distance();
2016-11-14 22:12:43 +01:00
}
unsigned n = segments(p2 - p1);
if (n > 1) {
2018-09-13 01:15:43 +02:00
GreatCircle gc(c1, c2);
// Great circle point
double f = (x - p1) / (p2 - p1);
QPointF p(_map->ll2xy(gc.pointAt(f)));
// Segment line of the great circle path
double f1 = floor(n * f) / n;
double f2 = ceil(n * f) / n;
QLineF l(_map->ll2xy(gc.pointAt(f1)), _map->ll2xy(gc.pointAt(f2)));
// Project the great circle point to the segment line
QLineF u = l.unitVector();
double lambda = (u.dx() * (p.x() - l.p1().x())) + (u.dy() * (p.y()
- l.p1().y()));
return QPointF((u.dx() * lambda) + l.p1().x(), (u.dy() * lambda)
+ l.p1().y());
2018-09-13 01:15:43 +02:00
} else {
QLineF l(_map->ll2xy(c1), _map->ll2xy(c2));
return l.pointAt((x - p1) / (p2 - p1));
}
2016-11-14 22:12:43 +01:00
}
2016-09-26 21:01:58 +02:00
void PathItem::moveMarker(qreal distance)
{
2019-02-16 12:39:23 +01:00
_markerDistance = distance;
2019-02-11 23:28:08 +01:00
QPointF pos(position(distance));
2019-02-16 12:39:23 +01:00
if (isValid(pos)) {
_marker->setVisible(_showMarker);
2019-02-11 23:28:08 +01:00
_marker->setPos(pos);
2019-02-16 12:39:23 +01:00
} else
_marker->setVisible(false);
2016-09-26 21:01:58 +02:00
}
2017-12-03 00:36:52 +01:00
void PathItem::setMarkerColor(const QColor &color)
{
_marker->setColor(color);
}
void PathItem::hover(bool hover)
{
if (hover) {
_pen.setWidth((_width + 1) * pow(2, -_digitalZoom));
setZValue(zValue() + 1.0);
} else {
_pen.setWidth(_width * pow(2, -_digitalZoom));
setZValue(zValue() - 1.0);
}
update();
}
2019-02-16 12:39:23 +01:00
void PathItem::showMarker(bool show)
{
if (_showMarker == show)
return;
_showMarker = show;
_marker->setVisible(show && isValid(position(_markerDistance)));
}
qreal PathItem::xInM() const
{
return (_units == Nautical) ? NMIINM : (_units == Imperial) ? MIINM : KMINM;
}
unsigned PathItem::tickSize() const
{
qreal res = _map->resolution(sceneBoundingRect());
if (res < 10)
return 1;
else if (res < 25)
return 5;
else if (res < 100)
return 10;
else if (res < 500)
return 50;
else if (res < 2000)
return 100;
else if (res < 10000)
return 500;
else if (res < 20000)
return 1000;
else
return 5000;
}
void PathItem::updateTicks()
{
for (int i = 0; i < _ticks.size(); i++)
delete _ticks[i];
_ticks.clear();
if (!_showTicks)
return;
int ts = tickSize();
int tc = _path.last().last().distance() / (ts * xInM());
QRect tr = PathTickItem::tickRect(ts * (tc - 1));
2019-05-23 19:19:07 +02:00
_ticks.resize(tc);
for (int i = 0; i < tc; i++) {
_ticks[i] = new PathTickItem(tr, (i + 1) * ts, this);
_ticks[i]->setPos(position((i + 1) * ts * xInM()));
_ticks[i]->setColor(_pen.color());
_ticks[i]->setToolTip(toolTip());
2019-05-23 19:19:07 +02:00
}
}
void PathItem::showTicks(bool show)
{
if (_showTicks == show)
return;
prepareGeometryChange();
_showTicks = show;
updateTicks();
}
void PathItem::setUnits(Units units)
{
if (_units == units)
return;
prepareGeometryChange();
_units = units;
updateTicks();
}
2016-09-26 21:01:58 +02:00
void PathItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
Q_UNUSED(event);
_pen.setWidthF((_width + 1) * pow(2, -_digitalZoom));
2016-09-26 21:01:58 +02:00
setZValue(zValue() + 1.0);
update();
emit selected(true);
}
void PathItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
Q_UNUSED(event);
_pen.setWidthF(_width * pow(2, -_digitalZoom));
2016-09-26 21:01:58 +02:00
setZValue(zValue() - 1.0);
update();
emit selected(false);
}