1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-12-01 07:01:16 +01:00
GPXSee/src/GUI/heartrategraph.cpp

111 lines
1.9 KiB
C++
Raw Normal View History

#include <QLocale>
2017-11-26 18:54:03 +01:00
#include "data/data.h"
#include "heartrategraphitem.h"
#include "heartrategraph.h"
2016-06-24 00:55:44 +02:00
HeartRateGraph::HeartRateGraph(QWidget *parent) : GraphTab(parent)
{
2016-08-19 19:48:44 +02:00
_showTracks = true;
GraphView::setYUnits(tr("bpm"));
setYLabel(tr("Heart rate"));
2016-03-27 13:23:00 +02:00
setSliderPrecision(0);
}
HeartRateGraph::~HeartRateGraph()
{
qDeleteAll(_tracks);
}
2016-08-09 01:16:19 +02:00
void HeartRateGraph::setInfo()
{
2016-08-19 19:48:44 +02:00
if (_showTracks) {
QLocale l(QLocale::system());
GraphView::addInfo(tr("Average"), l.toString(avg() * yScale(), 'f',
2016-08-19 19:48:44 +02:00
0) + UNIT_SPACE + yUnits());
GraphView::addInfo(tr("Maximum"), l.toString(max() * yScale(), 'f',
2016-08-19 19:48:44 +02:00
0) + UNIT_SPACE + yUnits());
} else
clearInfo();
}
QList<GraphItem*> HeartRateGraph::loadData(const Data &data)
{
QList<GraphItem*> graphs;
2016-10-23 11:09:20 +02:00
for (int i = 0; i < data.tracks().count(); i++) {
2019-01-31 01:46:53 +01:00
const Track &track = data.tracks().at(i);
const Graph &graph = track.heartRate();
2019-01-31 01:46:53 +01:00
if (!graph.isValid()) {
_palette.nextColor();
graphs.append(0);
} else {
HeartRateGraphItem *gi = new HeartRateGraphItem(graph, _graphType,
_width, _palette.nextColor());
_tracks.append(gi);
if (_showTracks)
addGraph(gi);
2019-01-31 01:46:53 +01:00
_avg.append(QPointF(track.distance(), gi->avg()));
graphs.append(gi);
}
}
2016-03-27 13:23:00 +02:00
for (int i = 0; i < data.routes().count(); i++) {
_palette.nextColor();
graphs.append(0);
}
2016-08-09 01:16:19 +02:00
2019-01-31 01:46:53 +01:00
for (int i = 0; i < data.areas().count(); i++)
_palette.nextColor();
2019-01-31 01:46:53 +01:00
2016-08-09 01:16:19 +02:00
setInfo();
redraw();
return graphs;
}
qreal HeartRateGraph::avg() const
{
qreal sum = 0, w = 0;
2019-03-15 19:43:17 +01:00
for (int i = 0; i < _avg.size(); i++) {
const QPointF &p = _avg.at(i);
sum += p.y() * p.x();
w += p.x();
}
return (sum / w);
}
void HeartRateGraph::clear()
{
qDeleteAll(_tracks);
_tracks.clear();
_avg.clear();
2019-03-05 22:34:50 +01:00
GraphTab::clear();
}
void HeartRateGraph::showTracks(bool show)
{
2016-08-19 19:48:44 +02:00
_showTracks = show;
for (int i = 0; i < _tracks.size(); i++) {
if (show)
addGraph(_tracks.at(i));
else
removeGraph(_tracks.at(i));
}
2016-08-20 11:28:08 +02:00
setInfo();
redraw();
}