#include "widget.h" #include "ui_widget.h" #include #include #include #include "buttonclickthread.h" #include #include "pdu_ctrl.h" #include Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); //ui->stackedWidget->setCurrentIndex(1); nChidIndex=0; nChidPHIndex=0; nMAXChildIndex=0; m_nAngle=0; m_nWidth = 160; m_nHeight = 128; m_pAngleCtrl=NULL; #ifdef RS485Type #ifdef RS485IpType //ui->label_MSName->hide(); //ui->label_MSData->hide(); //ui->label_3->hide(); //ui->label_4->hide(); //ui->label_5->hide(); ui->label_modbus_type->hide(); ui->label_modbus_Addr->hide(); #else ui->label_MSName->hide(); ui->label_MSData->hide(); ui->label_3->hide(); ui->label_4->hide(); ui->label_5->hide(); #endif #else //#ifdef hightSmartPDU ui->label_modbus_type->hide(); ui->label_modbus_Addr->hide(); //#endif #endif ui->stackedWidget_Main->setCurrentIndex(0); //ui->stackedWidget_Main->setCurrentWidget(ui->page_7); m_pMSGWidget=new MSGWidget(this); m_pMSGWidget->hide(); } Widget::~Widget() { delete ui; } void Widget::setInitData(QGraphicsProxyWidget *pAngleCtrl, QGraphicsView* pView, int nWidth, int nHeight) { m_pAngleCtrl=pAngleCtrl; m_nWidth= nWidth; m_nHeight = nHeight; m_pView=pView; } int Widget::getShowModeIndex() { return ui->stackedWidget->currentIndex(); } int Widget::getShowPagIndex() { return ui->stackedWidget_Main->currentIndex(); } int Widget::getShowSubIndex() { return nChidIndex; } int Widget::getShowPHIndex() { return nChidPHIndex; } void Widget::setShowPHIndex(int nIndex) { nChidPHIndex=nIndex; setPH(nChidIndex,nChidPHIndex); } void Widget::UpdateDate() { QDateTime currentTime = QDateTime::currentDateTime(); QString strDate="Date:"+currentTime.toString("yyyy.MM.dd"); QString strTime="Time:"+currentTime.toString("HH:mm:ss"); if(ui) { ui->label_4->setText(strDate); ui->label_5->setText(strTime); } QString strIP="IP:"+ReadIP(); ui->label_3->setText(strIP); if(pdu_Ctrl::instance()->m_nModbusMSType!=1) { ui->label_modbus_type->setText(QString("ModbusType: %1").arg( "master")); }else { ui->label_modbus_type->setText(QString("ModbusType: %1").arg("slave")); } ui->label_modbus_Addr->setText(QString("ModbusAddress: %1").arg(pdu_Ctrl::instance()->m_nModbusAddress)); } QString Widget::ReadIP() { QProcess process; QString program = "ip"; // 或者 "ifconfig" QStringList arguments; if (program == "ip") { arguments << "addr" << "show" << "eth0"; } else if (program == "ifconfig") { arguments << "eth0"; } process.start(program, arguments); process.waitForFinished(); QString output = process.readAllStandardOutput(); QRegExp ipRegex("\\binet (\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})/\\d{1,2}\\b"); int pos = 0; while ((pos = ipRegex.indexIn(output, pos)) != -1) { return ipRegex.cap(1); } return ""; // 没有找到IP地址 } void Widget::SetData(QLabel *pLab, QString strShow) { if(pLab) { pLab->setText(strShow); } } void Widget::SetV(QString strShow){SetData(ui->label_8,strShow);SetData(ui->label_phV,strShow);} void Widget::SetA(QString strShow){SetData(ui->label_13,strShow);SetData(ui->label_A,strShow);SetData(ui->label_phA,strShow);} void Widget::SetW(QString strShow){SetData(ui->label_16,strShow);SetData(ui->label_W,strShow);SetData(ui->label_phW,strShow);} void Widget::SetF(QString strShow){SetData(ui->label_15,strShow);SetData(ui->label_F,strShow);SetData(ui->label_phF,strShow);} void Widget::SetSUM(QString strShow){SetData(ui->label_14,strShow);SetData(ui->label_S,strShow);SetData(ui->label_phKw,strShow);} void Widget::SetT(QString strShow){SetData(ui->label_Temp,strShow+ " ℃");} void Widget::SetH(QString strShow){SetData(ui->label_Humidity,strShow+" %");} void Widget::SetCO2(QString strShow){SetData(ui->label_CO2,strShow+" PPM");} void Widget::SetVer(QString strShow){SetData(ui->label_2,"Version:"+strShow);} void Widget::SetStatus(QString strShow){SetData(ui->label_V,strShow);} void Widget::SetIP(QString strIP) { if(ui) { ui->label_3->setText(strIP); } } void Widget::setCH(int nIndex) { QString strShow=QString("Output CH%1").arg(nIndex); SetData(ui->label_chName,strShow); } void Widget::setPH(int nIndex,int nPHType) { QString strShow=""; switch (nPHType) { case 1: strShow="-L1"; break; case 2: strShow="-L2"; break; case 3: strShow="-L3"; break; default: break; } QString strPHShow=""; if(nIndex<=0) { strPHShow=QString("Input Vol%1").arg(strShow); }else { strPHShow=QString("Output CH%1%2").arg(nIndex).arg(strShow); } SetData(ui->label_18,strPHShow); qDebug()<<"CH_Select:"<stackedWidget_Main->currentIndex(); if(nIndex==0) { ui->stackedWidget_Main->setCurrentIndex(3); }else if(nIndex==3) { if(nMAXChildIndex>0) { nChidIndex=nMAXChildIndex; setCH(nChidIndex); ui->stackedWidget_Main->setCurrentIndex(2); }else { ui->stackedWidget_Main->setCurrentIndex(1); } } else if(nIndex==2) { if(nChidIndex<=1) { ui->stackedWidget_Main->setCurrentIndex(1); nChidIndex=0; } else { setCH(nChidIndex); nChidIndex-=1; } }else if(nIndex==4) { if(nChidPHIndex<=3&&nChidPHIndex>=2) { nChidPHIndex-=1; setPH(nChidIndex,nChidPHIndex); }else if(nChidPHIndex==1||nChidPHIndex==0) { nChidPHIndex=3; setPH(nChidIndex,nChidPHIndex); } }else { ui->stackedWidget_Main->setCurrentIndex(nIndex-1); } qDebug()<<"Index:"<ChangeView->widgetType_Main index="<stackedWidget->setCurrentIndex(0); ui->stackedWidget_Main->setCurrentIndex(0); nChidIndex=0; }else { ui->stackedWidget->setCurrentIndex(0); ui->stackedWidget_Main->setCurrentIndex(nIndex); } }else if(widgetType_Setting==widgetType) { qDebug()<<"Widget->ChangeView->widgetType_Setting index="<stackedWidget->setCurrentIndex(1); switch (nIndex) { case settingMode_Modbus: { ui->stackedWidget_Setting->setCurrentIndex(0); break; } case settingMode_IP: { ui->stackedWidget_Setting->setCurrentIndex(1); break; } } } } void Widget::setSelectStatus(int nIndex) { ui->label_addressName->setStyleSheet(LABEL_Defalut); ui->label_addressData->setStyleSheet(LABEL_Defalut); ui->label_MSData->setStyleSheet(LABEL_Defalut); ui->label_MSName->setStyleSheet(LABEL_Defalut); ui->label_SaveName->setStyleSheet(LABEL_Defalut); ui->label_SaveData->setStyleSheet(LABEL_Defalut); switch (nIndex) { case 0://address { ui->label_addressName->setStyleSheet(LABEL_SELECT); ui->label_addressData->setStyleSheet(LABEL_SELECT); }break; case 1://SAVE { ui->label_SaveName->setStyleSheet(LABEL_SELECT); ui->label_SaveData->setStyleSheet(LABEL_SELECT); }break; case 2://主从 { ui->label_MSData->setStyleSheet(LABEL_SELECT); ui->label_MSName->setStyleSheet(LABEL_SELECT); }break; } } void Widget::CtrlBtnSet(int nIndex, int nData) { switch (nIndex) { case 0://address { ui->label_addressData->setText(QString::number(nData)); }break; case 1://SAVE { if(nData==1) { ui->label_SaveData->setText("YES"); }else { ui->label_SaveData->setText("NO"); } }break; case 2://主从 { if(nData!=1) { ui->label_MSData->setText("master"); }else { ui->label_MSData->setText("slave"); } }break; } } void Widget::ShowMSGWidget(QString strShowData) { if(strShowData.isEmpty()) return; if(m_pMSGWidget&&m_pMSGWidget->isHidden()) { m_pMSGWidget->SetText(strShowData); adjustMSGWidgetPosition(false); if (m_pMSGWidget->isHidden()) { m_pMSGWidget->show(); } qDebug()<<"MSGWidget_Show geometry("<geometry().x()<<","<geometry().y()<<","<geometry().width()<<","<geometry().height()<<")"; } } void Widget::ClickMSGBtn() { if(m_pMSGWidget&&!m_pMSGWidget->isHidden()) { m_pMSGWidget->hide(); emit signal_ClickMSG(); } } void Widget::adjustMSGWidgetPosition(bool isAuto) { if(isAuto) { if(!(m_pMSGWidget&&!m_pMSGWidget->isHidden())) { return; } } // 获取主窗口的逻辑尺寸 QRect mainWindowSize = this->geometry(); int mainWidth = mainWindowSize.width(); int mainHeight = mainWindowSize.height(); qDebug()<<"Widget geometry("<setFW(displayWidth-4); // 根据旋转角度计算弹窗位置 QPoint popupPos; int offset=0; if(m_nAngle==0||m_nAngle==180) { if(m_nAngle==0) { popupPos = QPoint(offset,(displayHeight - m_pMSGWidget->height()) / 2); } else { popupPos = QPoint(-offset,-(displayHeight - m_pMSGWidget->height()) / 2); } }else { if(m_nAngle==90) { popupPos = QPoint(-(displayHeight - m_pMSGWidget->height()) / 2,offset); }else { popupPos = QPoint((displayHeight - m_pMSGWidget->height()) / 2,-offset); } } popupPos += QPoint(mainWindowSize.x(), mainWindowSize.y()); // 应用位置 m_pMSGWidget->move(popupPos); // 调试输出 qDebug() << "Rotation:" << m_nAngle; qDebug() << "MSGWidget position:" << popupPos; qDebug() << "MSGWidget geometry:" << m_pMSGWidget->geometry(); } void Widget::resizeEvent(QResizeEvent *event) { if(m_pMSGWidget&&!m_pMSGWidget->isHidden()) { adjustMSGWidgetPosition(); } }