1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-28 05:34:47 +01:00

Code cleanup

This commit is contained in:
Martin Tůma 2016-03-27 13:23:00 +02:00
parent b7f3a64577
commit c9b5e0f2cb
13 changed files with 155 additions and 138 deletions

View File

@ -34,7 +34,8 @@ HEADERS += src/config.h \
src/trackpoint.h \ src/trackpoint.h \
src/waypointitem.h \ src/waypointitem.h \
src/palette.h \ src/palette.h \
src/heartrategraph.h src/heartrategraph.h \
src/range.h
SOURCES += src/main.cpp \ SOURCES += src/main.cpp \
src/gui.cpp \ src/gui.cpp \
src/gpx.cpp \ src/gpx.cpp \
@ -59,7 +60,8 @@ SOURCES += src/main.cpp \
src/graphview.cpp \ src/graphview.cpp \
src/waypointitem.cpp \ src/waypointitem.cpp \
src/palette.cpp \ src/palette.cpp \
src/heartrategraph.cpp src/heartrategraph.cpp \
src/range.cpp
RESOURCES += gpxsee.qrc RESOURCES += gpxsee.qrc
TRANSLATIONS = lang/gpxsee_cs.ts TRANSLATIONS = lang/gpxsee_cs.ts
macx:ICON = icons/gpxsee.icns macx:ICON = icons/gpxsee.icns

View File

@ -36,7 +36,7 @@ AxisItem::AxisItem(Type type, QGraphicsItem *parent) : QGraphicsItem(parent)
_size = 0; _size = 0;
} }
void AxisItem::setRange(const QPointF &range) void AxisItem::setRange(const RangeF &range)
{ {
_range = range; _range = range;
updateBoundingRect(); updateBoundingRect();
@ -67,7 +67,7 @@ void AxisItem::updateBoundingRect()
struct Label l; struct Label l;
l = label(_range.x(), _range.y(), (_type == X) ? XTICKS : YTICKS); l = label(_range.min(), _range.max(), (_type == X) ? XTICKS : YTICKS);
es = fm.tightBoundingRect(QString::number(l.max)); es = fm.tightBoundingRect(QString::number(l.max));
ss = fm.tightBoundingRect(QString::number(l.min)); ss = fm.tightBoundingRect(QString::number(l.min));
ls = fm.tightBoundingRect(_label); ls = fm.tightBoundingRect(_label);
@ -106,7 +106,7 @@ void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QFontMetrics fm(font); QFontMetrics fm(font);
QRect ts, ls; QRect ts, ls;
struct Label l; struct Label l;
qreal range = _range.y() - _range.x(); qreal range = _range.size();
qreal val; qreal val;
@ -117,15 +117,15 @@ void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
if (_type == X) { if (_type == X) {
painter->drawLine(0, 0, _size, 0); painter->drawLine(0, 0, _size, 0);
l = label(_range.x(), _range.y(), XTICKS); l = label(_range.min(), _range.max(), XTICKS);
for (int i = 0; i < ((l.max - l.min) / l.d) + 1; i++) { for (int i = 0; i < ((l.max - l.min) / l.d) + 1; i++) {
val = l.min + i * l.d; val = l.min + i * l.d;
QString str = QString::number(val); QString str = QString::number(val);
painter->drawLine((_size/range) * (val - _range.x()), TICK/2, painter->drawLine((_size/range) * (val - _range.min()), TICK/2,
(_size/range) * (val - _range.x()), -TICK/2); (_size/range) * (val - _range.min()), -TICK/2);
ts = fm.tightBoundingRect(str); ts = fm.tightBoundingRect(str);
painter->drawText(((_size/range) * (val - _range.x())) painter->drawText(((_size/range) * (val - _range.min()))
- (ts.width()/2), ts.height() + TICK/2 + PADDING, str); - (ts.width()/2), ts.height() + TICK/2 + PADDING, str);
} }
@ -134,18 +134,18 @@ void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
} else { } else {
painter->drawLine(0, 0, 0, -_size); painter->drawLine(0, 0, 0, -_size);
l = label(_range.x(), _range.y(), YTICKS); l = label(_range.min(), _range.max(), YTICKS);
int mtw = 0; int mtw = 0;
for (int i = 0; i < ((l.max - l.min) / l.d) + 1; i++) { for (int i = 0; i < ((l.max - l.min) / l.d) + 1; i++) {
val = l.min + i * l.d; val = l.min + i * l.d;
QString str = QString::number(val); QString str = QString::number(val);
painter->drawLine(TICK/2, -((_size/range) * (val - _range.x())), painter->drawLine(TICK/2, -((_size/range) * (val - _range.min())),
-TICK/2, -((_size/range) * (val - _range.x()))); -TICK/2, -((_size/range) * (val - _range.min())));
ts = fm.tightBoundingRect(str); ts = fm.tightBoundingRect(str);
mtw = qMax(ts.width(), mtw); mtw = qMax(ts.width(), mtw);
painter->drawText(-(ts.width() + PADDING + TICK/2), -((_size/range) painter->drawText(-(ts.width() + PADDING + TICK/2), -((_size/range)
* (val - _range.x())) + (ts.height()/2), str); * (val - _range.min())) + (ts.height()/2), str);
} }
painter->rotate(-90); painter->rotate(-90);
@ -169,7 +169,7 @@ QSizeF AxisItem::margin() const
struct Label l; struct Label l;
l = label(_range.x(), _range.y(), (_type == X) ? XTICKS : YTICKS); l = label(_range.min(), _range.max(), (_type == X) ? XTICKS : YTICKS);
es = fm.tightBoundingRect(QString::number(l.max)); es = fm.tightBoundingRect(QString::number(l.max));
ss = fm.tightBoundingRect(QString::number(l.min)); ss = fm.tightBoundingRect(QString::number(l.min));
ls = fm.tightBoundingRect(_label); ls = fm.tightBoundingRect(_label);

View File

@ -2,6 +2,7 @@
#define AXISITEM_H #define AXISITEM_H
#include <QGraphicsItem> #include <QGraphicsItem>
#include "range.h"
class AxisItem : public QGraphicsItem class AxisItem : public QGraphicsItem
{ {
@ -14,7 +15,7 @@ public:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget); QWidget *widget);
void setRange(const QPointF &range); void setRange(const RangeF &range);
void setSize(qreal size); void setSize(qreal size);
void setLabel(const QString& label); void setLabel(const QString& label);
@ -24,7 +25,7 @@ private:
void updateBoundingRect(); void updateBoundingRect();
Type _type; Type _type;
QPointF _range; RangeF _range;
qreal _size; qreal _size;
QString _label; QString _label;
QRectF _boundingRect; QRectF _boundingRect;

View File

@ -1,4 +1,3 @@
#include <float.h>
#include "config.h" #include "config.h"
#include "gpx.h" #include "gpx.h"
#include "elevationgraph.h" #include "elevationgraph.h"
@ -8,34 +7,34 @@ ElevationGraph::ElevationGraph(QWidget *parent) : GraphView(parent)
{ {
_ascent = 0; _ascent = 0;
_descent = 0; _descent = 0;
_max = -FLT_MAX;
_min = FLT_MAX;
setXLabel(tr("Distance")); setXLabel(tr("Distance"));
setYLabel(tr("Elevation")); setYLabel(tr("Elevation"));
setXUnits(tr("km")); setXUnits(tr("km"));
setYUnits(tr("m")); setYUnits(tr("m"));
setXScale(M2KM); setXScale(M2KM);
setMinRange(50.0); setMinYRange(50.0);
} }
void ElevationGraph::addInfo() void ElevationGraph::addInfo()
{ {
GraphView::addInfo(tr("Ascent"), QString::number(_ascent * _yScale, 'f', 0) GraphView::addInfo(tr("Ascent"), QString::number(_ascent * yScale(), 'f', 0)
+ UNIT_SPACE + _yUnits); + UNIT_SPACE + yUnits());
GraphView::addInfo(tr("Descent"), QString::number(_descent * _yScale, 'f', 0) GraphView::addInfo(tr("Descent"), QString::number(_descent * yScale(), 'f',
+ UNIT_SPACE + _yUnits); 0) + UNIT_SPACE + yUnits());
GraphView::addInfo(tr("Maximum"), QString::number(_max * _yScale, 'f', 0) GraphView::addInfo(tr("Maximum"), QString::number(max() * yScale(), 'f', 0)
+ UNIT_SPACE + _yUnits); + UNIT_SPACE + yUnits());
GraphView::addInfo(tr("Minimum"), QString::number(_min * _yScale, 'f', 0) GraphView::addInfo(tr("Minimum"), QString::number(min() * yScale(), 'f', 0)
+ UNIT_SPACE + _yUnits); + UNIT_SPACE + yUnits());
redraw();
} }
void ElevationGraph::loadGPX(const GPX &gpx) void ElevationGraph::loadGPX(const GPX &gpx)
{ {
for (int i = 0; i < gpx.trackCount(); i++) { for (int i = 0; i < gpx.trackCount(); i++) {
QVector<QPointF> data; QVector<QPointF> data;
qreal min, max, ascent = 0, descent = 0; qreal ascent = 0, descent = 0;
gpx.track(i).elevationGraph(data); gpx.track(i).elevationGraph(data);
if (data.count() < 2) { if (data.count() < 2) {
@ -43,8 +42,6 @@ void ElevationGraph::loadGPX(const GPX &gpx)
continue; continue;
} }
min = max = data.at(0).y();
for (int j = 1; j < data.size(); j++) { for (int j = 1; j < data.size(); j++) {
qreal cur = data.at(j).y(); qreal cur = data.at(j).y();
qreal prev = data.at(j-1).y(); qreal prev = data.at(j-1).y();
@ -53,29 +50,21 @@ void ElevationGraph::loadGPX(const GPX &gpx)
ascent += cur - prev; ascent += cur - prev;
if (cur < prev) if (cur < prev)
descent += prev - cur; descent += prev - cur;
if (cur > max)
max = cur;
if (cur < min)
min = cur;
} }
_ascent += ascent; _ascent += ascent;
_descent += descent; _descent += descent;
_max = qMax(_max, max);
_min = qMin(_min, min);
addInfo();
loadData(data); loadData(data);
} }
addInfo();
} }
void ElevationGraph::clear() void ElevationGraph::clear()
{ {
_ascent = 0; _ascent = 0;
_descent = 0; _descent = 0;
_max = -FLT_MAX;
_min = FLT_MAX;
GraphView::clear(); GraphView::clear();
} }
@ -96,6 +85,4 @@ void ElevationGraph::setUnits(enum Units units)
clearInfo(); clearInfo();
addInfo(); addInfo();
redraw();
} }

View File

@ -19,14 +19,13 @@ public:
qreal ascent() const {return _ascent;} qreal ascent() const {return _ascent;}
qreal descent() const {return _descent;} qreal descent() const {return _descent;}
qreal max() const {return _max;} qreal max() const {return bounds().bottom();}
qreal min() const {return _min;} qreal min() const {return bounds().top();}
private: private:
void addInfo(); void addInfo();
qreal _ascent, _descent; qreal _ascent, _descent;
qreal _max, _min;
}; };
#endif // ELEVATIONGRAPH_H #endif // ELEVATIONGRAPH_H

View File

@ -1,4 +1,3 @@
#include <float.h>
#include <QGraphicsView> #include <QGraphicsView>
#include <QGraphicsSceneMouseEvent> #include <QGraphicsSceneMouseEvent>
#include <QEvent> #include <QEvent>
@ -44,16 +43,11 @@ GraphView::GraphView(QWidget *parent)
_sliderInfo = new SliderInfoItem(_slider); _sliderInfo = new SliderInfoItem(_slider);
_sliderInfo->setZValue(2.0); _sliderInfo->setZValue(2.0);
_xMax = -FLT_MAX;
_xMin = FLT_MAX;
_yMax = -FLT_MAX;
_yMin = FLT_MAX;
_xScale = 1; _xScale = 1;
_yScale = 1; _yScale = 1;
_precision = 0; _precision = 0;
_minRange = 0.01; _minYRange = 0.01;
} }
GraphView::~GraphView() GraphView::~GraphView()
@ -74,14 +68,14 @@ GraphView::~GraphView()
void GraphView::updateBounds(const QPointF &point) void GraphView::updateBounds(const QPointF &point)
{ {
if (point.x() < _xMin) if (point.x() < _bounds.left())
_xMin = point.x(); _bounds.setLeft(point.x());
if (point.x() > _xMax) if (point.x() > _bounds.right())
_xMax = point.x(); _bounds.setRight(point.x());
if (point.y() < _yMin) if (point.y() > _bounds.bottom())
_yMin = point.y(); _bounds.setBottom(point.y());
if (point.y() > _yMax) if (point.y() < _bounds.top())
_yMax = point.y(); _bounds.setTop(point.y());
} }
void GraphView::createXLabel() void GraphView::createXLabel()
@ -137,6 +131,9 @@ void GraphView::loadData(const QVector<QPointF> &data)
if (data.size() < 2) if (data.size() < 2)
return; return;
if (!_graphs.size())
_bounds.moveTo(data.at(0));
updateBounds(data.at(0)); updateBounds(data.at(0));
path.moveTo(data.at(0).x(), -data.at(0).y()); path.moveTo(data.at(0).x(), -data.at(0).y());
for (int i = 1; i < data.size(); i++) { for (int i = 1; i < data.size(); i++) {
@ -154,8 +151,6 @@ void GraphView::loadData(const QVector<QPointF> &data)
if (_graphs.size() > 1) if (_graphs.size() > 1)
_sliderInfo->hide(); _sliderInfo->hide();
redraw();
} }
void GraphView::redraw() void GraphView::redraw()
@ -168,9 +163,9 @@ void GraphView::redraw(const QSizeF &size)
{ {
QRectF r; QRectF r;
QSizeF mx, my; QSizeF mx, my;
QPointF rx, ry; RangeF rx, ry;
QTransform transform; QTransform transform;
qreal xs, ys, diff; qreal xs, ys;
if (_xAxis->scene() == _scene) if (_xAxis->scene() == _scene)
@ -185,20 +180,20 @@ void GraphView::redraw(const QSizeF &size)
for (int i = 0; i < _graphs.size(); i++) for (int i = 0; i < _graphs.size(); i++)
_graphs.at(i)->resetTransform(); _graphs.at(i)->resetTransform();
rx = QPointF(_xMin * _xScale, _xMax * _xScale); rx = RangeF(_bounds.left() * _xScale, _bounds.right() * _xScale);
ry = QPointF(_yMin * _yScale, _yMax * _yScale); ry = RangeF(_bounds.top() * _yScale, _bounds.bottom() * _yScale);
if ((diff = ry.y() - ry.x()) < _minRange) if (ry.size() < _minYRange)
ry = QPointF(ry.x() - (_minRange/2 - diff/2), ry.resize(_minYRange);
ry.y() + (_minRange/2 - diff/2));
_xAxis->setRange(rx); _xAxis->setRange(rx);
_yAxis->setRange(ry); _yAxis->setRange(ry);
mx = _xAxis->margin(); mx = _xAxis->margin();
my = _yAxis->margin(); my = _yAxis->margin();
r = _scene->itemsBoundingRect(); r = _scene->itemsBoundingRect();
if (r.height() < _minRange) if (r.height() < _minYRange)
r.adjust(0, -(_minRange/2 - r.height()/2), 0, r.adjust(0, -(_minYRange/2 - r.height()/2), 0,
_minRange/2 - r.height()/2); _minYRange/2 - r.height()/2);
xs = (size.width() - (my.width() + mx.width())) / r.width(); xs = (size.width() - (my.width() + mx.width())) / r.width();
ys = (size.height() - (mx.height() + my.height()) ys = (size.height() - (mx.height() + my.height())
@ -209,9 +204,9 @@ void GraphView::redraw(const QSizeF &size)
_graphs.at(i)->setTransform(transform); _graphs.at(i)->setTransform(transform);
r = _scene->itemsBoundingRect(); r = _scene->itemsBoundingRect();
if (r.height() < _minRange * ys) if (r.height() < _minYRange * ys)
r.adjust(0, -(_minRange/2 * ys - r.height()/2), 0, r.adjust(0, -(_minYRange/2 * ys - r.height()/2), 0,
(_minRange/2) * ys - r.height()/2); (_minYRange/2) * ys - r.height()/2);
_xAxis->setSize(r.width()); _xAxis->setSize(r.width());
_yAxis->setSize(r.height()); _yAxis->setSize(r.height());
@ -281,10 +276,7 @@ void GraphView::clear()
_graphs.clear(); _graphs.clear();
_palette.reset(); _palette.reset();
_xMax = -FLT_MAX; _bounds = QRectF();
_xMin = FLT_MAX;
_yMax = -FLT_MAX;
_yMin = FLT_MAX;
_scene->setSceneRect(0, 0, 0, 0); _scene->setSceneRect(0, 0, 0, 0);
} }
@ -326,18 +318,18 @@ void GraphView::emitSliderPositionChanged(const QPointF &pos)
return; return;
qreal val = pos.x() / _slider->area().width(); qreal val = pos.x() / _slider->area().width();
emit sliderPositionChanged(val * (_xMax - _xMin)); emit sliderPositionChanged(val * _bounds.width());
if (!_sliderInfo->isVisible()) if (!_sliderInfo->isVisible())
return; return;
const QPainterPath &path = _graphs.at(0)->path(); const QPainterPath &path = _graphs.at(0)->path();
QRectF br = path.boundingRect(); QRectF br = path.boundingRect();
if (br.height() < _minRange) if (br.height() < _minYRange)
br.adjust(0, -(_minRange/2 - br.height()/2), 0, br.adjust(0, -(_minYRange/2 - br.height()/2), 0,
_minRange/2 - br.height()/2); _minYRange/2 - br.height()/2);
qreal y = yAtX(path, val * (_xMax - _xMin)); qreal y = yAtX(path, val * _bounds.width());
qreal r = (y - br.bottom()) / br.height(); qreal r = (y - br.bottom()) / br.height();
_sliderInfo->setPos(QPointF(0, _slider->boundingRect().height() * r)); _sliderInfo->setPos(QPointF(0, _slider->boundingRect().height() * r));
_sliderInfo->setText(QString::number(-y * _yScale, 'f', _precision)); _sliderInfo->setText(QString::number(-y * _yScale, 'f', _precision));
@ -348,7 +340,7 @@ qreal GraphView::sliderPosition() const
if (!_slider->isVisible()) if (!_slider->isVisible())
return -1; return -1;
else else
return (_slider->pos().x() / _slider->area().width()) * (_xMax - _xMin); return (_slider->pos().x() / _slider->area().width()) * _bounds.width();
} }
void GraphView::setSliderPosition(qreal pos) void GraphView::setSliderPosition(qreal pos)
@ -356,10 +348,10 @@ void GraphView::setSliderPosition(qreal pos)
if (_graphs.isEmpty()) if (_graphs.isEmpty())
return; return;
if (pos > (_xMax - _xMin)) if (pos > _bounds.right() || pos < _bounds.left())
_slider->setVisible(false); _slider->setVisible(false);
else { else {
_slider->setPos((pos / (_xMax - _xMin)) * _slider->area().width(), 0); _slider->setPos((pos / _bounds.width()) * _slider->area().width(), 0);
_slider->setVisible(true); _slider->setVisible(true);
} }
} }

View File

@ -35,39 +35,45 @@ public:
~GraphView(); ~GraphView();
void loadData(const QVector<QPointF> &data); void loadData(const QVector<QPointF> &data);
void redraw();
void clear();
int count() const {return _graphs.count();}
const QString &xLabel() const {return _xLabel;}
const QString &yLabel() const {return _yLabel;}
const QString &xUnits() const {return _xUnits;}
const QString &yUnits() const {return _yUnits;}
qreal xScale() const {return _xScale;}
qreal yScale() const {return _yScale;}
void setXLabel(const QString &label); void setXLabel(const QString &label);
void setYLabel(const QString &label); void setYLabel(const QString &label);
void setXUnits(const QString &units); void setXUnits(const QString &units);
void setYUnits(const QString &units); void setYUnits(const QString &units);
void setXScale(qreal scale); void setXScale(qreal scale);
void setYScale(qreal scale); void setYScale(qreal scale);
void setPrecision(int precision) {_precision = precision;}
void setMinRange(qreal range) {_minRange = range;}
void plot(QPainter *painter, const QRectF &target); void setSliderPrecision(int precision) {_precision = precision;}
void clear(); void setMinYRange(qreal range) {_minYRange = range;}
qreal sliderPosition() const; qreal sliderPosition() const;
void setSliderPosition(qreal pos); void setSliderPosition(qreal pos);
int count() const {return _graphs.count();} void plot(QPainter *painter, const QRectF &target);
signals: signals:
void sliderPositionChanged(qreal); void sliderPositionChanged(qreal);
protected: protected:
const QRectF &bounds() const {return _bounds;}
void resizeEvent(QResizeEvent *); void resizeEvent(QResizeEvent *);
void redraw(); void redraw(const QSizeF &size);
void addInfo(const QString &key, const QString &value); void addInfo(const QString &key, const QString &value);
void clearInfo(); void clearInfo();
void skipColor() {_palette.color();} void skipColor() {_palette.color();}
qreal _xScale, _yScale;
QString _xUnits, _yUnits;
QString _xLabel, _yLabel;
int _precision;
qreal _minRange;
private slots: private slots:
void emitSliderPositionChanged(const QPointF &pos); void emitSliderPositionChanged(const QPointF &pos);
void newSliderPosition(const QPointF &pos); void newSliderPosition(const QPointF &pos);
@ -76,7 +82,12 @@ private:
void createXLabel(); void createXLabel();
void createYLabel(); void createYLabel();
void updateBounds(const QPointF &point); void updateBounds(const QPointF &point);
void redraw(const QSizeF &size);
qreal _xScale, _yScale;
QString _xUnits, _yUnits;
QString _xLabel, _yLabel;
int _precision;
qreal _minYRange;
Scene *_scene; Scene *_scene;
@ -86,7 +97,7 @@ private:
InfoItem *_info; InfoItem *_info;
QList<QGraphicsPathItem*> _graphs; QList<QGraphicsPathItem*> _graphs;
qreal _xMin, _xMax, _yMin, _yMax; QRectF _bounds;
Palette _palette; Palette _palette;
}; };

View File

@ -4,29 +4,29 @@
HeartRateGraph::HeartRateGraph(QWidget *parent) : GraphView(parent) HeartRateGraph::HeartRateGraph(QWidget *parent) : GraphView(parent)
{ {
_max = 0;
setXLabel(tr("Distance")); setXLabel(tr("Distance"));
setYLabel(tr("Heart rate")); setYLabel(tr("Heart rate"));
setXUnits(tr("km")); setXUnits(tr("km"));
setYUnits(tr("1/min")); setYUnits(tr("1/min"));
setXScale(M2KM); setXScale(M2KM);
setPrecision(0); setSliderPrecision(0);
} }
void HeartRateGraph::addInfo() void HeartRateGraph::addInfo()
{ {
GraphView::addInfo(tr("Average"), QString::number(avg() * _yScale, 'f', 0) GraphView::addInfo(tr("Average"), QString::number(avg() * yScale(), 'f', 0)
+ UNIT_SPACE + _yUnits); + UNIT_SPACE + yUnits());
GraphView::addInfo(tr("Maximum"), QString::number(_max * _yScale, 'f', 0) GraphView::addInfo(tr("Maximum"), QString::number(max() * yScale(), 'f', 0)
+ UNIT_SPACE + _yUnits); + UNIT_SPACE + yUnits());
redraw();
} }
void HeartRateGraph::loadGPX(const GPX &gpx) void HeartRateGraph::loadGPX(const GPX &gpx)
{ {
for (int i = 0; i < gpx.trackCount(); i++) { for (int i = 0; i < gpx.trackCount(); i++) {
QVector<QPointF> data; QVector<QPointF> data;
qreal max = 0, sum = 0, w = 0; qreal sum = 0, w = 0;
gpx.track(i).heartRateGraph(data); gpx.track(i).heartRateGraph(data);
if (data.count() < 2) { if (data.count() < 2) {
@ -40,13 +40,10 @@ void HeartRateGraph::loadGPX(const GPX &gpx)
} }
_avg.append(QPointF(gpx.track(i).distance(), sum/w)); _avg.append(QPointF(gpx.track(i).distance(), sum/w));
for (int j = 0; j < data.size(); j++)
max = qMax(max, data.at(j).y());
_max = qMax(_max, max);
addInfo();
loadData(data); loadData(data);
} }
addInfo();
} }
qreal HeartRateGraph::avg() const qreal HeartRateGraph::avg() const
@ -64,7 +61,6 @@ qreal HeartRateGraph::avg() const
void HeartRateGraph::clear() void HeartRateGraph::clear()
{ {
_max = 0;
_avg.clear(); _avg.clear();
GraphView::clear(); GraphView::clear();
@ -82,6 +78,4 @@ void HeartRateGraph::setUnits(enum Units units)
clearInfo(); clearInfo();
addInfo(); addInfo();
redraw();
} }

View File

@ -18,12 +18,11 @@ public:
void setUnits(enum Units units); void setUnits(enum Units units);
qreal avg() const; qreal avg() const;
qreal max() const {return _max;} qreal max() const {return bounds().bottom();}
private: private:
void addInfo(); void addInfo();
qreal _max;
QList<QPointF> _avg; QList<QPointF> _avg;
}; };

15
src/range.cpp Normal file
View File

@ -0,0 +1,15 @@
#include "range.h"
void RangeF::resize(qreal size)
{
qreal adj = (size/2 - this->size()/2);
_min -= adj;
_max += adj;
}
QDebug operator<<(QDebug dbg, const RangeF &range)
{
dbg.nospace() << "RangeF(" << range.min() << ", " << range.max() << ")";
return dbg.maybeSpace();
}

25
src/range.h Normal file
View File

@ -0,0 +1,25 @@
#ifndef RANGE_H
#define RANGE_H
#include <QtGlobal>
#include <QDebug>
class RangeF
{
public:
RangeF() {_min = 0; _max = 0;}
RangeF(qreal min, qreal max) {_min = min, _max = max;}
qreal min() const {return _min;}
qreal max() const {return _max;}
qreal size() const {return (_max - _min);}
void resize(qreal size);
private:
qreal _min, _max;
};
QDebug operator<<(QDebug dbg, const RangeF &range);
#endif // RANGE_H

View File

@ -5,30 +5,29 @@
SpeedGraph::SpeedGraph(QWidget *parent) : GraphView(parent) SpeedGraph::SpeedGraph(QWidget *parent) : GraphView(parent)
{ {
_max = 0;
setXLabel(tr("Distance")); setXLabel(tr("Distance"));
setYLabel(tr("Speed")); setYLabel(tr("Speed"));
setXUnits(tr("km")); setXUnits(tr("km"));
setYUnits(tr("km/h")); setYUnits(tr("km/h"));
setXScale(M2KM); setXScale(M2KM);
setYScale(MS2KMH); setYScale(MS2KMH);
setPrecision(1); setSliderPrecision(1);
} }
void SpeedGraph::addInfo() void SpeedGraph::addInfo()
{ {
GraphView::addInfo(tr("Average"), QString::number(avg() * _yScale, 'f', 1) GraphView::addInfo(tr("Average"), QString::number(avg() * yScale(), 'f', 1)
+ UNIT_SPACE + _yUnits); + UNIT_SPACE + yUnits());
GraphView::addInfo(tr("Maximum"), QString::number(_max * _yScale, 'f', 1) GraphView::addInfo(tr("Maximum"), QString::number(max() * yScale(), 'f', 1)
+ UNIT_SPACE + _yUnits); + UNIT_SPACE + yUnits());
redraw();
} }
void SpeedGraph::loadGPX(const GPX &gpx) void SpeedGraph::loadGPX(const GPX &gpx)
{ {
for (int i = 0; i < gpx.trackCount(); i++) { for (int i = 0; i < gpx.trackCount(); i++) {
QVector<QPointF> data; QVector<QPointF> data;
qreal max = 0;
gpx.track(i).speedGraph(data); gpx.track(i).speedGraph(data);
if (data.count() < 2) { if (data.count() < 2) {
@ -39,13 +38,10 @@ void SpeedGraph::loadGPX(const GPX &gpx)
_avg.append(QPointF(gpx.track(i).distance(), gpx.track(i).distance() _avg.append(QPointF(gpx.track(i).distance(), gpx.track(i).distance()
/ gpx.track(i).time())); / gpx.track(i).time()));
for (int j = 0; j < data.size(); j++)
max = qMax(max, data.at(j).y());
_max = qMax(_max, max);
addInfo();
loadData(data); loadData(data);
} }
addInfo();
} }
qreal SpeedGraph::avg() const qreal SpeedGraph::avg() const
@ -63,7 +59,6 @@ qreal SpeedGraph::avg() const
void SpeedGraph::clear() void SpeedGraph::clear()
{ {
_max = 0;
_avg.clear(); _avg.clear();
GraphView::clear(); GraphView::clear();
@ -85,6 +80,4 @@ void SpeedGraph::setUnits(enum Units units)
clearInfo(); clearInfo();
addInfo(); addInfo();
redraw();
} }

View File

@ -19,12 +19,11 @@ public:
void setUnits(enum Units units); void setUnits(enum Units units);
qreal avg() const; qreal avg() const;
qreal max() const {return _max;} qreal max() const {return bounds().bottom();}
private: private:
void addInfo(); void addInfo();
qreal _max;
QList<QPointF> _avg; QList<QPointF> _avg;
}; };