Changed ints to QPoint and basic line creation

This commit is contained in:
Alexey 2025-06-27 18:29:22 +03:00
commit 4ae8031e88
4 changed files with 58 additions and 43 deletions

View file

@ -41,17 +41,17 @@ void GridPreview::setLineColor(int index, QString color)
emit updatedContents(); 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(); update();
emit updatedContents(); 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(); update();
emit updatedContents(); emit updatedContents();
@ -136,16 +136,16 @@ void GridPreview::paintEvent(QPaintEvent *)
painter.setBrush(brush); painter.setBrush(brush);
// Draw start // 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.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); cell.setHeight(filledDotSize);
painter.drawEllipse(cell); painter.drawEllipse(cell);
// Draw end // 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.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); cell.setHeight(filledDotSize);
painter.drawEllipse(cell); painter.drawEllipse(cell);
} }
@ -172,6 +172,14 @@ void GridPreview::mousePressEvent(QMouseEvent *event)
QPoint gridPos = localMousePosition(event); QPoint gridPos = localMousePosition(event);
std::cout << gridPos.x() << ',' << gridPos.y() << std::endl; 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) void GridPreview::mouseReleaseEvent(QMouseEvent *event)
@ -182,10 +190,10 @@ void GridPreview::mouseReleaseEvent(QMouseEvent *event)
const QPoint GridPreview::localMousePosition(QMouseEvent *event) const QPoint GridPreview::localMousePosition(QMouseEvent *event)
{ {
QPoint position(event->position().toPoint()); 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 aspectRatio = (double) rect().width() / rect().height();
double cellAspectRatio = (double) width / 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 offsetX = (rect().width() - areaWidth) / 2;
int offsetY = (rect().height() - areaHeight) / 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;
} }

View file

@ -24,8 +24,8 @@ public:
void removeLine(int index); void removeLine(int index);
void setLineColor(int index, QString color); void setLineColor(int index, QString color);
void setLineStartPoint(int index, int x, int y); void setLineStartPoint(int index, QPoint point);
void setLineEndPoint(int index, int x, int y); void setLineEndPoint(int index, QPoint point);
int getWidth(); int getWidth();
int getHeight(); int getHeight();
@ -40,7 +40,10 @@ public:
void mouseReleaseEvent(QMouseEvent* event); void mouseReleaseEvent(QMouseEvent* event);
const QPoint localMousePosition(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: signals:
void updatedContents(); void updatedContents();
}; };

View file

@ -2,16 +2,14 @@
Line::Line() {} Line::Line() {}
void Line::setStartPoint(int x, int y) void Line::setStartPoint(QPoint newPoint)
{ {
startX = x; start = newPoint;
startY = y;
} }
void Line::setEndPoint(int x, int y) void Line::setEndPoint(QPoint newPoint)
{ {
endX = x; end = newPoint;
endY = y;
} }
void Line::setColor(QString newColor) void Line::setColor(QString newColor)
@ -19,24 +17,14 @@ void Line::setColor(QString newColor)
color = newColor; color = newColor;
} }
int Line::getStartX() const QPoint &Line::getStart()
{ {
return startX; return start;
} }
int Line::getStartY() const QPoint &Line::getEnd()
{ {
return startY; return start;
}
int Line::getEndX()
{
return endX;
}
int Line::getEndY()
{
return endY;
} }
QString Line::getColor() QString Line::getColor()
@ -46,5 +34,5 @@ QString Line::getColor()
QString Line::toLuaTable() 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);
} }

15
line.h
View file

@ -1,22 +1,21 @@
#ifndef LINE_H #ifndef LINE_H
#define LINE_H #define LINE_H
#include <QPoint>
#include <QString> #include <QString>
class Line class Line
{ {
private: private:
int startX, startY, endX, endY; QPoint start, end;
QString color; QString color = QString("red");
public: public:
Line(); Line();
void setStartPoint(int x, int y); void setStartPoint(QPoint newPoint);
void setEndPoint(int x, int y); void setEndPoint(QPoint newPoint);
void setColor(QString color); void setColor(QString color);
int getStartX(); const QPoint& getStart();
int getStartY(); const QPoint& getEnd();
int getEndX();
int getEndY();
QString getColor(); QString getColor();
QString toLuaTable(); QString toLuaTable();
}; };