4 #include "elevationgraph.h"
7 static qreal
nMin(qreal a
, qreal b
)
9 if (!std::isnan(a
) && !std::isnan(b
))
11 else if (!std::isnan(a
))
13 else if (!std::isnan(b
))
19 static qreal
nMax(qreal a
, qreal b
)
21 if (!std::isnan(a
) && !std::isnan(b
))
23 else if (!std::isnan(a
))
25 else if (!std::isnan(b
))
31 ElevationGraph::ElevationGraph(QWidget
*parent
) : GraphTab(parent
)
48 setYLabel(tr("Elevation"));
53 void ElevationGraph::setInfo()
55 if (std::isnan(max()) || std::isnan(min()))
58 GraphView::addInfo(tr("Ascent"), QString::number(ascent() * yScale(),
59 'f', 0) + UNIT_SPACE
+ yUnits());
60 GraphView::addInfo(tr("Descent"), QString::number(descent() * yScale(),
61 'f', 0) + UNIT_SPACE
+ yUnits());
62 GraphView::addInfo(tr("Maximum"), QString::number(max() * yScale(), 'f',
63 0) + UNIT_SPACE
+ yUnits());
64 GraphView::addInfo(tr("Minimum"), QString::number(min() * yScale(), 'f',
65 0) + UNIT_SPACE
+ yUnits());
69 void ElevationGraph::loadGraph(const Graph
&graph
, Type type
, PathItem
*path
)
71 qreal ascent
= 0, descent
= 0;
74 if (graph
.size() < 2) {
79 max
= min
= graph
.at(0).y();
80 for (int j
= 1; j
< graph
.size(); j
++) {
81 qreal cur
= graph
.at(j
).y();
82 qreal prev
= graph
.at(j
-1).y();
87 descent
+= prev
- cur
;
96 _trackAscent
+= ascent
;
97 _trackDescent
+= descent
;
98 _trackMax
= nMax(_trackMax
, max
);
99 _trackMin
= nMin(_trackMin
, min
);
101 _routeAscent
+= ascent
;
102 _routeDescent
+= descent
;
103 _routeMax
= nMax(_routeMax
, max
);
104 _routeMin
= nMin(_routeMin
, min
);
107 GraphView::loadGraph(graph
, path
, type
);
110 void ElevationGraph::loadData(const Data
&data
, const QList
<PathItem
*> &paths
)
114 for (int i
= 0; i
< data
.tracks().count(); i
++)
115 loadGraph(data
.tracks().at(i
)->elevation(), Track
, paths
.at(p
++));
116 for (int i
= 0; i
< data
.routes().count(); i
++)
117 loadGraph(data
.routes().at(i
)->elevation(), Route
, paths
.at(p
++));
124 void ElevationGraph::clear()
138 void ElevationGraph::setYUnits()
140 if (_units
== Metric
) {
141 GraphView::setYUnits(tr("m"));
144 GraphView::setYUnits(tr("ft"));
149 void ElevationGraph::setUnits(enum Units units
)
155 GraphView::setUnits(units
);
160 void ElevationGraph::showTracks(bool show
)
165 showGraph(show
, Track
);
170 void ElevationGraph::showRoutes(bool show
)
174 showGraph(show
, Route
);
180 qreal
ElevationGraph::ascent() const
192 qreal
ElevationGraph::descent() const
197 val
+= _routeDescent
;
199 val
+= _trackDescent
;
204 qreal
ElevationGraph::max() const
208 if (_showRoutes
&& _showTracks
)
209 val
= nMax(_routeMax
, _trackMax
);
210 else if (_showTracks
)
212 else if (_showRoutes
)
220 qreal
ElevationGraph::min() const
224 if (_showRoutes
&& _showTracks
)
225 val
= nMin(_routeMin
, _trackMin
);
226 else if (_showTracks
)
228 else if (_showRoutes
)