porymap/include/ui/mapruler.h

70 lines
2 KiB
C
Raw Normal View History

#ifndef MAPRULER_H
#define MAPRULER_H
2020-10-24 12:45:08 +01:00
#include <QGraphicsObject>
#include <QLine>
2020-10-24 12:45:08 +01:00
class MapRuler : public QGraphicsObject, private QLine
{
2020-10-24 12:45:08 +01:00
Q_OBJECT
public:
MapRuler(QColor innerColor = Qt::yellow, QColor borderColor = Qt::black);
QRectF boundingRect() const override;
QPainterPath shape() const override;
2020-10-24 12:45:08 +01:00
void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) override;
bool eventFilter(QObject *, QEvent *event) override;
2020-10-24 12:45:08 +01:00
bool isAnchored() const { return anchored; }
bool isLocked() const { return locked; }
// Ruler start point in metatiles
QPoint anchor() const { return QLine::p1(); }
// Ruler end point in metatiles
QPoint endPos() const { return QLine::p2(); }
2020-10-24 12:45:08 +01:00
// X-coordinate of the ruler's left edge in metatiles
int left() const { return qMin(anchor().x(), endPos().x()); }
2020-10-24 12:45:08 +01:00
// Y-coordinate of the ruler's top edge in metatiles
int top() const { return qMin(anchor().y(), endPos().y()); }
// Horizontal component of the ruler in metatiles
int deltaX() const { return QLine::dx(); }
// Vertical component of the ruler in metatiles
int deltaY() const { return QLine::dy(); }
// Ruler width in metatiles
int width() const { return qAbs(deltaX()); }
// Ruler height in metatiles
int height() const { return qAbs(deltaY()); }
2020-10-24 12:45:08 +01:00
public slots:
void mouseEvent(QGraphicsSceneMouseEvent *event);
void setMapDimensions(const QSize &size);
signals:
void statusChanged(const QString &statusMessage);
private:
QColor innerColor;
QColor borderColor;
2020-10-24 12:45:08 +01:00
QSize mapSize;
QString statusMessage;
QRect xRuler;
QRect yRuler;
2020-09-25 16:56:02 +01:00
QLineF cornerTick;
bool anchored;
2020-10-24 12:45:08 +01:00
bool locked;
static int thickness;
2020-10-24 12:45:08 +01:00
void init();
void setAnchor(const QPointF &scenePos);
void setEndPos(const QPointF &scenePos);
2020-10-24 12:45:08 +01:00
QPoint snapToWithinBounds(QPoint pos) const;
void updateGeometry();
2020-10-24 12:45:08 +01:00
int pixWidth() const { return width() * 16; }
int pixHeight() const { return height() * 16; }
};
#endif // MAPRULER_H