From c9b5e0f2cb372b07c72982b0ed535b2f79d4ab5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20T=C3=B9ma?= Date: Sun, 27 Mar 2016 13:23:00 +0200 Subject: [PATCH] Code cleanup --- gpxsee.pro | 6 ++-- src/axisitem.cpp | 24 ++++++------- src/axisitem.h | 5 +-- src/elevationgraph.cpp | 41 ++++++++--------------- src/elevationgraph.h | 5 ++- src/graphview.cpp | 76 +++++++++++++++++++----------------------- src/graphview.h | 39 ++++++++++++++-------- src/heartrategraph.cpp | 26 ++++++--------- src/heartrategraph.h | 3 +- src/range.cpp | 15 +++++++++ src/range.h | 25 ++++++++++++++ src/speedgraph.cpp | 25 +++++--------- src/speedgraph.h | 3 +- 13 files changed, 155 insertions(+), 138 deletions(-) create mode 100644 src/range.cpp create mode 100644 src/range.h diff --git a/gpxsee.pro b/gpxsee.pro index df040db6..bde502dd 100644 --- a/gpxsee.pro +++ b/gpxsee.pro @@ -34,7 +34,8 @@ HEADERS += src/config.h \ src/trackpoint.h \ src/waypointitem.h \ src/palette.h \ - src/heartrategraph.h + src/heartrategraph.h \ + src/range.h SOURCES += src/main.cpp \ src/gui.cpp \ src/gpx.cpp \ @@ -59,7 +60,8 @@ SOURCES += src/main.cpp \ src/graphview.cpp \ src/waypointitem.cpp \ src/palette.cpp \ - src/heartrategraph.cpp + src/heartrategraph.cpp \ + src/range.cpp RESOURCES += gpxsee.qrc TRANSLATIONS = lang/gpxsee_cs.ts macx:ICON = icons/gpxsee.icns diff --git a/src/axisitem.cpp b/src/axisitem.cpp index 0e399fc5..a104f7f5 100644 --- a/src/axisitem.cpp +++ b/src/axisitem.cpp @@ -36,7 +36,7 @@ AxisItem::AxisItem(Type type, QGraphicsItem *parent) : QGraphicsItem(parent) _size = 0; } -void AxisItem::setRange(const QPointF &range) +void AxisItem::setRange(const RangeF &range) { _range = range; updateBoundingRect(); @@ -67,7 +67,7 @@ void AxisItem::updateBoundingRect() 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)); ss = fm.tightBoundingRect(QString::number(l.min)); ls = fm.tightBoundingRect(_label); @@ -106,7 +106,7 @@ void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QFontMetrics fm(font); QRect ts, ls; struct Label l; - qreal range = _range.y() - _range.x(); + qreal range = _range.size(); qreal val; @@ -117,15 +117,15 @@ void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, if (_type == X) { 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++) { val = l.min + i * l.d; QString str = QString::number(val); - painter->drawLine((_size/range) * (val - _range.x()), TICK/2, - (_size/range) * (val - _range.x()), -TICK/2); + painter->drawLine((_size/range) * (val - _range.min()), TICK/2, + (_size/range) * (val - _range.min()), -TICK/2); 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); } @@ -134,18 +134,18 @@ void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, } else { painter->drawLine(0, 0, 0, -_size); - l = label(_range.x(), _range.y(), YTICKS); + l = label(_range.min(), _range.max(), YTICKS); int mtw = 0; for (int i = 0; i < ((l.max - l.min) / l.d) + 1; i++) { val = l.min + i * l.d; QString str = QString::number(val); - painter->drawLine(TICK/2, -((_size/range) * (val - _range.x())), - -TICK/2, -((_size/range) * (val - _range.x()))); + painter->drawLine(TICK/2, -((_size/range) * (val - _range.min())), + -TICK/2, -((_size/range) * (val - _range.min()))); ts = fm.tightBoundingRect(str); mtw = qMax(ts.width(), mtw); 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); @@ -169,7 +169,7 @@ QSizeF AxisItem::margin() const 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)); ss = fm.tightBoundingRect(QString::number(l.min)); ls = fm.tightBoundingRect(_label); diff --git a/src/axisitem.h b/src/axisitem.h index 149de499..9b930d0c 100644 --- a/src/axisitem.h +++ b/src/axisitem.h @@ -2,6 +2,7 @@ #define AXISITEM_H #include +#include "range.h" class AxisItem : public QGraphicsItem { @@ -14,7 +15,7 @@ public: void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); - void setRange(const QPointF &range); + void setRange(const RangeF &range); void setSize(qreal size); void setLabel(const QString& label); @@ -24,7 +25,7 @@ private: void updateBoundingRect(); Type _type; - QPointF _range; + RangeF _range; qreal _size; QString _label; QRectF _boundingRect; diff --git a/src/elevationgraph.cpp b/src/elevationgraph.cpp index 69360a65..02f1f0d2 100644 --- a/src/elevationgraph.cpp +++ b/src/elevationgraph.cpp @@ -1,4 +1,3 @@ -#include #include "config.h" #include "gpx.h" #include "elevationgraph.h" @@ -8,34 +7,34 @@ ElevationGraph::ElevationGraph(QWidget *parent) : GraphView(parent) { _ascent = 0; _descent = 0; - _max = -FLT_MAX; - _min = FLT_MAX; setXLabel(tr("Distance")); setYLabel(tr("Elevation")); setXUnits(tr("km")); setYUnits(tr("m")); setXScale(M2KM); - setMinRange(50.0); + setMinYRange(50.0); } void ElevationGraph::addInfo() { - GraphView::addInfo(tr("Ascent"), QString::number(_ascent * _yScale, 'f', 0) - + UNIT_SPACE + _yUnits); - GraphView::addInfo(tr("Descent"), QString::number(_descent * _yScale, 'f', 0) - + UNIT_SPACE + _yUnits); - GraphView::addInfo(tr("Maximum"), QString::number(_max * _yScale, 'f', 0) - + UNIT_SPACE + _yUnits); - GraphView::addInfo(tr("Minimum"), QString::number(_min * _yScale, 'f', 0) - + UNIT_SPACE + _yUnits); + GraphView::addInfo(tr("Ascent"), QString::number(_ascent * yScale(), 'f', 0) + + UNIT_SPACE + yUnits()); + GraphView::addInfo(tr("Descent"), QString::number(_descent * yScale(), 'f', + 0) + UNIT_SPACE + yUnits()); + GraphView::addInfo(tr("Maximum"), QString::number(max() * yScale(), 'f', 0) + + UNIT_SPACE + yUnits()); + GraphView::addInfo(tr("Minimum"), QString::number(min() * yScale(), 'f', 0) + + UNIT_SPACE + yUnits()); + + redraw(); } void ElevationGraph::loadGPX(const GPX &gpx) { for (int i = 0; i < gpx.trackCount(); i++) { QVector data; - qreal min, max, ascent = 0, descent = 0; + qreal ascent = 0, descent = 0; gpx.track(i).elevationGraph(data); if (data.count() < 2) { @@ -43,8 +42,6 @@ void ElevationGraph::loadGPX(const GPX &gpx) continue; } - min = max = data.at(0).y(); - for (int j = 1; j < data.size(); j++) { qreal cur = data.at(j).y(); qreal prev = data.at(j-1).y(); @@ -53,29 +50,21 @@ void ElevationGraph::loadGPX(const GPX &gpx) ascent += cur - prev; if (cur < prev) descent += prev - cur; - - if (cur > max) - max = cur; - if (cur < min) - min = cur; } _ascent += ascent; _descent += descent; - _max = qMax(_max, max); - _min = qMin(_min, min); - addInfo(); loadData(data); } + + addInfo(); } void ElevationGraph::clear() { _ascent = 0; _descent = 0; - _max = -FLT_MAX; - _min = FLT_MAX; GraphView::clear(); } @@ -96,6 +85,4 @@ void ElevationGraph::setUnits(enum Units units) clearInfo(); addInfo(); - - redraw(); } diff --git a/src/elevationgraph.h b/src/elevationgraph.h index edbf136f..bc6443ac 100644 --- a/src/elevationgraph.h +++ b/src/elevationgraph.h @@ -19,14 +19,13 @@ public: qreal ascent() const {return _ascent;} qreal descent() const {return _descent;} - qreal max() const {return _max;} - qreal min() const {return _min;} + qreal max() const {return bounds().bottom();} + qreal min() const {return bounds().top();} private: void addInfo(); qreal _ascent, _descent; - qreal _max, _min; }; #endif // ELEVATIONGRAPH_H diff --git a/src/graphview.cpp b/src/graphview.cpp index 36bbba3b..f934c188 100644 --- a/src/graphview.cpp +++ b/src/graphview.cpp @@ -1,4 +1,3 @@ -#include #include #include #include @@ -44,16 +43,11 @@ GraphView::GraphView(QWidget *parent) _sliderInfo = new SliderInfoItem(_slider); _sliderInfo->setZValue(2.0); - _xMax = -FLT_MAX; - _xMin = FLT_MAX; - _yMax = -FLT_MAX; - _yMin = FLT_MAX; - _xScale = 1; _yScale = 1; _precision = 0; - _minRange = 0.01; + _minYRange = 0.01; } GraphView::~GraphView() @@ -74,14 +68,14 @@ GraphView::~GraphView() void GraphView::updateBounds(const QPointF &point) { - if (point.x() < _xMin) - _xMin = point.x(); - if (point.x() > _xMax) - _xMax = point.x(); - if (point.y() < _yMin) - _yMin = point.y(); - if (point.y() > _yMax) - _yMax = point.y(); + if (point.x() < _bounds.left()) + _bounds.setLeft(point.x()); + if (point.x() > _bounds.right()) + _bounds.setRight(point.x()); + if (point.y() > _bounds.bottom()) + _bounds.setBottom(point.y()); + if (point.y() < _bounds.top()) + _bounds.setTop(point.y()); } void GraphView::createXLabel() @@ -137,6 +131,9 @@ void GraphView::loadData(const QVector &data) if (data.size() < 2) return; + if (!_graphs.size()) + _bounds.moveTo(data.at(0)); + updateBounds(data.at(0)); path.moveTo(data.at(0).x(), -data.at(0).y()); for (int i = 1; i < data.size(); i++) { @@ -154,8 +151,6 @@ void GraphView::loadData(const QVector &data) if (_graphs.size() > 1) _sliderInfo->hide(); - - redraw(); } void GraphView::redraw() @@ -168,9 +163,9 @@ void GraphView::redraw(const QSizeF &size) { QRectF r; QSizeF mx, my; - QPointF rx, ry; + RangeF rx, ry; QTransform transform; - qreal xs, ys, diff; + qreal xs, ys; if (_xAxis->scene() == _scene) @@ -185,20 +180,20 @@ void GraphView::redraw(const QSizeF &size) for (int i = 0; i < _graphs.size(); i++) _graphs.at(i)->resetTransform(); - rx = QPointF(_xMin * _xScale, _xMax * _xScale); - ry = QPointF(_yMin * _yScale, _yMax * _yScale); - if ((diff = ry.y() - ry.x()) < _minRange) - ry = QPointF(ry.x() - (_minRange/2 - diff/2), - ry.y() + (_minRange/2 - diff/2)); + rx = RangeF(_bounds.left() * _xScale, _bounds.right() * _xScale); + ry = RangeF(_bounds.top() * _yScale, _bounds.bottom() * _yScale); + if (ry.size() < _minYRange) + ry.resize(_minYRange); + _xAxis->setRange(rx); _yAxis->setRange(ry); mx = _xAxis->margin(); my = _yAxis->margin(); r = _scene->itemsBoundingRect(); - if (r.height() < _minRange) - r.adjust(0, -(_minRange/2 - r.height()/2), 0, - _minRange/2 - r.height()/2); + if (r.height() < _minYRange) + r.adjust(0, -(_minYRange/2 - r.height()/2), 0, + _minYRange/2 - r.height()/2); xs = (size.width() - (my.width() + mx.width())) / r.width(); ys = (size.height() - (mx.height() + my.height()) @@ -209,9 +204,9 @@ void GraphView::redraw(const QSizeF &size) _graphs.at(i)->setTransform(transform); r = _scene->itemsBoundingRect(); - if (r.height() < _minRange * ys) - r.adjust(0, -(_minRange/2 * ys - r.height()/2), 0, - (_minRange/2) * ys - r.height()/2); + if (r.height() < _minYRange * ys) + r.adjust(0, -(_minYRange/2 * ys - r.height()/2), 0, + (_minYRange/2) * ys - r.height()/2); _xAxis->setSize(r.width()); _yAxis->setSize(r.height()); @@ -281,10 +276,7 @@ void GraphView::clear() _graphs.clear(); _palette.reset(); - _xMax = -FLT_MAX; - _xMin = FLT_MAX; - _yMax = -FLT_MAX; - _yMin = FLT_MAX; + _bounds = QRectF(); _scene->setSceneRect(0, 0, 0, 0); } @@ -326,18 +318,18 @@ void GraphView::emitSliderPositionChanged(const QPointF &pos) return; qreal val = pos.x() / _slider->area().width(); - emit sliderPositionChanged(val * (_xMax - _xMin)); + emit sliderPositionChanged(val * _bounds.width()); if (!_sliderInfo->isVisible()) return; const QPainterPath &path = _graphs.at(0)->path(); QRectF br = path.boundingRect(); - if (br.height() < _minRange) - br.adjust(0, -(_minRange/2 - br.height()/2), 0, - _minRange/2 - br.height()/2); + if (br.height() < _minYRange) + br.adjust(0, -(_minYRange/2 - br.height()/2), 0, + _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(); _sliderInfo->setPos(QPointF(0, _slider->boundingRect().height() * r)); _sliderInfo->setText(QString::number(-y * _yScale, 'f', _precision)); @@ -348,7 +340,7 @@ qreal GraphView::sliderPosition() const if (!_slider->isVisible()) return -1; else - return (_slider->pos().x() / _slider->area().width()) * (_xMax - _xMin); + return (_slider->pos().x() / _slider->area().width()) * _bounds.width(); } void GraphView::setSliderPosition(qreal pos) @@ -356,10 +348,10 @@ void GraphView::setSliderPosition(qreal pos) if (_graphs.isEmpty()) return; - if (pos > (_xMax - _xMin)) + if (pos > _bounds.right() || pos < _bounds.left()) _slider->setVisible(false); else { - _slider->setPos((pos / (_xMax - _xMin)) * _slider->area().width(), 0); + _slider->setPos((pos / _bounds.width()) * _slider->area().width(), 0); _slider->setVisible(true); } } diff --git a/src/graphview.h b/src/graphview.h index a06d5577..d76ac6d7 100644 --- a/src/graphview.h +++ b/src/graphview.h @@ -35,39 +35,45 @@ public: ~GraphView(); void loadData(const QVector &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 setYLabel(const QString &label); void setXUnits(const QString &units); void setYUnits(const QString &units); void setXScale(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 clear(); + void setSliderPrecision(int precision) {_precision = precision;} + void setMinYRange(qreal range) {_minYRange = range;} qreal sliderPosition() const; void setSliderPosition(qreal pos); - int count() const {return _graphs.count();} + void plot(QPainter *painter, const QRectF &target); signals: void sliderPositionChanged(qreal); protected: + const QRectF &bounds() const {return _bounds;} void resizeEvent(QResizeEvent *); - void redraw(); + void redraw(const QSizeF &size); void addInfo(const QString &key, const QString &value); void clearInfo(); void skipColor() {_palette.color();} - qreal _xScale, _yScale; - QString _xUnits, _yUnits; - QString _xLabel, _yLabel; - int _precision; - qreal _minRange; - private slots: void emitSliderPositionChanged(const QPointF &pos); void newSliderPosition(const QPointF &pos); @@ -76,7 +82,12 @@ private: void createXLabel(); void createYLabel(); 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; @@ -86,7 +97,7 @@ private: InfoItem *_info; QList _graphs; - qreal _xMin, _xMax, _yMin, _yMax; + QRectF _bounds; Palette _palette; }; diff --git a/src/heartrategraph.cpp b/src/heartrategraph.cpp index b3043e10..f8ab34ee 100644 --- a/src/heartrategraph.cpp +++ b/src/heartrategraph.cpp @@ -4,29 +4,29 @@ HeartRateGraph::HeartRateGraph(QWidget *parent) : GraphView(parent) { - _max = 0; - setXLabel(tr("Distance")); setYLabel(tr("Heart rate")); setXUnits(tr("km")); setYUnits(tr("1/min")); setXScale(M2KM); - setPrecision(0); + setSliderPrecision(0); } void HeartRateGraph::addInfo() { - GraphView::addInfo(tr("Average"), QString::number(avg() * _yScale, 'f', 0) - + UNIT_SPACE + _yUnits); - GraphView::addInfo(tr("Maximum"), QString::number(_max * _yScale, 'f', 0) - + UNIT_SPACE + _yUnits); + GraphView::addInfo(tr("Average"), QString::number(avg() * yScale(), 'f', 0) + + UNIT_SPACE + yUnits()); + GraphView::addInfo(tr("Maximum"), QString::number(max() * yScale(), 'f', 0) + + UNIT_SPACE + yUnits()); + + redraw(); } void HeartRateGraph::loadGPX(const GPX &gpx) { for (int i = 0; i < gpx.trackCount(); i++) { QVector data; - qreal max = 0, sum = 0, w = 0; + qreal sum = 0, w = 0; gpx.track(i).heartRateGraph(data); if (data.count() < 2) { @@ -40,13 +40,10 @@ void HeartRateGraph::loadGPX(const GPX &gpx) } _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); } + + addInfo(); } qreal HeartRateGraph::avg() const @@ -64,7 +61,6 @@ qreal HeartRateGraph::avg() const void HeartRateGraph::clear() { - _max = 0; _avg.clear(); GraphView::clear(); @@ -82,6 +78,4 @@ void HeartRateGraph::setUnits(enum Units units) clearInfo(); addInfo(); - - redraw(); } diff --git a/src/heartrategraph.h b/src/heartrategraph.h index 372c78a5..c6010868 100644 --- a/src/heartrategraph.h +++ b/src/heartrategraph.h @@ -18,12 +18,11 @@ public: void setUnits(enum Units units); qreal avg() const; - qreal max() const {return _max;} + qreal max() const {return bounds().bottom();} private: void addInfo(); - qreal _max; QList _avg; }; diff --git a/src/range.cpp b/src/range.cpp new file mode 100644 index 00000000..21639106 --- /dev/null +++ b/src/range.cpp @@ -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(); +} diff --git a/src/range.h b/src/range.h new file mode 100644 index 00000000..ed5718b0 --- /dev/null +++ b/src/range.h @@ -0,0 +1,25 @@ +#ifndef RANGE_H +#define RANGE_H + +#include +#include + +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 diff --git a/src/speedgraph.cpp b/src/speedgraph.cpp index e4465df0..b815fc42 100644 --- a/src/speedgraph.cpp +++ b/src/speedgraph.cpp @@ -5,30 +5,29 @@ SpeedGraph::SpeedGraph(QWidget *parent) : GraphView(parent) { - _max = 0; - setXLabel(tr("Distance")); setYLabel(tr("Speed")); setXUnits(tr("km")); setYUnits(tr("km/h")); setXScale(M2KM); setYScale(MS2KMH); - setPrecision(1); + setSliderPrecision(1); } void SpeedGraph::addInfo() { - GraphView::addInfo(tr("Average"), QString::number(avg() * _yScale, 'f', 1) - + UNIT_SPACE + _yUnits); - GraphView::addInfo(tr("Maximum"), QString::number(_max * _yScale, 'f', 1) - + UNIT_SPACE + _yUnits); + GraphView::addInfo(tr("Average"), QString::number(avg() * yScale(), 'f', 1) + + UNIT_SPACE + yUnits()); + GraphView::addInfo(tr("Maximum"), QString::number(max() * yScale(), 'f', 1) + + UNIT_SPACE + yUnits()); + + redraw(); } void SpeedGraph::loadGPX(const GPX &gpx) { for (int i = 0; i < gpx.trackCount(); i++) { QVector data; - qreal max = 0; gpx.track(i).speedGraph(data); 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() / 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); } + + addInfo(); } qreal SpeedGraph::avg() const @@ -63,7 +59,6 @@ qreal SpeedGraph::avg() const void SpeedGraph::clear() { - _max = 0; _avg.clear(); GraphView::clear(); @@ -85,6 +80,4 @@ void SpeedGraph::setUnits(enum Units units) clearInfo(); addInfo(); - - redraw(); } diff --git a/src/speedgraph.h b/src/speedgraph.h index 78ca05a9..fc945256 100644 --- a/src/speedgraph.h +++ b/src/speedgraph.h @@ -19,12 +19,11 @@ public: void setUnits(enum Units units); qreal avg() const; - qreal max() const {return _max;} + qreal max() const {return bounds().bottom();} private: void addInfo(); - qreal _max; QList _avg; };