1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-30 22:51:16 +01:00
GPXSee/src/GUI/powergraph.cpp

117 lines
2.2 KiB
C++
Raw Normal View History

#include <QLocale>
2017-11-26 18:54:03 +01:00
#include "data/data.h"
#include "powergraphitem.h"
2016-11-06 03:28:08 +01:00
#include "powergraph.h"
PowerGraph::PowerGraph(QWidget *parent) : GraphTab(parent)
{
2023-02-04 23:59:47 +01:00
_showTracks = false;
2016-11-06 03:28:08 +01:00
GraphView::setYUnits(tr("W"));
setYLabel(tr("Power"));
setSliderPrecision(1);
}
PowerGraph::~PowerGraph()
{
qDeleteAll(_tracks);
}
2016-11-06 03:28:08 +01:00
void PowerGraph::setInfo()
{
if (_showTracks) {
QLocale l(QLocale::system());
2022-05-28 14:05:14 +02:00
#ifdef Q_OS_ANDROID
GraphView::addInfo(tr("Avg"), l.toString(avg() * yScale() + yOffset(),
'f', 1) + UNIT_SPACE + yUnits());
GraphView::addInfo(tr("Max"), l.toString(max() * yScale() + yOffset(),
'f', 1) + UNIT_SPACE + yUnits());
#else // Q_OS_ANDROID
GraphView::addInfo(tr("Average"), l.toString(avg() * yScale()
2016-11-06 03:28:08 +01:00
+ yOffset(), 'f', 1) + UNIT_SPACE + yUnits());
GraphView::addInfo(tr("Maximum"), l.toString(max() * yScale()
2022-05-28 14:05:14 +02:00
+ yOffset(), 'f', 1) + UNIT_SPACE + yUnits());
#endif // Q_OS_ANDROID
2016-11-06 03:28:08 +01:00
} else
clearInfo();
}
QList<GraphItem*> PowerGraph::loadData(const Data &data)
2016-11-06 03:28:08 +01:00
{
QList<GraphItem*> graphs;
2016-11-06 03:28:08 +01: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.power();
2016-11-06 03:28:08 +01:00
if (graph.isEmpty()) {
_palette.nextColor();
graphs.append(0);
} else {
PowerGraphItem *gi = new PowerGraphItem(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-11-06 03:28:08 +01:00
}
}
for (int i = 0; i < data.routes().count(); i++) {
_palette.nextColor();
graphs.append(0);
}
2016-11-06 03:28:08 +01: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-11-06 03:28:08 +01:00
setInfo();
redraw();
return graphs;
2016-11-06 03:28:08 +01:00
}
qreal PowerGraph::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();
2016-11-06 03:28:08 +01:00
}
return (sum / w);
}
void PowerGraph::clear()
{
qDeleteAll(_tracks);
_tracks.clear();
2016-11-06 03:28:08 +01:00
_avg.clear();
2019-03-05 22:34:50 +01:00
GraphTab::clear();
2016-11-06 03:28:08 +01:00
}
void PowerGraph::showTracks(bool show)
{
_showTracks = show;
for (int i = 0; i < _tracks.size(); i++) {
if (show)
addGraph(_tracks.at(i));
else
removeGraph(_tracks.at(i));
}
2016-11-06 03:28:08 +01:00
setInfo();
redraw();
}