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

126 lines
2.0 KiB
C++
Raw Normal View History

#include "config.h"
2016-02-12 10:23:14 +01:00
#include "gpx.h"
2015-10-12 01:12:12 +02:00
#include "speedgraph.h"
2016-06-24 00:55:44 +02:00
SpeedGraph::SpeedGraph(QWidget *parent) : GraphTab(parent)
2015-10-12 01:12:12 +02:00
{
_units = Metric;
2016-08-19 19:48:44 +02:00
_showTracks = true;
setYUnits();
setXLabel(tr("Distance"));
setYLabel(tr("Speed"));
2016-03-27 13:23:00 +02:00
setSliderPrecision(1);
2015-10-12 01:12:12 +02:00
}
2016-08-09 01:16:19 +02:00
void SpeedGraph::setInfo()
2015-12-19 20:23:07 +01:00
{
2016-08-19 19:48:44 +02:00
if (_showTracks) {
GraphView::addInfo(tr("Average"), QString::number(avg() * yScale(), 'f',
1) + UNIT_SPACE + yUnits());
GraphView::addInfo(tr("Maximum"), QString::number(max() * yScale(), 'f',
1) + UNIT_SPACE + yUnits());
} else
clearInfo();
2015-12-19 20:23:07 +01:00
}
2015-10-17 12:08:30 +02:00
void SpeedGraph::loadGPX(const GPX &gpx)
2015-10-12 01:12:12 +02:00
{
for (int i = 0; i < gpx.tracks().count(); i++) {
QVector<QPointF> data = gpx.tracks().at(i)->speed();
if (data.count() < 2) {
skipColor();
continue;
}
2015-10-17 12:08:30 +02:00
_avg.append(QPointF(gpx.tracks().at(i)->distance(),
gpx.tracks().at(i)->distance() / gpx.tracks().at(i)->time()));
loadData(data);
}
2016-03-27 13:23:00 +02:00
for (int i = 0; i < gpx.routes().count(); i++)
2016-08-09 01:16:19 +02:00
skipColor();
setXUnits();
2016-08-09 01:16:19 +02:00
setInfo();
redraw();
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()
{
_avg.clear();
GraphView::clear();
2015-10-12 01:12:12 +02:00
}
2015-12-19 20:23:07 +01:00
void SpeedGraph::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 SpeedGraph::setYUnits()
2015-12-19 20:23:07 +01:00
{
if (_units == Metric) {
GraphView::setYUnits(tr("km/h"));
setYScale(MS2KMH);
2015-12-19 20:23:07 +01:00
} else {
GraphView::setYUnits(tr("mi/h"));
setYScale(MS2MIH);
2015-12-19 20:23:07 +01:00
}
}
void SpeedGraph::setUnits(enum Units units)
{
_units = units;
2016-08-09 01:16:19 +02:00
setXUnits();
setYUnits();
2016-08-09 01:16:19 +02:00
setInfo();
2015-12-19 20:23:07 +01:00
2016-08-09 01:16:19 +02:00
redraw();
2015-12-19 20:23:07 +01:00
}
void SpeedGraph::showTracks(bool show)
{
2016-08-19 19:48:44 +02:00
_showTracks = show;
showGraph(show);
2016-08-19 19:48:44 +02:00
setXUnits();
2016-08-20 11:28:08 +02:00
setInfo();
redraw();
}