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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
};
|
||||
|
|
|
|||
30
line.cpp
30
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);
|
||||
}
|
||||
|
|
|
|||
15
line.h
15
line.h
|
|
@ -1,22 +1,21 @@
|
|||
#ifndef LINE_H
|
||||
#define LINE_H
|
||||
|
||||
#include <QPoint>
|
||||
#include <QString>
|
||||
|
||||
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();
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue