2020-09-25 16:53:07 +01:00
|
|
|
#ifndef MAPRULER_H
|
|
|
|
#define MAPRULER_H
|
|
|
|
|
2020-10-24 12:45:08 +01:00
|
|
|
#include <QGraphicsObject>
|
2020-10-26 03:49:00 +00:00
|
|
|
#include <QLine>
|
2020-09-25 16:53:07 +01:00
|
|
|
|
2021-02-18 00:20:14 +00:00
|
|
|
class MapRuler : public QGraphicsObject, private QLine {
|
2020-10-24 12:45:08 +01:00
|
|
|
Q_OBJECT
|
|
|
|
|
2020-09-25 16:53:07 +01:00
|
|
|
public:
|
2020-11-19 05:55:22 +00:00
|
|
|
// thickness is given in scene pixels
|
|
|
|
MapRuler(int thickness, QColor innerColor = Qt::yellow, QColor borderColor = Qt::black);
|
2020-10-26 03:49:00 +00:00
|
|
|
|
2020-09-25 16:53:07 +01:00
|
|
|
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;
|
2020-09-25 16:53:07 +01:00
|
|
|
|
2021-02-18 00:20:14 +00:00
|
|
|
bool isAnchored() const {
|
|
|
|
return anchored;
|
|
|
|
}
|
|
|
|
bool isLocked() const {
|
|
|
|
return locked;
|
|
|
|
}
|
2020-10-02 17:12:19 +01:00
|
|
|
|
2020-09-25 16:53:07 +01:00
|
|
|
// Ruler start point in metatiles
|
2021-02-18 00:20:14 +00:00
|
|
|
QPoint anchor() const {
|
|
|
|
return QLine::p1();
|
|
|
|
}
|
2020-09-25 16:53:07 +01:00
|
|
|
// 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());
|
|
|
|
}
|
2020-09-25 16:53:07 +01:00
|
|
|
// Horizontal component of the ruler in metatiles
|
2021-02-18 00:20:14 +00:00
|
|
|
int deltaX() const {
|
|
|
|
return QLine::dx();
|
|
|
|
}
|
2020-09-25 16:53:07 +01:00
|
|
|
// Vertical component of the ruler in metatiles
|
2021-02-18 00:20:14 +00:00
|
|
|
int deltaY() const {
|
|
|
|
return QLine::dy();
|
|
|
|
}
|
2020-09-25 16:53:07 +01:00
|
|
|
// Ruler width in metatiles
|
2021-02-18 00:20:14 +00:00
|
|
|
int width() const {
|
|
|
|
return qAbs(deltaX());
|
|
|
|
}
|
2020-09-25 16:53:07 +01:00
|
|
|
// Ruler height in metatiles
|
2021-02-18 00:20:14 +00:00
|
|
|
int height() const {
|
|
|
|
return qAbs(deltaY());
|
|
|
|
}
|
2020-10-02 17:12:19 +01:00
|
|
|
|
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
|
|
|
|
2020-10-26 03:49:00 +00:00
|
|
|
signals:
|
2021-02-18 00:20:14 +00:00
|
|
|
void statusChanged(const QString& statusMessage);
|
2020-10-26 03:49:00 +00:00
|
|
|
|
2020-09-25 16:53:07 +01:00
|
|
|
private:
|
2020-11-19 05:55:22 +00:00
|
|
|
const int thickness;
|
|
|
|
const qreal half_thickness;
|
|
|
|
const QColor innerColor;
|
|
|
|
const QColor borderColor;
|
2020-10-24 12:45:08 +01:00
|
|
|
QSize mapSize;
|
2020-11-19 05:55:22 +00:00
|
|
|
QRectF xRuler;
|
|
|
|
QRectF yRuler;
|
2020-09-25 16:56:02 +01:00
|
|
|
QLineF cornerTick;
|
2020-10-02 17:12:19 +01:00
|
|
|
bool anchored;
|
2020-10-24 12:45:08 +01:00
|
|
|
bool locked;
|
2020-09-25 16:53:07 +01:00
|
|
|
|
2020-11-19 05:55:22 +00:00
|
|
|
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;
|
2020-10-02 17:12:19 +01:00
|
|
|
void updateGeometry();
|
2020-11-19 05:55:22 +00:00
|
|
|
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;
|
|
|
|
}
|
2020-09-25 16:53:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // MAPRULER_H
|