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

140 lines
2.7 KiB
C++
Raw Normal View History

#include <QLocale>
2017-11-26 18:54:03 +01:00
#include "data/data.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) {
QLocale l(QLocale::system());
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");
GraphView::addInfo(tr("Average"), l.toString(avg() * yScale(), 'f',
2016-08-19 19:48:44 +02:00
1) + UNIT_SPACE + yUnits());
GraphView::addInfo(tr("Maximum"), l.toString(max() * yScale(), 'f',
2016-08-19 19:48:44 +02:00
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++) {
2019-01-31 01:46:53 +01:00
const Track &track = data.tracks().at(i);
const Graph &graph = track.speed();
2019-01-31 01:46:53 +01:00
if (!graph.isValid()) {
skipColor();
graphs.append(0);
} else {
SpeedGraphItem *gi = new SpeedGraphItem(graph, _graphType,
2019-01-31 01:46:53 +01:00
track.movingTime());
gi->setTimeType(_timeType);
GraphView::addGraph(gi);
2019-01-31 01:46:53 +01:00
_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
2019-01-31 01:46:53 +01:00
for (int i = 0; i < data.areas().count(); i++)
skipColor();
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;
2019-03-15 19:43:17 +01:00
const QVector<QPointF> &vector = (_timeType == Moving) ? _mavg : _avg;
2019-03-15 19:43:17 +01:00
for (int i = 0; i < vector.size(); i++) {
const QPointF &p = vector.at(i);
sum += p.y() * p.x();
w += p.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
2019-03-05 22:34:50 +01:00
GraphTab::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();
}