1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-24 03:35:53 +01:00

Some more graph handling improvements

This commit is contained in:
Martin Tůma 2016-08-19 19:48:44 +02:00
parent d48a2aac93
commit 0ee0bd882e
14 changed files with 164 additions and 146 deletions

View File

@ -87,7 +87,8 @@ SOURCES += src/main.cpp \
src/trackitem.cpp \
src/tooltip.cpp \
src/route.cpp \
src/routeitem.cpp
src/routeitem.cpp \
src/graphitem.cpp
RESOURCES += gpxsee.qrc
TRANSLATIONS = lang/gpxsee_cs.ts
macx {

View File

@ -536,48 +536,48 @@
<context>
<name>HeartRateGraph</name>
<message>
<location filename="../src/heartrategraph.cpp" line="10"/>
<location filename="../src/heartrategraph.cpp" line="11"/>
<source>Distance</source>
<translation>Vzdálenost</translation>
</message>
<message>
<location filename="../src/heartrategraph.cpp" line="11"/>
<location filename="../src/heartrategraph.cpp" line="12"/>
<location filename="../src/heartrategraph.h" line="15"/>
<source>Heart rate</source>
<translation>Tep</translation>
</message>
<message>
<location filename="../src/heartrategraph.cpp" line="80"/>
<location filename="../src/heartrategraph.cpp" line="84"/>
<source>km</source>
<translation>km</translation>
</message>
<message>
<location filename="../src/heartrategraph.cpp" line="9"/>
<location filename="../src/heartrategraph.cpp" line="10"/>
<source>1/min</source>
<translation>1/min</translation>
</message>
<message>
<location filename="../src/heartrategraph.cpp" line="18"/>
<location filename="../src/heartrategraph.cpp" line="20"/>
<source>Average</source>
<translation>Průměr</translation>
</message>
<message>
<location filename="../src/heartrategraph.cpp" line="20"/>
<location filename="../src/heartrategraph.cpp" line="22"/>
<source>Maximum</source>
<translation>Maximum</translation>
</message>
<message>
<location filename="../src/heartrategraph.cpp" line="77"/>
<location filename="../src/heartrategraph.cpp" line="81"/>
<source>m</source>
<translation>m</translation>
</message>
<message>
<location filename="../src/heartrategraph.cpp" line="85"/>
<location filename="../src/heartrategraph.cpp" line="89"/>
<source>ft</source>
<translation>ft</translation>
</message>
<message>
<location filename="../src/heartrategraph.cpp" line="88"/>
<location filename="../src/heartrategraph.cpp" line="92"/>
<source>mi</source>
<translation>mi</translation>
</message>
@ -633,53 +633,53 @@
<context>
<name>SpeedGraph</name>
<message>
<location filename="../src/speedgraph.cpp" line="11"/>
<location filename="../src/speedgraph.cpp" line="12"/>
<source>Distance</source>
<translation>Vzdálenost</translation>
</message>
<message>
<location filename="../src/speedgraph.cpp" line="12"/>
<location filename="../src/speedgraph.cpp" line="13"/>
<location filename="../src/speedgraph.h" line="16"/>
<source>Speed</source>
<translation>Rychlost</translation>
</message>
<message>
<location filename="../src/speedgraph.cpp" line="73"/>
<location filename="../src/speedgraph.cpp" line="77"/>
<source>m</source>
<translation>m</translation>
</message>
<message>
<location filename="../src/speedgraph.cpp" line="76"/>
<location filename="../src/speedgraph.cpp" line="80"/>
<source>km</source>
<translation></translation>
</message>
<message>
<location filename="../src/speedgraph.cpp" line="81"/>
<location filename="../src/speedgraph.cpp" line="85"/>
<source>ft</source>
<translation>ft</translation>
</message>
<message>
<location filename="../src/speedgraph.cpp" line="93"/>
<location filename="../src/speedgraph.cpp" line="97"/>
<source>km/h</source>
<translation>km/h</translation>
</message>
<message>
<location filename="../src/speedgraph.cpp" line="19"/>
<location filename="../src/speedgraph.cpp" line="21"/>
<source>Average</source>
<translation>Průměr</translation>
</message>
<message>
<location filename="../src/speedgraph.cpp" line="21"/>
<location filename="../src/speedgraph.cpp" line="23"/>
<source>Maximum</source>
<translation>Maximum</translation>
</message>
<message>
<location filename="../src/speedgraph.cpp" line="84"/>
<location filename="../src/speedgraph.cpp" line="88"/>
<source>mi</source>
<translation>mi</translation>
</message>
<message>
<location filename="../src/speedgraph.cpp" line="96"/>
<location filename="../src/speedgraph.cpp" line="100"/>
<source>mi/h</source>
<translation>mi/h</translation>
</message>
@ -687,58 +687,58 @@
<context>
<name>TemperatureGraph</name>
<message>
<location filename="../src/temperaturegraph.cpp" line="10"/>
<location filename="../src/temperaturegraph.cpp" line="11"/>
<source>Distance</source>
<translation>Vzdálenost</translation>
</message>
<message>
<location filename="../src/temperaturegraph.cpp" line="11"/>
<location filename="../src/temperaturegraph.cpp" line="12"/>
<location filename="../src/temperaturegraph.h" line="15"/>
<source>Temperature</source>
<translation>Teplota</translation>
</message>
<message>
<location filename="../src/temperaturegraph.cpp" line="18"/>
<location filename="../src/temperaturegraph.cpp" line="20"/>
<source>Average</source>
<translation>Průměr</translation>
</message>
<message>
<location filename="../src/temperaturegraph.cpp" line="20"/>
<location filename="../src/temperaturegraph.cpp" line="22"/>
<source>Minimum</source>
<translation>Minimum</translation>
</message>
<message>
<location filename="../src/temperaturegraph.cpp" line="22"/>
<location filename="../src/temperaturegraph.cpp" line="24"/>
<source>Maximum</source>
<translation>Maximum</translation>
</message>
<message>
<location filename="../src/temperaturegraph.cpp" line="79"/>
<location filename="../src/temperaturegraph.cpp" line="83"/>
<source>m</source>
<translation>m</translation>
</message>
<message>
<location filename="../src/temperaturegraph.cpp" line="82"/>
<location filename="../src/temperaturegraph.cpp" line="86"/>
<source>km</source>
<translation>km</translation>
</message>
<message>
<location filename="../src/temperaturegraph.cpp" line="87"/>
<location filename="../src/temperaturegraph.cpp" line="91"/>
<source>ft</source>
<translation>ft</translation>
</message>
<message>
<location filename="../src/temperaturegraph.cpp" line="90"/>
<location filename="../src/temperaturegraph.cpp" line="94"/>
<source>mi</source>
<translation>mi</translation>
</message>
<message>
<location filename="../src/temperaturegraph.cpp" line="99"/>
<location filename="../src/temperaturegraph.cpp" line="103"/>
<source>C</source>
<translation>C</translation>
</message>
<message>
<location filename="../src/temperaturegraph.cpp" line="103"/>
<location filename="../src/temperaturegraph.cpp" line="107"/>
<source>F</source>
<translation>F</translation>
</message>

View File

@ -184,6 +184,7 @@ void ElevationGraph::showTracks(bool show)
setInfo();
showGraph(show, Track);
setXUnits();
redraw();
}
@ -194,6 +195,7 @@ void ElevationGraph::showRoutes(bool show)
setInfo();
showGraph(show, Route);
setXUnits();
redraw();
}

10
src/graphitem.cpp Normal file
View File

@ -0,0 +1,10 @@
#include <QBrush>
#include <QPen>
#include "graphitem.h"
void GraphItem::setColor(const QColor &color)
{
QBrush brush(color, Qt::SolidPattern);
QPen pen(brush, 0);
setPen(pen);
}

View File

@ -9,8 +9,9 @@ public:
GraphItem(const QPainterPath &path, QGraphicsItem * parent = 0)
: QGraphicsPathItem(path, parent) {_id = 0;}
int id() {return _id;}
int id() const {return _id;}
void setId(int id) {_id = id;}
void setColor(const QColor &color);
private:
int _id;

View File

@ -39,6 +39,11 @@ GraphView::GraphView(QWidget *parent)
_sliderInfo->setZValue(2.0);
_info = new InfoItem();
_scene->addItem(_xAxis);
_scene->addItem(_yAxis);
_scene->addItem(_slider);
_scene->addItem(_info);
connect(_slider, SIGNAL(positionChanged(const QPointF&)), this,
SLOT(emitSliderPositionChanged(const QPointF&)));
connect(_scene, SIGNAL(mouseClicked(const QPointF&)), this,
@ -54,30 +59,6 @@ GraphView::GraphView(QWidget *parent)
_sliderPos = 0;
}
GraphView::~GraphView()
{
if (_xAxis->scene() != _scene)
delete _xAxis;
if (_yAxis->scene() != _scene)
delete _yAxis;
if (_slider->scene() != _scene)
delete _slider;
if (_info->scene() != _scene)
delete _info;
}
void GraphView::updateBounds(const QPointF &point)
{
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()
{
_xAxis->setLabel(QString("%1 [%2]").arg(_xLabel).arg(_xUnits));
@ -121,46 +102,70 @@ void GraphView::loadData(const QVector<QPointF> &data, int id)
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++) {
const QPointF &p = data.at(i);
path.lineTo(p.x(), -p.y());
updateBounds(p);
}
pi = new GraphItem(path);
QBrush brush(_palette.color(), Qt::SolidPattern);
QPen pen(brush, 0);
pi->setPen(pen);
pi->setId(id);
if (_hide.contains(id))
pi->hide();
_scene->addItem(pi);
pi->setColor(_palette.color());
_graphs.append(pi);
if (!_hide.contains(id)) {
_visible.append(pi);
_scene->addItem(pi);
updateBounds(path);
}
}
void GraphView::showGraph(bool show, int id)
{
for (int i = 0; i < _graphs.count(); i++)
if (_graphs.at(i)->id() == id)
_graphs.at(i)->setVisible(show);
if (show)
_hide.remove(id);
else
_hide.insert(id);
_visible.clear();
_bounds = QRectF();
for (int i = 0; i < _graphs.count(); i++) {
GraphItem* gi = _graphs.at(i);
if (_hide.contains(gi->id())) {
if (gi->scene() == _scene)
_scene->removeItem(gi);
} else {
if (gi->scene() != _scene)
_scene->addItem(gi);
_visible.append(gi);
updateBounds(gi->path());
}
}
}
void GraphView::redraw()
{
if (!_graphs.isEmpty())
redraw(viewport()->size() - QSizeF(MARGIN, MARGIN));
}
void GraphView::updateBounds(const QPainterPath &path)
{
QRectF br = path.boundingRect();
br.moveTopLeft(QPointF(br.left(), -br.top() - br.height()));
_bounds |= br;
}
QRectF GraphView::graphsBoundingRect() const
{
QRectF rect;
for (int i = 0; i < _visible.count(); i++)
rect |= _visible.at(i)->boundingRect();
return rect;
}
void GraphView::redraw(const QSizeF &size)
{
QRectF r;
@ -170,18 +175,6 @@ void GraphView::redraw(const QSizeF &size)
qreal xs, ys;
if (_xAxis->scene() == _scene)
_scene->removeItem(_xAxis);
if (_yAxis->scene() == _scene)
_scene->removeItem(_yAxis);
if (_slider->scene() == _scene)
_scene->removeItem(_slider);
if (_info->scene() == _scene)
_scene->removeItem(_info);
for (int i = 0; i < _graphs.size(); i++)
_graphs.at(i)->resetTransform();
rx = RangeF(_bounds.left() * _xScale, _bounds.right() * _xScale);
ry = RangeF(_bounds.top() * _yScale + _yOffset, _bounds.bottom() * _yScale
+ _yOffset);
@ -193,7 +186,7 @@ void GraphView::redraw(const QSizeF &size)
mx = _xAxis->margin();
my = _yAxis->margin();
r = _scene->itemsBoundingRect();
r = graphsBoundingRect();
if (r.height() < _minYRange)
r.adjust(0, -(_minYRange/2 - r.height()/2), 0,
_minYRange/2 - r.height()/2);
@ -203,10 +196,12 @@ void GraphView::redraw(const QSizeF &size)
- _info->boundingRect().height()) / r.height();
transform.scale(xs, ys);
for (int i = 0; i < _graphs.size(); i++)
_graphs.at(i)->setTransform(transform);
for (int i = 0; i < _visible.size(); i++)
_visible.at(i)->setTransform(transform);
r = _scene->itemsBoundingRect();
QPointF p(r.left() * xs, r.top() * ys);
QSizeF s(r.width() * xs, r.height() * ys);
r = QRectF(p, s);
if (r.height() < _minYRange * ys)
r.adjust(0, -(_minYRange/2 * ys - r.height()/2), 0,
(_minYRange/2) * ys - r.height()/2);
@ -215,24 +210,20 @@ void GraphView::redraw(const QSizeF &size)
_yAxis->setSize(r.height());
_xAxis->setPos(r.bottomLeft());
_yAxis->setPos(r.bottomLeft());
_scene->addItem(_xAxis);
_scene->addItem(_yAxis);
_slider->setArea(r);
updateSliderPosition();
_scene->addItem(_slider);
r = _scene->itemsBoundingRect();
r |= _xAxis->sceneBoundingRect();
r |= _yAxis->sceneBoundingRect();
_info->setPos(r.topLeft() + QPointF(r.width()/2
- _info->boundingRect().width()/2, -_info->boundingRect().height()));
_scene->addItem(_info);
_scene->setSceneRect(_scene->itemsBoundingRect());
}
void GraphView::resizeEvent(QResizeEvent *)
{
if (!_graphs.empty())
redraw();
}
@ -254,19 +245,14 @@ void GraphView::plot(QPainter *painter, const QRectF &target)
void GraphView::clear()
{
if (_xAxis->scene() == _scene)
_scene->removeItem(_xAxis);
if (_yAxis->scene() == _scene)
_scene->removeItem(_yAxis);
if (_slider->scene() == _scene)
_scene->removeItem(_slider);
if (_info->scene() == _scene)
_scene->removeItem(_info);
_slider->clear();
_info->clear();
_scene->clear();
for (int i = 0; i < _graphs.count(); i++)
delete _graphs[i];
_graphs.clear();
_visible.clear();
_palette.reset();
_bounds = QRectF();
@ -308,26 +294,28 @@ static qreal yAtX(const QPainterPath &path, qreal x)
void GraphView::updateSliderPosition()
{
Q_ASSERT(_bounds.width() > 0);
if (_bounds.width() <= 0)
return;
if (_sliderPos <= _bounds.right() && _sliderPos >= _bounds.left()) {
_slider->setPos((_sliderPos / _bounds.width()) * _slider->area().width(),
_slider->area().bottom());
_slider->setVisible(true);
} else
_slider->setVisible(!_visible.isEmpty());
} else {
_slider->setPos(_slider->area().left(), _slider->area().bottom());
_slider->setVisible(false);
}
updateSliderInfo();
}
void GraphView::updateSliderInfo()
{
_sliderInfo->setVisible(_graphs.size() == 1);
_sliderInfo->setVisible(_visible.count() == 1);
if (!_sliderInfo->isVisible())
return;
const QPainterPath &path = _graphs.at(0)->path();
const QPainterPath &path = _visible.first()->path();
QRectF br = path.boundingRect();
if (br.height() < _minYRange)
br.adjust(0, -(_minYRange/2 - br.height()/2), 0,
@ -348,17 +336,18 @@ void GraphView::updateSliderInfo()
void GraphView::emitSliderPositionChanged(const QPointF &pos)
{
Q_ASSERT(_slider->area().width() > 0);
if (_slider->area().width() <= 0)
return;
_sliderPos = (pos.x() / _slider->area().width()) * _bounds.width();
updateSliderPosition();
emit sliderPositionChanged(_sliderPos);
}
void GraphView::setSliderPosition(qreal pos)
{
if (_graphs.isEmpty())
if (_visible.isEmpty())
return;
_sliderPos = pos;
@ -368,7 +357,7 @@ void GraphView::setSliderPosition(qreal pos)
void GraphView::newSliderPosition(const QPointF &pos)
{
if (_slider->area().contains(pos))
emitSliderPositionChanged(pos);
_slider->setPos(pos);
}
void GraphView::addInfo(const QString &key, const QString &value)

View File

@ -33,7 +33,6 @@ class GraphView : public QGraphicsView
public:
GraphView(QWidget *parent = 0);
~GraphView();
void loadData(const QVector<QPointF> &data, int id = 0);
int count() const {return _graphs.count();}
@ -84,9 +83,10 @@ private slots:
private:
void createXLabel();
void createYLabel();
void updateBounds(const QPointF &point);
void updateSliderPosition();
void updateSliderInfo();
void updateBounds(const QPainterPath &path);
QRectF graphsBoundingRect() const;
qreal _xScale, _yScale;
qreal _yOffset;
@ -104,8 +104,8 @@ private:
InfoItem *_info;
QList<GraphItem*> _graphs;
QList<GraphItem*> _visible;
QSet<int> _hide;
bool _hideAll;
QRectF _bounds;
Palette _palette;
};

View File

@ -5,6 +5,7 @@
HeartRateGraph::HeartRateGraph(QWidget *parent) : GraphTab(parent)
{
_units = Metric;
_showTracks = true;
GraphView::setYUnits(tr("1/min"));
setXLabel(tr("Distance"));
@ -15,10 +16,13 @@ HeartRateGraph::HeartRateGraph(QWidget *parent) : GraphTab(parent)
void HeartRateGraph::setInfo()
{
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());
if (_showTracks) {
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());
} else
clearInfo();
}
void HeartRateGraph::loadGPX(const GPX &gpx)
@ -103,12 +107,11 @@ void HeartRateGraph::setUnits(enum Units units)
void HeartRateGraph::showTracks(bool show)
{
if (show)
setInfo();
else
clearInfo();
_showTracks = show;
setInfo();
showGraph(show);
setXUnits();
redraw();
}

View File

@ -26,7 +26,9 @@ private:
void setInfo();
QList<QPointF> _avg;
enum Units _units;
bool _showTracks;
};
#endif // HEARTRATEGRAPH_H

View File

@ -24,7 +24,7 @@ void SliderItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option
painter->setPen(Qt::red);
painter->drawLine(0, 0, 0, -_area.height());
//painter->drawRect(boundingRect());
// painter->drawRect(boundingRect());
}
QVariant SliderItem::itemChange(GraphicsItemChange change, const QVariant &value)

View File

@ -6,6 +6,7 @@
SpeedGraph::SpeedGraph(QWidget *parent) : GraphTab(parent)
{
_units = Metric;
_showTracks = true;
setYUnits();
setXLabel(tr("Distance"));
@ -16,10 +17,13 @@ SpeedGraph::SpeedGraph(QWidget *parent) : GraphTab(parent)
void SpeedGraph::setInfo()
{
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());
if (_showTracks) {
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());
} else
clearInfo();
}
void SpeedGraph::loadGPX(const GPX &gpx)
@ -111,12 +115,11 @@ void SpeedGraph::setUnits(enum Units units)
void SpeedGraph::showTracks(bool show)
{
if (show)
setInfo();
else
clearInfo();
_showTracks = show;
setInfo();
showGraph(show);
setXUnits();
redraw();
}

View File

@ -28,7 +28,9 @@ private:
void setInfo();
QList<QPointF> _avg;
enum Units _units;
bool _showTracks;
};
#endif // SPEEDGRAPH_H

View File

@ -5,6 +5,7 @@
TemperatureGraph::TemperatureGraph(QWidget *parent) : GraphTab(parent)
{
_units = Metric;
_showTracks = true;
setYUnits();
setXLabel(tr("Distance"));
@ -15,12 +16,15 @@ TemperatureGraph::TemperatureGraph(QWidget *parent) : GraphTab(parent)
void TemperatureGraph::setInfo()
{
if (_showTracks) {
GraphView::addInfo(tr("Average"), QString::number(avg() * yScale()
+ yOffset(), 'f', 1) + UNIT_SPACE + yUnits());
GraphView::addInfo(tr("Minimum"), QString::number(min() * yScale()
+ yOffset(), 'f', 1) + UNIT_SPACE + yUnits());
GraphView::addInfo(tr("Maximum"), QString::number(max() * yScale()
+ yOffset(), 'f', 1) + UNIT_SPACE + yUnits());
} else
clearInfo();
}
void TemperatureGraph::loadGPX(const GPX &gpx)
@ -119,12 +123,11 @@ void TemperatureGraph::setUnits(enum Units units)
void TemperatureGraph::showTracks(bool show)
{
if (show)
setInfo();
else
clearInfo();
_showTracks = show;
setInfo();
showGraph(show);
setXUnits();
redraw();
}

View File

@ -28,7 +28,9 @@ private:
void setInfo();
QList<QPointF> _avg;
enum Units _units;
bool _showTracks;
};
#endif // TEMPERATUREGRAPH_H