1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-10-07 07:13:21 +02:00
GPXSee/src/elevationgraph.cpp

99 lines
2.0 KiB
C++
Raw Normal View History

2015-10-12 01:12:12 +02:00
#include <float.h>
#include "config.h"
2016-02-12 10:23:14 +01:00
#include "gpx.h"
2015-10-12 01:12:12 +02:00
#include "elevationgraph.h"
2015-11-25 23:58:46 +01:00
ElevationGraph::ElevationGraph(QWidget *parent) : GraphView(parent)
2015-10-12 01:12:12 +02:00
{
_ascent = 0;
_descent = 0;
_max = -FLT_MAX;
_min = FLT_MAX;
GraphView::setXLabel(tr("Distance"));
GraphView::setYLabel(tr("Elevation"));
GraphView::setXUnits(tr("km"));
GraphView::setYUnits(tr("m"));
GraphView::setXScale(M2KM);
2015-12-19 20:23:07 +01:00
}
void ElevationGraph::addInfo()
{
GraphView::addInfo(tr("Ascent"), QString::number(_ascent * _yScale, 'f', 0)
2016-02-13 12:13:56 +01:00
+ UNIT_SPACE + _yUnits);
GraphView::addInfo(tr("Descent"), QString::number(_descent * _yScale, 'f', 0)
2016-02-13 12:13:56 +01:00
+ UNIT_SPACE + _yUnits);
GraphView::addInfo(tr("Maximum"), QString::number(_max * _yScale, 'f', 0)
2016-02-13 12:13:56 +01:00
+ UNIT_SPACE + _yUnits);
GraphView::addInfo(tr("Minimum"), QString::number(_min * _yScale, 'f', 0)
2016-02-13 12:13:56 +01:00
+ UNIT_SPACE + _yUnits);
2015-10-12 01:12:12 +02:00
}
2015-10-17 12:08:30 +02:00
void ElevationGraph::loadGPX(const GPX &gpx)
2015-10-12 01:12:12 +02:00
{
for (int i = 0; i < gpx.trackCount(); i++) {
QVector<QPointF> data;
qreal min, max, ascent = 0, descent = 0;
gpx.track(i).elevationGraph(data);
if (data.isEmpty())
return;
min = max = data.at(0).y();
for (int i = 1; i < data.size(); i++) {
qreal cur = data.at(i).y();
qreal prev = data.at(i-1).y();
if (cur > prev)
ascent += cur - prev;
if (cur < prev)
descent += prev - cur;
if (cur > max)
max = cur;
if (cur < min)
min = cur;
}
_ascent += ascent;
_descent += descent;
_max = qMax(_max, max);
_min = qMin(_min, min);
addInfo();
loadData(data);
2015-10-12 01:12:12 +02:00
}
}
void ElevationGraph::clear()
{
_ascent = 0;
_descent = 0;
_max = -FLT_MAX;
_min = FLT_MAX;
GraphView::clear();
2015-10-12 01:12:12 +02:00
}
2015-12-19 20:23:07 +01:00
void ElevationGraph::setUnits(enum Units units)
{
if (units == Metric) {
GraphView::setXUnits(tr("km"));
GraphView::setYUnits(tr("m"));
GraphView::setXScale(M2KM);
GraphView::setYScale(1);
2015-12-19 20:23:07 +01:00
} else {
GraphView::setXUnits(tr("mi"));
GraphView::setYUnits(tr("ft"));
GraphView::setXScale(M2MI);
GraphView::setYScale(M2FT);
2015-12-19 20:23:07 +01:00
}
clearInfo();
addInfo();
redraw();
}