| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612 |
- #include "widget.h"
- #include "ui_widget.h"
- #include<QNetworkInterface>
- #include<QDateTime>
- #include<QProcess>
- #include "buttonclickthread.h"
- #include <QPainter>
- #include "pdu_ctrl.h"
- #include <QResizeEvent>
- 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:"<<nIndex<<"PH_Select:"<<nPHType;
- }
- /*
- void Widget::BtnDown(int nButtonID, int nDownType)
- {
- switch (nButtonID) {
- case GPIO_KEY1:
- //1按钮
- if(nDownType==BtnDown_C)
- {
- pageUp();
- }
- break;
- case GPIO_KEY2:
- {
- }
- break;
- case GPIO_KEY3:
- //3按钮
- if(nDownType==BtnDown_C)
- {
- pageDown();
- }
- break;
- case GPIO_KEY4:
- break;
- default:
- break;
- }
- qDebug()<<"Key"<<nButtonID<<" Type="<<nDownType;
- }
- */
- void Widget::pageUp()
- {
- int nIndex=ui->stackedWidget_Main->currentIndex();
- switch (nIndex) {
- case 1://
- {
- if(nMAXChildIndex>0)
- {
- nChidIndex=1;
- nChidPHIndex=0;
- setCH(nChidIndex);
- ui->stackedWidget_Main->setCurrentIndex(2);
- }else
- {
- ui->stackedWidget_Main->setCurrentIndex(3);
- }
- }
- break;
- case 2://通道页面
- {
- if(nChidIndex<nMAXChildIndex)
- {
- setCH(nChidIndex);
- nChidIndex+=1;
- nChidPHIndex=0;
- }else
- {
- ui->stackedWidget_Main->setCurrentIndex(3);
- nChidIndex=0;
- nChidPHIndex=0;
- }
- }
- break;
- case 3://传感器页面
- {
- ui->stackedWidget_Main->setCurrentIndex(0);
- }
- break;
- case 4://ph通道页面
- {
- if(nChidPHIndex<=2&&nChidPHIndex>=1)
- {
- nChidPHIndex+=1;
- setPH(nChidIndex,nChidPHIndex);
- }else if(nChidPHIndex==3||nChidPHIndex==0)
- {
- nChidPHIndex=1;
- setPH(nChidIndex,nChidPHIndex);
- }
- }
- break;
- default:
- {
- ui->stackedWidget_Main->setCurrentIndex(nIndex+1);
- }
- break;
- }
- int nMainIndex= ui->stackedWidget_Main->currentIndex();
- int nbaseIndex= ui->stackedWidget->currentIndex();
- int nSetingIndex= ui->stackedWidget_Setting->currentIndex();
- qDebug()<<"Index:"<<nIndex<<"ChID:"<<nChidIndex;
- qDebug()<<"QTStackedIndex Base="<<nbaseIndex<<" Main="<<nMainIndex<<" Set="<<nSetingIndex;
- }
- void Widget::pageDown()
- {
- int nIndex=ui->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:"<<nIndex<<"ChID:"<<nChidIndex;
- int nMainIndex= ui->stackedWidget_Main->currentIndex();
- int nbaseIndex= ui->stackedWidget->currentIndex();
- int nSetingIndex= ui->stackedWidget_Setting->currentIndex();
- qDebug()<<"QTStackedIndex Base="<<nbaseIndex<<" Main="<<nMainIndex<<" Set="<<nSetingIndex;
- }
- void Widget::ChangeView(int widgetType, int nIndex)
- {
- if(widgetType_Main==widgetType)
- {
- qDebug()<<"Widget->ChangeView->widgetType_Main index="<<nIndex;
- if(nIndex==0)
- {
- ui->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="<<nIndex;
- ui->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("<<m_pMSGWidget->geometry().x()<<","<<m_pMSGWidget->geometry().y()<<","<<m_pMSGWidget->geometry().width()<<","<<m_pMSGWidget->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("<<mainWindowSize.x()<<","<<mainWindowSize.y()<<","<<mainWindowSize.width()<<","<<mainWindowSize.height()<<")";
- // 根据旋转角度确定实际显示尺寸
- int displayWidth, displayHeight;
- displayWidth = mainWidth;
- displayHeight = mainHeight;
- // 设置弹窗宽度(略小于显示宽度)
- m_pMSGWidget->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();
- }
- }
|