2018-09-09 18:46:43 +02:00
|
|
|
#include <QLocale>
|
2017-11-26 18:54:03 +01:00
|
|
|
#include "data/data.h"
|
2017-09-24 19:54:13 +02:00
|
|
|
#include "tooltip.h"
|
2018-03-10 20:25:13 +01:00
|
|
|
#include "format.h"
|
2017-09-24 19:54:13 +02:00
|
|
|
#include "speedgraphitem.h"
|
2015-10-12 01:12:12 +02:00
|
|
|
#include "speedgraph.h"
|
|
|
|
|
2015-10-22 00:05:45 +02:00
|
|
|
|
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;
|
2016-03-27 15:04:58 +02:00
|
|
|
|
2018-03-10 20:25:13 +01:00
|
|
|
setYUnits();
|
2016-03-21 22:37:55 +01:00
|
|
|
setYLabel(tr("Speed"));
|
2016-03-27 15:04:58 +02:00
|
|
|
|
2016-03-27 13:23:00 +02:00
|
|
|
setSliderPrecision(1);
|
2015-10-12 01:12:12 +02:00
|
|
|
}
|
|
|
|
|
2019-08-25 10:54:25 +02:00
|
|
|
SpeedGraph::~SpeedGraph()
|
|
|
|
{
|
|
|
|
qDeleteAll(_tracks);
|
|
|
|
}
|
|
|
|
|
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-09-09 18:46:43 +02:00
|
|
|
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");
|
|
|
|
|
2018-09-09 18:46:43 +02:00
|
|
|
GraphView::addInfo(tr("Average"), l.toString(avg() * yScale(), 'f',
|
2016-08-19 19:48:44 +02:00
|
|
|
1) + UNIT_SPACE + yUnits());
|
2018-09-09 18:46:43 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-03-25 23:08:26 +01:00
|
|
|
GraphItem *SpeedGraph::loadGraph(const Graph &graph, const Track &track,
|
|
|
|
const QColor &color, bool primary)
|
|
|
|
{
|
|
|
|
if (!graph.isValid())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
SpeedGraphItem *gi = new SpeedGraphItem(graph, _graphType, _width,
|
|
|
|
color, primary ? Qt::SolidLine : Qt::DashLine, track.movingTime());
|
|
|
|
gi->setTimeType(_timeType);
|
|
|
|
gi->setUnits(_units);
|
|
|
|
|
|
|
|
_tracks.append(gi);
|
|
|
|
if (_showTracks)
|
|
|
|
addGraph(gi);
|
|
|
|
|
|
|
|
if (primary) {
|
|
|
|
_avg.append(QPointF(track.distance(), gi->avg()));
|
|
|
|
_mavg.append(QPointF(track.distance(), gi->mavg()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return gi;
|
|
|
|
}
|
|
|
|
|
2018-05-04 19:36:37 +02:00
|
|
|
QList<GraphItem*> SpeedGraph::loadData(const Data &data)
|
2015-10-12 01:12:12 +02:00
|
|
|
{
|
2018-05-04 19:36:37 +02:00
|
|
|
QList<GraphItem*> graphs;
|
|
|
|
|
2016-10-23 11:09:20 +02:00
|
|
|
for (int i = 0; i < data.tracks().count(); i++) {
|
2020-03-25 23:08:26 +01:00
|
|
|
GraphItem *primary, *secondary;
|
|
|
|
QColor color(_palette.nextColor());
|
2019-01-31 01:46:53 +01:00
|
|
|
const Track &track = data.tracks().at(i);
|
2020-03-25 23:08:26 +01:00
|
|
|
const GraphPair &gp = track.speed();
|
|
|
|
|
|
|
|
primary = loadGraph(gp.primary(), track, color, true);
|
|
|
|
secondary = primary
|
|
|
|
? loadGraph(gp.secondary(), track, color, false) : 0;
|
|
|
|
if (primary && secondary)
|
|
|
|
primary->setSecondaryGraph(secondary);
|
|
|
|
|
|
|
|
graphs.append(primary);
|
2016-02-08 17:53:09 +01:00
|
|
|
}
|
2016-03-27 13:23:00 +02:00
|
|
|
|
2018-05-04 19:36:37 +02:00
|
|
|
for (int i = 0; i < data.routes().count(); i++) {
|
2019-08-25 10:54:25 +02:00
|
|
|
_palette.nextColor();
|
2018-05-04 19:36:37 +02:00
|
|
|
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++)
|
2019-08-25 10:54:25 +02:00
|
|
|
_palette.nextColor();
|
2019-01-31 01:46:53 +01:00
|
|
|
|
2016-08-09 01:16:19 +02:00
|
|
|
setInfo();
|
|
|
|
redraw();
|
2018-05-04 19:36:37 +02:00
|
|
|
|
|
|
|
return graphs;
|
2015-10-12 01:12:12 +02:00
|
|
|
}
|
|
|
|
|
2015-10-19 00:35:08 +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;
|
2015-10-19 00:35:08 +02:00
|
|
|
|
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();
|
2015-10-19 00:35:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return (sum / w);
|
|
|
|
}
|
|
|
|
|
2015-10-12 01:12:12 +02:00
|
|
|
void SpeedGraph::clear()
|
|
|
|
{
|
2019-08-25 10:54:25 +02:00
|
|
|
qDeleteAll(_tracks);
|
|
|
|
_tracks.clear();
|
|
|
|
|
2015-10-12 01:12:12 +02:00
|
|
|
_avg.clear();
|
2017-09-24 19:54:13 +02:00
|
|
|
_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) {
|
2016-03-27 15:04:58 +02:00
|
|
|
GraphView::setYUnits(tr("mi/h"));
|
2016-03-21 22:37:55 +01:00
|
|
|
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
|
|
|
}
|
2016-03-27 15:04:58 +02:00
|
|
|
}
|
|
|
|
|
2017-10-04 23:15:39 +02:00
|
|
|
void SpeedGraph::setUnits(Units units)
|
2016-03-27 15:04:58 +02:00
|
|
|
{
|
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
|
|
|
}
|
2016-08-16 00:27:54 +02:00
|
|
|
|
2017-02-12 19:57:55 +01:00
|
|
|
void SpeedGraph::setTimeType(enum TimeType type)
|
|
|
|
{
|
|
|
|
_timeType = type;
|
|
|
|
|
2019-08-25 10:54:25 +02:00
|
|
|
for (int i = 0; i < _tracks.size(); i++)
|
|
|
|
_tracks.at(i)->setTimeType(type);
|
2017-09-25 19:56:04 +02:00
|
|
|
|
2017-02-12 19:57:55 +01:00
|
|
|
setInfo();
|
|
|
|
redraw();
|
|
|
|
}
|
|
|
|
|
2016-08-16 00:27:54 +02:00
|
|
|
void SpeedGraph::showTracks(bool show)
|
|
|
|
{
|
2016-08-19 19:48:44 +02:00
|
|
|
_showTracks = show;
|
2016-08-16 00:27:54 +02:00
|
|
|
|
2019-08-25 10:54:25 +02:00
|
|
|
for (int i = 0; i < _tracks.size(); i++) {
|
|
|
|
if (show)
|
|
|
|
addGraph(_tracks.at(i));
|
|
|
|
else
|
|
|
|
removeGraph(_tracks.at(i));
|
|
|
|
}
|
|
|
|
|
2016-08-20 11:28:08 +02:00
|
|
|
setInfo();
|
2016-08-16 00:27:54 +02:00
|
|
|
|
|
|
|
redraw();
|
|
|
|
}
|