1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-10-06 14:53:21 +02:00

km/mi markers polishing

This commit is contained in:
Martin Tůma 2019-05-23 19:19:07 +02:00
parent c688b5fc09
commit 8bec307e03
2 changed files with 27 additions and 16 deletions

View File

@ -46,6 +46,7 @@ PathItem::PathItem(const Path &path, Map *map, QGraphicsItem *parent)
updatePainterPath(); updatePainterPath();
updateShape(); updateShape();
updateTickRects();
_markerDistance = _path.first().first().distance(); _markerDistance = _path.first().first().distance();
_marker = new MarkerItem(this); _marker = new MarkerItem(this);
@ -53,8 +54,6 @@ PathItem::PathItem(const Path &path, Map *map, QGraphicsItem *parent)
setCursor(Qt::ArrowCursor); setCursor(Qt::ArrowCursor);
setAcceptHoverEvents(true); setAcceptHoverEvents(true);
computeTickInfo();
} }
void PathItem::updateShape() void PathItem::updateShape()
@ -132,17 +131,19 @@ void PathItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
painter->setRenderHint(QPainter::Antialiasing, false); painter->setRenderHint(QPainter::Antialiasing, false);
for (int i = 1; i < _tickCount; i++) { for (int i = 1; i < _tickCount; i++) {
QPointF pos(position(i * _tickSize * xInM())); QPoint pos(position(i * _tickSize * xInM()).toPoint());
QPointF arrow[3] = {pos, QPointF(pos.x() + 3, pos.y() - 3), QPointF arrow[3] = {QPointF(pos.x() - 0.5, pos.y()),
QPointF(pos.x() - 3, pos.y() - 3)}; QPointF(pos.x() + 2.5, pos.y() - 3),
QPointF(pos.x() - 3.5, pos.y() - 3)};
QString val(QString::number(i * _tickSize)); QString val(QString::number(i * _tickSize));
QRect br(_tickRect); QRectF br(_tickRect);
br.moveCenter(QPoint(pos.x(), pos.y() - br.height()/2 - 5)); br.moveCenter(QPointF(pos.x() - 0.5, pos.y() - br.height()/2.0
- 2.5));
painter->setPen(Qt::white); painter->setPen(Qt::white);
painter->setBrush(_pen.color()); painter->setBrush(_pen.color());
painter->drawPolygon(arrow, 3); painter->drawPolygon(arrow, 3);
painter->drawRoundedRect(br, 2, 2); painter->drawRoundedRect(br, 1.5, 1.5);
painter->drawText(br, Qt::AlignCenter, val); painter->drawText(br, Qt::AlignCenter, val);
} }
} }
@ -162,6 +163,7 @@ void PathItem::setMap(Map *map)
updatePainterPath(); updatePainterPath();
updateShape(); updateShape();
updateTickRects();
QPointF pos = position(_markerDistance); QPointF pos = position(_markerDistance);
if (isValid(pos)) if (isValid(pos))
@ -332,15 +334,15 @@ unsigned PathItem::tickSize() const
if (r < 1) if (r < 1)
return 0; return 0;
else if (r < 10) else if (r < 15)
return 1; return 1;
else if (r < 50) else if (r < 50)
return 5; return 5;
else if (r < 100) else if (r < 150)
return 10; return 10;
else if (r < 500) else if (r < 500)
return 50; return 50;
else if (r < 1000) else if (r < 1500)
return 100; return 100;
else if (r < 5000) else if (r < 5000)
return 500; return 500;
@ -348,7 +350,7 @@ unsigned PathItem::tickSize() const
return 1000; return 1000;
} }
void PathItem::computeTickInfo() void PathItem::updateTickRects()
{ {
_tickSize = tickSize(); _tickSize = tickSize();
qreal f = xInM(); qreal f = xInM();
@ -358,6 +360,15 @@ void PathItem::computeTickInfo()
_tickRect = fm.boundingRect(QRect(), Qt::AlignCenter, _tickRect = fm.boundingRect(QRect(), Qt::AlignCenter,
QString::number(qMax(_tickSize * (_tickCount - 1), 10))) QString::number(qMax(_tickSize * (_tickCount - 1), 10)))
.adjusted(-2, 0, 2, 0); .adjusted(-2, 0, 2, 0);
_tickBoundingRect = QRectF();
for (int i = 1; i < _tickCount; i++) {
QPoint pos(position(i * _tickSize * xInM()).toPoint());
QRectF br(_tickRect);
br.moveCenter(QPointF(pos.x() - 0.5, pos.y() - br.height()/2.0
- 2.5));
_tickBoundingRect |= br;
}
} }
void PathItem::showTicks(bool show) void PathItem::showTicks(bool show)
@ -376,7 +387,7 @@ void PathItem::setUnits(Units units)
prepareGeometryChange(); prepareGeometryChange();
_units = units; _units = units;
computeTickInfo(); updateTickRects();
} }
void PathItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) void PathItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
@ -404,7 +415,6 @@ void PathItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
QRectF PathItem::boundingRect() const QRectF PathItem::boundingRect() const
{ {
return _showTicks return _showTicks
? _shape.boundingRect().adjusted(-_tickRect.width()/2, ? _shape.boundingRect() | _tickBoundingRect
-(_tickRect.height() + 4), _tickRect.width()/2, 0)
: _shape.boundingRect(); : _shape.boundingRect();
} }

View File

@ -56,7 +56,7 @@ private:
qreal xInM() const; qreal xInM() const;
unsigned tickSize() const; unsigned tickSize() const;
void computeTickInfo(); void updateTickRects();
void hoverEnterEvent(QGraphicsSceneHoverEvent *event); void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event); void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
@ -73,6 +73,7 @@ private:
bool _showMarker; bool _showMarker;
bool _showTicks; bool _showTicks;
QRect _tickRect; QRect _tickRect;
QRectF _tickBoundingRect;
int _tickSize, _tickCount; int _tickSize, _tickCount;
static QFont _font; static QFont _font;