Changed ints to QPoint and basic line creation
This commit is contained in:
parent
bcd01a95b3
commit
4ae8031e88
4 changed files with 58 additions and 43 deletions
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue