Files
EJM_Display/Pages/P00_PublicPage.cpp
2025-08-20 23:06:28 +08:00

339 lines
15 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 "P00_PublicPage.h"
#include "ui_P00_PublicPage.h"
#include <QDebug>
#include <QVBoxLayout>
#include <QMovie>
P00_PublicPage::P00_PublicPage(QWidget *parent)
: QWidget(parent)
, ui(new Ui::P00_PublicPage)
, m_opcManager(new OpcUaManager(this))
, m_dataCenter(new DataCenter(m_opcManager, this))
{
ui->setupUi(this);
CursorInit();
WinInit();
SignalSlotInit();
// 启动连接
m_opcManager->connectToServer(
"opc.tcp://zmj_plc:4840",
"ZMJ",
"Zmj@123..."
);
}
P00_PublicPage::~P00_PublicPage()
{
delete ui;
}
void P00_PublicPage::CursorInit()
{
/* 隐藏系统光标 */
QApplication::setOverrideCursor(QCursor(Qt::BlankCursor));
/* 自绘光标 */
m_cursorLabel = new QLabel(this);
m_cursorLabel->setPixmap(QPixmap(":/Icos/Icos/Sursor.png"));
m_cursorLabel->setFixedSize(m_cursorLabel->pixmap()->size());
m_cursorLabel->setAttribute(Qt::WA_TransparentForMouseEvents);
m_cursorLabel->raise(); // 让光标在最上层
m_cursorLabel->show();
/* 光标控制器 */
gCursor = new CursorController(this);
connect(gCursor, &CursorController::cursorMoved,
this, &P00_PublicPage::onCursorMoved);
gCursor->cfg.step = 5; // 每次移动 5px
gCursor->cfg.wrap = false; // 不环绕
gCursor->cfg.emulate = true; // 允许模拟点击
setFocusPolicy(Qt::StrongFocus);
setFocus();
activateWindow();
grabKeyboard();
}
void P00_PublicPage::onCursorMoved(const QPoint &global)
{
QPoint local = mapFromGlobal(global);
m_cursorLabel->move(local - m_cursorLabel->rect().center());
}
void P00_PublicPage::WinInit(){
this->move(0,0);
setWindowFlag(Qt::FramelessWindowHint ); // 设置窗口无边框,设置后窗口无法移动
setAttribute(Qt::WA_TranslucentBackground, true);
ui->But_Page_0->setChecked(0);
Pub_MianPage = new P01_MianPage(this);
Pub_MianPage->setWindowFlags(Qt::Widget);
if (!ui->widgetContainer->layout()) {ui->widgetContainer->setLayout(new QVBoxLayout);}
ui->widgetContainer->layout()->addWidget(Pub_MianPage);
Pub_MianPage->show();
// Pub_ShieldPage = new P02_ShieldPage(this);
// Pub_ShieldPage->setWindowFlags(Qt::Widget);
// if (!ui->widgetContainer->layout()) {ui->widgetContainer->setLayout(new QVBoxLayout);}
// ui->widgetContainer->layout()->addWidget(Pub_ShieldPage);
// Pub_ShieldPage->hide();
// Pub_CylinderPage = new P07_CylinderPage(this);
// Pub_CylinderPage->setWindowFlags(Qt::Widget); // 设置窗口标志为Widget
// if (!ui->widgetContainer->layout()) {ui->widgetContainer->setLayout(new QVBoxLayout);}
// ui->widgetContainer->layout()->addWidget(Pub_CylinderPage);
// Pub_CylinderPage->hide();
// Pub_AlarmPage = new P08_AlarmPage(this);
// Pub_AlarmPage->setWindowFlags(Qt::Widget); // 设置窗口标志为Widget
// if (!ui->widgetContainer->layout()) {ui->widgetContainer->setLayout(new QVBoxLayout);}
// ui->widgetContainer->layout()->addWidget(Pub_AlarmPage);
// Pub_AlarmPage->hide();
// Pub_IntelligentPage = new P10_IntelligentPage(this);
// Pub_IntelligentPage->setWindowFlags(Qt::Widget); // 设置窗口标志为Widget
// if (!ui->widgetContainer->layout()) {ui->widgetContainer->setLayout(new QVBoxLayout);}
// ui->widgetContainer->layout()->addWidget(Pub_IntelligentPage);
// Pub_IntelligentPage->hide();
// Pub_RemotePage = new P09_RemotePage(this);
// Pub_RemotePage->setWindowFlags(Qt::Widget); // 设置窗口标志为Widget
// if (!ui->widgetContainer->layout()) {ui->widgetContainer->setLayout(new QVBoxLayout);}
// ui->widgetContainer->layout()->addWidget(Pub_RemotePage);
// Pub_RemotePage->hide();
// Pub_IsolationPage = new P11_IsolationPage(this);
// Pub_IsolationPage->setWindowFlags(Qt::Widget); // 设置窗口标志为Widget
// if (!ui->widgetContainer->layout()) {ui->widgetContainer->setLayout(new QVBoxLayout);}
// ui->widgetContainer->layout()->addWidget(Pub_IsolationPage);
// Pub_IsolationPage->hide();
// Pub_NetworkPage = new P12_NetworkPage(this);
// Pub_NetworkPage->setWindowFlags(Qt::Widget); // 设置窗口标志为Widget
// if (!ui->widgetContainer->layout()) {ui->widgetContainer->setLayout(new QVBoxLayout);}
// ui->widgetContainer->layout()->addWidget(Pub_NetworkPage);
// Pub_InsPage = new P13_InsPage(this);
// Pub_InsPage->setWindowFlags(Qt::Widget); // 设置窗口标志为Widget
// if (!ui->widgetContainer->layout()) {ui->widgetContainer->setLayout(new QVBoxLayout);}
// ui->widgetContainer->layout()->addWidget(Pub_InsPage);
// Pub_InsPage->hide();
// Pub_AtlasPage = new P14_AtlasPage(this);
// Pub_AtlasPage->setWindowFlags(Qt::Widget); // 设置窗口标志为Widget
// if (!ui->widgetContainer->layout()) {ui->widgetContainer->setLayout(new QVBoxLayout);}
// ui->widgetContainer->layout()->addWidget(Pub_AtlasPage);
// Pub_AtlasPage->hide();
// Pub_ParameterPage = new P15_ParameterPage(this);
// Pub_ParameterPage->setWindowFlags(Qt::Widget); // 设置窗口标志为Widget
// if (!ui->widgetContainer->layout()) {ui->widgetContainer->setLayout(new QVBoxLayout);}
// ui->widgetContainer->layout()->addWidget(Pub_ParameterPage);
// Pub_ParameterPage->hide();
// Pub_SettingPage = new P16_SettingPage(this);
// Pub_SettingPage->setWindowFlags(Qt::Widget); // 设置窗口标志为Widget
// if (!ui->widgetContainer->layout()) {ui->widgetContainer->setLayout(new QVBoxLayout);}
// ui->widgetContainer->layout()->addWidget(Pub_SettingPage);
// Pub_SettingPage->hide();
// Pub_PumpPage = new P301_PumpPage(this);
// Pub_PumpPage->setWindowFlags(Qt::Widget); // 设置窗口标志为Widget
// if (!ui->widgetContainer->layout()) {ui->widgetContainer->setLayout(new QVBoxLayout);}
// ui->widgetContainer->layout()->addWidget(Pub_PumpPage);
// Pub_PumpPage->hide();
// Pub_CuttingPage = new P401_CuttingPage(this);
// Pub_CuttingPage->setWindowFlags(Qt::Widget); // 设置窗口标志为Widget
// if (!ui->widgetContainer->layout()) {ui->widgetContainer->setLayout(new QVBoxLayout);}
// ui->widgetContainer->layout()->addWidget(Pub_CuttingPage);
// Pub_CuttingPage->hide();
// Pub_LoaderPage = new P501_LoaderPage(this);
// Pub_LoaderPage->setWindowFlags(Qt::Widget); // 设置窗口标志为Widget
// if (!ui->widgetContainer->layout()) {ui->widgetContainer->setLayout(new QVBoxLayout);}
// ui->widgetContainer->layout()->addWidget(Pub_LoaderPage);
// Pub_LoaderPage->hide();
// Pub_TransportPage = new P601_TransportPage(this);
// Pub_TransportPage->setWindowFlags(Qt::Widget); // 设置窗口标志为Widget
// if (!ui->widgetContainer->layout()) {ui->widgetContainer->setLayout(new QVBoxLayout);}
// ui->widgetContainer->layout()->addWidget(Pub_TransportPage);
// Pub_TransportPage->hide();
// QMovie *MLoading = new QMovie("./Gif/Loading.gif");
// ui->Bq_Loading->setMovie(MLoading); // 1. 设置要显示的 GIF 动画图片
// MLoading->start(); // 2. 启动动画
// ui->Bq_Loading->hide();
//所用定时器初始化
QTimer* UIRefresh_Timer = new QTimer(this);
connect(UIRefresh_Timer, &QTimer::timeout, this, &P00_PublicPage::UIRefreshTimeOut);
UIRefresh_Timer->setInterval(100); // 设置定时器间隔为 1000 毫秒1 秒)
UIRefresh_Timer->start();
//所用定时器初始化
QTimer* CursorScan_Timer = new QTimer(this);
connect(CursorScan_Timer, &QTimer::timeout, this, &P00_PublicPage::CursorScanTimerOut);
CursorScan_Timer->setInterval(50); // 设置定时器间隔为 1000 毫秒1 秒)
CursorScan_Timer->start();
}
void P00_PublicPage::SignalSlotInit(){
m_dataCenter->InitData();
// 订阅数据中心的更新(仅展示)
connect(m_dataCenter, &DataCenter::nodeValueUpdated,
this, &P00_PublicPage::onNodeValueUpdated);
// 订阅OPC状态变化仅展示
connect(m_opcManager, &OpcUaManager::stateChanged,
this, &P00_PublicPage::onOpcStateChanged);
// 订阅错误信息(仅展示)
connect(m_dataCenter, &DataCenter::errorOccurred,
this, &P00_PublicPage::onOpcError);
}
void P00_PublicPage::keyPressEvent(QKeyEvent *e)
{
gCursor->handleKey(e->key()); // 一句话完成所有工作
}
// 处理节点值更新仅更新UI展示
void P00_PublicPage::onNodeValueUpdated(const QString &nodeId, const QString &nodeName,const QString &varName, const QVariant &value)
{
Q_UNUSED(nodeId); // 不关心ID只根据名称更新UI
//qDebug()<<varName<<nodeName<<value.toString();
//qDebug()<<gOPC_NodeValue["ns=6;s=::AsGlobalPV:IN_SystemVal.U"];
}
// 处理连接状态变化仅更新UI展示
void P00_PublicPage::onOpcStateChanged(QOpcUaClient::ClientState state)
{
QString stateText;
switch (state) {
case QOpcUaClient::Disconnected:
ui->LED_ConnectionStatus->show();
//ui->Bq_Loading->show();
m_dataCenter->stopCyclicRead();
PLC_Started = false;
break;
case QOpcUaClient::Connecting:
ui->LED_ConnectionStatus->show();
//ui->Bq_Loading->show();
m_dataCenter->stopCyclicRead();
PLC_Started = false;
break;
case QOpcUaClient::Connected: {
ui->LED_ConnectionStatus->hide();
//ui->Bq_Loading->hide();
// 连接成功后通知数据中心开始读取(仅触发,不处理读取逻辑)
m_dataCenter->startCyclicRead(3);
break;
}
default: stateText = "未知状态";
}
}
// 处理错误信息(仅展示错误弹窗)
void P00_PublicPage::onOpcError(const QString &errorString)
{
if(errorString == "获取端点失败,状态码: -2136080384"){
qCritical()<<"连接OPC UA 服务器失败";
// // 启动连接
// m_opcManager->connectToServer(
// "opc.tcp://zmj_plc:4840",
// "ZMJ",
// "Zmj@123..."
// );
}else{
qCritical()<<"读取 OPC UA 错误:"<<errorString;
}
}
void P00_PublicPage::UIRefreshTimeOut(){
// 获取当前时间
QDateTime currentTime = QDateTime::currentDateTime();
QString currentTimeStr = currentTime.toString("yyyy-MM-dd HH:mm:ss");
ui->Lable_SystemTime->setText(currentTimeStr);
ui->LCD_SystemVal_U1->display(gOPC_NodeValue["ns=6;s=::AsGlobalPV:IN_SystemVal.Filtered30_U"] .toReal());
ui->LCD_SystemVal_V1->display(gOPC_NodeValue["ns=6;s=::AsGlobalPV:IN_SystemVal.Filtered30_V"] .toReal());
ui->LCD_SystemVal_W1->display(gOPC_NodeValue["ns=6;s=::AsGlobalPV:IN_SystemVal.Filtered30_W"] .toReal());
}
void P00_PublicPage::CursorScanTimerOut(){
bool HomePage = gOPC_NodeValue["ns=6;s=::AsGlobalPV:I_CursorControl.HomePage"] .toBool();
bool PageUp = gOPC_NodeValue["ns=6;s=::AsGlobalPV:I_CursorControl.PageUp"] .toBool();
bool PageDown = gOPC_NodeValue["ns=6;s=::AsGlobalPV:I_CursorControl.PageDown"] .toBool();
bool Setting = gOPC_NodeValue["ns=6;s=::AsGlobalPV:I_CursorControl.Setting"] .toBool();
bool Increase = gOPC_NodeValue["ns=6;s=::AsGlobalPV:I_CursorControl.Increase"] .toBool();
bool Reduce = gOPC_NodeValue["ns=6;s=::AsGlobalPV:I_CursorControl.Reduce"] .toBool();
bool Increases = gOPC_NodeValue["ns=6;s=::AsGlobalPV:I_CursorControl.Increases"] .toBool();
bool Reduces = gOPC_NodeValue["ns=6;s=::AsGlobalPV:I_CursorControl.Reduces"] .toBool();
bool Return = gOPC_NodeValue["ns=6;s=::AsGlobalPV:I_CursorControl.Return"] .toBool();
bool Confirm = gOPC_NodeValue["ns=6;s=::AsGlobalPV:I_CursorControl.Confirm"] .toBool();
bool Up = gOPC_NodeValue["ns=6;s=::AsGlobalPV:I_CursorControl.Up"] .toBool();
bool Down = gOPC_NodeValue["ns=6;s=::AsGlobalPV:I_CursorControl.Down"] .toBool();
bool Left = gOPC_NodeValue["ns=6;s=::AsGlobalPV:I_CursorControl.Left"] .toBool();
bool Right = gOPC_NodeValue["ns=6;s=::AsGlobalPV:I_CursorControl.Right"] .toBool();
bool Fn = gOPC_NodeValue["ns=6;s=::AsGlobalPV:I_CursorControl.Fn"] .toBool();
bool Cursor = gOPC_NodeValue["ns=6;s=::AsGlobalPV:I_CursorControl.Cursor"] .toBool();
// if(HomePage) {gCursor->handleKey(1234);}
// if(PageUp) {gCursor->handleKey(1234);}
// if(PageDown) {gCursor->handleKey(1234);}
// if(Setting) {gCursor->handleKey(1234);}
// if(Increase) {gCursor->handleKey(1234);}
// if(Reduce) {gCursor->handleKey(1234);}
// if(Increases) {gCursor->handleKey(1234);}
// if(Reduces) {gCursor->handleKey(1234);}
if(Return) {gCursor->handleKey(Qt::Key_Enter);}
if(Confirm) {gCursor->handleKey(Qt::Key_Return);}
if(Up) {gCursor->handleKey(Qt::Key_Up);}
if(Down) {gCursor->handleKey(Qt::Key_Down);}
if(Left) {gCursor->handleKey(Qt::Key_Left);}
if(Right) {gCursor->handleKey(Qt::Key_Right);}
// if(Fn) {gCursor->handleKey(1234)};
// if(Cursor) {gCursor->handleKey(1234)};
}
void P00_PublicPage::on_But_Close_clicked()
{
qApp->quit();
}
template <typename SubWidget>
inline void P00_PublicPage::replaceSingleWidget()
{
QWidget *container = ui->widgetContainer;
if (!container) return;
if (!container->layout())
container->setLayout(new QVBoxLayout(container));
// 删除旧控件
if (QWidget *old = container->findChild<QWidget*>())
old->deleteLater();
// 创建并嵌入新控件
SubWidget *w = new SubWidget(container);
w->setWindowFlags(Qt::Widget);
container->layout()->addWidget(w);
}
void P00_PublicPage::on_But_Page_0_clicked(){ replaceSingleWidget<P01_MianPage>(); }
void P00_PublicPage::on_But_Page_1_clicked(){ replaceSingleWidget<P02_ShieldPage>(); }
void P00_PublicPage::on_But_Page_2_clicked(){ replaceSingleWidget<P301_PumpPage>(); }
void P00_PublicPage::on_But_Page_3_clicked(){ replaceSingleWidget<P401_CuttingPage>(); }
void P00_PublicPage::on_But_Page_4_clicked(){ replaceSingleWidget<P501_LoaderPage>(); }
void P00_PublicPage::on_But_Page_5_clicked(){ replaceSingleWidget<P601_TransportPage>(); }
void P00_PublicPage::on_But_Page_6_clicked(){ replaceSingleWidget<P07_CylinderPage>(); }
void P00_PublicPage::on_But_Page_7_clicked(){ replaceSingleWidget<P08_AlarmPage>(); }
void P00_PublicPage::on_But_Page_8_clicked(){ replaceSingleWidget<P09_RemotePage>(); }
void P00_PublicPage::on_But_Page_9_clicked(){ replaceSingleWidget<P10_IntelligentPage>(); }
void P00_PublicPage::on_But_Page_10_clicked(){ replaceSingleWidget<P11_IsolationPage>(); }
void P00_PublicPage::on_But_Page_11_clicked(){ replaceSingleWidget<P12_NetworkPage>(); }
void P00_PublicPage::on_But_Page_12_clicked(){ replaceSingleWidget<P13_InsPage>(); }
void P00_PublicPage::on_But_Page_13_clicked(){ replaceSingleWidget<P14_AtlasPage>(); }
void P00_PublicPage::on_But_Page_14_clicked(){ replaceSingleWidget<P15_ParameterPage>(); }
void P00_PublicPage::on_But_Page_15_clicked(){ replaceSingleWidget<P16_SettingPage>(); }