porymap/include/ui/mapruler.h

82 lines
2.4 KiB
C
Raw Normal View History

#ifndef MAPRULER_H
#define MAPRULER_H
2020-10-24 12:45:08 +01:00
#include <QGraphicsObject>
#include <QPainter>
#include <QColor>
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) :
innerColor(innerColor),
2020-10-24 12:45:08 +01:00
borderColor(borderColor),
mapSize(QSize())
{
init();
}
void init();
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
void setEnabled(bool enabled);
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()); }
QString statusMessage;
2020-10-24 12:45:08 +01:00
public slots:
void mouseEvent(QGraphicsSceneMouseEvent *event);
void setMapDimensions(const QSize &size);
private:
QColor innerColor;
QColor borderColor;
2020-10-24 12:45:08 +01:00
QSize mapSize;
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
QPoint snapToWithinBounds(QPoint pos) const;
void setAnchor(const QPointF &scenePos, const QPoint &screenPos);
void endAnchor();
void setEndPos(const QPointF &scenePos, const QPoint &screenPos);
void showDimensions(const QPoint &screenPos) const;
void hideDimensions() const;
void updateGeometry();
2020-10-24 12:45:08 +01:00
int pixWidth() const { return width() * 16; }
int pixHeight() const { return height() * 16; }
signals:
void lengthChanged();
void deactivated(const QPoint &endPos);
};
#endif // MAPRULER_H