123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- #include "Waffle.h"
- #include <QPainter>
- #include <QPaintEvent>
- Waffle::Waffle(int flag, QWidget *parent) : QWidget(parent) {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- rows = 6;
- cols = 18;
- centerX = 25;
- centerY = 25;
-
- std::srand(std::time(nullptr));
- for (int row = 0; row < rows; ++row) {
- for (int col = 0; col < cols; ++col) {
- WAFFLE_MATRIX_POINT_STRUCT point;
- point.nPackMatrixId = 1;
- point.nPackRow = 6;
- point.nPackCol = 18;
- point.nDieRow = row;
- point.nDieCol = col;
- point.nDieMatrixId = 1;
- point.iDieIndex = 1;
- point.bDisable = false;
- point.stPosition.x = col * 10.0;
- point.stPosition.y = row * 10.0;
- int randomStatus = std::rand() % 5;
- point.eStatus = static_cast<PICK_DIE_STATUS>(randomStatus);
-
- waffleData.append(point);
- }
- }
- }
- 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);
- }
- }
- }
- QColor Waffle::getColorByStatus(PICK_DIE_STATUS status) {
- switch (status) {
- case DIE_EXIST: return QColor(0, 102, 255);
- case NO_EXIST: return QColor(200, 200, 200);
- case PICK_ING: return QColor(255, 255, 0);
- case SKIP_DIE: return QColor(128, 128, 128);
- case EDGE_DIE: return QColor(255, 165, 0);
- default: return QColor(0, 0, 0);
- }
- }
- void Waffle::paintInitFrom(QWidget *parent) {
-
- int width = parent->width();
- int height = parent->height();
-
- int cellSize = qMin(width, height) / qMax(rows, cols);
-
- int offsetX = (width - cellSize * cols) / 2;
- int offsetY = (height - cellSize * rows) / 2;
-
- globalPixmap = QPixmap(width, height);
- globalPixmap.fill(Qt::white);
-
- QPainter painter(&globalPixmap);
- painter.setRenderHint(QPainter::Antialiasing);
-
- painter.setPen(Qt::NoPen);
-
- for (int i = 0; i < waffleData.size(); ++i) {
- int x = offsetX + waffleData[i].nDieCol * cellSize;
- int y = offsetY + waffleData[i].nDieRow * cellSize;
-
- painter.setBrush(getColorByStatus(waffleData[i].eStatus));
-
- painter.drawRect(x, y, cellSize, cellSize);
- }
-
-
- painter.end();
- }
- void Waffle::initFrom(QWidget *parent) {
- scene = new QGraphicsScene(parent);
- view = new WaffleGraphicsView(scene);
-
- int width = parent->width();
- int height = parent->height();
-
- int cellSize = qMin(width, height) / qMax(rows, cols);
- for (int i = 0; i < waffleData.size(); ++i) {
- DieItem* die = new DieItem(waffleData[i].nDieRow, waffleData[i].nDieCol, waffleData[i].eStatus, cellSize);
- die->setPos(waffleData[i].nDieCol * cellSize, waffleData[i].nDieRow * cellSize);
- scene->addItem(die);
- }
- view->setSceneRect(scene->itemsBoundingRect());
- view->resize(width, height);
- }
- QPixmap Waffle::getGlobalPixmap() const {
- return globalPixmap;
- }
|