Use QGraphicsObject's signalling to enable/disable MapRuler

This commit is contained in:
BigBahss 2020-11-17 16:49:34 -05:00 committed by huderlem
parent bc0127430b
commit 6ca4802948
2 changed files with 20 additions and 20 deletions

View file

@ -10,24 +10,13 @@ class MapRuler : public QGraphicsObject, private QLine
Q_OBJECT Q_OBJECT
public: public:
MapRuler(QColor innerColor = Qt::yellow, QColor borderColor = Qt::black) : 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)
{ }
QRectF boundingRect() const override; QRectF boundingRect() const override;
QPainterPath shape() const override; QPainterPath shape() const override;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) override; void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) override;
bool eventFilter(QObject *, QEvent *event) override; bool eventFilter(QObject *, QEvent *event) override;
void setEnabled(bool enabled);
bool isAnchored() const { return anchored; } bool isAnchored() const { return anchored; }
bool isLocked() const { return locked; } bool isLocked() const { return locked; }

View file

@ -8,6 +8,23 @@
int MapRuler::thickness = 3; 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 { QRectF MapRuler::boundingRect() const {
return QRectF(-thickness, -thickness, pixWidth() + thickness * 2, pixHeight() + thickness * 2); return QRectF(-thickness, -thickness, pixWidth() + thickness * 2, pixHeight() + thickness * 2);
} }
@ -18,9 +35,9 @@ QPainterPath MapRuler::shape() const {
ruler.addRect(xRuler); ruler.addRect(xRuler);
ruler.addRect(yRuler); ruler.addRect(yRuler);
ruler = ruler.simplified(); 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); 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); ruler.addRect(yRuler.x(), y, thickness, 0);
return ruler; return ruler;
} }
@ -65,12 +82,6 @@ void MapRuler::setMapDimensions(const QSize &size) {
init(); init();
} }
void MapRuler::setEnabled(bool enabled) {
QGraphicsItem::setEnabled(enabled);
if (!enabled && anchored)
init();
}
void MapRuler::init() { void MapRuler::init() {
prepareGeometryChange(); prepareGeometryChange();
hide(); hide();