Graph loading performance improvement
[GPXSee.git] / src / GUI / elevationgraph.cpp
blob53562716d770f7b7a06e494d756d9c7391ab7638
1 #include <cmath>
2 #include <QLocale>
3 #include "data/data.h"
4 #include "tooltip.h"
5 #include "elevationgraphitem.h"
6 #include "elevationgraph.h"
9 static qreal nMin(qreal a, qreal b)
11 if (std::isnan(a))
12 return std::isnan(b) ? NAN : b;
13 else
14 return std::isnan(b) ? a : qMin(a, b);
17 static qreal nMax(qreal a, qreal b)
19 if (std::isnan(a))
20 return std::isnan(b) ? NAN : b;
21 else
22 return std::isnan(b) ? a : qMax(a, b);
25 ElevationGraph::ElevationGraph(QWidget *parent) : GraphTab(parent)
27 _trackAscent = 0;
28 _routeAscent = 0;
29 _trackDescent = 0;
30 _routeDescent = 0;
31 _trackMin = NAN;
32 _trackMax = NAN;
33 _routeMin = NAN;
34 _routeMax = NAN;
36 _showRoutes = true;
37 _showTracks = true;
39 setYUnits(Metric);
40 setYLabel(tr("Elevation"));
41 setMinYRange(50.0);
44 ElevationGraph::~ElevationGraph()
46 qDeleteAll(_tracks);
47 qDeleteAll(_routes);
50 void ElevationGraph::setInfo()
52 if (std::isnan(max()) || std::isnan(min()))
53 clearInfo();
54 else {
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()) {
71 _palette.nextColor();
72 return 0;
75 ElevationGraphItem *gi = new ElevationGraphItem(graph, _graphType, _width,
76 _palette.nextColor());
77 gi->setUnits(_units);
79 if (type == Track) {
80 _tracks.append(gi);
81 if (_showTracks)
82 addGraph(gi);
84 _trackAscent += gi->ascent();
85 _trackDescent += gi->descent();
86 _trackMax = nMax(_trackMax, gi->max());
87 _trackMin = nMin(_trackMin, gi->min());
88 } else {
89 _routes.append(gi);
90 if (_showRoutes)
91 addGraph(gi);
93 _routeAscent += gi->ascent();
94 _routeDescent += gi->descent();
95 _routeMax = nMax(_routeMax, gi->max());
96 _routeMin = nMin(_routeMin, gi->min());
99 return gi;
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();
113 setInfo();
114 redraw();
116 return graphs;
119 void ElevationGraph::clear()
121 qDeleteAll(_tracks);
122 _tracks.clear();
123 qDeleteAll(_routes);
124 _routes.clear();
126 _trackAscent = 0;
127 _routeAscent = 0;
128 _trackDescent = 0;
129 _routeDescent = 0;
130 _trackMin = NAN;
131 _trackMax = NAN;
132 _routeMin = NAN;
133 _routeMax = NAN;
135 GraphTab::clear();
138 void ElevationGraph::setYUnits(Units units)
140 if (units == Metric) {
141 GraphView::setYUnits(tr("m"));
142 setYScale(1);
143 } else {
144 GraphView::setYUnits(tr("ft"));
145 setYScale(M2FT);
149 void ElevationGraph::setUnits(Units units)
151 setYUnits(units);
152 setInfo();
154 GraphView::setUnits(units);
157 void ElevationGraph::showItems(const QList<ElevationGraphItem *> &list,
158 bool show)
160 for (int i = 0; i < list.size(); i++) {
161 if (show)
162 addGraph(list.at(i));
163 else
164 removeGraph(list.at(i));
168 void ElevationGraph::showTracks(bool show)
170 _showTracks = show;
172 showItems(_tracks, show);
173 setInfo();
175 redraw();
178 void ElevationGraph::showRoutes(bool show)
180 _showRoutes = show;
182 showItems(_routes, show);
183 setInfo();
185 redraw();
188 qreal ElevationGraph::ascent() const
190 qreal val = 0;
192 if (_showRoutes)
193 val += _routeAscent;
194 if (_showTracks)
195 val += _trackAscent;
197 return val;
200 qreal ElevationGraph::descent() const
202 qreal val = 0;
204 if (_showRoutes)
205 val += _routeDescent;
206 if (_showTracks)
207 val += _trackDescent;
209 return val;
212 qreal ElevationGraph::max() const
214 qreal val;
216 if (_showRoutes && _showTracks)
217 val = nMax(_routeMax, _trackMax);
218 else if (_showTracks)
219 val = _trackMax;
220 else if (_showRoutes)
221 val = _routeMax;
222 else
223 val = NAN;
225 return val;
228 qreal ElevationGraph::min() const
230 qreal val;
232 if (_showRoutes && _showTracks)
233 val = nMin(_routeMin, _trackMin);
234 else if (_showTracks)
235 val = _trackMin;
236 else if (_showRoutes)
237 val = _routeMin;
238 else
239 val = NAN;
241 return val;