-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGridWidget.h
More file actions
164 lines (141 loc) · 4.69 KB
/
Copy pathGridWidget.h
File metadata and controls
164 lines (141 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#pragma once
#include <QWidget>
#include <QPainter>
#include <QPaintEvent>
#include <QDebug>
#include <vector>
class GridWidget : public QWidget
{
Q_OBJECT
public:
explicit GridWidget(int rows, int cols, QWidget *parent = nullptr)
: QWidget(parent)
, m_rows(rows)
, m_cols(cols)
, m_cells(rows * cols, 0)
{
if (rows <= 0 || cols <= 0)
{
qCritical() << "[GridWidget] Invalid grid dimensions:"
<< rows << "x" << cols
<< "- both must be > 0";
}
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
}
GridWidget(int windowW, int windowH, int cellSizePx, QWidget *parent = nullptr)
: QWidget(parent)
, m_rows(windowH / cellSizePx)
, m_cols(windowW / cellSizePx)
, m_cells((windowH / cellSizePx) * (windowW / cellSizePx), 0)
{
if (cellSizePx <= 0)
{
qCritical() << "[GridWidget] Invalid cell size:" << cellSizePx
<< "- must be > 0";
}
if (windowW <= 0 || windowH <= 0)
{
qCritical() << "[GridWidget] Invalid window size:"
<< windowW << "x" << windowH
<< "- both must be > 0";
}
if (m_rows <= 0 || m_cols <= 0)
{
qCritical() << "[GridWidget] Calculated grid dimensions:"
<< m_rows << "x" << m_cols
<< "- cell size too large for window";
}
qInfo() << "[GridWidget] Window" << windowW << "x" << windowH
<< "cellSize" << cellSizePx << "px -> grid"
<< m_cols << "x" << m_rows;
resize(windowW, windowH);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
}
void setCells(const std::vector<int>& cells)
{
const size_t expected = static_cast<size_t>(m_rows) * m_cols;
if (cells.size() != expected)
{
qWarning() << "[GridWidget] setCells: expected" << expected
<< "values (" << m_rows << "x" << m_cols << ") but got"
<< cells.size() << "- ignoring update";
return;
}
m_cells = cells;
update();
}
void setCell(int row, int col, int value)
{
if (row < 0 || row >= m_rows || col < 0 || col >= m_cols)
{
qWarning() << "[GridWidget] setCell: (" << row << "," << col
<< ") out of bounds for" << m_rows << "x" << m_cols << "grid";
return;
}
m_cells[row * m_cols + col] = value;
}
void setGridSize(int rows, int cols)
{
if (rows <= 0 || cols <= 0)
{
qCritical() << "[GridWidget] setGridSize: invalid dimensions"
<< rows << "x" << cols;
return;
}
m_rows = rows;
m_cols = cols;
m_cells.assign(rows * cols, 0);
update();
}
int rows() const { return m_rows; }
int cols() const { return m_cols; }
protected:
void paintEvent(QPaintEvent * /*event*/) override
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, false);
const float cellSize = qMin(
static_cast<float>(width()) / m_cols,
static_cast<float>(height()) / m_rows
);
const float gridW = cellSize * m_cols;
const float gridH = cellSize * m_rows;
const float offsetX = (width() - gridW) * 0.5f;
const float offsetY = (height() - gridH) * 0.5f;
painter.fillRect(rect(), palette().window());
painter.save();
painter.translate(offsetX, offsetY);
const QColor fillColor(63, 216, 216);
painter.setPen(Qt::NoPen);
painter.setBrush(fillColor);
for (int r = 0; r < m_rows; ++r)
{
for (int c = 0; c < m_cols; ++c)
{
if (m_cells[r * m_cols + c])
{
painter.drawRect(QRectF(c * cellSize, r * cellSize, cellSize, cellSize));
}
}
}
QPen gridPen(QColor(200, 200, 200), 0.1, Qt::SolidLine);
painter.setPen(gridPen);
painter.setBrush(Qt::NoBrush);
for (int c = 0; c <= m_cols; ++c)
{
const float x = c * cellSize;
painter.drawLine(QPointF(x, 0), QPointF(x, gridH));
}
for (int r = 0; r <= m_rows; ++r)
{
const float y = r * cellSize;
painter.drawLine(QPointF(0, y), QPointF(gridW, y));
}
painter.restore();
painter.end();
}
private:
int m_rows;
int m_cols;
std::vector<int> m_cells;
};