From 48674bb50b94020525d56ac0359d0ff87648eb80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20T=C5=AFma?= Date: Tue, 28 May 2019 07:24:39 +0200 Subject: [PATCH] Propper path tick tooltip + code cleanup --- src/GUI/pathitem.cpp | 13 ++++--------- src/GUI/pathtickitem.cpp | 15 +++++++++++++++ src/GUI/pathtickitem.h | 3 ++- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/GUI/pathitem.cpp b/src/GUI/pathitem.cpp index d1e888b4..3cbea450 100644 --- a/src/GUI/pathitem.cpp +++ b/src/GUI/pathitem.cpp @@ -325,20 +325,15 @@ void PathItem::updateTicks() return; int ts = tickSize(); - qreal f = xInM(); - int tc = (int)_path.last().last().distance() / (ts * f); - - QFontMetrics fm(PathTickItem::font()); - QRect tr = fm.boundingRect(QRect(), Qt::AlignCenter, - QString::number(qMax(ts * (tc - 1), 10))) - .adjusted(-2, 0, 2, 0); + int tc = _path.last().last().distance() / (ts * xInM()); + QRect tr = PathTickItem::tickRect(ts * (tc - 1)); _ticks.resize(tc); for (int i = 0; i < tc; i++) { - QPoint pos(position((i + 1) * ts * xInM()).toPoint()); _ticks[i] = new PathTickItem(tr, (i + 1) * ts, this); - _ticks[i]->setPos(QPointF(pos.x() - 0.5, pos.y() - 0.5)); + _ticks[i]->setPos(position((i + 1) * ts * xInM())); _ticks[i]->setColor(_pen.color()); + _ticks[i]->setToolTip(toolTip()); } } diff --git a/src/GUI/pathtickitem.cpp b/src/GUI/pathtickitem.cpp index 46f7c119..4c5c4b51 100644 --- a/src/GUI/pathtickitem.cpp +++ b/src/GUI/pathtickitem.cpp @@ -54,3 +54,18 @@ void PathTickItem::paint(QPainter *painter, painter->drawRect(boundingRect()); */ } + +void PathTickItem::setPos(const QPointF &pos) +{ + /* For propper rounded rect rendering, the item must be positioned in the + middle of a pixel */ + QPoint p(pos.toPoint()); + QGraphicsItem::setPos(QPointF(p.x() - 0.5, p.y() - 0.5)); +} + +QRect PathTickItem::tickRect(int value) +{ + QFontMetrics fm(_font); + return fm.boundingRect(QRect(), Qt::AlignCenter, + QString::number(qMax(value, 10))).adjusted(-2, 0, 2, 0); +} diff --git a/src/GUI/pathtickitem.h b/src/GUI/pathtickitem.h index 9c428a36..1deb4a7e 100644 --- a/src/GUI/pathtickitem.h +++ b/src/GUI/pathtickitem.h @@ -13,9 +13,10 @@ public: void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); + void setPos(const QPointF &pos); void setColor(const QColor &color) {_brush = QBrush(color);} - static const QFont &font() {return _font;} + static QRect tickRect(int value); private: QRectF _tickRect;