From 4ae8031e887988557aede9c5c2aeeb03f35c93b6 Mon Sep 17 00:00:00 2001 From: 2ndbeam <2ndbeam@disroot.org> Date: Fri, 27 Jun 2025 18:29:22 +0300 Subject: [PATCH] Changed ints to QPoint and basic line creation --- gridpreview.cpp | 47 ++++++++++++++++++++++++++++++++++++----------- gridpreview.h | 9 ++++++--- line.cpp | 30 +++++++++--------------------- line.h | 15 +++++++-------- 4 files changed, 58 insertions(+), 43 deletions(-) diff --git a/gridpreview.cpp b/gridpreview.cpp index 8aee464..1257bc9 100644 --- a/gridpreview.cpp +++ b/gridpreview.cpp @@ -41,17 +41,17 @@ void GridPreview::setLineColor(int index, QString color) emit updatedContents(); } -void GridPreview::setLineStartPoint(int index, int x, int y) +void GridPreview::setLineStartPoint(int index, QPoint point) { - lines[index].setStartPoint(x, y); + lines[index].setStartPoint(point); update(); emit updatedContents(); } -void GridPreview::setLineEndPoint(int index, int x, int y) +void GridPreview::setLineEndPoint(int index, QPoint point) { - lines[index].setEndPoint(x, y); + lines[index].setEndPoint(point); update(); emit updatedContents(); @@ -136,16 +136,16 @@ void GridPreview::paintEvent(QPaintEvent *) painter.setBrush(brush); // Draw start - cell.setLeft(offsetX + (cellSize - filledDotSize) / 2 + (iter->getStartX() - 1) * cellSize); + cell.setLeft(offsetX + (cellSize - filledDotSize) / 2 + (iter->getStart().x() - 1) * cellSize); cell.setWidth(filledDotSize); - cell.setTop(offsetY + (cellSize - filledDotSize) / 2 + (iter->getStartY() - 1) * cellSize); + cell.setTop(offsetY + (cellSize - filledDotSize) / 2 + (iter->getStart().y() - 1) * cellSize); cell.setHeight(filledDotSize); painter.drawEllipse(cell); // Draw end - cell.setLeft(offsetX + (cellSize - filledDotSize) / 2 + (iter->getEndX() - 1) * cellSize); + cell.setLeft(offsetX + (cellSize - filledDotSize) / 2 + (iter->getEnd().x() - 1) * cellSize); cell.setWidth(filledDotSize); - cell.setTop(offsetY + (cellSize - filledDotSize) / 2 + (iter->getEndY() - 1) * cellSize); + cell.setTop(offsetY + (cellSize - filledDotSize) / 2 + (iter->getEnd().y() - 1) * cellSize); cell.setHeight(filledDotSize); painter.drawEllipse(cell); } @@ -172,6 +172,14 @@ void GridPreview::mousePressEvent(QMouseEvent *event) QPoint gridPos = localMousePosition(event); std::cout << gridPos.x() << ',' << gridPos.y() << std::endl; + + if (!isPointOccupied(gridPos) && pointInBounds(gridPos)) + { + Line line; + line.setStartPoint(gridPos); + line.setEndPoint(gridPos); + addLine(line); + } } void GridPreview::mouseReleaseEvent(QMouseEvent *event) @@ -182,10 +190,10 @@ void GridPreview::mouseReleaseEvent(QMouseEvent *event) const QPoint GridPreview::localMousePosition(QMouseEvent *event) { QPoint position(event->position().toPoint()); - return snapToGrid(position.x(), position.y()); + return snapToGrid(position); } -const QPoint GridPreview::snapToGrid(int x, int y) +const QPoint GridPreview::snapToGrid(const QPoint &global) { double aspectRatio = (double) rect().width() / rect().height(); double cellAspectRatio = (double) width / height; @@ -200,5 +208,22 @@ const QPoint GridPreview::snapToGrid(int x, int y) int offsetX = (rect().width() - areaWidth) / 2; int offsetY = (rect().height() - areaHeight) / 2; - return QPoint(1 + (x - offsetX) / cellSize, 1 + (y - offsetY) / cellSize); + return QPoint(1 + (global.x() - offsetX) / cellSize, 1 + (global.y() - offsetY) / cellSize); +} + +double GridPreview::isPointOccupied(const QPoint &point) +{ + for (auto iter = lines.begin(); iter < lines.end(); iter++) + { + if (iter->getStart() == point || iter->getEnd() == point) + { + return true; + } + } + return false; +} + +double GridPreview::pointInBounds(const QPoint &point) +{ + return point.x() > 0 && point.y() > 0 && point.x() <= width && point.y() <= height; } diff --git a/gridpreview.h b/gridpreview.h index 7b65b55..081f75f 100644 --- a/gridpreview.h +++ b/gridpreview.h @@ -24,8 +24,8 @@ public: void removeLine(int index); void setLineColor(int index, QString color); - void setLineStartPoint(int index, int x, int y); - void setLineEndPoint(int index, int x, int y); + void setLineStartPoint(int index, QPoint point); + void setLineEndPoint(int index, QPoint point); int getWidth(); int getHeight(); @@ -40,7 +40,10 @@ public: void mouseReleaseEvent(QMouseEvent* event); const QPoint localMousePosition(QMouseEvent* event); - const QPoint snapToGrid(int x, int y); + const QPoint snapToGrid(const QPoint& global); + + double isPointOccupied(const QPoint& point); + double pointInBounds(const QPoint& point); signals: void updatedContents(); }; diff --git a/line.cpp b/line.cpp index c62eda2..6f4c942 100644 --- a/line.cpp +++ b/line.cpp @@ -2,16 +2,14 @@ Line::Line() {} -void Line::setStartPoint(int x, int y) +void Line::setStartPoint(QPoint newPoint) { - startX = x; - startY = y; + start = newPoint; } -void Line::setEndPoint(int x, int y) +void Line::setEndPoint(QPoint newPoint) { - endX = x; - endY = y; + end = newPoint; } void Line::setColor(QString newColor) @@ -19,24 +17,14 @@ void Line::setColor(QString newColor) color = newColor; } -int Line::getStartX() +const QPoint &Line::getStart() { - return startX; + return start; } -int Line::getStartY() +const QPoint &Line::getEnd() { - return startY; -} - -int Line::getEndX() -{ - return endX; -} - -int Line::getEndY() -{ - return endY; + return start; } QString Line::getColor() @@ -46,5 +34,5 @@ QString Line::getColor() QString Line::toLuaTable() { - return QString("{ %1, %2, %3, %4, \"%5\" }").arg(startX).arg(startY).arg(endX).arg(endY).arg(color); + return QString("{ %1, %2, %3, %4, \"%5\" }").arg(start.x()).arg(start.y()).arg(end.x()).arg(end.y()).arg(color); } diff --git a/line.h b/line.h index 72d9d45..2d25509 100644 --- a/line.h +++ b/line.h @@ -1,22 +1,21 @@ #ifndef LINE_H #define LINE_H +#include #include class Line { private: - int startX, startY, endX, endY; - QString color; + QPoint start, end; + QString color = QString("red"); public: Line(); - void setStartPoint(int x, int y); - void setEndPoint(int x, int y); + void setStartPoint(QPoint newPoint); + void setEndPoint(QPoint newPoint); void setColor(QString color); - int getStartX(); - int getStartY(); - int getEndX(); - int getEndY(); + const QPoint& getStart(); + const QPoint& getEnd(); QString getColor(); QString toLuaTable(); };