5 #include "elevationgraphitem.h"
6 #include "elevationgraph.h"
9 static qreal
nMin(qreal a
, qreal b
)
12 return std::isnan(b
) ? NAN
: b
;
14 return std::isnan(b
) ? a
: qMin(a
, b
);
17 static qreal
nMax(qreal a
, qreal b
)
20 return std::isnan(b
) ? NAN
: b
;
22 return std::isnan(b
) ? a
: qMax(a
, b
);
25 ElevationGraph::ElevationGraph(QWidget
*parent
) : GraphTab(parent
)
40 setYLabel(tr("Elevation"));
44 ElevationGraph::~ElevationGraph()
50 void ElevationGraph::setInfo()
52 if (std::isnan(max()) || std::isnan(min()))
55 QLocale
l(QLocale::system());
57 GraphView::addInfo(tr("Ascent"), l
.toString(ascent() * yScale(),
58 'f', 0) + UNIT_SPACE
+ yUnits());
59 GraphView::addInfo(tr("Descent"), l
.toString(descent() * yScale(),
60 'f', 0) + UNIT_SPACE
+ yUnits());
61 GraphView::addInfo(tr("Maximum"), l
.toString(max() * yScale(), 'f',
62 0) + UNIT_SPACE
+ yUnits());
63 GraphView::addInfo(tr("Minimum"), l
.toString(min() * yScale(), 'f',
64 0) + UNIT_SPACE
+ yUnits());
68 GraphItem
*ElevationGraph::loadGraph(const Graph
&graph
, Type type
)
70 if (!graph
.isValid()) {
75 ElevationGraphItem
*gi
= new ElevationGraphItem(graph
, _graphType
, _width
,
76 _palette
.nextColor());
84 _trackAscent
+= gi
->ascent();
85 _trackDescent
+= gi
->descent();
86 _trackMax
= nMax(_trackMax
, gi
->max());
87 _trackMin
= nMin(_trackMin
, gi
->min());
93 _routeAscent
+= gi
->ascent();
94 _routeDescent
+= gi
->descent();
95 _routeMax
= nMax(_routeMax
, gi
->max());
96 _routeMin
= nMin(_routeMin
, gi
->min());
102 QList
<GraphItem
*> ElevationGraph::loadData(const Data
&data
)
104 QList
<GraphItem
*> graphs
;
106 for (int i
= 0; i
< data
.tracks().count(); i
++)
107 graphs
.append(loadGraph(data
.tracks().at(i
).elevation(), Track
));
108 for (int i
= 0; i
< data
.routes().count(); i
++)
109 graphs
.append(loadGraph(data
.routes().at(i
).elevation(), Route
));
110 for (int i
= 0; i
< data
.areas().count(); i
++)
111 _palette
.nextColor();
119 void ElevationGraph::clear()
138 void ElevationGraph::setYUnits(Units units
)
140 if (units
== Metric
) {
141 GraphView::setYUnits(tr("m"));
144 GraphView::setYUnits(tr("ft"));
149 void ElevationGraph::setUnits(Units units
)
154 GraphView::setUnits(units
);
157 void ElevationGraph::showItems(const QList
<ElevationGraphItem
*> &list
,
160 for (int i
= 0; i
< list
.size(); i
++) {
162 addGraph(list
.at(i
));
164 removeGraph(list
.at(i
));
168 void ElevationGraph::showTracks(bool show
)
172 showItems(_tracks
, show
);
178 void ElevationGraph::showRoutes(bool show
)
182 showItems(_routes
, show
);
188 qreal
ElevationGraph::ascent() const
200 qreal
ElevationGraph::descent() const
205 val
+= _routeDescent
;
207 val
+= _trackDescent
;
212 qreal
ElevationGraph::max() const
216 if (_showRoutes
&& _showTracks
)
217 val
= nMax(_routeMax
, _trackMax
);
218 else if (_showTracks
)
220 else if (_showRoutes
)
228 qreal
ElevationGraph::min() const
232 if (_showRoutes
&& _showTracks
)
233 val
= nMin(_routeMin
, _trackMin
);
234 else if (_showTracks
)
236 else if (_showRoutes
)