223 lines
9.1 KiB
C++
223 lines
9.1 KiB
C++
#include "widget.h"
|
||
#include "ui_widget.h"
|
||
|
||
#include "pages/dashboardpage.h"
|
||
#include "pages/devicepage.h"
|
||
#include "pages/logpage.h"
|
||
#include "pages/orderpage.h"
|
||
#include "pages/qualitypage.h"
|
||
#include "pages/rackpage.h"
|
||
#include "pages/uihelpers.h"
|
||
#include "pages/visionpage.h"
|
||
#include "services/logservice.h"
|
||
#include "services/visionservice.h"
|
||
#include "config/configmanager.h"
|
||
|
||
#include <QDebug>
|
||
#include <QFrame>
|
||
#include <QHBoxLayout>
|
||
#include <QLabel>
|
||
#include <QListWidget>
|
||
#include <QStackedWidget>
|
||
#include <QTimer>
|
||
#include <QVBoxLayout>
|
||
|
||
Widget::Widget(QWidget *parent)
|
||
: QWidget(parent)
|
||
, ui(new Ui::Widget)
|
||
, navList(nullptr)
|
||
, pageStack(nullptr)
|
||
, dashboardPage(nullptr)
|
||
, logPage(nullptr)
|
||
, qualityPage(nullptr)
|
||
, visionService(nullptr)
|
||
, visionPage(nullptr)
|
||
, systemStateLabel(nullptr)
|
||
, currentOrderLabel(nullptr)
|
||
, currentWorkpieceLabel(nullptr)
|
||
, currentStepLabel(nullptr)
|
||
, plcStatusLabel(nullptr)
|
||
, robotStatusLabel(nullptr)
|
||
, safetyStatusLabel(nullptr)
|
||
, cameraStatusLabel(nullptr)
|
||
{
|
||
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);
|
||
|
||
appendLog("生产日志", "系统", "启动", "生产控制系统已启动", "成功");
|
||
}
|
||
|
||
Widget::~Widget()
|
||
{
|
||
if (visionService) visionService->shutdown();
|
||
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");
|
||
currentOrderLabel = UiHelpers::makeBadge("订单:-", "#455a64");
|
||
currentWorkpieceLabel = UiHelpers::makeBadge("工件:-", "#455a64");
|
||
currentStepLabel = UiHelpers::makeBadge("工序:未采集", "#607d8b");
|
||
|
||
topLayout->addWidget(systemStateLabel);
|
||
topLayout->addWidget(currentOrderLabel);
|
||
topLayout->addWidget(currentWorkpieceLabel);
|
||
topLayout->addWidget(currentStepLabel);
|
||
topLayout->addStretch();
|
||
plcStatusLabel = UiHelpers::makeBadge("PLC:未采集", "#607d8b");
|
||
robotStatusLabel = UiHelpers::makeBadge("机器人:未采集", "#607d8b");
|
||
safetyStatusLabel = UiHelpers::makeBadge("安全回路:未采集", "#607d8b");
|
||
cameraStatusLabel = UiHelpers::makeBadge(
|
||
ConfigManager::instance().visionConfig().enabled
|
||
? "相机:离线" : "相机:未启用",
|
||
"#607d8b");
|
||
topLayout->addWidget(plcStatusLabel);
|
||
topLayout->addWidget(robotStatusLabel);
|
||
topLayout->addWidget(safetyStatusLabel);
|
||
topLayout->addWidget(cameraStatusLabel);
|
||
|
||
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);
|
||
navList->addItems(QStringList() << "控制面板" << "订单管理" << "料架/托盘"
|
||
<< "设备状态" << "日志" << "质量管理" << "视觉引导");
|
||
navList->setCurrentRow(0);
|
||
bodyLayout->addWidget(navList);
|
||
|
||
dashboardPage = new DashboardPage(this);
|
||
OrderPage *orderPage = new OrderPage(this);
|
||
RackPage *rackPage = new RackPage(this);
|
||
DevicePage *devicePage = new DevicePage(this);
|
||
logPage = new LogPage(this);
|
||
qualityPage = new QualityPage(this);
|
||
visionService = new VisionService(ConfigManager::instance().visionConfig(), this);
|
||
visionPage = new VisionPage(visionService, this);
|
||
connect(dashboardPage, &DashboardPage::visionCycleChanged, visionPage, &VisionPage::updateCycle);
|
||
connect(dashboardPage, &DashboardPage::visionProductionActiveChanged, visionPage, &VisionPage::setProductionActive);
|
||
dashboardPage->attachVisionService(visionService);
|
||
|
||
pageStack = new QStackedWidget;
|
||
pageStack->addWidget(dashboardPage);
|
||
pageStack->addWidget(orderPage);
|
||
pageStack->addWidget(rackPage);
|
||
pageStack->addWidget(devicePage);
|
||
pageStack->addWidget(logPage);
|
||
pageStack->addWidget(qualityPage);
|
||
pageStack->addWidget(visionPage);
|
||
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);
|
||
});
|
||
connect(dashboardPage, &DashboardPage::currentOrderChanged,
|
||
this, [this](const QString &orderNo) {
|
||
currentOrderLabel->setText("订单:" + orderNo);
|
||
});
|
||
connect(dashboardPage, &DashboardPage::currentWorkpieceChanged,
|
||
this, [this](const QString &workpiece) {
|
||
currentWorkpieceLabel->setText("工件:" + workpiece);
|
||
});
|
||
connect(dashboardPage, &DashboardPage::deviceStatusChanged,
|
||
devicePage, &DevicePage::updateStatus);
|
||
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);
|
||
});
|
||
connect(dashboardPage, &DashboardPage::deviceCommunicationError,
|
||
devicePage, &DevicePage::handleCommunicationError);
|
||
connect(dashboardPage, &DashboardPage::logRequested, this, &Widget::appendLog);
|
||
connect(orderPage, &OrderPage::logRequested, this, &Widget::appendLog);
|
||
connect(rackPage, &RackPage::logRequested, this, &Widget::appendLog);
|
||
connect(visionService, &VisionService::logMessage, this, [this](const QString &message) {
|
||
appendLog("生产日志", "视觉引导", "通信/测量", message, "记录");
|
||
});
|
||
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);
|
||
}
|
||
|
||
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;
|
||
}
|
||
}
|