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

86 lines
1.5 KiB
C++
Raw Normal View History

#include "config.h"
2015-10-12 01:12:12 +02:00
#include "speedgraph.h"
2015-10-17 01:33:02 +02:00
SpeedGraph::SpeedGraph(QWidget *parent) : Graph(parent)
2015-10-12 01:12:12 +02:00
{
_max = 0;
Graph::setXLabel(tr("Distance"));
Graph::setYLabel(tr("Speed"));
Graph::setXUnits(tr("km"));
Graph::setYUnits(tr("km/h"));
2015-12-19 20:23:07 +01:00
Graph::setXScale(M2KM);
Graph::setYScale(MS2KMH);
2015-10-17 01:33:02 +02:00
Graph::setPrecision(1);
2015-10-12 01:12:12 +02:00
}
2015-12-19 20:23:07 +01:00
void SpeedGraph::addInfo()
{
Graph::addInfo(tr("Average"), QString::number(avg() * _yScale, 'f', 1)
+ THIN_SPACE + _yUnits);
Graph::addInfo(tr("Maximum"), QString::number(_max * _yScale, 'f', 1)
+ THIN_SPACE + _yUnits);
}
2015-10-17 12:08:30 +02:00
void SpeedGraph::loadGPX(const GPX &gpx)
2015-10-12 01:12:12 +02:00
{
2015-10-17 12:08:30 +02:00
QVector<QPointF> data;
qreal max = 0;
2015-10-17 12:08:30 +02:00
gpx.speedGraph(data);
if (data.isEmpty())
return;
_avg.append(QPointF(gpx.distance(), gpx.distance() / gpx.time()));
2015-10-12 01:12:12 +02:00
2015-10-16 18:28:47 +02:00
for (int i = 0; i < data.size(); i++)
max = qMax(max, data.at(i).y());
2015-10-12 01:12:12 +02:00
_max = qMax(_max, max);
2015-12-19 20:23:07 +01:00
addInfo();
loadData(data);
2015-10-12 01:12:12 +02:00
}
qreal SpeedGraph::avg() const
{
qreal sum = 0, w = 0;
QList<QPointF>::const_iterator it;
for (it = _avg.begin(); it != _avg.end(); it++) {
sum += it->y() * it->x();
w += it->x();
}
return (sum / w);
}
2015-10-12 01:12:12 +02:00
void SpeedGraph::clear()
{
_max = 0;
_avg.clear();
Graph::clear();
}
2015-12-19 20:23:07 +01:00
void SpeedGraph::setUnits(enum Units units)
{
if (units == Metric) {
Graph::setXUnits(tr("km"));
Graph::setYUnits(tr("km/h"));
Graph::setXScale(M2KM);
Graph::setYScale(MS2KMH);
} else {
Graph::setXUnits(tr("mi"));
Graph::setYUnits(tr("mi/h"));
Graph::setXScale(M2MI);
Graph::setYScale(MS2MIH);
}
clearInfo();
addInfo();
redraw();
}