Cosmetics
[GPXSee.git] / src / speedgraph.cpp
blob6b3381b73e4303b87bfc9b2a81800a1587f031b4
1 #include "config.h"
2 #include "data.h"
3 #include "speedgraph.h"
6 SpeedGraph::SpeedGraph(QWidget *parent) : GraphTab(parent)
8 _units = Metric;
9 _timeType = Total;
10 _showTracks = true;
12 setYUnits();
13 setYLabel(tr("Speed"));
15 setSliderPrecision(1);
18 void SpeedGraph::setInfo()
20 if (_showTracks) {
21 GraphView::addInfo(tr("Average"), QString::number(avg() * yScale(), 'f',
22 1) + UNIT_SPACE + yUnits());
23 GraphView::addInfo(tr("Maximum"), QString::number(max() * yScale(), 'f',
24 1) + UNIT_SPACE + yUnits());
25 } else
26 clearInfo();
29 void SpeedGraph::loadData(const Data &data, const QList<PathItem *> &paths)
31 for (int i = 0; i < data.tracks().count(); i++) {
32 const Graph &graph = data.tracks().at(i)->speed();
33 if (graph.size() < 2) {
34 skipColor();
35 continue;
38 _avg.append(QPointF(data.tracks().at(i)->distance(),
39 data.tracks().at(i)->distance() / data.tracks().at(i)->time()));
40 _avgM.append(QPointF(data.tracks().at(i)->distance(),
41 data.tracks().at(i)->distance() / data.tracks().at(i)->movingTime()));
43 GraphView::loadGraph(graph, paths.at(i));
46 for (int i = 0; i < data.routes().count(); i++)
47 skipColor();
49 setInfo();
51 redraw();
54 qreal SpeedGraph::avg() const
56 qreal sum = 0, w = 0;
57 QList<QPointF>::const_iterator it;
58 const QList<QPointF> &list = (_timeType == Moving) ? _avgM : _avg;
60 for (it = list.begin(); it != list.end(); it++) {
61 sum += it->y() * it->x();
62 w += it->x();
65 return (sum / w);
68 void SpeedGraph::clear()
70 _avg.clear();
71 _avgM.clear();
73 GraphView::clear();
76 void SpeedGraph::setYUnits()
78 if (_units == Metric) {
79 GraphView::setYUnits(tr("km/h"));
80 setYScale(MS2KMH);
81 } else {
82 GraphView::setYUnits(tr("mi/h"));
83 setYScale(MS2MIH);
87 void SpeedGraph::setUnits(enum Units units)
89 _units = units;
91 setYUnits();
92 setInfo();
93 GraphView::setUnits(units);
95 redraw();
98 void SpeedGraph::setTimeType(enum TimeType type)
100 _timeType = type;
102 setInfo();
103 redraw();
106 void SpeedGraph::showTracks(bool show)
108 _showTracks = show;
110 showGraph(show);
111 setInfo();
113 redraw();