#include "P12_NetworkPage.h" #include "ui_P12_NetworkPage.h" #include #include #include #include #include #include #include class ReadOnlyItem : public QTableWidgetItem { public: explicit ReadOnlyItem(const QString &text) : QTableWidgetItem(text) { setFlags(flags() & ~Qt::ItemIsEditable); // 去掉可编辑标志 } }; P12_NetworkPage::P12_NetworkPage(QWidget *parent) : QWidget(parent), ui(new Ui::P12_NetworkPage) { ui->setupUi(this); QTimer::singleShot(1000, this, &P12_NetworkPage::WinInit); } P12_NetworkPage::~P12_NetworkPage() { delete ui; } void P12_NetworkPage::WinInit() { ui->MainWidget->setCurrentIndex(0); gPageIndexStr12 = "P12.0"; for (uint8_t i=0;i<9;i++) { QString FileName = QString("画面配置/UI_IO扩展页面%1_配置.csv").arg(i+1); QString TableWidgetObjName = QString("TableWidget_PLC_%1").arg(i+1); mCsvConfig[i].FileName = ConfigurationPath + FileName; QFileInfo checkFile(mCsvConfig[i].FileName); if (checkFile.exists() && checkFile.isFile()) { QTableWidget* TableWidget = findChild(TableWidgetObjName); if(TableWidget) TableViewInit(TableWidget,&mCsvConfig[i]); } //gPageName["P12.0"]) QPushButton* Button = findChild(QString("But_Network_%1").arg(i)); if(Button){ Button->setText(gPageName["P12."+QString::number(i)]); } } // mCsvConfig[0].FileName = ConfigurationPath + "画面配置/UI_IO扩展页面1_配置.csv"; // mCsvConfig[1].FileName = ConfigurationPath + "画面配置/UI_IO扩展页面2_配置.csv"; // mCsvConfig[2].FileName = ConfigurationPath + "画面配置/UI_IO扩展页面3_配置.csv"; // mCsvConfig[3].FileName = ConfigurationPath + "画面配置/UI_IO扩展页面4_配置.csv"; // mCsvConfig[4].FileName = ConfigurationPath + "画面配置/UI_IO扩展页面5_配置.csv"; // mCsvConfig[5].FileName = ConfigurationPath + "画面配置/UI_IO扩展页面6_配置.csv"; // mCsvConfig[6].FileName = ConfigurationPath + "画面配置/UI_IO扩展页面7_配置.csv"; // mCsvConfig[7].FileName = ConfigurationPath + "画面配置/UI_IO扩展页面8_配置.csv"; // mCsvConfig[8].FileName = ConfigurationPath + "画面配置/UI_IO扩展页面9_配置.csv"; // TableViewInit(ui->TableWidget_PLC_1,&mCsvConfig[0]); // TableViewInit(ui->TableWidget_PLC_2,&mCsvConfig[1]); // TableViewInit(ui->TableWidget_PLC_3,&mCsvConfig[2]); //所用定时器初始化 QTimer* UIRefresh_Timer = new QTimer(this); connect(UIRefresh_Timer, &QTimer::timeout, this, &P12_NetworkPage::UIRefreshTimeOut); UIRefresh_Timer->setInterval(100); // 设置定时器间隔为 1000 毫秒(1 秒) UIRefresh_Timer->start(); } void P12_NetworkPage::TableViewInit( QTableWidget *tw,tsCsvConfigText *CCT){ tw->move(2,5); tw->setSizeIncrement(1023,512); tw->setRowCount(16); tw->setColumnCount(15); tw->horizontalHeader()->setVisible(false); tw->verticalHeader()->setVisible(false); tw->horizontalHeader()->setMinimumSectionSize(0); for (uint8_t i=0; i<16;i++) { tw->setRowHeight(i,32); } for (int i=0;i<4;i++) { //qDebug()<<0+i*4<<1+i*4<<2+i*4<<3+i*4; tw->setColumnWidth(0+i*4, 42); // 60+200+10+ tw->setColumnWidth(1+i*4, 150); tw->setColumnWidth(2+i*4, 60); if(3+i*4<15) tw->setColumnWidth(3+i*4, 4); } QList FileData = ConfigFiles().ReadFile_Csv(CCT->FileName); for (int row = 1; row < FileData.size()-1; ++row) { CCT->Type.append(FileData.at(row)[1]); CCT->PinName.append(FileData.at(row)[2]); QString Text = FileData.at(row)[3]; Text.replace("\\n","\n",Qt::CaseInsensitive); CCT->ShowText.append(Text); CCT->TColor.append(FileData.at(row)[4]); CCT->FColor.append(FileData.at(row)[5]); CCT->DataNode.append(FileData.at(row)[6]); } for (uint8_t i=0;i<16;i++) { tw->setItem(i,0,new ReadOnlyItem(CCT->PinName[i])); tw->setItem(i,1,new ReadOnlyItem(CCT->ShowText[i])); tw->setItem(i,4,new ReadOnlyItem(CCT->PinName[i+16])); tw->setItem(i,5,new ReadOnlyItem(CCT->ShowText[i+16])); tw->setItem(i,8,new ReadOnlyItem(CCT->PinName[i+32])); tw->setItem(i,9,new ReadOnlyItem(CCT->ShowText[i+32])); tw->setItem(i,12,new ReadOnlyItem(CCT->PinName[i+48])); tw->setItem(i,13,new ReadOnlyItem(CCT->ShowText[i+48])); } } void P12_NetworkPage::TableViewDataUpdate( QTableWidget *tw,tsCsvConfigText *CCT){ for (uint8_t i=0;i<16;i++) { for (uint8_t j=0;j<4;j++) { uint8_t Index = i+j*16; if(CCT->Type[Index] == "数值"){ if(CCT->DataNode[Index] == "Q_OilCylindShorten.TrackR.OutPWM") qDebug()<DataNode[Index])<DataNode[Index]), 2); auto *item = new ReadOnlyItem(variantToFormattedString(getNodeValue(CCT->DataNode[Index]), 2)); item->setForeground(QBrush(getColor(CCT->TColor[Index]))); item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter); tw->setItem(i, 2 + j * 4, item); }else if(CCT->Type[Index] != ""){ if(CCT->DataNode[Index].contains("#")){ QList NodeId = CCT->DataNode[Index].split("#"); bool TmpBool = getBitOf32Data(getNodeValue(NodeId[0]).toUInt(),NodeId[1].toInt()); tw->setItem(i,2+j*4,makeColorItem(CCT->Type[Index],TmpBool,CCT->FColor[Index],CCT->TColor[Index])); } } } } } inline QTableWidgetItem *P12_NetworkPage::makeColorItem(const QString &Type, const bool Color, const QString &Fcolor, const QString &TFcolor, bool editable) { QColor c; QString text; c = Color?getColor(TFcolor):getColor(Fcolor); if(Type == "输入") text = "●"; else if(Type == "输出") text = "■"; else text = "★"; QTableWidgetItem *item = new QTableWidgetItem(text); item->setForeground(QBrush(c)); item->setTextAlignment(Qt::AlignCenter); // 居中 QFont f = item->font(); f.setPointSize(20); item->setFont(f); if (!editable) item->setFlags(item->flags() & ~Qt::ItemIsEditable); return item; } void P12_NetworkPage::UIRefresh_Page0() { // // 使用多核心管理器异步获取网络数据 // MultiCoreManager::instance()->submitTask([this]() { // // 准备数据容器 // QMap networkData; // // 异步读取网络状态数据 // networkData["NetworkStatus"] = getNodeValue("PageContol.NetworkStatus"); // networkData["RemoteNetStatus"] = getNodeValue("PageContol.RemoteNetStatus"); // networkData["NetworkConnectBit"] = getNodeValue("PageContol.NetworkConnectBit"); // // 异步读取网络配置数据 // networkData["RemoteIP"] = getNodeValue("PageContol.RemoteIP"); // networkData["RemotePort"] = getNodeValue("PageContol.RemotePort"); // networkData["LocalIP"] = getNodeValue("PageContol.LocalIP"); // networkData["LocalGateway"] = getNodeValue("PageContol.LocalGateway"); // networkData["LocalNetmask"] = getNodeValue("PageContol.LocalNetmask"); // networkData["LocalPort"] = getNodeValue("PageContol.LocalPort"); // // 在主线程中更新UI // QMetaObject::invokeMethod(this, [this, networkData]() { // // 本地网络连接状态 // ui->Label_LocalNetSta->setText(networkData["NetworkStatus"].toString()); // ui->Label_LocalNetSta->setStyleSheet(StyleSet(ui->Label_LocalNetSta, // getBitOf32Data(networkData["NetworkConnectBit"].toUInt(), 0, 0), // BitMaps().NetSta_Styles)); // // 远程网络连接状态 // ui->Label_RemoteNetSta->setText(networkData["RemoteNetStatus"].toString()); // ui->Label_RemoteNetSta->setStyleSheet(StyleSet(ui->Label_RemoteNetSta, // getBitOf32Data(networkData["NetworkConnectBit"].toUInt(), 1, 0), // BitMaps().NetSta_Styles)); // // 远程IP // ui->LineEdit_RemoteIP->setText(networkData["RemoteIP"].toString()); // // 远程端口 // ui->LineEdit_RemotePort->setText(networkData["RemotePort"].toString()); // // 本地IP // ui->LineEdit_LocalIP->setText(networkData["LocalIP"].toString()); // // 本地网关 // ui->LineEdit_LocalGateway->setText(networkData["LocalGateway"].toString()); // // 子网掩码 // ui->LineEdit_LocalNetmask->setText(networkData["LocalNetmask"].toString()); // // 本地端口 // ui->LineEdit_LocalPort->setText(networkData["LocalPort"].toString()); // }, Qt::QueuedConnection); // }, "network_page0_data_processing"); } void P12_NetworkPage::UIRefresh_Page1(){ } void P12_NetworkPage::UIRefresh_Page2(){} void P12_NetworkPage::UIRefresh_Page3(){} void P12_NetworkPage::UIRefresh_Page4(){} void P12_NetworkPage::UIRefresh_Page5(){} void P12_NetworkPage::UIRefresh_Page6(){} void P12_NetworkPage::UIRefresh_Page7(){} void P12_NetworkPage::UIRefresh_Page8(){} void P12_NetworkPage::UIRefresh_Page9(){} void P12_NetworkPage::UIRefreshTimeOut() { if(!gPageIndexStr.contains("P12")) return; // 使用多核心管理器异步处理数据更新 //MultiCoreManager::instance()->submitTask([this]() { const int currentIndex = ui->MainWidget->currentIndex(); // 根据当前页面索引异步更新数据 if (currentIndex > 0 && currentIndex < 9) { // 准备更新数据 QMap updateFlags; for (int i = 1; i < 9; i++) { if (i == currentIndex) { updateFlags[i] = true; } } // 在主线程中更新UI QMetaObject::invokeMethod(this, [this, currentIndex]() { switch (currentIndex) { case 1:TableViewDataUpdate(ui->TableWidget_PLC_1,&mCsvConfig[0]);break; case 2:TableViewDataUpdate(ui->TableWidget_PLC_2,&mCsvConfig[1]);break; case 3:TableViewDataUpdate(ui->TableWidget_PLC_3,&mCsvConfig[2]);break; case 4:TableViewDataUpdate(ui->TableWidget_PLC_4,&mCsvConfig[3]);break; case 5:TableViewDataUpdate(ui->TableWidget_PLC_5,&mCsvConfig[4]);break; case 6:TableViewDataUpdate(ui->TableWidget_PLC_6,&mCsvConfig[5]);break; case 7:TableViewDataUpdate(ui->TableWidget_PLC_7,&mCsvConfig[6]);break; case 8:TableViewDataUpdate(ui->TableWidget_PLC_8,&mCsvConfig[7]);break; } }, Qt::QueuedConnection); } else if (currentIndex == 0) { // 处理页面0的更新 QMetaObject::invokeMethod(this, &P12_NetworkPage::UIRefresh_Page0, Qt::QueuedConnection); } //}, "network_page_update_task"); } void P12_NetworkPage::on_But_Network_0_clicked(){ slideToIndex(0, "P12.0");} void P12_NetworkPage::on_But_Network_1_clicked(){ slideToIndex(1, "P12.1");} void P12_NetworkPage::on_But_Network_2_clicked(){ slideToIndex(2, "P12.2");} void P12_NetworkPage::on_But_Network_3_clicked(){ slideToIndex(3, "P12.3");} void P12_NetworkPage::on_But_Network_4_clicked(){ slideToIndex(4, "P12.4");} void P12_NetworkPage::on_But_Network_5_clicked(){ slideToIndex(5, "P12.5");} void P12_NetworkPage::on_But_Network_6_clicked(){ slideToIndex(6, "P12.6");} void P12_NetworkPage::on_But_Network_7_clicked(){ slideToIndex(7, "P12.7");} void P12_NetworkPage::on_But_Network_8_clicked(){ slideToIndex(8, "P12.8");} void P12_NetworkPage::on_But_Network_9_clicked(){ slideToIndex(9, "P12.9");} void P12_NetworkPage::on_MainWidget_currentChanged(int arg1) { } void P12_NetworkPage::slideToIndex(int newIndex, QString PageName) { gPageIndexStr = PageName; gPageIndexStr12 = PageName; int direction =0; uint8_t OldIndex = ui->MainWidget->currentIndex(); if (newIndex == OldIndex) return; if(newIndex > OldIndex) direction = 1; else direction = -1; int oldIndex = ui->MainWidget->currentIndex(); QWidget *currentWidget = ui->MainWidget->widget(oldIndex); QWidget *nextWidget = ui->MainWidget->widget(newIndex); if (!currentWidget || !nextWidget) return; // 1. 截图 QPixmap currentPx(currentWidget->size()); currentWidget->render(¤tPx); QPixmap nextPx(nextWidget->size()); nextWidget->render(&nextPx); // 2. 临时层 QWidget *overlay = new QWidget(this); overlay->setGeometry(ui->MainWidget->geometry()); overlay->setStyleSheet("background: transparent;"); QLabel *oldLabel = new QLabel(overlay); oldLabel->setPixmap(currentPx); oldLabel->setFixedSize(currentWidget->size()); QLabel *newLabel = new QLabel(overlay); newLabel->setPixmap(nextPx); newLabel->setFixedSize(nextWidget->size()); newLabel->move(direction * currentWidget->width(), 0); overlay->raise(); overlay->show(); // 3. 动画 int dx = currentWidget->width(); QPropertyAnimation *animOld = new QPropertyAnimation(oldLabel, "pos"); animOld->setDuration(300); animOld->setStartValue(QPoint(0, 0)); animOld->setEndValue(QPoint(-direction * dx, 0)); QPropertyAnimation *animNew = new QPropertyAnimation(newLabel, "pos"); animNew->setDuration(300); animNew->setStartValue(QPoint(direction * dx, 0)); animNew->setEndValue(QPoint(0, 0)); connect(animNew, &QPropertyAnimation::finished, this, [=]() { ui->MainWidget->setCurrentIndex(newIndex); overlay->deleteLater(); }); animOld->start(QAbstractAnimation::DeleteWhenStopped); animNew->start(QAbstractAnimation::DeleteWhenStopped); }