1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2025-07-14 19:04:23 +02:00

Fixed path marker inaccuracy issue.

This commit is contained in:
2016-11-14 22:12:43 +01:00
parent 367427b26a
commit eb5692a0ab
11 changed files with 86 additions and 20 deletions

View File

@ -26,7 +26,6 @@ PathItem::PathItem(QGraphicsItem *parent) : QGraphicsObject(parent)
_pen = QPen(brush, PATH_WIDTH);
_units = Metric;
_distance = 0;
_marker = new MarkerItem(this);
@ -72,13 +71,46 @@ void PathItem::setUnits(enum Units units)
_units = units;
}
QPointF PathItem::position(qreal x) const
{
int low = 0;
int high = _distance.count() - 1;
int mid = 0;
Q_ASSERT(high > low);
Q_ASSERT(x >= _distance.at(low) && x <= _distance.at(high));
while (low <= high) {
mid = low + ((high - low) / 2);
qreal val = _distance.at(mid);
if (val > x)
high = mid - 1;
else if (val < x)
low = mid + 1;
else
return _path.elementAt(mid);
}
QLineF l;
qreal p1, p2;
if (_distance.at(mid) < x) {
l = QLineF(_path.elementAt(mid), _path.elementAt(mid+1));
p1 = _distance.at(mid); p2 = _distance.at(mid+1);
} else {
l = QLineF(_path.elementAt(mid-1), _path.elementAt(mid));
p1 = _distance.at(mid-1); p2 = _distance.at(mid);
}
return l.pointAt((x - p1) / (p2 - p1));
}
void PathItem::moveMarker(qreal distance)
{
if (distance > _distance)
if (distance > _distance.last())
_marker->setVisible(false);
else {
_marker->setVisible(true);
_marker->setPos(_path.pointAtPercent(distance / _distance));
_marker->setPos(position(distance));
}
}