#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(); bool bDataSet=connect(this,SIGNAL(signal_SetData(QLabel*,QString)),this,SLOT(SetData(QLabel*,QString)),Qt::QueuedConnection); if(bDataSet) { qDebug()<<"Connect Link Success"; }else { qDebug()<<"Connect Link Error"; } } 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) { if(pLab==ui->label_V) { if(ui->label_V->text()!=strShow) { if(strShow=="OFF") { ui->label_V->setStyleSheet(LABEL_RED); }else { ui->label_V->setStyleSheet("QLabel{color: rgb(0, 255, 0);}"); } } } pLab->setText(strShow); } } void Widget::SetV(QString strShow) { emit signal_SetData(ui->label_8,strShow);emit signal_SetData(ui->label_phV,strShow); if(m_nMonwayType==3) { emit signal_SetData(ui->label_V,strShow); } } void Widget::SetA(QString strShow){emit signal_SetData(ui->label_13,strShow);emit signal_SetData(ui->label_A,strShow);emit signal_SetData(ui->label_phA,strShow);} void Widget::SetW(QString strShow){emit signal_SetData(ui->label_16,strShow);emit signal_SetData(ui->label_W,strShow);emit signal_SetData(ui->label_phW,strShow);} void Widget::SetF(QString strShow){emit signal_SetData(ui->label_15,strShow);emit signal_SetData(ui->label_F,strShow);emit signal_SetData(ui->label_phF,strShow);} void Widget::SetSUM(QString strShow){emit signal_SetData(ui->label_14,strShow);emit signal_SetData(ui->label_S,strShow);emit signal_SetData(ui->label_phKw,strShow);} void Widget::SetT(QString strShow){emit signal_SetData(ui->label_Temp,strShow+ " ℃");} void Widget::SetH(QString strShow){emit signal_SetData(ui->label_Humidity,strShow+" %");} void Widget::SetCO2(QString strShow){emit signal_SetData(ui->label_CO2,strShow+" PPM");} void Widget::SetVer(QString strShow){emit signal_SetData(ui->label_2,"Version:"+strShow);} void Widget::SetStatus(QString strShow){ if(m_nMonwayType==2) { emit signal_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); emit signal_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); } emit signal_SetData(ui->label_18,strPHShow); qDebug()<<"CH_Select:"<stackedWidget_Main->currentIndex(); int nbaseIndex= ui->stackedWidget->currentIndex(); int nSetingIndex= ui->stackedWidget_Setting->currentIndex(); qDebug()<<"QTStackedIndex Base="<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::setMAXChildIndex(int nIndex) { if(m_nMonwayType==1) { nMAXChildIndex=0; }else { nMAXChildIndex=nIndex; } } 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::setMonwayType(int nType) { m_nMonwayType= nType; switch (m_nMonwayType) { case 1://总监控 { nMAXChildIndex=0; } break; case 3://分监视不控制 { ui->label_34->setText("Volt(V):"); ui->label_V->setStyleSheet(""); emit signal_SetData(ui->label_V,"0.00"); } break; default: { ui->label_34->setText("Switch"); emit signal_SetData(ui->label_V,"OFF"); #ifdef RS485Type ui->label_V->setStyleSheet("QLabel {color: rgb(0,255,255);}"); #else ui->label_V->setStyleSheet("QLabel {color: rgb(0,0,255);}"); #endif } break; } } void Widget::resizeEvent(QResizeEvent *event) { if(m_pMSGWidget&&!m_pMSGWidget->isHidden()) { adjustMSGWidgetPosition(); } }