2026-08-18 09:40:43 +08:00
|
|
|
|
#include "widget.h"
|
|
|
|
|
|
#include "ui_widget.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include "pages/dashboardpage.h"
|
|
|
|
|
|
#include "pages/devicepage.h"
|
|
|
|
|
|
#include "pages/logpage.h"
|
|
|
|
|
|
#include "pages/orderpage.h"
|
2026-10-19 11:17:12 +08:00
|
|
|
|
#include "pages/qualitypage.h"
|
2026-08-18 09:40:43 +08:00
|
|
|
|
#include "pages/rackpage.h"
|
|
|
|
|
|
#include "pages/uihelpers.h"
|
2026-10-27 22:16:55 +08:00
|
|
|
|
#include "pages/visionpage.h"
|
2026-08-18 09:40:43 +08:00
|
|
|
|
#include "services/logservice.h"
|
2026-10-27 22:16:55 +08:00
|
|
|
|
#include "services/visionservice.h"
|
|
|
|
|
|
#include "config/configmanager.h"
|
2026-08-18 09:40:43 +08:00
|
|
|
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
#include <QFrame>
|
|
|
|
|
|
#include <QHBoxLayout>
|
|
|
|
|
|
#include <QLabel>
|
|
|
|
|
|
#include <QListWidget>
|
|
|
|
|
|
#include <QStackedWidget>
|
2026-10-29 11:57:47 +08:00
|
|
|
|
#include <QTimer>
|
2026-08-18 09:40:43 +08:00
|
|
|
|
#include <QVBoxLayout>
|
|
|
|
|
|
|
|
|
|
|
|
Widget::Widget(QWidget *parent)
|
|
|
|
|
|
: QWidget(parent)
|
|
|
|
|
|
, ui(new Ui::Widget)
|
|
|
|
|
|
, navList(nullptr)
|
|
|
|
|
|
, pageStack(nullptr)
|
|
|
|
|
|
, dashboardPage(nullptr)
|
|
|
|
|
|
, logPage(nullptr)
|
2026-10-19 11:17:12 +08:00
|
|
|
|
, qualityPage(nullptr)
|
2026-10-27 22:16:55 +08:00
|
|
|
|
, visionService(nullptr)
|
|
|
|
|
|
, visionPage(nullptr)
|
2026-08-18 09:40:43 +08:00
|
|
|
|
, systemStateLabel(nullptr)
|
|
|
|
|
|
, currentOrderLabel(nullptr)
|
|
|
|
|
|
, currentWorkpieceLabel(nullptr)
|
|
|
|
|
|
, currentStepLabel(nullptr)
|
2026-08-24 17:28:59 +08:00
|
|
|
|
, plcStatusLabel(nullptr)
|
|
|
|
|
|
, robotStatusLabel(nullptr)
|
|
|
|
|
|
, safetyStatusLabel(nullptr)
|
2026-10-29 11:57:47 +08:00
|
|
|
|
, cameraStatusLabel(nullptr)
|
2026-08-18 09:40:43 +08:00
|
|
|
|
{
|
|
|
|
|
|
ui->setupUi(this);
|
|
|
|
|
|
setWindowTitle("小轴多参数上下料单元管控系统");
|
|
|
|
|
|
resize(1280, 820);
|
|
|
|
|
|
setupStyle();
|
|
|
|
|
|
|
|
|
|
|
|
QVBoxLayout *rootLayout = new QVBoxLayout(this);
|
|
|
|
|
|
rootLayout->setContentsMargins(0, 0, 0, 0);
|
|
|
|
|
|
rootLayout->setSpacing(0);
|
|
|
|
|
|
|
|
|
|
|
|
setupTopBar(rootLayout);
|
|
|
|
|
|
setupMainArea(rootLayout);
|
|
|
|
|
|
|
2026-08-24 17:28:59 +08:00
|
|
|
|
appendLog("生产日志", "系统", "启动", "生产控制系统已启动", "成功");
|
2026-08-18 09:40:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Widget::~Widget()
|
|
|
|
|
|
{
|
2026-10-27 22:16:55 +08:00
|
|
|
|
if (visionService) visionService->shutdown();
|
2026-08-18 09:40:43 +08:00
|
|
|
|
delete ui;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Widget::setupStyle()
|
|
|
|
|
|
{
|
|
|
|
|
|
setStyleSheet(
|
|
|
|
|
|
"QWidget { font-family: 'Microsoft YaHei'; font-size: 13px; color: #1f2933; }"
|
|
|
|
|
|
"QListWidget { background: #263238; color: #e8eef2; border: none; padding: 8px; }"
|
|
|
|
|
|
"QListWidget::item { height: 42px; padding-left: 12px; border-radius: 4px; }"
|
|
|
|
|
|
"QListWidget::item:selected { background: #00897b; color: white; }"
|
|
|
|
|
|
"QPushButton { min-height: 28px; padding: 5px 12px; border: 1px solid #cfd8dc; border-radius: 4px; background: #ffffff; }"
|
|
|
|
|
|
"QPushButton:hover { background: #eef7f5; }"
|
|
|
|
|
|
"QPushButton#primaryButton { background: #00897b; color: white; border-color: #00897b; }"
|
|
|
|
|
|
"QPushButton#dangerButton { background: #d32f2f; color: white; border-color: #d32f2f; }"
|
|
|
|
|
|
"QFrame#card { background: #ffffff; border: 1px solid #d8dee4; border-radius: 6px; }"
|
|
|
|
|
|
"QLabel#cardTitle { font-size: 15px; font-weight: bold; color: #263238; }"
|
|
|
|
|
|
"QGroupBox { font-weight: bold; border: 1px solid #d8dee4; border-radius: 6px; margin-top: 10px; padding-top: 12px; }"
|
|
|
|
|
|
"QGroupBox::title { subcontrol-origin: margin; left: 10px; padding: 0 4px; }"
|
|
|
|
|
|
"QTableWidget { background: white; border: 1px solid #d8dee4; gridline-color: #e5e9ef; }"
|
|
|
|
|
|
"QHeaderView::section { background: #eef3f5; padding: 6px; border: none; border-right: 1px solid #d8dee4; }"
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Widget::setupTopBar(QVBoxLayout *rootLayout)
|
|
|
|
|
|
{
|
|
|
|
|
|
QFrame *topBar = new QFrame;
|
|
|
|
|
|
topBar->setStyleSheet("QFrame { background: #f5f7f8; border-bottom: 1px solid #d8dee4; }");
|
|
|
|
|
|
|
|
|
|
|
|
QHBoxLayout *topLayout = new QHBoxLayout(topBar);
|
|
|
|
|
|
topLayout->setContentsMargins(14, 8, 14, 8);
|
|
|
|
|
|
topLayout->setSpacing(10);
|
|
|
|
|
|
topLayout->addWidget(new QLabel("小轴单元"));
|
|
|
|
|
|
|
|
|
|
|
|
systemStateLabel = UiHelpers::makeBadge("系统:待机", "#607d8b");
|
2026-08-24 17:28:59 +08:00
|
|
|
|
currentOrderLabel = UiHelpers::makeBadge("订单:-", "#455a64");
|
|
|
|
|
|
currentWorkpieceLabel = UiHelpers::makeBadge("工件:-", "#455a64");
|
|
|
|
|
|
currentStepLabel = UiHelpers::makeBadge("工序:未采集", "#607d8b");
|
2026-08-18 09:40:43 +08:00
|
|
|
|
|
|
|
|
|
|
topLayout->addWidget(systemStateLabel);
|
|
|
|
|
|
topLayout->addWidget(currentOrderLabel);
|
|
|
|
|
|
topLayout->addWidget(currentWorkpieceLabel);
|
|
|
|
|
|
topLayout->addWidget(currentStepLabel);
|
|
|
|
|
|
topLayout->addStretch();
|
2026-08-24 17:28:59 +08:00
|
|
|
|
plcStatusLabel = UiHelpers::makeBadge("PLC:未采集", "#607d8b");
|
|
|
|
|
|
robotStatusLabel = UiHelpers::makeBadge("机器人:未采集", "#607d8b");
|
|
|
|
|
|
safetyStatusLabel = UiHelpers::makeBadge("安全回路:未采集", "#607d8b");
|
2026-10-29 11:57:47 +08:00
|
|
|
|
cameraStatusLabel = UiHelpers::makeBadge(
|
|
|
|
|
|
ConfigManager::instance().visionConfig().enabled
|
|
|
|
|
|
? "相机:离线" : "相机:未启用",
|
|
|
|
|
|
"#607d8b");
|
2026-08-24 17:28:59 +08:00
|
|
|
|
topLayout->addWidget(plcStatusLabel);
|
|
|
|
|
|
topLayout->addWidget(robotStatusLabel);
|
|
|
|
|
|
topLayout->addWidget(safetyStatusLabel);
|
2026-10-29 11:57:47 +08:00
|
|
|
|
topLayout->addWidget(cameraStatusLabel);
|
2026-08-18 09:40:43 +08:00
|
|
|
|
|
|
|
|
|
|
rootLayout->addWidget(topBar);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Widget::setupMainArea(QVBoxLayout *rootLayout)
|
|
|
|
|
|
{
|
|
|
|
|
|
QHBoxLayout *bodyLayout = new QHBoxLayout;
|
|
|
|
|
|
bodyLayout->setContentsMargins(0, 0, 0, 0);
|
|
|
|
|
|
bodyLayout->setSpacing(0);
|
|
|
|
|
|
|
|
|
|
|
|
navList = new QListWidget;
|
|
|
|
|
|
navList->setFixedWidth(170);
|
2026-10-19 11:17:12 +08:00
|
|
|
|
navList->addItems(QStringList() << "控制面板" << "订单管理" << "料架/托盘"
|
2026-11-05 14:14:29 +08:00
|
|
|
|
<< "设备状态" << "日志记录" << "质量管理" << "视觉引导");
|
2026-08-18 09:40:43 +08:00
|
|
|
|
navList->setCurrentRow(0);
|
|
|
|
|
|
bodyLayout->addWidget(navList);
|
|
|
|
|
|
|
|
|
|
|
|
dashboardPage = new DashboardPage(this);
|
|
|
|
|
|
OrderPage *orderPage = new OrderPage(this);
|
|
|
|
|
|
RackPage *rackPage = new RackPage(this);
|
2026-11-09 11:05:12 +08:00
|
|
|
|
connect(dashboardPage, &DashboardPage::productionDataChanged,
|
|
|
|
|
|
orderPage, &OrderPage::refresh);
|
|
|
|
|
|
connect(dashboardPage, &DashboardPage::productionDataChanged,
|
|
|
|
|
|
rackPage, &RackPage::refresh);
|
2026-11-09 15:04:11 +08:00
|
|
|
|
connect(orderPage, &OrderPage::productionDataChanged,
|
|
|
|
|
|
rackPage, &RackPage::refresh);
|
2026-08-18 09:40:43 +08:00
|
|
|
|
DevicePage *devicePage = new DevicePage(this);
|
|
|
|
|
|
logPage = new LogPage(this);
|
2026-10-19 11:17:12 +08:00
|
|
|
|
qualityPage = new QualityPage(this);
|
2026-10-27 22:16:55 +08:00
|
|
|
|
visionService = new VisionService(ConfigManager::instance().visionConfig(), this);
|
|
|
|
|
|
visionPage = new VisionPage(visionService, this);
|
|
|
|
|
|
connect(dashboardPage, &DashboardPage::visionCycleChanged, visionPage, &VisionPage::updateCycle);
|
|
|
|
|
|
dashboardPage->attachVisionService(visionService);
|
2026-08-18 09:40:43 +08:00
|
|
|
|
|
|
|
|
|
|
pageStack = new QStackedWidget;
|
|
|
|
|
|
pageStack->addWidget(dashboardPage);
|
|
|
|
|
|
pageStack->addWidget(orderPage);
|
|
|
|
|
|
pageStack->addWidget(rackPage);
|
|
|
|
|
|
pageStack->addWidget(devicePage);
|
|
|
|
|
|
pageStack->addWidget(logPage);
|
2026-10-19 11:17:12 +08:00
|
|
|
|
pageStack->addWidget(qualityPage);
|
2026-10-27 22:16:55 +08:00
|
|
|
|
pageStack->addWidget(visionPage);
|
2026-08-18 09:40:43 +08:00
|
|
|
|
bodyLayout->addWidget(pageStack, 1);
|
|
|
|
|
|
|
|
|
|
|
|
rootLayout->addLayout(bodyLayout, 1);
|
|
|
|
|
|
|
|
|
|
|
|
connect(navList, &QListWidget::currentRowChanged, pageStack, &QStackedWidget::setCurrentIndex);
|
|
|
|
|
|
connect(dashboardPage, &DashboardPage::systemStateChanged, this, [this](const QString &state) {
|
|
|
|
|
|
systemStateLabel->setText("系统:" + state);
|
|
|
|
|
|
});
|
|
|
|
|
|
connect(dashboardPage, &DashboardPage::currentStepChanged, this, [this](const QString &step) {
|
|
|
|
|
|
currentStepLabel->setText("工序:" + step);
|
|
|
|
|
|
});
|
2026-08-24 17:28:59 +08:00
|
|
|
|
connect(dashboardPage, &DashboardPage::currentOrderChanged,
|
|
|
|
|
|
this, [this](const QString &orderNo) {
|
|
|
|
|
|
currentOrderLabel->setText("订单:" + orderNo);
|
|
|
|
|
|
});
|
|
|
|
|
|
connect(dashboardPage, &DashboardPage::currentWorkpieceChanged,
|
|
|
|
|
|
this, [this](const QString &workpiece) {
|
|
|
|
|
|
currentWorkpieceLabel->setText("工件:" + workpiece);
|
|
|
|
|
|
});
|
2026-08-23 09:41:44 +08:00
|
|
|
|
connect(dashboardPage, &DashboardPage::deviceStatusChanged,
|
|
|
|
|
|
devicePage, &DevicePage::updateStatus);
|
2026-08-24 17:28:59 +08:00
|
|
|
|
connect(dashboardPage, &DashboardPage::deviceStatusChanged,
|
|
|
|
|
|
this, [this](const DeviceStatus &status) {
|
|
|
|
|
|
const QString plc = !status.statusValid ? "未采集"
|
|
|
|
|
|
: status.plcConnected ? "在线" : "离线";
|
|
|
|
|
|
const QString robot = !status.statusValid ? "未采集"
|
|
|
|
|
|
: status.robotReady ? "就绪" : "未就绪";
|
|
|
|
|
|
const QString safety = !status.statusValid ? "未采集"
|
|
|
|
|
|
: status.safetyDoorClosed && !status.emergencyStop ? "正常" : "异常";
|
|
|
|
|
|
plcStatusLabel->setText("PLC:" + plc);
|
|
|
|
|
|
robotStatusLabel->setText("机器人:" + robot);
|
|
|
|
|
|
safetyStatusLabel->setText("安全回路:" + safety);
|
|
|
|
|
|
});
|
2026-08-23 09:41:44 +08:00
|
|
|
|
connect(dashboardPage, &DashboardPage::deviceCommunicationError,
|
|
|
|
|
|
devicePage, &DevicePage::handleCommunicationError);
|
2026-08-18 09:40:43 +08:00
|
|
|
|
connect(dashboardPage, &DashboardPage::logRequested, this, &Widget::appendLog);
|
|
|
|
|
|
connect(orderPage, &OrderPage::logRequested, this, &Widget::appendLog);
|
|
|
|
|
|
connect(rackPage, &RackPage::logRequested, this, &Widget::appendLog);
|
2026-10-27 22:16:55 +08:00
|
|
|
|
connect(visionService, &VisionService::logMessage, this, [this](const QString &message) {
|
|
|
|
|
|
appendLog("生产日志", "视觉引导", "通信/测量", message, "记录");
|
|
|
|
|
|
});
|
2026-10-29 11:57:47 +08:00
|
|
|
|
connect(visionService, &VisionService::availabilityChanged,
|
|
|
|
|
|
this, [this](bool ready, bool, const QString &session) {
|
|
|
|
|
|
if (!ConfigManager::instance().visionConfig().enabled) {
|
|
|
|
|
|
cameraStatusLabel->setText("相机:未启用");
|
|
|
|
|
|
} else if (session.isEmpty()) {
|
|
|
|
|
|
cameraStatusLabel->setText("相机:离线");
|
|
|
|
|
|
} else {
|
|
|
|
|
|
cameraStatusLabel->setText(ready ? "相机:就绪" : "相机:在线");
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
if (ConfigManager::instance().visionConfig().enabled)
|
|
|
|
|
|
QTimer::singleShot(0, visionService, &VisionService::startService);
|
2026-08-18 09:40:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Widget::appendLog(const QString &type, const QString &module, const QString &action,
|
|
|
|
|
|
const QString &content, const QString &status)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (dashboardPage) {
|
|
|
|
|
|
dashboardPage->addLog(type, module, content, status);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (logPage) {
|
|
|
|
|
|
logPage->addLog(type, module, action, content, status);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QString error;
|
|
|
|
|
|
if (!LogService::instance().record(type, module, action, content, status, &error)) {
|
|
|
|
|
|
qWarning() << "Failed to persist operation log:" << error;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|