forked from tumic0/GPXSee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeedgraph.cpp
173 lines (136 loc) · 3.32 KB
/
speedgraph.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <QLocale>
#include "data/data.h"
#include "tooltip.h"
#include "format.h"
#include "speedgraphitem.h"
#include "speedgraph.h"
SpeedGraph::SpeedGraph(QWidget *parent) : GraphTab(parent)
{
_units = Metric;
_timeType = Total;
_showTracks = true;
setYUnits();
setYLabel(tr("Speed"));
setSliderPrecision(1);
}
SpeedGraph::~SpeedGraph()
{
qDeleteAll(_tracks);
}
void SpeedGraph::setInfo()
{
if (_showTracks) {
QLocale l(QLocale::system());
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',
1) + UNIT_SPACE + yUnits());
GraphView::addInfo(tr("Maximum"), l.toString(max() * yScale(), 'f',
1) + UNIT_SPACE + yUnits());
GraphView::addInfo(tr("Pace"), pace + UNIT_SPACE + pu);
} else
clearInfo();
}
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;
}
QList<GraphItem*> SpeedGraph::loadData(const Data &data)
{
QList<GraphItem*> graphs;
for (int i = 0; i < data.tracks().count(); i++) {
GraphItem *primary, *secondary;
QColor color(_palette.nextColor());
const Track &track = data.tracks().at(i);
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);
}
for (int i = 0; i < data.routes().count(); i++) {
_palette.nextColor();
graphs.append(0);
}
for (int i = 0; i < data.areas().count(); i++)
_palette.nextColor();
setInfo();
redraw();
return graphs;
}
qreal SpeedGraph::avg() const
{
qreal sum = 0, w = 0;
const QVector<QPointF> &vector = (_timeType == Moving) ? _mavg : _avg;
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);
}
void SpeedGraph::clear()
{
qDeleteAll(_tracks);
_tracks.clear();
_avg.clear();
_mavg.clear();
GraphTab::clear();
}
void SpeedGraph::setYUnits()
{
if (_units == Nautical) {
GraphView::setYUnits(tr("kn"));
setYScale(MS2KN);
} else if (_units == Imperial) {
GraphView::setYUnits(tr("mi/h"));
setYScale(MS2MIH);
} else {
GraphView::setYUnits(tr("km/h"));
setYScale(MS2KMH);
}
}
void SpeedGraph::setUnits(Units units)
{
_units = units;
setYUnits();
setInfo();
GraphView::setUnits(units);
}
void SpeedGraph::setTimeType(enum TimeType type)
{
_timeType = type;
for (int i = 0; i < _tracks.size(); i++)
_tracks.at(i)->setTimeType(type);
setInfo();
redraw();
}
void SpeedGraph::showTracks(bool show)
{
_showTracks = show;
for (int i = 0; i < _tracks.size(); i++) {
if (show)
addGraph(_tracks.at(i));
else
removeGraph(_tracks.at(i));
}
setInfo();
redraw();
}