mirror of
https://github.com/tumic0/GPXSee.git
synced 2025-07-09 00:34:27 +02:00
Added support for track segments
This commit is contained in:
@ -3,26 +3,17 @@
|
||||
|
||||
|
||||
GraphItem::GraphItem(const Graph &graph, GraphType type, QGraphicsItem *parent)
|
||||
: QGraphicsObject(parent)
|
||||
: QGraphicsObject(parent), _graph(graph), _type(type)
|
||||
{
|
||||
Q_ASSERT(_graph.isValid());
|
||||
|
||||
_id = 0;
|
||||
_width = 1;
|
||||
|
||||
_pen = QPen(Qt::black, _width);
|
||||
|
||||
_type = type;
|
||||
_graph = graph;
|
||||
_sx = 1.0; _sy = 1.0;
|
||||
_time = _graph.hasTime();
|
||||
|
||||
_time = true;
|
||||
for (int i = 0; i < _graph.size(); i++) {
|
||||
if (std::isnan(_graph.at(i).t())) {
|
||||
_time = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
setZValue(1.0);
|
||||
setZValue(2.0);
|
||||
|
||||
updatePath();
|
||||
updateShape();
|
||||
@ -89,18 +80,31 @@ void GraphItem::setWidth(int width)
|
||||
updateShape();
|
||||
}
|
||||
|
||||
const GraphSegment *GraphItem::segment(qreal x, GraphType type) const
|
||||
{
|
||||
for (int i = 0; i < _graph.size(); i++)
|
||||
if (x <= _graph.at(i).last().x(type))
|
||||
return &(_graph.at(i));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
qreal GraphItem::yAtX(qreal x)
|
||||
{
|
||||
const GraphSegment *seg = segment(x, _type);
|
||||
if (!seg)
|
||||
return NAN;
|
||||
|
||||
int low = 0;
|
||||
int high = _graph.count() - 1;
|
||||
int high = seg->count() - 1;
|
||||
int mid = 0;
|
||||
|
||||
Q_ASSERT(high > low);
|
||||
Q_ASSERT(x >= _graph.at(low).x(_type) && x <= _graph.at(high).x(_type));
|
||||
if (!(x >= seg->at(low).x(_type) && x <= seg->at(high).x(_type)))
|
||||
return NAN;
|
||||
|
||||
while (low <= high) {
|
||||
mid = low + ((high - low) / 2);
|
||||
const GraphPoint &p = _graph.at(mid);
|
||||
const GraphPoint &p = seg->at(mid);
|
||||
if (p.x(_type) > x)
|
||||
high = mid - 1;
|
||||
else if (p.x(_type) < x)
|
||||
@ -110,58 +114,56 @@ qreal GraphItem::yAtX(qreal x)
|
||||
}
|
||||
|
||||
QLineF l;
|
||||
if (_graph.at(mid).x(_type) < x)
|
||||
l = QLineF(_graph.at(mid).x(_type), _graph.at(mid).y(),
|
||||
_graph.at(mid+1).x(_type), _graph.at(mid+1).y());
|
||||
if (seg->at(mid).x(_type) < x)
|
||||
l = QLineF(seg->at(mid).x(_type), seg->at(mid).y(),
|
||||
seg->at(mid+1).x(_type), seg->at(mid+1).y());
|
||||
else
|
||||
l = QLineF(_graph.at(mid-1).x(_type), _graph.at(mid-1).y(),
|
||||
_graph.at(mid).x(_type), _graph.at(mid).y());
|
||||
l = QLineF(seg->at(mid-1).x(_type), seg->at(mid-1).y(),
|
||||
seg->at(mid).x(_type), seg->at(mid).y());
|
||||
|
||||
return -l.pointAt((x - l.p1().x()) / (l.p2().x() - l.p1().x())).y();
|
||||
}
|
||||
|
||||
qreal GraphItem::distanceAtTime(qreal time)
|
||||
{
|
||||
const GraphSegment *seg = segment(time, Time);
|
||||
if (!seg)
|
||||
return NAN;
|
||||
|
||||
int low = 0;
|
||||
int high = _graph.count() - 1;
|
||||
int high = seg->count() - 1;
|
||||
int mid = 0;
|
||||
|
||||
Q_ASSERT(high > low);
|
||||
Q_ASSERT(time >= _graph.at(low).t() && time <= _graph.at(high).t());
|
||||
if (!(time >= seg->at(low).t() && time <= seg->at(high).t()))
|
||||
return NAN;
|
||||
|
||||
while (low <= high) {
|
||||
mid = low + ((high - low) / 2);
|
||||
const GraphPoint &p = _graph.at(mid);
|
||||
const GraphPoint &p = seg->at(mid);
|
||||
if (p.t() > time)
|
||||
high = mid - 1;
|
||||
else if (p.t() < time)
|
||||
low = mid + 1;
|
||||
else
|
||||
return _graph.at(mid).s();
|
||||
return seg->at(mid).s();
|
||||
}
|
||||
|
||||
QLineF l;
|
||||
if (_graph.at(mid).t() < time)
|
||||
l = QLineF(_graph.at(mid).t(), _graph.at(mid).s(), _graph.at(mid+1).t(),
|
||||
_graph.at(mid+1).s());
|
||||
if (seg->at(mid).t() < time)
|
||||
l = QLineF(seg->at(mid).t(), seg->at(mid).s(), seg->at(mid+1).t(),
|
||||
seg->at(mid+1).s());
|
||||
else
|
||||
l = QLineF(_graph.at(mid-1).t(), _graph.at(mid-1).s(),
|
||||
_graph.at(mid).t(), _graph.at(mid).s());
|
||||
l = QLineF(seg->at(mid-1).t(), seg->at(mid-1).s(),
|
||||
seg->at(mid).t(), seg->at(mid).s());
|
||||
|
||||
return l.pointAt((time - l.p1().x()) / (l.p2().x() - l.p1().x())).y();
|
||||
}
|
||||
|
||||
void GraphItem::emitSliderPositionChanged(qreal pos)
|
||||
{
|
||||
if (_type == Time) {
|
||||
if (_time) {
|
||||
if (pos >= _graph.first().t() && pos <= _graph.last().t())
|
||||
emit sliderPositionChanged(distanceAtTime(pos));
|
||||
else
|
||||
emit sliderPositionChanged(NAN);
|
||||
} else
|
||||
emit sliderPositionChanged(NAN);
|
||||
} else
|
||||
if (_type == Time)
|
||||
emit sliderPositionChanged(_time ? distanceAtTime(pos) : NAN);
|
||||
else
|
||||
emit sliderPositionChanged(pos);
|
||||
}
|
||||
|
||||
@ -197,9 +199,13 @@ void GraphItem::updatePath()
|
||||
if (_type == Time && !_time)
|
||||
return;
|
||||
|
||||
_path.moveTo(_graph.first().x(_type) * _sx, -_graph.first().y() * _sy);
|
||||
for (int i = 1; i < _graph.size(); i++)
|
||||
_path.lineTo(_graph.at(i).x(_type) * _sx, -_graph.at(i).y() * _sy);
|
||||
for (int i = 0; i < _graph.size(); i++) {
|
||||
const GraphSegment &segment = _graph.at(i);
|
||||
|
||||
_path.moveTo(segment.first().x(_type) * _sx, -segment.first().y() * _sy);
|
||||
for (int i = 1; i < segment.size(); i++)
|
||||
_path.lineTo(segment.at(i).x(_type) * _sx, -segment.at(i).y() * _sy);
|
||||
}
|
||||
}
|
||||
|
||||
void GraphItem::updateBounds()
|
||||
@ -211,18 +217,71 @@ void GraphItem::updateBounds()
|
||||
|
||||
qreal bottom, top, left, right;
|
||||
|
||||
QPointF p = QPointF(_graph.first().x(_type), -_graph.first().y());
|
||||
QPointF p = QPointF(_graph.first().first().x(_type),
|
||||
-_graph.first().first().y());
|
||||
bottom = p.y(); top = p.y(); left = p.x(); right = p.x();
|
||||
|
||||
for (int i = 1; i < _graph.size(); i++) {
|
||||
p = QPointF(_graph.at(i).x(_type), -_graph.at(i).y());
|
||||
bottom = qMax(bottom, p.y()); top = qMin(top, p.y());
|
||||
right = qMax(right, p.x()); left = qMin(left, p.x());
|
||||
for (int i = 0; i < _graph.size(); i++) {
|
||||
const GraphSegment &segment = _graph.at(i);
|
||||
|
||||
for (int j = 0; j < segment.size(); j++) {
|
||||
p = QPointF(segment.at(j).x(_type), -segment.at(j).y());
|
||||
bottom = qMax(bottom, p.y()); top = qMin(top, p.y());
|
||||
right = qMax(right, p.x()); left = qMin(left, p.x());
|
||||
}
|
||||
}
|
||||
|
||||
_bounds = QRectF(QPointF(left, top), QPointF(right, bottom));
|
||||
}
|
||||
|
||||
qreal GraphItem::max() const
|
||||
{
|
||||
qreal ret = _graph.first().first().y();
|
||||
|
||||
for (int i = 0; i < _graph.size(); i++) {
|
||||
const GraphSegment &segment = _graph.at(i);
|
||||
|
||||
for (int j = 0; j < segment.size(); j++) {
|
||||
qreal y = segment.at(j).y();
|
||||
if (y > ret)
|
||||
ret = y;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
qreal GraphItem::min() const
|
||||
{
|
||||
qreal ret = _graph.first().first().y();
|
||||
|
||||
for (int i = 0; i < _graph.size(); i++) {
|
||||
const GraphSegment &segment = _graph.at(i);
|
||||
|
||||
for (int j = 0; j < segment.size(); j++) {
|
||||
qreal y = segment.at(j).y();
|
||||
if (y < ret)
|
||||
ret = y;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
qreal GraphItem::avg() const
|
||||
{
|
||||
qreal sum = 0;
|
||||
|
||||
for (int i = 0; i < _graph.size(); i++) {
|
||||
const GraphSegment &segment = _graph.at(i);
|
||||
|
||||
for (int j = 1; j < segment.size(); j++)
|
||||
sum += segment.at(j).y() * (segment.at(j).s() - segment.at(j-1).s());
|
||||
}
|
||||
|
||||
return sum/_graph.last().last().s();
|
||||
}
|
||||
|
||||
void GraphItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
|
||||
{
|
||||
Q_UNUSED(event);
|
||||
|
Reference in New Issue
Block a user