Merge remote-tracking branch 'weblate/master'
[GPXSee.git] / src / GUI / speedgraph.cpp
blob2f9c1574cdcdc460f3873ce7333ca7508108ad06
1 #include <QLocale>
2 #include "data/data.h"
3 #include "tooltip.h"
4 #include "format.h"
5 #include "speedgraphitem.h"
6 #include "speedgraph.h"
9 SpeedGraph::SpeedGraph(QWidget *parent) : GraphTab(parent)
11 _units = Metric;
12 _timeType = Total;
13 _showTracks = false;
15 setYUnits();
16 setYLabel(tr("Speed"));
18 setSliderPrecision(1);
21 SpeedGraph::~SpeedGraph()
23 qDeleteAll(_tracks);
26 void SpeedGraph::setInfo()
28 if (_showTracks) {
29 QLocale l(QLocale::system());
30 QString pace = Format::timeSpan((3600.0 / (avg() * yScale())), false);
31 QString pu = (_units == Metric) ? tr("min/km") : (_units == Imperial) ?
32 tr("min/mi") : tr("min/nmi");
34 #ifdef Q_OS_ANDROID
35 GraphView::addInfo(tr("Avg"), l.toString(avg() * yScale(), 'f', 1)
36 + UNIT_SPACE + yUnits());
37 GraphView::addInfo(tr("Max"), l.toString(max() * yScale(), 'f', 1)
38 + UNIT_SPACE + yUnits());
39 #else // Q_OS_ANDROID
40 GraphView::addInfo(tr("Average"), l.toString(avg() * yScale(), 'f',
41 1) + UNIT_SPACE + yUnits());
42 GraphView::addInfo(tr("Maximum"), l.toString(max() * yScale(), 'f',
43 1) + UNIT_SPACE + yUnits());
44 #endif // Q_OS_ANDROID
45 GraphView::addInfo(tr("Pace"), pace + UNIT_SPACE + pu);
46 } else
47 clearInfo();
50 GraphItem *SpeedGraph::loadGraph(const Graph &graph, const Track &track,
51 const QColor &color, bool primary)
53 if (graph.isEmpty())
54 return 0;
56 SpeedGraphItem *gi = new SpeedGraphItem(graph, _graphType, _width,
57 color, primary ? Qt::SolidLine : Qt::DashLine, track.movingTime());
58 gi->setTimeType(_timeType);
59 gi->setUnits(_units);
61 _tracks.append(gi);
62 if (_showTracks)
63 addGraph(gi);
65 if (primary) {
66 _avg.append(QPointF(track.distance(), gi->avg()));
67 _mavg.append(QPointF(track.distance(), gi->mavg()));
70 return gi;
73 QList<GraphItem*> SpeedGraph::loadData(const Data &data)
75 QList<GraphItem*> graphs;
77 for (int i = 0; i < data.tracks().count(); i++) {
78 GraphItem *primary, *secondary;
79 QColor color(_palette.nextColor());
80 const Track &track = data.tracks().at(i);
81 const GraphPair &gp = track.speed();
83 primary = loadGraph(gp.primary(), track, color, true);
84 secondary = primary
85 ? loadGraph(gp.secondary(), track, color, false) : 0;
86 if (primary && secondary)
87 primary->setSecondaryGraph(secondary);
89 graphs.append(primary);
92 for (int i = 0; i < data.routes().count(); i++) {
93 _palette.nextColor();
94 graphs.append(0);
97 for (int i = 0; i < data.areas().count(); i++)
98 _palette.nextColor();
100 setInfo();
101 redraw();
103 return graphs;
106 qreal SpeedGraph::avg() const
108 qreal sum = 0, w = 0;
109 const QVector<QPointF> &vector = (_timeType == Moving) ? _mavg : _avg;
111 for (int i = 0; i < vector.size(); i++) {
112 const QPointF &p = vector.at(i);
113 sum += p.y() * p.x();
114 w += p.x();
117 return (sum / w);
120 void SpeedGraph::clear()
122 qDeleteAll(_tracks);
123 _tracks.clear();
125 _avg.clear();
126 _mavg.clear();
128 GraphTab::clear();
131 void SpeedGraph::setYUnits()
133 if (_units == Nautical) {
134 GraphView::setYUnits(tr("kn"));
135 setYScale(MS2KN);
136 } else if (_units == Imperial) {
137 GraphView::setYUnits(tr("mi/h"));
138 setYScale(MS2MIH);
139 } else {
140 GraphView::setYUnits(tr("km/h"));
141 setYScale(MS2KMH);
145 void SpeedGraph::setUnits(Units units)
147 _units = units;
149 setYUnits();
150 setInfo();
152 GraphView::setUnits(units);
155 void SpeedGraph::setTimeType(enum TimeType type)
157 _timeType = type;
159 for (int i = 0; i < _tracks.size(); i++)
160 _tracks.at(i)->setTimeType(type);
162 setInfo();
163 redraw();
166 void SpeedGraph::showTracks(bool show)
168 _showTracks = show;
170 for (int i = 0; i < _tracks.size(); i++) {
171 if (show)
172 addGraph(_tracks.at(i));
173 else
174 removeGraph(_tracks.at(i));
177 setInfo();
179 redraw();