1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- #include "Waffle.h"
- #include <QPainter>
- #include <QPaintEvent>
- Waffle::Waffle(int flag, QWidget *parent) : QWidget(parent) {
-
- if (flag == 0) {
-
- gridSize = 5;
- gap = 3;
- boxGap = 6;
- } else if (flag == 1) {
-
- gridSize = 20;
- gap = 6;
- boxGap = 12;
- } else if (flag == 2) {
-
- gridSize = 50;
- gap = 6;
- boxGap = 12;
- }
- }
- void Waffle::paintEvent(QPaintEvent *event) {
- QPainter painter(this);
-
- painter.setRenderHint(QPainter::Antialiasing);
-
- painter.fillRect(this->rect(), Qt::white);
-
- int totalWidth = cols * gridSize + (cols - 1) * gap;
- int totalHeight = rows * gridSize + (rows - 1) * gap;
-
- int boxTotalWidth = boxCols * totalWidth + (boxCols - 1) * boxGap;
- int boxTotalHeight = boxRows * totalHeight + (boxRows - 1) * boxGap;
-
- int boxStartX = (width() - boxTotalWidth) / 2;
- int boxStartY = (height() - boxTotalHeight) / 2;
-
- for (int i = 0; i < boxRows; ++i) {
- for (int j = 0; j < boxCols; ++j) {
-
- int startX = boxStartX + j * (totalWidth + boxGap);
- int startY = boxStartY + i * (totalHeight + boxGap);
-
- drawWaffle(painter, startX, startY, rows, cols, gridSize, gap);
- }
- }
- }
- void Waffle::drawWaffle(QPainter &painter, int startX, int startY, int rows, int cols, int gridSize, int gap) {
-
- for (int i = 0; i < rows; ++i) {
- for (int j = 0; j < cols; ++j) {
-
- int x = startX + j * (gridSize + gap);
- int y = startY + i * (gridSize + gap);
- QRect cellRect(x, y, gridSize, gridSize);
-
- painter.fillRect(cellRect, Qt::yellow);
-
- painter.setPen(Qt::black);
- painter.drawRect(cellRect);
- }
- }
- }
|