2016-11-18 18:02:40 +01:00
|
|
|
#include <cmath>
|
2016-09-26 21:01:58 +02:00
|
|
|
#include <QApplication>
|
|
|
|
#include <QCursor>
|
|
|
|
#include <QPainter>
|
2018-09-13 00:43:28 +02:00
|
|
|
#include "common/greatcircle.h"
|
2017-11-26 18:54:03 +01:00
|
|
|
#include "map/map.h"
|
2016-09-26 21:01:58 +02:00
|
|
|
#include "tooltip.h"
|
2017-11-26 18:54:03 +01:00
|
|
|
#include "nicenum.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
|
|
|
|
|
2018-09-15 10:41:00 +02:00
|
|
|
static unsigned segments(qreal distance)
|
|
|
|
{
|
|
|
|
return ceil(distance / GEOGRAPHICAL_MILE);
|
|
|
|
}
|
|
|
|
|
2017-03-18 01:30:31 +01:00
|
|
|
PathItem::PathItem(const Path &path, Map *map, QGraphicsItem *parent)
|
|
|
|
: QGraphicsObject(parent)
|
2016-09-26 21:01:58 +02:00
|
|
|
{
|
2017-03-18 01:30:31 +01:00
|
|
|
Q_ASSERT(path.count() >= 2);
|
2017-05-01 12:59:56 +02:00
|
|
|
|
2017-03-18 01:30:31 +01:00
|
|
|
_path = path;
|
|
|
|
_map = map;
|
2017-05-01 12:59:56 +02:00
|
|
|
_digitalZoom = 0;
|
2016-12-06 01:48:26 +01:00
|
|
|
|
2017-03-18 01:30:31 +01:00
|
|
|
_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);
|
2016-09-26 21:01:58 +02:00
|
|
|
|
2017-05-01 12:59:56 +02:00
|
|
|
updatePainterPath(map);
|
|
|
|
updateShape();
|
|
|
|
|
2016-09-26 21:01:58 +02:00
|
|
|
_marker = new MarkerItem(this);
|
2017-03-18 01:30:31 +01:00
|
|
|
_marker->setPos(position(_path.at(0).distance()));
|
2017-05-01 12:59:56 +02:00
|
|
|
_markerDistance = _path.at(0).distance();
|
2016-09-26 21:01:58 +02:00
|
|
|
|
|
|
|
setCursor(Qt::ArrowCursor);
|
|
|
|
setAcceptHoverEvents(true);
|
|
|
|
}
|
|
|
|
|
2017-03-18 01:30:31 +01:00
|
|
|
void PathItem::updateShape()
|
|
|
|
{
|
|
|
|
QPainterPathStroker s;
|
2017-05-01 12:59:56 +02:00
|
|
|
s.setWidth((_width + 1) * pow(2, -_digitalZoom));
|
2017-03-18 01:30:31 +01:00
|
|
|
_shape = s.createStroke(_painterPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PathItem::updatePainterPath(Map *map)
|
|
|
|
{
|
|
|
|
_painterPath = QPainterPath();
|
|
|
|
|
|
|
|
_painterPath.moveTo(map->ll2xy(_path.first().coordinates()));
|
2018-09-13 00:43:28 +02:00
|
|
|
for (int i = 1; i < _path.size(); i++) {
|
2018-09-15 00:22:27 +02:00
|
|
|
const PathPoint &p1 = _path.at(i-1);
|
|
|
|
const PathPoint &p2 = _path.at(i);
|
2018-09-15 10:41:00 +02:00
|
|
|
unsigned n = segments(p2.distance() - p1.distance());
|
2018-09-13 00:43:28 +02:00
|
|
|
|
2018-09-15 08:40:37 +02:00
|
|
|
if (n > 1) {
|
2018-09-15 00:22:27 +02:00
|
|
|
Coordinates c1(p1.coordinates());
|
|
|
|
Coordinates c2(p2.coordinates());
|
2018-09-13 01:15:43 +02:00
|
|
|
GreatCircle gc(c1, c2);
|
2018-09-15 00:22:27 +02:00
|
|
|
double prev = c1.lon();
|
2018-09-13 01:15:43 +02:00
|
|
|
|
2018-09-15 00:22:27 +02:00
|
|
|
for (unsigned j = 1; j <= n; j++) {
|
|
|
|
Coordinates c(gc.pointAt(j/(double)n));
|
2018-09-13 00:43:28 +02:00
|
|
|
double current = c.lon();
|
|
|
|
if (fabs(current - prev) > 180.0)
|
|
|
|
_painterPath.moveTo(map->ll2xy(c));
|
|
|
|
else
|
|
|
|
_painterPath.lineTo(map->ll2xy(c));
|
|
|
|
prev = current;
|
|
|
|
}
|
|
|
|
} else
|
2018-09-15 00:22:27 +02:00
|
|
|
_painterPath.lineTo(map->ll2xy(p2.coordinates()));
|
2018-09-13 00:43:28 +02:00
|
|
|
}
|
2017-03-18 01:30:31 +01:00
|
|
|
}
|
|
|
|
|
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);
|
2017-03-18 01:30:31 +01:00
|
|
|
painter->drawPath(_painterPath);
|
2016-09-26 21:01:58 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
QPen p = QPen(QBrush(Qt::red), 0);
|
|
|
|
painter->setPen(p);
|
|
|
|
painter->drawRect(boundingRect());
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2017-03-18 01:30:31 +01:00
|
|
|
void PathItem::setMap(Map *map)
|
2016-09-26 21:01:58 +02:00
|
|
|
{
|
|
|
|
prepareGeometryChange();
|
|
|
|
|
2017-10-04 23:15:39 +02:00
|
|
|
_map = map;
|
|
|
|
|
2017-03-18 01:30:31 +01:00
|
|
|
updatePainterPath(map);
|
2016-09-26 21:01:58 +02:00
|
|
|
updateShape();
|
2017-03-18 01:30:31 +01:00
|
|
|
|
2017-05-01 12:59:56 +02:00
|
|
|
_marker->setPos(position(_markerDistance));
|
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;
|
2017-05-01 12:59:56 +02:00
|
|
|
_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();
|
|
|
|
}
|
|
|
|
|
2017-05-01 12:59:56 +02:00
|
|
|
void PathItem::setDigitalZoom(int zoom)
|
|
|
|
{
|
2017-10-04 23:15:39 +02:00
|
|
|
if (_digitalZoom == zoom)
|
|
|
|
return;
|
|
|
|
|
2017-05-01 12:59:56 +02:00
|
|
|
prepareGeometryChange();
|
|
|
|
|
|
|
|
_digitalZoom = zoom;
|
|
|
|
_pen.setWidthF(_width * pow(2, -_digitalZoom));
|
|
|
|
_marker->setScale(pow(2, -_digitalZoom));
|
|
|
|
|
|
|
|
updateShape();
|
|
|
|
}
|
|
|
|
|
2016-11-14 22:12:43 +01:00
|
|
|
QPointF PathItem::position(qreal x) const
|
|
|
|
{
|
|
|
|
int low = 0;
|
2017-03-18 01:30:31 +01:00
|
|
|
int high = _path.count() - 1;
|
2016-11-14 22:12:43 +01:00
|
|
|
int mid = 0;
|
|
|
|
|
|
|
|
Q_ASSERT(high > low);
|
2017-03-18 01:30:31 +01:00
|
|
|
Q_ASSERT(x >= _path.at(low).distance() && x <= _path.at(high).distance());
|
2016-11-14 22:12:43 +01:00
|
|
|
|
|
|
|
while (low <= high) {
|
|
|
|
mid = low + ((high - low) / 2);
|
2017-03-18 01:30:31 +01:00
|
|
|
qreal val = _path.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
|
2017-03-18 01:30:31 +01:00
|
|
|
return _map->ll2xy(_path.at(mid).coordinates());
|
2016-11-14 22:12:43 +01:00
|
|
|
}
|
|
|
|
|
2018-09-13 00:43:28 +02:00
|
|
|
Coordinates c1, c2;
|
2016-11-14 22:12:43 +01:00
|
|
|
qreal p1, p2;
|
2018-09-13 00:43:28 +02:00
|
|
|
|
2017-03-18 01:30:31 +01:00
|
|
|
if (_path.at(mid).distance() < x) {
|
2018-09-13 00:43:28 +02:00
|
|
|
c1 = _path.at(mid).coordinates(); c2 = _path.at(mid+1).coordinates();
|
2017-03-18 01:30:31 +01:00
|
|
|
p1 = _path.at(mid).distance(); p2 = _path.at(mid+1).distance();
|
2016-11-14 22:12:43 +01:00
|
|
|
} else {
|
2018-09-13 00:43:28 +02:00
|
|
|
c1 = _path.at(mid-1).coordinates(); c2 = _path.at(mid).coordinates();
|
2017-03-18 01:30:31 +01:00
|
|
|
p1 = _path.at(mid-1).distance(); p2 = _path.at(mid).distance();
|
2016-11-14 22:12:43 +01:00
|
|
|
}
|
|
|
|
|
2018-09-15 10:41:00 +02:00
|
|
|
unsigned n = segments(p2 - p1);
|
|
|
|
if (n > 1) {
|
2018-09-13 01:15:43 +02:00
|
|
|
GreatCircle gc(c1, c2);
|
2018-09-15 10:41:00 +02:00
|
|
|
|
|
|
|
// 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 {
|
2018-09-13 00:43:28 +02:00
|
|
|
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)
|
|
|
|
{
|
2017-03-18 01:30:31 +01:00
|
|
|
if (distance >= _path.first().distance()
|
|
|
|
&& distance <= _path.last().distance()) {
|
2016-09-26 21:01:58 +02:00
|
|
|
_marker->setVisible(true);
|
2016-11-14 22:12:43 +01:00
|
|
|
_marker->setPos(position(distance));
|
2017-05-01 12:59:56 +02:00
|
|
|
_markerDistance = distance;
|
2016-11-18 18:02:40 +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);
|
|
|
|
}
|
|
|
|
|
2017-09-24 19:54:13 +02:00
|
|
|
void PathItem::hover(bool hover)
|
|
|
|
{
|
|
|
|
if (hover) {
|
2018-07-02 18:50:41 +02:00
|
|
|
_pen.setWidth((_width + 1) * pow(2, -_digitalZoom));
|
2017-09-24 19:54:13 +02:00
|
|
|
setZValue(zValue() + 1.0);
|
|
|
|
} else {
|
2018-07-02 18:50:41 +02:00
|
|
|
_pen.setWidth(_width * pow(2, -_digitalZoom));
|
2017-09-24 19:54:13 +02:00
|
|
|
setZValue(zValue() - 1.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2016-09-26 21:01:58 +02:00
|
|
|
void PathItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
|
|
|
|
{
|
|
|
|
Q_UNUSED(event);
|
|
|
|
|
2017-05-01 12:59:56 +02:00
|
|
|
_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);
|
|
|
|
|
2017-05-01 12:59:56 +02:00
|
|
|
_pen.setWidthF(_width * pow(2, -_digitalZoom));
|
2016-09-26 21:01:58 +02:00
|
|
|
setZValue(zValue() - 1.0);
|
|
|
|
update();
|
|
|
|
|
|
|
|
emit selected(false);
|
|
|
|
}
|