This commit is contained in:
lidg
2026-10-19 11:17:12 +08:00
parent 8f9a89a61e
commit 23674e0ede
20 changed files with 1509 additions and 333 deletions
+77 -81
View File
@@ -8,7 +8,6 @@
#include "uihelpers.h"
#include <QAbstractItemView>
#include <QComboBox>
#include <QDateTime>
#include <QFormLayout>
#include <QGridLayout>
@@ -16,7 +15,7 @@
#include <QHeaderView>
#include <QLabel>
#include <QMessageBox>
#include <QSignalBlocker>
#include <QSet>
#include <QtGlobal>
#include <QPushButton>
#include <QMetaObject>
@@ -73,15 +72,27 @@ QString displayProcessStatus(const QString &status)
return status.isEmpty() ? "未采集" : status;
}
void updateStatusCell(QLabel *cell, const QString &text, const QString &color)
QString displayOrderStatus(const QString &status)
{
if (status == "NOT_START") return "待启动";
if (status == "RUNNING") return "加工中";
if (status == "PAUSED") return "已暂停";
if (status == "FINISHED" || status == "COMPLETED") return "已完成";
if (status == "CANCELED") return "已取消";
return status.isEmpty() ? "未采集" : status;
}
void updateStatusCell(QLabel *cell, const QString &text, const QString &color,
bool highlighted = false)
{
if (!cell) {
return;
}
cell->setText(text);
cell->setStyleSheet(QString("QLabel { background: %1; border: 1px solid #ffffff; "
cell->setStyleSheet(QString("QLabel { background: %1; border: %2; "
"border-radius: 3px; color: white; font-size: 11px; }")
.arg(color));
.arg(color)
.arg(highlighted ? "3px solid #ffb300" : "1px solid #ffffff"));
}
QString boolStatus(bool valid, bool value, const QString &yes, const QString &no)
@@ -94,7 +105,6 @@ QString boolStatus(bool valid, bool value, const QString &yes, const QString &no
DashboardPage::DashboardPage(QWidget *parent)
: QWidget(parent)
, ui(new Ui::DashboardPage)
, orderCombo(new QComboBox(this))
, rackBox(nullptr)
, deviceStatusLabel(new QLabel("PLC:未采集 | 机器人:未采集 | 安全回路:未采集", this))
, inspectionMachineCell(nullptr)
@@ -119,6 +129,8 @@ DashboardPage::DashboardPage(QWidget *parent)
, productionThread(new QThread(this))
, productionWorker(new ProductionWorker)
, productionState(ProductionService::Idle)
, currentOrderId(0)
, currentOrderStartable(false)
, productionTrayNo(0)
, productionSlotNo(0)
{
@@ -151,11 +163,8 @@ DashboardPage::DashboardPage(QWidget *parent)
stopButton->setObjectName("stopButton");
stopButton->setVisible(false);
ui->buttonLayout->insertWidget(6, stopButton);
orderCombo->setObjectName("orderSelector");
orderCombo->setMinimumWidth(220);
ui->taskLayout->insertWidget(0, orderCombo);
deviceStatusLabel->setStyleSheet("color: #546e7a;");
ui->taskLayout->insertWidget(1, deviceStatusLabel);
ui->taskLayout->insertWidget(0, deviceStatusLabel);
QFormLayout *processSummaryLayout = new QFormLayout;
processSummaryLayout->addRow("当前工序:", currentProcessLabel);
@@ -246,9 +255,6 @@ DashboardPage::DashboardPage(QWidget *parent)
this, &DashboardPage::retryRecovery);
connect(markNgButton, &QPushButton::clicked,
this, &DashboardPage::markWorkpieceNg);
connect(orderCombo, QOverload<int>::of(&QComboBox::currentIndexChanged),
this, &DashboardPage::onOrderChanged);
connect(productionWorker, &ProductionWorker::stateChanged,
this, &DashboardPage::onProductionStateChanged);
connect(productionWorker, &ProductionWorker::currentWorkpieceChanged,
@@ -278,10 +284,12 @@ DashboardPage::DashboardPage(QWidget *parent)
QMetaObject::invokeMethod(productionWorker, "requestRestore",
Qt::QueuedConnection);
loadOrderOptions();
loadCurrentOrder();
processRefreshTimer->setInterval(1000);
connect(processRefreshTimer, &QTimer::timeout,
this, &DashboardPage::refreshProcessRecords);
connect(processRefreshTimer, &QTimer::timeout, this, [this]() {
refreshProcessRecords();
loadCurrentOrder();
});
processRefreshTimer->start();
refreshProcessRecords();
updateRackSummary();
@@ -350,12 +358,8 @@ void DashboardPage::startProduction()
return;
}
int selectedOrderId = orderCombo->currentData().toInt();
if (selectedOrderId <= 0) {
loadOrderOptions();
selectedOrderId = orderCombo->currentData().toInt();
}
if (selectedOrderId <= 0) {
loadCurrentOrder();
if (!currentOrderStartable || currentOrderId <= 0) {
QMessageBox::information(this, "启动生产",
"没有可生产的订单,请先生成工件。");
return;
@@ -364,7 +368,7 @@ void DashboardPage::startProduction()
ui->startButton->setEnabled(false);
if (!QMetaObject::invokeMethod(
productionWorker, "requestStart", Qt::QueuedConnection,
Q_ARG(int, selectedOrderId))) {
Q_ARG(int, currentOrderId))) {
updateRecoveryButtons();
QMessageBox::warning(this, "启动生产",
"提交生产启动请求失败。");
@@ -398,17 +402,13 @@ void DashboardPage::clearAlarm()
"已请求 PLC 清除报警", "待处理");
}
void DashboardPage::onOrderChanged(int)
{
updateOrderSummary();
}
void DashboardPage::onProductionStateChanged(ProductionService::State state,
const QString &name)
{
productionState = state;
const QString stateText = displayState(name);
ui->actionLabel->setText("生产状态:" + stateText);
loadCurrentOrder();
updateRecoveryButtons();
emit systemStateChanged(stateText);
emit currentStepChanged(stateText);
@@ -464,7 +464,7 @@ void DashboardPage::onProductionFinished(int orderId)
{
emit logRequested("生产日志", "生产流程", "订单完成",
QString("订单 %1 已完成").arg(orderId), "成功");
loadOrderOptions();
loadCurrentOrder();
updateProductionStats();
updateRackSummary();
}
@@ -535,8 +535,9 @@ void DashboardPage::updateRecoveryButtons()
const bool running = state != ProductionService::Idle
&& state != ProductionService::Completed;
const bool terminating = state == ProductionService::Terminating;
const bool canStart = state == ProductionService::Idle
|| state == ProductionService::Completed;
const bool canStart = (state == ProductionService::Idle
|| state == ProductionService::Completed)
&& currentOrderStartable;
ui->startButton->setEnabled(canStart);
recoverButton->setVisible(recoveryRequired);
retryButton->setVisible(recoveryRequired);
@@ -722,6 +723,21 @@ void DashboardPage::updateRackSummary()
{
QString error;
const QList<Tray> trays = TrayService::instance().listTrays(&error);
QSet<int> highlightedTrayIds;
if (error.isEmpty() && currentOrderId > 0) {
QString workpieceError;
const QList<Workpiece> workpieces =
WorkpieceService::instance().listByOrder(currentOrderId,
&workpieceError);
if (workpieceError.isEmpty()) {
for (const Workpiece &workpiece : workpieces) {
if (workpiece.trayId > 0) {
highlightedTrayIds.insert(workpiece.trayId);
}
}
}
}
for (int index = 0; index < rackTrayCells.size(); ++index) {
QLabel *cell = rackTrayCells.at(index);
if (!error.isEmpty() || index >= trays.size()) {
@@ -747,76 +763,56 @@ void DashboardPage::updateRackSummary()
.arg(index + 1)
.arg(statusText)
.arg(tray.loadedCount),
color);
color, highlightedTrayIds.contains(tray.id));
}
}
void DashboardPage::loadOrderOptions()
void DashboardPage::loadCurrentOrder()
{
QString error;
const QList<Order> orders = OrderService::instance().listOrders(&error);
if (!error.isEmpty()) {
orderCombo->clear();
orderCombo->addItem("无订单数据", 0);
Order order;
if (!OrderService::instance().findUnfinishedOrder(&order, &error)) {
currentOrderId = 0;
currentOrderStartable = false;
ui->orderLabel->setText("当前订单:读取失败");
updateProductionStats();
updateRackSummary();
emit logRequested("操作日志", "控制面板", "加载订单",
error, "失败");
return;
}
const int previousOrderId = orderCombo->currentData().toInt();
QSignalBlocker blocker(orderCombo);
orderCombo->clear();
for (const Order &order : orders) {
if (order.status != "NOT_START") {
continue;
}
QString workpieceError;
const QList<Workpiece> workpieces =
WorkpieceService::instance().listByOrder(order.id, &workpieceError);
if (!workpieceError.isEmpty() || workpieces.isEmpty()) {
continue;
}
orderCombo->addItem(order.no, order.id);
}
if (orderCombo->count() == 0) {
orderCombo->addItem("暂无可生产订单", 0);
} else {
const int previousIndex = orderCombo->findData(previousOrderId);
orderCombo->setCurrentIndex(previousIndex >= 0 ? previousIndex : 0);
}
updateOrderSummary();
}
void DashboardPage::updateOrderSummary()
{
const int selectedOrderId = orderCombo->currentData().toInt();
if (selectedOrderId <= 0) {
ui->orderLabel->setText("当前订单:-");
ui->trayLabel->setText("当前托盘:-");
ui->workpieceLabel->setText("当前工件:-");
currentOrderId = order.id;
currentOrderStartable = false;
if (order.id <= 0) {
ui->orderLabel->setText("当前订单:暂无未完成订单");
emit currentOrderChanged("-");
updateProductionStats();
updateRackSummary();
updateRecoveryButtons();
return;
}
Order order;
QString error;
if (!OrderService::instance().findOrder(selectedOrderId, &order, &error)) {
ui->orderLabel->setText("当前订单:-");
emit currentOrderChanged("-");
return;
QString workpieceError;
const QList<Workpiece> workpieces =
WorkpieceService::instance().listByOrder(order.id, &workpieceError);
if (workpieceError.isEmpty()) {
currentOrderStartable = order.status == "NOT_START"
&& !workpieces.isEmpty();
}
ui->orderLabel->setText("当前订单:" + order.no);
ui->orderLabel->setText(QString("当前订单:%1(%2)")
.arg(order.no)
.arg(displayOrderStatus(order.status)));
emit currentOrderChanged(order.no);
updateProductionStats();
updateRackSummary();
updateRecoveryButtons();
}
void DashboardPage::updateProductionStats()
{
const int selectedOrderId = orderCombo->currentData().toInt();
if (selectedOrderId <= 0) {
if (currentOrderId <= 0) {
ui->planValue->setText("0");
ui->pendingValue->setText("0");
ui->frontValue->setText("0");
@@ -828,11 +824,11 @@ void DashboardPage::updateProductionStats()
Order order;
QString error;
if (!OrderService::instance().findOrder(selectedOrderId, &order, &error)) {
if (!OrderService::instance().findOrder(currentOrderId, &order, &error)) {
return;
}
const QList<Workpiece> workpieces =
WorkpieceService::instance().listByOrder(selectedOrderId, &error);
WorkpieceService::instance().listByOrder(currentOrderId, &error);
if (!error.isEmpty()) {
return;
}