From f0c3f9b8c8e1bf434f9206a7bcecdc2cefef9742 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20T=C5=AFma?= Date: Thu, 16 Jun 2016 20:33:56 +0200 Subject: [PATCH] Added temperature graphs Fixed handling of GPX files with inconsistent trackpoint entries --- src/temperaturegraph.cpp | 115 +++++++++++++++++++++++++++++++++++++++ src/temperaturegraph.h | 33 +++++++++++ src/trackpoint.cpp | 11 ++++ 3 files changed, 159 insertions(+) create mode 100644 src/temperaturegraph.cpp create mode 100644 src/temperaturegraph.h create mode 100644 src/trackpoint.cpp diff --git a/src/temperaturegraph.cpp b/src/temperaturegraph.cpp new file mode 100644 index 00000000..cdfd272b --- /dev/null +++ b/src/temperaturegraph.cpp @@ -0,0 +1,115 @@ +#include "gpx.h" +#include "temperaturegraph.h" + + +TemperatureGraph::TemperatureGraph(QWidget *parent) : GraphView(parent) +{ + _units = Metric; + + setYUnits(); + setXLabel(tr("Distance")); + setYLabel(tr("Temperature")); + + setSliderPrecision(1); +} + +void TemperatureGraph::addInfo() +{ + GraphView::addInfo(tr("Average"), QString::number(avg() * yScale() + + yOffset(), 'f', 1) + UNIT_SPACE + yUnits()); + GraphView::addInfo(tr("Minimum"), QString::number(min() * yScale() + + yOffset(), 'f', 1) + UNIT_SPACE + yUnits()); + GraphView::addInfo(tr("Maximum"), QString::number(max() * yScale() + + yOffset(), 'f', 1) + UNIT_SPACE + yUnits()); + + redraw(); +} + +void TemperatureGraph::loadGPX(const GPX &gpx) +{ + for (int i = 0; i < gpx.trackCount(); i++) { + QVector data; + qreal sum = 0, w = 0; + + gpx.track(i).temperatureGraph(data); + if (data.count() < 2) { + skipColor(); + continue; + } + + for (int j = 1; j < data.size(); j++) { + sum += data.at(j).y() * (data.at(j).x() - data.at(j-1).x()); + w += data.at(j).x() - data.at(j-1).x(); + } + _avg.append(QPointF(gpx.track(i).distance(), sum/w)); + + loadData(data); + } + + setXUnits(); + addInfo(); +} + +qreal TemperatureGraph::avg() const +{ + qreal sum = 0, w = 0; + QList::const_iterator it; + + for (it = _avg.begin(); it != _avg.end(); it++) { + sum += it->y() * it->x(); + w += it->x(); + } + + return (sum / w); +} + +void TemperatureGraph::clear() +{ + _avg.clear(); + + GraphView::clear(); +} + +void TemperatureGraph::setXUnits() +{ + if (_units == Metric) { + if (bounds().width() < KMINM) { + GraphView::setXUnits(tr("m")); + setXScale(1); + } else { + GraphView::setXUnits(tr("km")); + setXScale(M2KM); + } + } else { + if (bounds().width() < MIINM) { + GraphView::setXUnits(tr("ft")); + setXScale(M2FT); + } else { + GraphView::setXUnits(tr("mi")); + setXScale(M2MI); + } + } +} + +void TemperatureGraph::setYUnits() +{ + if (_units == Metric) { + GraphView::setYUnits(QString::fromUtf8("\xC2\xB0") + tr("C")); + setYScale(1); + setYOffset(0); + } else { + GraphView::setYUnits(QString::fromUtf8("\xC2\xB0") + tr("F")); + setYScale(C2FS); + setYOffset(C2FO); + } +} + +void TemperatureGraph::setUnits(enum Units units) +{ + _units = units; + setXUnits(); + setYUnits(); + + clearInfo(); + addInfo(); +} diff --git a/src/temperaturegraph.h b/src/temperaturegraph.h new file mode 100644 index 00000000..88fafec8 --- /dev/null +++ b/src/temperaturegraph.h @@ -0,0 +1,33 @@ +#ifndef TEMPERATUREGRAPH_H +#define TEMPERATUREGRAPH_H + +#include "units.h" +#include "graphview.h" + +class GPX; + +class TemperatureGraph : public GraphView +{ + Q_OBJECT + +public: + TemperatureGraph(QWidget *parent = 0); + + void loadGPX(const GPX &gpx); + void clear(); + void setUnits(enum Units units); + + qreal avg() const; + qreal min() const {return bounds().top();} + qreal max() const {return bounds().bottom();} + +private: + void setXUnits(); + void setYUnits(); + void addInfo(); + + QList _avg; + enum Units _units; +}; + +#endif // TEMPERATUREGRAPH_H diff --git a/src/trackpoint.cpp b/src/trackpoint.cpp new file mode 100644 index 00000000..b6ef4207 --- /dev/null +++ b/src/trackpoint.cpp @@ -0,0 +1,11 @@ +#include "trackpoint.h" + +QDebug operator<<(QDebug dbg, const Trackpoint &trackpoint) +{ + dbg.nospace() << "Trackpoint(" << trackpoint.coordinates << ", " + << trackpoint.timestamp << ", " << trackpoint.elevation << ", " + << trackpoint.geoidheight << ", " << trackpoint.speed << ", " + << trackpoint.heartRate << ", " << trackpoint.temperature << ")"; + + return dbg.maybeSpace(); +}