123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- #include "MaterialBox.h"
- MaterialBox::MaterialBox(int flag, QWidget *parent) : QWidget(parent) {
-
- boxes = {
- Material_box("Box 1", Idle),
- Material_box("Box 2", Occupied),
- Material_box("Box 3", Warning),
- Material_box("Box 4", Idle),
- Material_box("Box 5", Occupied),
- Material_box("Box 6", Warning)
- };
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- void MaterialBox::paintEvent(QPaintEvent *event) {
- QPainter painter(this);
- painter.setRenderHint(QPainter::Antialiasing);
-
- painter.fillRect(this->rect(), Qt::white);
-
- int boxWidth = 30;
- int boxHeight = 18;
- int gap = 10;
- int cols = 3;
-
- int totalWidth = cols * boxWidth + (cols - 1) * gap;
- int rows = (boxes.size() + cols - 1) / cols;
- int totalHeight = rows * boxHeight + (rows - 1) * gap;
-
- int startX = (width() - totalWidth) / 2;
- int startY = (height() - totalHeight) / 2;
-
- for (size_t i = 0; i < boxes.size(); ++i) {
- int row = i / cols;
- int col = i % cols;
- int x = startX + col * (boxWidth + gap);
- int y = startY + row * (boxHeight + gap);
-
- QRect boxRect(x, y, boxWidth, boxHeight);
- painter.setPen(Qt::black);
- painter.drawRect(boxRect);
-
- switch (boxes[i].status) {
- case Idle:
- painter.setBrush(Qt::green);
- break;
- case Occupied:
- painter.setBrush(Qt::blue);
- break;
- case Warning:
- painter.setBrush(Qt::red);
- break;
- }
- painter.fillRect(boxRect, painter.brush());
-
- painter.setPen(Qt::white);
- painter.drawText(boxRect, Qt::AlignCenter, boxes[i].name);
- }
- }
|