1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-10-06 23:03:22 +02:00
GPXSee/src/GUI/speedgraph.cpp

136 lines
2.6 KiB
C++
Raw Normal View History

2017-11-26 18:54:03 +01:00
#include "data/data.h"
#include "config.h"
#include "tooltip.h"
2018-03-10 20:25:13 +01:00
#include "format.h"
#include "speedgraphitem.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
{
2018-03-10 20:25:13 +01:00
_units = Metric;
2017-02-13 20:12:48 +01:00
_timeType = Total;
2016-08-19 19:48:44 +02:00
_showTracks = true;
2018-03-10 20:25:13 +01:00
setYUnits();
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) {
2018-03-10 20:25:13 +01:00
QString pace = Format::timeSpan((3600.0 / (avg() * yScale())), false);
QString pu = (_units == Metric) ? tr("min/km") : (_units == Imperial) ?
tr("min/mi") : tr("min/nmi");
2016-08-19 19:48:44 +02:00
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());
2018-03-10 20:25:13 +01:00
GraphView::addInfo(tr("Pace"), pace + UNIT_SPACE + pu);
2016-08-19 19:48:44 +02:00
} else
clearInfo();
2015-12-19 20:23:07 +01:00
}
QList<GraphItem*> SpeedGraph::loadData(const Data &data)
2015-10-12 01:12:12 +02:00
{
QList<GraphItem*> graphs;
2016-10-23 11:09:20 +02:00
for (int i = 0; i < data.tracks().count(); i++) {
const Track *track = data.tracks().at(i);
const Graph &graph = track->speed();
2016-09-19 23:35:04 +02:00
if (graph.size() < 2) {
skipColor();
graphs.append(0);
} else {
SpeedGraphItem *gi = new SpeedGraphItem(graph, _graphType,
track->movingTime());
gi->setTimeType(_timeType);
GraphView::addGraph(gi);
_avg.append(QPointF(track->distance(), gi->avg()));
_mavg.append(QPointF(track->distance(), gi->mavg()));
graphs.append(gi);
}
}
2016-03-27 13:23:00 +02:00
for (int i = 0; i < data.routes().count(); i++) {
2016-08-09 01:16:19 +02:00
skipColor();
graphs.append(0);
}
2016-08-09 01:16:19 +02:00
setInfo();
redraw();
return graphs;
2015-10-12 01:12:12 +02:00
}
qreal SpeedGraph::avg() const
{
qreal sum = 0, w = 0;
QList<QPointF>::const_iterator it;
const QList<QPointF> &list = (_timeType == Moving) ? _mavg : _avg;
for (it = list.begin(); it != list.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();
_mavg.clear();
2015-10-12 01:12:12 +02:00
GraphView::clear();
2015-10-12 01:12:12 +02:00
}
2015-12-19 20:23:07 +01:00
2018-03-10 20:25:13 +01:00
void SpeedGraph::setYUnits()
2015-12-19 20:23:07 +01:00
{
2018-03-10 20:25:13 +01:00
if (_units == Nautical) {
2018-02-11 23:51:57 +01:00
GraphView::setYUnits(tr("kn"));
setYScale(MS2KN);
2018-03-10 20:25:13 +01:00
} else if (_units == Imperial) {
GraphView::setYUnits(tr("mi/h"));
setYScale(MS2MIH);
2018-02-11 23:51:57 +01:00
} else {
GraphView::setYUnits(tr("km/h"));
setYScale(MS2KMH);
2015-12-19 20:23:07 +01:00
}
}
2017-10-04 23:15:39 +02:00
void SpeedGraph::setUnits(Units units)
{
2018-03-10 20:25:13 +01:00
_units = units;
setYUnits();
2016-08-09 01:16:19 +02:00
setInfo();
2015-12-19 20:23:07 +01:00
2017-10-04 23:15:39 +02:00
GraphView::setUnits(units);
2015-12-19 20:23:07 +01:00
}
void SpeedGraph::setTimeType(enum TimeType type)
{
_timeType = type;
2017-09-25 19:56:04 +02:00
for (int i = 0; i < _graphs.size(); i++)
static_cast<SpeedGraphItem*>(_graphs.at(i))->setTimeType(type);
setInfo();
redraw();
}
void SpeedGraph::showTracks(bool show)
{
2016-08-19 19:48:44 +02:00
_showTracks = show;
showGraph(show);
2016-08-20 11:28:08 +02:00
setInfo();
redraw();
}