Files
ssp/pages/rackpage.cpp
T
2026-08-18 09:40:43 +08:00

246 lines
8.7 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "rackpage.h"
#include "ui_rackpage.h"
#include "squaretraybutton.h"
#include "uihelpers.h"
#include "../services/trayservice.h"
#include <QAbstractButton>
#include <QButtonGroup>
#include <QDebug>
#include <QLabel>
#include <QResizeEvent>
#include <QSizePolicy>
#include <QtGlobal>
RackPage::RackPage(QWidget *parent)
: QWidget(parent)
, ui(new Ui::RackPage)
, rackButtonGroup(new QButtonGroup(this))
, selectedTrayId(0)
, selectedTrayIndex(0)
{
ui->setupUi(this);
ui->mainLayout->setStretch(0, 1);
ui->mainLayout->setStretch(1, 1);
ui->rackGroup->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
ui->trayGroup->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
ui->rackGridLayout->setContentsMargins(8, 18, 8, 8);
ui->rackGridLayout->setSpacing(4);
ui->rackGridLayout->setAlignment(Qt::AlignCenter);
rackButtonGroup->setExclusive(true);
for (int i = 0; i < 6; ++i) {
SquareTrayButton *trayButton = new SquareTrayButton(i, 0, this);
rackTrayButtons.append(trayButton);
rackButtonGroup->addButton(trayButton, i);
ui->rackGridLayout->addWidget(trayButton, i / 3, i % 3, Qt::AlignCenter);
connect(trayButton, &QAbstractButton::clicked, this, [this, i]() {
showTrayDetail(i);
});
}
ui->trayGridLayout->setContentsMargins(8, 8, 8, 8);
ui->trayGridLayout->setSpacing(4);
ui->trayGridLayout->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
ui->trayOuterLayout->setStretch(0, 0);
ui->trayOuterLayout->setStretch(1, 1);
ui->trayOuterLayout->setStretch(2, 0);
ui->trayOuterLayout->setStretch(3, 0);
for (int r = 0; r < 7; ++r) {
for (int c = 0; c < 7; ++c) {
QLabel *cell = UiHelpers::makeSquareStatusCell(QString("%1-%2").arg(r + 1).arg(c + 1), "#78909c");
trayCells.append(cell);
ui->trayGridLayout->addWidget(cell, r, c, Qt::AlignCenter);
}
}
setupLegend();
loadTrays();
updateCellSizes();
}
RackPage::~RackPage()
{
delete ui;
}
void RackPage::setupLegend()
{
ui->pendingLegend->setStyleSheet("QLabel { color: white; background: #f9a825; border-radius: 4px; padding: 4px 10px; }");
ui->frontLegend->setStyleSheet("QLabel { color: white; background: #1e88e5; border-radius: 4px; padding: 4px 10px; }");
ui->backLegend->setStyleSheet("QLabel { color: white; background: #8e24aa; border-radius: 4px; padding: 4px 10px; }");
ui->okLegend->setStyleSheet("QLabel { color: white; background: #2e7d32; border-radius: 4px; padding: 4px 10px; }");
ui->ngLegend->setStyleSheet("QLabel { color: white; background: #d32f2f; border-radius: 4px; padding: 4px 10px; }");
ui->emptyLegend->setStyleSheet("QLabel { color: white; background: #90a4ae; border-radius: 4px; padding: 4px 10px; }");
}
void RackPage::resizeEvent(QResizeEvent *event)
{
QWidget::resizeEvent(event);
updateCellSizes();
}
void RackPage::updateCellSizes()
{
const int rackSpacing = ui->rackGridLayout->spacing();
const int rackSide = qMax(70, qMin((ui->rackGroup->width() - 24 - rackSpacing * 2) / 3,
(ui->rackGroup->height() - 34 - rackSpacing) / 2));
for (SquareTrayButton *button : rackTrayButtons) {
button->setFixedSize(rackSide, rackSide);
}
const int detailSpacing = ui->trayGridLayout->spacing();
const int detailAvailableSide = qMin((ui->trayGroup->width() - 32 - detailSpacing * 6) / 7,
(ui->trayGroup->height() - 100 - detailSpacing * 6) / 7);
const int detailSide = qMax(24, qMin(42, detailAvailableSide));
for (QLabel *cell : trayCells) {
cell->setFixedSize(detailSide, detailSide);
}
}
void RackPage::loadTrays()
{
QString error;
if (!TrayService::instance().ensureAllTrayGrids(&error)) {
trays.clear();
updateTrayButtons();
updateTrayCells(QList<TraySlot>());
ui->trayDetailTitle->setText("暂无托盘数据");
emit logRequested("操作日志", "料架/托盘", "加载托盘", error, "失败");
qWarning() << "Failed to initialize tray grids:" << error;
return;
}
trays = TrayService::instance().listTrays(&error);
if (!error.isEmpty()) {
trays.clear();
updateTrayButtons();
updateTrayCells(QList<TraySlot>());
ui->trayDetailTitle->setText("暂无托盘数据");
emit logRequested("操作日志", "料架/托盘", "加载托盘", error, "失败");
qWarning() << "Failed to load trays:" << error;
return;
}
updateTrayButtons();
if (trays.isEmpty()) {
selectedTrayId = 0;
selectedTrayIndex = -1;
ui->trayDetailTitle->setText("暂无托盘数据");
updateTrayCells(QList<TraySlot>());
return;
}
selectedTrayIndex = qBound(0, selectedTrayIndex, trays.size() - 1);
showTrayDetail(selectedTrayIndex);
}
void RackPage::updateTrayButtons()
{
for (int index = 0; index < rackTrayButtons.size(); ++index) {
SquareTrayButton *button = rackTrayButtons.at(index);
if (index >= trays.size()) {
button->setTrayInfo(0, QString(), 0, "无数据", false);
button->setChecked(false);
continue;
}
const Tray &tray = trays.at(index);
button->setTrayInfo(tray.id, tray.code, tray.loadedCount,
tray.status, tray.present);
button->setChecked(index == selectedTrayIndex);
}
}
void RackPage::updateTrayCells(const QList<TraySlot> &traySlots)
{
const QStringList colors = QStringList()
<< "#90a4ae" << "#f9a825" << "#1e88e5" << "#8e24aa"
<< "#2e7d32" << "#d32f2f" << "#ef6c00";
const QStringList names = QStringList()
<< "空" << "待" << "正检" << "反检" << "OK" << "NG" << "异常";
for (int index = 0; index < trayCells.size(); ++index) {
const int rowNo = index / 7 + 1;
const int columnNo = index % 7 + 1;
const TraySlot *slot = nullptr;
for (const TraySlot &candidate : traySlots) {
if (candidate.rowNo == rowNo && candidate.columnNo == columnNo) {
slot = &candidate;
break;
}
}
QString state = "EMPTY";
int workpieceId = 0;
if (slot) {
state = slot->state;
workpieceId = slot->workpieceId;
}
int stateIndex = 0;
if (state == "PENDING") {
stateIndex = 1;
} else if (state == "FRONT_CHECKING") {
stateIndex = 2;
} else if (state == "BACK_CHECKING") {
stateIndex = 3;
} else if (state == "OK") {
stateIndex = 4;
} else if (state == "NG") {
stateIndex = 5;
} else if (state == "ERROR" || state == "PROCESSING") {
stateIndex = 6;
}
const QString workpieceText = workpieceId > 0
? QString("工件 %1").arg(workpieceId) : QString();
trayCells.at(index)->setText(QString("%1-%2\n%3%4")
.arg(rowNo)
.arg(columnNo)
.arg(names.at(stateIndex))
.arg(workpieceText.isEmpty() ? QString()
: "\n" + workpieceText));
trayCells.at(index)->setStyleSheet(
QString("QLabel { background: %1; border: 1px solid #ffffff; "
"border-radius: 3px; color: white; font-size: 11px; }")
.arg(colors.at(stateIndex)));
}
}
void RackPage::showTrayDetail(int trayIndex)
{
if (trayIndex < 0 || trayIndex >= trays.size()) {
selectedTrayId = 0;
selectedTrayIndex = -1;
ui->trayDetailTitle->setText("暂无托盘数据");
updateTrayCells(QList<TraySlot>());
return;
}
selectedTrayIndex = trayIndex;
selectedTrayId = trays.at(trayIndex).id;
if (trayIndex < rackTrayButtons.size()) {
rackTrayButtons.at(trayIndex)->setChecked(true);
}
const Tray &tray = trays.at(trayIndex);
ui->trayDetailTitle->setText(QString("%1:7 行 x 7 列").arg(tray.code));
QString error;
const QList<TraySlot> loadedSlots = TrayService::instance().listSlots(selectedTrayId, &error);
if (!error.isEmpty()) {
updateTrayCells(QList<TraySlot>());
emit logRequested("操作日志", "料架/托盘", "查看托盘", error, "失败");
qWarning() << "Failed to load tray slots:" << error;
return;
}
updateTrayCells(loadedSlots);
emit logRequested("操作日志", "料架/托盘", "查看托盘",
QString("查看托盘 %1 的 7x7 工件状态").arg(tray.code), "成功");
updateCellSizes();
}