diff --git a/src/elevationgraph.cpp b/src/elevationgraph.cpp index ee5dc04a..34a69d0b 100644 --- a/src/elevationgraph.cpp +++ b/src/elevationgraph.cpp @@ -16,6 +16,7 @@ ElevationGraph::ElevationGraph(QWidget *parent) : GraphView(parent) GraphView::setXUnits(tr("km")); GraphView::setYUnits(tr("m")); GraphView::setXScale(M2KM); + GraphView::setMinRange(50.0); } void ElevationGraph::addInfo() diff --git a/src/graphview.cpp b/src/graphview.cpp index e84766e6..fe2826ea 100644 --- a/src/graphview.cpp +++ b/src/graphview.cpp @@ -53,6 +53,7 @@ GraphView::GraphView(QWidget *parent) _yScale = 1; _precision = 0; + _minRange = 0.01; } GraphView::~GraphView() @@ -166,8 +167,9 @@ void GraphView::resize(const QSizeF &size) { QRectF r; QSizeF mx, my; + QPointF rx, ry; QTransform transform; - qreal xs, ys; + qreal xs, ys, diff; if (_xAxis->scene() == _scene) @@ -182,11 +184,21 @@ void GraphView::resize(const QSizeF &size) for (int i = 0; i < _graphs.size(); i++) _graphs.at(i)->resetTransform(); - _xAxis->setRange(QPointF(_xMin * _xScale, _xMax * _xScale)); - _yAxis->setRange(QPointF(_yMin * _yScale, _yMax * _yScale)); + rx = QPointF(_xMin * _xScale, _xMax * _xScale); + ry = QPointF(_yMin * _yScale, _yMax * _yScale); + if ((diff = ry.y() - ry.x()) < _minRange) + ry = QPointF(ry.x() - (_minRange/2 - diff/2), + ry.y() + (_minRange/2 - diff/2)); + _xAxis->setRange(rx); + _yAxis->setRange(ry); mx = _xAxis->margin(); my = _yAxis->margin(); + r = _scene->itemsBoundingRect(); + if (r.height() < _minRange) + r.adjust(0, -(_minRange/2 - r.height()/2), 0, + _minRange/2 - r.height()/2); + xs = (size.width() - (my.width() + mx.width())) / r.width(); ys = (size.height() - (mx.height() + my.height()) - _info->boundingRect().height()) / r.height(); @@ -196,6 +208,10 @@ void GraphView::resize(const QSizeF &size) _graphs.at(i)->setTransform(transform); r = _scene->itemsBoundingRect(); + if (r.height() < _minRange * ys) + r.adjust(0, -(_minRange/2 * ys - r.height()/2), 0, + (_minRange/2) * ys - r.height()/2); + _xAxis->setSize(r.width()); _yAxis->setSize(r.height()); _xAxis->setPos(r.bottomLeft()); @@ -280,9 +296,12 @@ void GraphView::emitSliderPositionChanged(const QPointF &pos) emit sliderPositionChanged(val); const QPainterPath &path = _graphs.at(0)->path(); + QRectF br = path.boundingRect(); + if (br.height() < _minRange) + br.adjust(0, -(_minRange/2 - br.height()/2), 0, + _minRange/2 - br.height()/2); QPointF p = path.pointAtPercent(val); - qreal r = (p.y() - path.boundingRect().bottom()) - / path.boundingRect().height(); + qreal r = (p.y() - br.bottom()) / br.height(); _sliderInfo->setPos(QPointF(0, _slider->boundingRect().height() * r)); _sliderInfo->setText(QString::number(-p.y() * _yScale, 'f', _precision)); } diff --git a/src/graphview.h b/src/graphview.h index aff2be98..8373b7da 100644 --- a/src/graphview.h +++ b/src/graphview.h @@ -41,7 +41,8 @@ public: void setYUnits(const QString &units); void setXScale(qreal scale); void setYScale(qreal scale); - void setPrecision(int p) {_precision = p;} + void setPrecision(int precision) {_precision = precision;} + void setMinRange(qreal range) {_minRange = range;} void redraw(); @@ -64,6 +65,7 @@ protected: QString _xUnits, _yUnits; QString _xLabel, _yLabel; int _precision; + qreal _minRange; private slots: void emitSliderPositionChanged(const QPointF &pos); diff --git a/src/sliderinfoitem.cpp b/src/sliderinfoitem.cpp index 4adbbea0..9bd0059b 100644 --- a/src/sliderinfoitem.cpp +++ b/src/sliderinfoitem.cpp @@ -7,7 +7,6 @@ SliderInfoItem::SliderInfoItem(QGraphicsItem *parent) : QGraphicsItem(parent) { - } void SliderInfoItem::updateBoundingRect() diff --git a/src/track.cpp b/src/track.cpp index f09215a3..806cea01 100644 --- a/src/track.cpp +++ b/src/track.cpp @@ -1,6 +1,8 @@ +#include #include "ll.h" #include "track.h" + #define WINDOW_EF 3 #define WINDOW_SE 11 #define WINDOW_SF 11 @@ -84,9 +86,13 @@ void Track::elevationGraph(QVector &graph) const if (!_data.size()) return; + if (isnan(_data.at(0).elevation)) + return; raw.append(QPointF(0, _data.at(0).elevation)); for (int i = 1; i < _data.size(); i++) { dist += llDistance(_data.at(i).coordinates, _data.at(i-1).coordinates); + if (isnan(_data.at(i).elevation)) + return; raw.append(QPointF(dist, _data.at(i).elevation - _data.at(i).geoidheight)); } @@ -108,7 +114,7 @@ void Track::speedGraph(QVector &graph) const dt = _data.at(i-1).timestamp.msecsTo(_data.at(i).timestamp) / 1000.0; dist += ds; - if (_data.at(i).speed < 0) { + if (isnan(_data.at(i).speed)) { if (dt == 0) continue; v = ds / dt; diff --git a/src/trackpoint.h b/src/trackpoint.h index 0e181e93..773e3e21 100644 --- a/src/trackpoint.h +++ b/src/trackpoint.h @@ -3,6 +3,7 @@ #include #include +#include struct Trackpoint { @@ -12,7 +13,7 @@ struct Trackpoint qreal geoidheight; qreal speed; - Trackpoint() {elevation = 0; geoidheight = 0; speed = -1;} + Trackpoint() {elevation = NAN; geoidheight = 0; speed = NAN;} }; #endif // TRACKPOINT_H