1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-27 21:24: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/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

View File

@ -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);

View File

@ -2,6 +2,7 @@
#define AXISITEM_H
#include <QGraphicsItem>
#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;

View File

@ -1,4 +1,3 @@
#include <float.h>
#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<QPointF> 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();
}

View File

@ -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

View File

@ -1,4 +1,3 @@
#include <float.h>
#include <QGraphicsView>
#include <QGraphicsSceneMouseEvent>
#include <QEvent>
@ -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<QPointF> &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<QPointF> &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);
}
}

View File

@ -35,39 +35,45 @@ public:
~GraphView();
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 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<QGraphicsPathItem*> _graphs;
qreal _xMin, _xMax, _yMin, _yMax;
QRectF _bounds;
Palette _palette;
};

View File

@ -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<QPointF> 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();
}

View File

@ -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<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)
{
_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<QPointF> 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();
}

View File

@ -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<QPointF> _avg;
};