lines-grid-editor/gridpreview.h
2025-07-02 16:58:22 +03:00

79 lines
2 KiB
C++

#ifndef GRIDPREVIEW_H
#define GRIDPREVIEW_H
#include <QWidget>
#include <QPainter>
#include <QMouseEvent>
#include <iostream>
#include "line.h"
struct PointProbe
{
int ownerIndex = -1;
bool isStart = true;
bool inBounds = true;
PointProbe() {}
PointProbe(int owner, bool start = true, bool bounds = true):
ownerIndex(owner),
isStart(start),
inBounds(bounds) {}
};
class GridPreview : public QWidget
{
Q_OBJECT
QMap<QString, QColor> colorMap;
int width = 5, height = 5;
QVector<Line> lines {};
const QString tableTemplate = "return {\n width = %1,\n height = %2,\n lines = {\n%3\n }\n}";
bool mouseCaptured = false;
PointProbe capturedProbe, lastProbe;
public:
explicit GridPreview(QWidget *parent = nullptr);
void setSize(int newWidth, int newHeight);
void addLine(Line &line);
void removeLine(int index);
void setLineColor(int index, const QString &color);
void setLineStartPoint(int index, const QPoint &point);
void setLineEndPoint(int index, const QPoint &point);
void setColorMap(QMap<QString, QColor> &map);
int getWidth();
int getHeight();
const Line& getLine(int index) const;
const PointProbe& getCapturedProbe() const;
PointProbe& getCapturedProbe();
const QList<Line>& getLines() const;
void clear();
const QString toLuaTable();
void paintEvent(QPaintEvent* event);
const QColor colorFromString(const QString color);
void mouseMoveEvent(QMouseEvent* event);
void mousePressEvent(QMouseEvent* event);
void mouseReleaseEvent(QMouseEvent* event);
const QPoint localMousePosition(QMouseEvent* event);
const QPoint snapToGrid(const QPoint& global);
bool isPointOccupied(const QPoint& point);
bool pointInBounds(const QPoint& point);
bool pointIsFree(PointProbe &pp);
int getPointOwner(const QPoint& point);
const PointProbe probePoint(const QPoint& point);
const QList<PointProbe> pointsOutOfBounds();
signals:
void updatedContents();
};
#endif // GRIDPREVIEW_H