porymap/include/ui/mapruler.h

93 lines
2.3 KiB
C
Raw Normal View History

#ifndef MAPRULER_H
#define MAPRULER_H
2020-10-24 12:45:08 +01:00
#include <QGraphicsObject>
#include <QLine>
2021-02-18 00:20:14 +00:00
class MapRuler : public QGraphicsObject, private QLine {
2020-10-24 12:45:08 +01:00
Q_OBJECT
public:
// thickness is given in scene pixels
MapRuler(int thickness, QColor innerColor = Qt::yellow, QColor borderColor = Qt::black);
QRectF boundingRect() const override;
QPainterPath shape() const override;
2021-02-18 00:20:14 +00:00
void paint(QPainter* painter, const QStyleOptionGraphicsItem*, QWidget*) override;
bool eventFilter(QObject*, QEvent* event) override;
2021-02-18 00:20:14 +00:00
bool isAnchored() const {
return anchored;
}
bool isLocked() const {
return locked;
}
// Ruler start point in metatiles
2021-02-18 00:20:14 +00:00
QPoint anchor() const {
return QLine::p1();
}
// Ruler end point in metatiles
2021-02-18 00:20:14 +00:00
QPoint endPos() const {
return QLine::p2();
}
2020-10-24 12:45:08 +01:00
// X-coordinate of the ruler's left edge in metatiles
2021-02-18 00:20:14 +00:00
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
2021-02-18 00:20:14 +00:00
int top() const {
return qMin(anchor().y(), endPos().y());
}
// Horizontal component of the ruler in metatiles
2021-02-18 00:20:14 +00:00
int deltaX() const {
return QLine::dx();
}
// Vertical component of the ruler in metatiles
2021-02-18 00:20:14 +00:00
int deltaY() const {
return QLine::dy();
}
// Ruler width in metatiles
2021-02-18 00:20:14 +00:00
int width() const {
return qAbs(deltaX());
}
// Ruler height in metatiles
2021-02-18 00:20:14 +00:00
int height() const {
return qAbs(deltaY());
}
2020-10-24 12:45:08 +01:00
public slots:
2021-02-18 00:20:14 +00:00
void mouseEvent(QGraphicsSceneMouseEvent* event);
void setMapDimensions(const QSize& size);
2020-10-24 12:45:08 +01:00
signals:
2021-02-18 00:20:14 +00:00
void statusChanged(const QString& statusMessage);
private:
const int thickness;
const qreal half_thickness;
const QColor innerColor;
const QColor borderColor;
2020-10-24 12:45:08 +01:00
QSize mapSize;
QRectF xRuler;
QRectF yRuler;
2020-09-25 16:56:02 +01:00
QLineF cornerTick;
bool anchored;
2020-10-24 12:45:08 +01:00
bool locked;
void reset();
2021-02-18 00:20:14 +00:00
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();
void updateStatus(Qt::Corner corner);
2021-02-18 00:20:14 +00:00
int pixWidth() const {
return width() * 16;
}
int pixHeight() const {
return height() * 16;
}
};
#endif // MAPRULER_H