From 52ea52ff4e9cae68baab7c2e917bf89b9f3a4d21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20T=C5=AFma?= Date: Mon, 10 Apr 2023 10:00:30 +0200 Subject: [PATCH] Fixed broken graphs when data contains empty segments fixes #490 --- src/data/track.cpp | 25 ++++++++++++++++++++----- src/data/track.h | 2 ++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/data/track.cpp b/src/data/track.cpp index 4ad53133..9c32e495 100644 --- a/src/data/track.cpp +++ b/src/data/track.cpp @@ -88,6 +88,24 @@ static GraphSegment filter(const GraphSegment &g, int window) } +qreal Track::lastDistance(int seg) +{ + for (int i = seg - 1; i >= 0; i--) + if (!_segments.at(i).distance.isEmpty()) + return _segments.at(i).distance.last(); + + return 0; +} + +qreal Track::lastTime(int seg) +{ + for (int i = seg - 1; i >= 0; i--) + if (!_segments.at(i).time.isEmpty()) + return _segments.at(i).time.last(); + + return 0; +} + Track::Track(const TrackData &data) : _pause(0) { qreal ds, dt; @@ -113,11 +131,8 @@ Track::Track(const TrackData &data) : _pause(0) Segment &seg = _segments.last(); - seg.distance.append(i && !_segments.at(i-1).distance.isEmpty() - ? _segments.at(i-1).distance.last() : 0); - seg.time.append(i && !_segments.at(i-1).time.isEmpty() - ? _segments.at(i-1).time.last() : - sd.first().hasTimestamp() ? 0 : NAN); + seg.distance.append(lastDistance(i)); + seg.time.append(sd.first().hasTimestamp() ? lastTime(i) : NAN); seg.speed.append(sd.first().hasTimestamp() ? 0 : NAN); acceleration.append(sd.first().hasTimestamp() ? 0 : NAN); bool hasTime = !std::isnan(seg.time.first()); diff --git a/src/data/track.h b/src/data/track.h index 3abe69dd..ab16b37d 100644 --- a/src/data/track.h +++ b/src/data/track.h @@ -63,6 +63,8 @@ private: QSet stop; }; + qreal lastDistance(int seg); + qreal lastTime(int seg); bool discardStopPoint(const Segment &seg, int i) const; Graph demElevation() const;