| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512 |
- #include "widget.h"
- #include "ui_widget.h"
- #include<QNetworkInterface>
- #include<QDateTime>
- #include<QProcess>
- #include "buttonclickthread.h"
- #include <QPainter>
- #include "pdu_ctrl.h"
- #include <QSettings>
- 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
- ui->label_MSName->hide();
- ui->label_MSData->hide();
- ui->label_3->hide();
- ui->label_4->hide();
- ui->label_5->hide();
- #else
- //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();
- #endif
- #ifdef hightSmartPDU
- ui->label_modbus_type->hide();
- ui->label_modbus_Addr->hide();
- #endif
- //定时器初始化读取配置文件:默认5秒一刷新
- QString pAddress = QCoreApplication::applicationDirPath() + "/" + "smartPDU.ini";
- qDebug()<<"SettingPath:"<<pAddress;
- QSettings settings(pAddress, QSettings::IniFormat);
- int nTimerData=5000;
- switch (settings.status()) {
- case QSettings::NoError:
- qDebug() << "successed";
- break;
- case QSettings::FormatError:
- qDebug() << "code Error";
- break;
- case QSettings::AccessError:
- qDebug() << "load Error";
- break;
- }
- if(settings.contains("misc/fliptime"))
- {
- QString strTime=settings.value("misc/fliptime").toString();
- nTimerData=strTime.toInt();
- nTimerData*=1000;
- qDebug()<<"[misc]fliptime="<<strTime <<"Int="<<nTimerData;
- }else
- {
- nTimerData=5000;
- QString strTime=settings.value("misc/fliptime").toString();
- qDebug()<<"misc/fliptime="<<strTime;
- }
- if(nTimerData>0)
- {
- m_Timer = new QTimer;
- m_Timer->setInterval(nTimerData);// 设置默认定时时长1000ms
- connect(m_Timer, &QTimer::timeout, this,&Widget::slot_TimerRun);
- m_Timer->start(nTimerData);
- qDebug()<<"Timer Start msec="<<nTimerData;
- }else
- {
- qDebug()<<"Timer Not Run";
- m_Timer=nullptr;
- }
- }
- 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();
- #ifdef Language_Zh
- QString strDate="日期:"+currentTime.toString("yyyy.MM.dd");
- #else
- QString strDate="Date:"+currentTime.toString("yyyy.MM.dd");
- #endif
- QString strTime=currentTime.toString("HH:mm:ss");
- if(ui)
- {
- ui->label_4->setText(strDate);
- ui->label_5->setText(strTime);
- }
- #ifdef Language_Zh
- QString strIP="IP地址:"+ReadIP();
- #else
- QString strIP="IP:"+ReadIP();
- #endif
- 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::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)
- {
- #ifdef Language_Zh
- QString strShow=QString("输出通道CH%1").arg(nIndex);
- #else
- QString strShow=QString("Output CH%1").arg(nIndex);
- #endif
- 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)
- {
- #ifdef Language_Zh
- strPHShow=QString("输入通道%1").arg(strShow);
- #else
- strPHShow=QString("Input Vol%1").arg(strShow);
- #endif
- }else
- {
- #ifdef Language_Zh
- strPHShow=QString("输出通道CH%1%2").arg(nIndex).arg(strShow);
- #else
- strPHShow=QString("输出通道CH%1%2").arg(nIndex).arg(strShow);
- #endif
- }
- 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;
- }
- qDebug()<<"Index:"<<nIndex<<"ChID:"<<nChidIndex;
- }
- 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;
- }
- 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::slot_TimerRun()
- {
- #ifdef JY242
- pageUp();
- #else
- int nIndex=ui->stackedWidget->currentIndex();
- int nSubIndex=ui->stackedWidget_Main->currentIndex();
- if(nIndex==0&&nSubIndex==2)//通道页面标记
- {
- if(nChidIndex<nMAXChildIndex)
- {
- setCH(nChidIndex);
- nChidIndex+=1;
- nChidPHIndex=0;
- }else
- {
- nChidIndex=1;
- nChidPHIndex=0;
- setCH(nChidIndex);
- }
- qDebug()<<"Timer Once ----nChidIndex="<<nChidIndex;
- }
- #endif
- }
|