From 6ca4802948720616fda162b9e8172a5a4e38e241 Mon Sep 17 00:00:00 2001 From: BigBahss Date: Tue, 17 Nov 2020 16:49:34 -0500 Subject: [PATCH] Use QGraphicsObject's signalling to enable/disable MapRuler --- include/ui/mapruler.h | 13 +------------ src/ui/mapruler.cpp | 27 +++++++++++++++++++-------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/include/ui/mapruler.h b/include/ui/mapruler.h index 650ecd65..c9531703 100644 --- a/include/ui/mapruler.h +++ b/include/ui/mapruler.h @@ -10,24 +10,13 @@ class MapRuler : public QGraphicsObject, private QLine Q_OBJECT public: - MapRuler(QColor innerColor = Qt::yellow, QColor borderColor = Qt::black) : - innerColor(innerColor), - borderColor(borderColor), - mapSize(QSize()), - statusMessage(QString()), - xRuler(QRect()), - yRuler(QRect()), - cornerTick(QLine()), - anchored(false), - locked(false) - { } + MapRuler(QColor innerColor = Qt::yellow, QColor borderColor = Qt::black); QRectF boundingRect() const override; QPainterPath shape() const override; void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) override; bool eventFilter(QObject *, QEvent *event) override; - void setEnabled(bool enabled); bool isAnchored() const { return anchored; } bool isLocked() const { return locked; } diff --git a/src/ui/mapruler.cpp b/src/ui/mapruler.cpp index 1f4bcc36..77f6322b 100644 --- a/src/ui/mapruler.cpp +++ b/src/ui/mapruler.cpp @@ -8,6 +8,23 @@ int MapRuler::thickness = 3; +MapRuler::MapRuler(QColor innerColor, QColor borderColor) : + innerColor(innerColor), + borderColor(borderColor), + mapSize(QSize()), + statusMessage(QString()), + xRuler(QRect()), + yRuler(QRect()), + cornerTick(QLine()), + anchored(false), + locked(false) +{ + connect(this, &QGraphicsObject::enabledChanged, [this]() { + if (!isEnabled() && anchored) + init(); + }); +} + QRectF MapRuler::boundingRect() const { return QRectF(-thickness, -thickness, pixWidth() + thickness * 2, pixHeight() + thickness * 2); } @@ -18,9 +35,9 @@ QPainterPath MapRuler::shape() const { ruler.addRect(xRuler); ruler.addRect(yRuler); ruler = ruler.simplified(); - for (int x = 17.5; x < pixWidth(); x += 16) + for (int x = 17; x < pixWidth(); x += 16) ruler.addRect(x, xRuler.y(), 0, thickness); - for (int y = 17.5; y < pixHeight(); y += 16) + for (int y = 17; y < pixHeight(); y += 16) ruler.addRect(yRuler.x(), y, thickness, 0); return ruler; } @@ -65,12 +82,6 @@ void MapRuler::setMapDimensions(const QSize &size) { init(); } -void MapRuler::setEnabled(bool enabled) { - QGraphicsItem::setEnabled(enabled); - if (!enabled && anchored) - init(); -} - void MapRuler::init() { prepareGeometryChange(); hide();