Grid painting
This commit is contained in:
parent
a9c7952f5c
commit
e24837649c
3 changed files with 92 additions and 2 deletions
|
|
@ -2,7 +2,11 @@
|
||||||
|
|
||||||
GridPreview::GridPreview(QWidget *parent)
|
GridPreview::GridPreview(QWidget *parent)
|
||||||
: QWidget{parent}
|
: QWidget{parent}
|
||||||
{}
|
{
|
||||||
|
colorMap.insert(QString("red"), Qt::red);
|
||||||
|
colorMap.insert(QString("green"), Qt::green);
|
||||||
|
colorMap.insert(QString("blue"), Qt::blue);
|
||||||
|
}
|
||||||
|
|
||||||
void GridPreview::setSize(int newWidth, int newHeight)
|
void GridPreview::setSize(int newWidth, int newHeight)
|
||||||
{
|
{
|
||||||
|
|
@ -32,3 +36,74 @@ QString GridPreview::toLuaTable()
|
||||||
linesSubTable.removeLast(); // ,
|
linesSubTable.removeLast(); // ,
|
||||||
return output.arg(linesSubTable);
|
return output.arg(linesSubTable);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void GridPreview::paintEvent(QPaintEvent *event)
|
||||||
|
{
|
||||||
|
QPainter painter(this);
|
||||||
|
QPen pen(Qt::black, 2);
|
||||||
|
painter.setPen(pen);
|
||||||
|
QBrush brush(Qt::black);
|
||||||
|
bool widthBased = rect().width() > rect().height();
|
||||||
|
int cellSize = widthBased ?
|
||||||
|
rect().height() / height :
|
||||||
|
rect().width() / width;
|
||||||
|
|
||||||
|
int dotSize = cellSize * 0.5;
|
||||||
|
int filledDotSize = cellSize * 0.7;
|
||||||
|
|
||||||
|
int areaWidth = cellSize * width;
|
||||||
|
int areaHeight = cellSize * height;
|
||||||
|
|
||||||
|
int offsetX = (rect().width() - areaWidth) / 2;
|
||||||
|
int offsetY = (rect().height() - areaHeight) / 2;
|
||||||
|
|
||||||
|
QRect drawArea(offsetX, offsetY, areaWidth, areaHeight);
|
||||||
|
|
||||||
|
// Draw main grid
|
||||||
|
|
||||||
|
painter.drawRect(drawArea);
|
||||||
|
|
||||||
|
QRect cell(0, 0, cellSize, cellSize);
|
||||||
|
painter.setBrush(brush);
|
||||||
|
for (int cellX = 0; cellX < width; cellX++)
|
||||||
|
{
|
||||||
|
for (int cellY = 0; cellY < height; cellY++)
|
||||||
|
{
|
||||||
|
cell.setLeft(offsetX + (cellSize - dotSize) / 2 + cellX * cellSize);
|
||||||
|
cell.setWidth(dotSize);
|
||||||
|
cell.setTop(offsetY + (cellSize - dotSize) / 2 + cellY * cellSize);
|
||||||
|
cell.setHeight(dotSize);
|
||||||
|
painter.drawEllipse(cell);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw line points
|
||||||
|
|
||||||
|
for (auto iter = lines.begin(); iter < lines.end(); iter++)
|
||||||
|
{
|
||||||
|
QColor color(colorFromString(iter->getColor()));
|
||||||
|
brush.setColor(color);
|
||||||
|
painter.setBrush(brush);
|
||||||
|
|
||||||
|
// Draw start
|
||||||
|
cell.setLeft(offsetX + (cellSize - filledDotSize) / 2 + (iter->getStartX() - 1) * cellSize);
|
||||||
|
cell.setWidth(filledDotSize);
|
||||||
|
cell.setTop(offsetY + (cellSize - filledDotSize) / 2 + (iter->getStartY() - 1) * cellSize);
|
||||||
|
cell.setHeight(filledDotSize);
|
||||||
|
painter.drawEllipse(cell);
|
||||||
|
|
||||||
|
// Draw end
|
||||||
|
cell.setLeft(offsetX + (cellSize - filledDotSize) / 2 + (iter->getEndX() - 1) * cellSize);
|
||||||
|
cell.setWidth(filledDotSize);
|
||||||
|
cell.setTop(offsetY + (cellSize - filledDotSize) / 2 + (iter->getEndY() - 1) * cellSize);
|
||||||
|
cell.setHeight(filledDotSize);
|
||||||
|
painter.drawEllipse(cell);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
QColor GridPreview::colorFromString(QString color)
|
||||||
|
{
|
||||||
|
if (!colorMap.contains(color))
|
||||||
|
return colorMap[QString("red")];
|
||||||
|
return colorMap[color];
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
#define GRIDPREVIEW_H
|
#define GRIDPREVIEW_H
|
||||||
|
|
||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
|
#include <QPainter>
|
||||||
#include "line.h"
|
#include "line.h"
|
||||||
|
|
||||||
class GridPreview : public QWidget
|
class GridPreview : public QWidget
|
||||||
|
|
@ -13,9 +14,12 @@ public:
|
||||||
void addLine(Line &line);
|
void addLine(Line &line);
|
||||||
void removeLine(int index);
|
void removeLine(int index);
|
||||||
QString toLuaTable();
|
QString toLuaTable();
|
||||||
|
void paintEvent(QPaintEvent* event);
|
||||||
|
QColor colorFromString(QString color);
|
||||||
private:
|
private:
|
||||||
int width = 5, height = 5;
|
int width = 5, height = 5;
|
||||||
QVector<Line> lines {};
|
QVector<Line> lines {};
|
||||||
|
QMap<QString, QColor> colorMap;
|
||||||
const QString tableTemplate = "return {\n width = %1,\n height = %2,\n lines = {\n%3\n }\n}";
|
const QString tableTemplate = "return {\n width = %1,\n height = %2,\n lines = {\n%3\n }\n}";
|
||||||
signals:
|
signals:
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -96,7 +96,11 @@
|
||||||
<number>6</number>
|
<number>6</number>
|
||||||
</property>
|
</property>
|
||||||
<item>
|
<item>
|
||||||
<widget class="QPlainTextEdit" name="textPreview"/>
|
<widget class="QPlainTextEdit" name="textPreview">
|
||||||
|
<property name="readOnly">
|
||||||
|
<bool>true</bool>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item>
|
<item>
|
||||||
<layout class="QHBoxLayout" name="layoutButtons">
|
<layout class="QHBoxLayout" name="layoutButtons">
|
||||||
|
|
@ -395,6 +399,13 @@
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QPushButton" name="buttonDeleteLine">
|
||||||
|
<property name="text">
|
||||||
|
<string>Delete selected line</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
</item>
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue