1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2025-06-27 11:39:16 +02:00

Added support for track segments

This commit is contained in:
2019-02-11 23:28:08 +01:00
parent 3c112b0b6f
commit 0308dbbb09
46 changed files with 731 additions and 537 deletions

View File

@ -6,17 +6,6 @@
CadenceGraphItem::CadenceGraphItem(const Graph &graph, GraphType type,
QGraphicsItem *parent) : GraphItem(graph, type, parent)
{
qreal sum = 0;
_max = graph.first().y();
for (int i = 1; i < graph.size(); i++) {
qreal y = graph.at(i).y();
sum += y * (graph.at(i).s() - graph.at(i-1).s());
if (y > _max)
_max = y;
}
_avg = sum/graph.last().s();
setToolTip(toolTip());
}

View File

@ -11,13 +11,8 @@ public:
CadenceGraphItem(const Graph &graph, GraphType type,
QGraphicsItem *parent = 0);
qreal max() const {return _max;}
qreal avg() const {return _avg;}
private:
QString toolTip() const;
qreal _avg, _max;
};
#endif // CADENCEGRAPHITEM_H

View File

@ -6,22 +6,22 @@
ElevationGraphItem::ElevationGraphItem(const Graph &graph, GraphType type,
QGraphicsItem *parent) : GraphItem(graph, type, parent)
{
_min = GraphItem::min();
_max = GraphItem::max();
_ascent = _descent = 0;
_min = _max = graph.first().y();
for (int i = 0; i < graph.size(); i++) {
const GraphSegment &segment = graph.at(i);
for (int j = 1; j < graph.size(); j++) {
qreal cur = graph.at(j).y();
qreal prev = graph.at(j-1).y();
for (int j = 1; j < segment.size(); j++) {
qreal cur = segment.at(j).y();
qreal prev = segment.at(j-1).y();
if (cur > prev)
_ascent += cur - prev;
if (cur < prev)
_descent += prev - cur;
if (cur < _min)
_min = cur;
if (cur > _max)
_max = cur;
if (cur > prev)
_ascent += cur - prev;
if (cur < prev)
_descent += prev - cur;
}
}
setToolTip(toolTip(Metric));

View File

@ -13,8 +13,8 @@ public:
qreal ascent() const {return _ascent;}
qreal descent() const {return _descent;}
qreal min() const {return _min;}
qreal max() const {return _max;}
qreal max() const {return _min;}
qreal min() const {return _max;}
void setUnits(Units units);

View File

@ -8,19 +8,6 @@ GearRatioGraphItem::GearRatioGraphItem(const Graph &graph, GraphType type,
QGraphicsItem *parent) : GraphItem(graph, type, parent), _top(NAN)
{
qreal val = NAN;
_min = _max = graph.first().y();
for (int i = 1; i < graph.size(); i++) {
const GraphPoint &p = graph.at(i);
qreal val = _map.value(p.y());
_map.insert(p.y(), val + (p.s() - graph.at(i-1).s()));
if (p.y() < _min)
_min = p.y();
if (p.y() > _max)
_max = p.y();
}
for (QMap<qreal, qreal>::const_iterator it = _map.constBegin();
it != _map.constEnd(); ++it) {

View File

@ -12,8 +12,6 @@ public:
GearRatioGraphItem(const Graph &graph, GraphType type,
QGraphicsItem *parent = 0);
qreal min() const {return _min;}
qreal max() const {return _max;}
qreal top() const {return _top;}
const QMap<qreal, qreal> &map() const {return _map;}
@ -22,7 +20,7 @@ private:
QString toolTip() const;
QMap<qreal, qreal> _map;
qreal _top, _min, _max;
qreal _top;
};
#endif // GEARRATIOGRAPHITEM_H

View File

@ -3,26 +3,17 @@
GraphItem::GraphItem(const Graph &graph, GraphType type, QGraphicsItem *parent)
: QGraphicsObject(parent)
: QGraphicsObject(parent), _graph(graph), _type(type)
{
Q_ASSERT(_graph.isValid());
_id = 0;
_width = 1;
_pen = QPen(Qt::black, _width);
_type = type;
_graph = graph;
_sx = 1.0; _sy = 1.0;
_time = _graph.hasTime();
_time = true;
for (int i = 0; i < _graph.size(); i++) {
if (std::isnan(_graph.at(i).t())) {
_time = false;
break;
}
}
setZValue(1.0);
setZValue(2.0);
updatePath();
updateShape();
@ -89,18 +80,31 @@ void GraphItem::setWidth(int width)
updateShape();
}
const GraphSegment *GraphItem::segment(qreal x, GraphType type) const
{
for (int i = 0; i < _graph.size(); i++)
if (x <= _graph.at(i).last().x(type))
return &(_graph.at(i));
return 0;
}
qreal GraphItem::yAtX(qreal x)
{
const GraphSegment *seg = segment(x, _type);
if (!seg)
return NAN;
int low = 0;
int high = _graph.count() - 1;
int high = seg->count() - 1;
int mid = 0;
Q_ASSERT(high > low);
Q_ASSERT(x >= _graph.at(low).x(_type) && x <= _graph.at(high).x(_type));
if (!(x >= seg->at(low).x(_type) && x <= seg->at(high).x(_type)))
return NAN;
while (low <= high) {
mid = low + ((high - low) / 2);
const GraphPoint &p = _graph.at(mid);
const GraphPoint &p = seg->at(mid);
if (p.x(_type) > x)
high = mid - 1;
else if (p.x(_type) < x)
@ -110,58 +114,56 @@ qreal GraphItem::yAtX(qreal x)
}
QLineF l;
if (_graph.at(mid).x(_type) < x)
l = QLineF(_graph.at(mid).x(_type), _graph.at(mid).y(),
_graph.at(mid+1).x(_type), _graph.at(mid+1).y());
if (seg->at(mid).x(_type) < x)
l = QLineF(seg->at(mid).x(_type), seg->at(mid).y(),
seg->at(mid+1).x(_type), seg->at(mid+1).y());
else
l = QLineF(_graph.at(mid-1).x(_type), _graph.at(mid-1).y(),
_graph.at(mid).x(_type), _graph.at(mid).y());
l = QLineF(seg->at(mid-1).x(_type), seg->at(mid-1).y(),
seg->at(mid).x(_type), seg->at(mid).y());
return -l.pointAt((x - l.p1().x()) / (l.p2().x() - l.p1().x())).y();
}
qreal GraphItem::distanceAtTime(qreal time)
{
const GraphSegment *seg = segment(time, Time);
if (!seg)
return NAN;
int low = 0;
int high = _graph.count() - 1;
int high = seg->count() - 1;
int mid = 0;
Q_ASSERT(high > low);
Q_ASSERT(time >= _graph.at(low).t() && time <= _graph.at(high).t());
if (!(time >= seg->at(low).t() && time <= seg->at(high).t()))
return NAN;
while (low <= high) {
mid = low + ((high - low) / 2);
const GraphPoint &p = _graph.at(mid);
const GraphPoint &p = seg->at(mid);
if (p.t() > time)
high = mid - 1;
else if (p.t() < time)
low = mid + 1;
else
return _graph.at(mid).s();
return seg->at(mid).s();
}
QLineF l;
if (_graph.at(mid).t() < time)
l = QLineF(_graph.at(mid).t(), _graph.at(mid).s(), _graph.at(mid+1).t(),
_graph.at(mid+1).s());
if (seg->at(mid).t() < time)
l = QLineF(seg->at(mid).t(), seg->at(mid).s(), seg->at(mid+1).t(),
seg->at(mid+1).s());
else
l = QLineF(_graph.at(mid-1).t(), _graph.at(mid-1).s(),
_graph.at(mid).t(), _graph.at(mid).s());
l = QLineF(seg->at(mid-1).t(), seg->at(mid-1).s(),
seg->at(mid).t(), seg->at(mid).s());
return l.pointAt((time - l.p1().x()) / (l.p2().x() - l.p1().x())).y();
}
void GraphItem::emitSliderPositionChanged(qreal pos)
{
if (_type == Time) {
if (_time) {
if (pos >= _graph.first().t() && pos <= _graph.last().t())
emit sliderPositionChanged(distanceAtTime(pos));
else
emit sliderPositionChanged(NAN);
} else
emit sliderPositionChanged(NAN);
} else
if (_type == Time)
emit sliderPositionChanged(_time ? distanceAtTime(pos) : NAN);
else
emit sliderPositionChanged(pos);
}
@ -197,9 +199,13 @@ void GraphItem::updatePath()
if (_type == Time && !_time)
return;
_path.moveTo(_graph.first().x(_type) * _sx, -_graph.first().y() * _sy);
for (int i = 1; i < _graph.size(); i++)
_path.lineTo(_graph.at(i).x(_type) * _sx, -_graph.at(i).y() * _sy);
for (int i = 0; i < _graph.size(); i++) {
const GraphSegment &segment = _graph.at(i);
_path.moveTo(segment.first().x(_type) * _sx, -segment.first().y() * _sy);
for (int i = 1; i < segment.size(); i++)
_path.lineTo(segment.at(i).x(_type) * _sx, -segment.at(i).y() * _sy);
}
}
void GraphItem::updateBounds()
@ -211,18 +217,71 @@ void GraphItem::updateBounds()
qreal bottom, top, left, right;
QPointF p = QPointF(_graph.first().x(_type), -_graph.first().y());
QPointF p = QPointF(_graph.first().first().x(_type),
-_graph.first().first().y());
bottom = p.y(); top = p.y(); left = p.x(); right = p.x();
for (int i = 1; i < _graph.size(); i++) {
p = QPointF(_graph.at(i).x(_type), -_graph.at(i).y());
bottom = qMax(bottom, p.y()); top = qMin(top, p.y());
right = qMax(right, p.x()); left = qMin(left, p.x());
for (int i = 0; i < _graph.size(); i++) {
const GraphSegment &segment = _graph.at(i);
for (int j = 0; j < segment.size(); j++) {
p = QPointF(segment.at(j).x(_type), -segment.at(j).y());
bottom = qMax(bottom, p.y()); top = qMin(top, p.y());
right = qMax(right, p.x()); left = qMin(left, p.x());
}
}
_bounds = QRectF(QPointF(left, top), QPointF(right, bottom));
}
qreal GraphItem::max() const
{
qreal ret = _graph.first().first().y();
for (int i = 0; i < _graph.size(); i++) {
const GraphSegment &segment = _graph.at(i);
for (int j = 0; j < segment.size(); j++) {
qreal y = segment.at(j).y();
if (y > ret)
ret = y;
}
}
return ret;
}
qreal GraphItem::min() const
{
qreal ret = _graph.first().first().y();
for (int i = 0; i < _graph.size(); i++) {
const GraphSegment &segment = _graph.at(i);
for (int j = 0; j < segment.size(); j++) {
qreal y = segment.at(j).y();
if (y < ret)
ret = y;
}
}
return ret;
}
qreal GraphItem::avg() const
{
qreal sum = 0;
for (int i = 0; i < _graph.size(); i++) {
const GraphSegment &segment = _graph.at(i);
for (int j = 1; j < segment.size(); j++)
sum += segment.at(j).y() * (segment.at(j).s() - segment.at(j-1).s());
}
return sum/_graph.last().last().s();
}
void GraphItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
Q_UNUSED(event);

View File

@ -21,6 +21,10 @@ public:
const QRectF &bounds() const {return _bounds;}
qreal max() const;
qreal min() const;
qreal avg() const;
void setScale(qreal sx, qreal sy);
void setGraphType(GraphType type);
int id() const {return _id;}
@ -46,6 +50,7 @@ private:
void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
const GraphSegment *segment(qreal x, GraphType type) const;
void updatePath();
void updateShape();
void updateBounds();

View File

@ -34,9 +34,9 @@ GraphView::GraphView(QWidget *parent)
setBackgroundBrush(QBrush(palette().brush(QPalette::Base)));
_xAxis = new AxisItem(AxisItem::X);
_xAxis->setZValue(2.0);
_xAxis->setZValue(1.0);
_yAxis = new AxisItem(AxisItem::Y);
_yAxis->setZValue(2.0);
_yAxis->setZValue(1.0);
_slider = new SliderItem();
_slider->setZValue(3.0);
_sliderInfo = new SliderInfoItem(_slider);

View File

@ -6,17 +6,6 @@
HeartRateGraphItem::HeartRateGraphItem(const Graph &graph, GraphType type,
QGraphicsItem *parent) : GraphItem(graph, type, parent)
{
qreal sum = 0;
_max = graph.first().y();
for (int i = 1; i < graph.size(); i++) {
qreal y = graph.at(i).y();
sum += y * (graph.at(i).s() - graph.at(i-1).s());
if (y > _max)
_max = y;
}
_avg = sum/graph.last().s();
setToolTip(toolTip());
}

View File

@ -11,13 +11,8 @@ public:
HeartRateGraphItem(const Graph &graph, GraphType type,
QGraphicsItem *parent = 0);
qreal max() const {return _max;}
qreal avg() const {return _avg;}
private:
QString toolTip() const;
qreal _avg, _max;
};
#endif // HEARTRATEGRAPHITEM_H

View File

@ -8,19 +8,22 @@
#define GEOGRAPHICAL_MILE 1855.3248
static unsigned segments(qreal distance)
static inline bool isInvalid(const QPointF &p)
{
return (std::isnan(p.x()) || std::isnan(p.y()));
}
static inline unsigned segments(qreal distance)
{
return ceil(distance / GEOGRAPHICAL_MILE);
}
PathItem::PathItem(const Path &path, Map *map, QGraphicsItem *parent)
: QGraphicsObject(parent), _path(path)
: QGraphicsObject(parent), _path(path), _map(map)
{
Q_ASSERT(path.count() >= 2);
Q_ASSERT(_path.isValid());
_map = map;
_digitalZoom = 0;
_width = 3;
QBrush brush(Qt::SolidPattern);
_pen = QPen(brush, _width);
@ -29,8 +32,8 @@ PathItem::PathItem(const Path &path, Map *map, QGraphicsItem *parent)
updateShape();
_marker = new MarkerItem(this);
_marker->setPos(position(_path.at(0).distance()));
_markerDistance = _path.at(0).distance();
_marker->setPos(position(_path.first().first().distance()));
_markerDistance = _path.first().first().distance();
setCursor(Qt::ArrowCursor);
setAcceptHoverEvents(true);
@ -73,23 +76,27 @@ void PathItem::updatePainterPath()
{
_painterPath = QPainterPath();
_painterPath.moveTo(_map->ll2xy(_path.first().coordinates()));
for (int i = 1; i < _path.size(); i++) {
const PathPoint &p1 = _path.at(i-1);
const PathPoint &p2 = _path.at(i);
unsigned n = segments(p2.distance() - p1.distance());
for (int i = 0; i < _path.size(); i++) {
const PathSegment &segment = _path.at(i);
_painterPath.moveTo(_map->ll2xy(segment.first().coordinates()));
if (n > 1) {
GreatCircle gc(p1.coordinates(), p2.coordinates());
Coordinates last = p1.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());
for (unsigned j = 1; j <= n; j++) {
Coordinates c(gc.pointAt(j/(double)n));
addSegment(last, c);
last = c;
}
} else
addSegment(p1.coordinates(), p2.coordinates());
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());
}
}
}
@ -166,35 +173,48 @@ void PathItem::setDigitalZoom(int zoom)
updateShape();
}
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;
}
QPointF PathItem::position(qreal x) const
{
const PathSegment *seg = segment(x);
if (!seg)
return QPointF(NAN, NAN);
int low = 0;
int high = _path.count() - 1;
int high = seg->count() - 1;
int mid = 0;
Q_ASSERT(high > low);
Q_ASSERT(x >= _path.at(low).distance() && x <= _path.at(high).distance());
if (!(x >= seg->first().distance() && x <= seg->last().distance()))
return QPointF(NAN, NAN);
while (low <= high) {
mid = low + ((high - low) / 2);
qreal val = _path.at(mid).distance();
qreal val = seg->at(mid).distance();
if (val > x)
high = mid - 1;
else if (val < x)
low = mid + 1;
else
return _map->ll2xy(_path.at(mid).coordinates());
return _map->ll2xy(seg->at(mid).coordinates());
}
Coordinates c1, c2;
qreal p1, p2;
if (_path.at(mid).distance() < x) {
c1 = _path.at(mid).coordinates(); c2 = _path.at(mid+1).coordinates();
p1 = _path.at(mid).distance(); p2 = _path.at(mid+1).distance();
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();
} else {
c1 = _path.at(mid-1).coordinates(); c2 = _path.at(mid).coordinates();
p1 = _path.at(mid-1).distance(); p2 = _path.at(mid).distance();
c1 = seg->at(mid-1).coordinates(); c2 = seg->at(mid).coordinates();
p1 = seg->at(mid-1).distance(); p2 = seg->at(mid).distance();
}
unsigned n = segments(p2 - p1);
@ -224,13 +244,15 @@ QPointF PathItem::position(qreal x) const
void PathItem::moveMarker(qreal distance)
{
if (distance >= _path.first().distance()
&& distance <= _path.last().distance()) {
_marker->setVisible(true);
_marker->setPos(position(distance));
_markerDistance = distance;
} else
QPointF pos(position(distance));
if (isInvalid(pos))
_marker->setVisible(false);
else {
_marker->setVisible(true);
_marker->setPos(pos);
_markerDistance = distance;
}
}
void PathItem::setMarkerColor(const QColor &color)

View File

@ -42,6 +42,7 @@ protected:
MarkerItem *_marker;
private:
const PathSegment *segment(qreal x) const;
QPointF position(qreal distance) const;
void updatePainterPath();
void updateShape();

View File

@ -6,17 +6,6 @@
PowerGraphItem::PowerGraphItem(const Graph &graph, GraphType type,
QGraphicsItem *parent) : GraphItem(graph, type, parent)
{
qreal sum = 0;
_max = graph.first().y();
for (int i = 1; i < graph.size(); i++) {
qreal y = graph.at(i).y();
sum += y * (graph.at(i).s() - graph.at(i-1).s());
if (y > _max)
_max = y;
}
_avg = sum/graph.last().s();
setToolTip(toolTip());
}

View File

@ -11,13 +11,8 @@ public:
PowerGraphItem(const Graph &graph, GraphType type,
QGraphicsItem *parent = 0);
qreal max() const {return _max;}
qreal avg() const {return _avg;}
private:
QString toolTip() const;
qreal _avg, _max;
};
#endif // POWERGRAPHITEM_H

View File

@ -15,7 +15,8 @@ QString RouteItem::toolTip(Units units) const
tt.insert(tr("Name"), _name);
if (!_desc.isEmpty())
tt.insert(tr("Description"), _desc);
tt.insert(tr("Distance"), Format::distance(_path.last().distance(), units));
tt.insert(tr("Distance"), Format::distance(_path.last().last().distance(),
units));
return tt.toString();
}

View File

@ -10,15 +10,9 @@ SpeedGraphItem::SpeedGraphItem(const Graph &graph, GraphType type,
_units = Metric;
_timeType = Total;
_avg = graph.last().s() / graph.last().t();
_mavg = graph.last().s() / movingTime;
_max = graph.first().y();
for (int i = 1; i < graph.size(); i++) {
qreal y = graph.at(i).y();
if (y > _max)
_max = y;
}
_max = GraphItem::max();
_avg = graph.last().last().s() / graph.last().last().t();
_mavg = graph.last().last().s() / movingTime;
setToolTip(toolTip());
}

View File

@ -12,9 +12,9 @@ public:
SpeedGraphItem(const Graph &graph, GraphType type, qreal movingTime,
QGraphicsItem *parent = 0);
qreal max() const {return _max;}
qreal avg() const {return _avg;}
qreal mavg() const {return _mavg;}
qreal max() const {return _max;}
void setUnits(Units units);
void setTimeType(TimeType type);

View File

@ -6,20 +6,9 @@
TemperatureGraphItem::TemperatureGraphItem(const Graph &graph, GraphType type,
QGraphicsItem *parent) : GraphItem(graph, type, parent)
{
qreal sum = 0;
_min = _max = graph.first().y();
for (int j = 1; j < graph.size(); j++) {
qreal y = graph.at(j).y();
sum += graph.at(j).y() * (graph.at(j).s() - graph.at(j-1).s());
if (y > _max)
_max = y;
if (y < _min)
_min = y;
}
_avg = sum/graph.last().s();
_min = GraphItem::min();
_max = GraphItem::max();
_avg = GraphItem::avg();
setToolTip(toolTip(Metric));
}

View File

@ -20,7 +20,7 @@ public:
private:
QString toolTip(Units units) const;
qreal _avg, _min, _max;
qreal _min, _max, _avg;
};
#endif // TEMPERATUREGRAPHITEM_H

View File

@ -13,7 +13,8 @@ QString TrackItem::toolTip(Units units) const
tt.insert(tr("Name"), _name);
if (!_desc.isEmpty())
tt.insert(tr("Description"), _desc);
tt.insert(tr("Distance"), Format::distance(_path.last().distance(), units));
tt.insert(tr("Distance"), Format::distance(_path.last().last().distance(),
units));
if (_time > 0)
tt.insert(tr("Total time"), Format::timeSpan(_time));
if (_movingTime > 0)