| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- #include "mainwidget.h"
- #include "ui_mainwidget.h"
- #include <QDateTime>
- #include "global.h"
- #include "pdu_ctrl.h"
- #define MaxStack 3
- const QString strName[]={
- "系统信息","通道信息","告警信息","运行日志"
- };
- MainWidget::MainWidget(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::MainWidget)
- {
- ui->setupUi(this);
- InitWidget();
- nTimerIndex=4;
- }
- MainWidget::~MainWidget()
- {
- delete ui;
- }
- void MainWidget::InitWidget()
- {
- //图片设置
- QString strPath=QCoreApplication::applicationDirPath() + QDir::separator()+"image_qt" + QDir::separator();
- QImage rgbImage=QImage(strPath+"Home.png");
- QImage bgrImage = rgbImage.rgbSwapped();
- ui->toolButton->setIcon(QIcon(QPixmap::fromImage(bgrImage)));
- rgbImage=QImage(strPath+"T.png");
- bgrImage = rgbImage.rgbSwapped();
- ui->label_5->setPixmap(QPixmap::fromImage(bgrImage));
- rgbImage=QImage(strPath+"humidity.png");
- bgrImage = rgbImage.rgbSwapped();
- ui->label_6->setPixmap(QPixmap::fromImage(bgrImage));
- rgbImage=QImage(strPath+"error.png");
- bgrImage = rgbImage.rgbSwapped();
- ui->label_12->setPixmap(QPixmap::fromImage(bgrImage));
- rgbImage=QImage(strPath+"warning-fill.png");
- bgrImage = rgbImage.rgbSwapped();
- ui->label_10->setPixmap(QPixmap::fromImage(bgrImage));
- rgbImage=QImage(strPath+"log.png");
- bgrImage = rgbImage.rgbSwapped();
- ui->label_14->setPixmap(QPixmap::fromImage(bgrImage));
- ui->label_12->hide();
- ui->label_ErrorNum->hide();
- m_pTimer=new QTimer(this);
- connect(m_pTimer,SIGNAL(timeout()),this,SLOT(slot_TimerUpdate()));
- m_pTimer->start(1000);
- m_pHome = new HomeWidget(this);
- m_pCH=new RunChWiget(this);
- m_pThreshold=new ThresholdListWidget(this);
- m_pLogWidget=new RunLogWidget();
- ui->stackedWidget->addWidget(m_pHome);
- ui->stackedWidget->addWidget(m_pCH);
- ui->stackedWidget->addWidget(m_pThreshold);
- ui->stackedWidget->addWidget(m_pLogWidget);
- nIndex=0;
- connect(ui->toolButton,SIGNAL(clicked()),this,SLOT(slot_BtnHome()));
- connect(m_pHome,SIGNAL(signal_setPage(int)),this,SLOT(slot_SwitchWnd(int)));
- //
- // connect(ui->pushButton_DownPage,SIGNAL(clicked()),this,SLOT(slot_BtnDown()));
- }
- int MainWidget::UpdateLogData(int Status,int Status2,int Status3)
- {
- QSqlQuery query(pdu_Ctrl::instance()->getDB());
- QDateTime pdateTime=QDateTime::currentDateTime();
- QString strBegin = pdateTime.toString("yyyy-MM-dd ")+"00:00:00";
- QString strEnd=pdateTime.toString("yyyy-MM-dd ")+"23:59:59";
- QString strExec=QString("SELECT COUNT(*) FROM Table_AlarmManage WHERE (alarmDate BETWEEN '%1' AND '%2') and (alarmType=%3 ").arg(strBegin).arg(strEnd).arg(Status);
- if(Status2>=0)
- {
- strExec.append(QString(" or alarmType=%1 ").arg(Status2));
- }
- if(Status3)
- {
- strExec.append(QString(" or alarmType=%1 ").arg(Status3));
- }
- strExec.append(");");
- //qDebug()<<strExec;
- if (query.exec(strExec) && query.next()) {
- int totalRecords = query.value(0).toInt();
- return totalRecords;
- }
- return 0;
- }
- void MainWidget::UpdateTemp(QString strT, QString strH)
- {
- ui->label_Temp->setText(strT);
- ui->label_Temp2->setText(strH);
- update();
- }
- QString formatNumber(int num) {
- // 边界条件处理
- int adjustedNum = num;
- if (num < 0) {
- adjustedNum = 0; // 负数强制为0 → 后续补零为"000"
- } else if (num >= 1000) {
- adjustedNum = 999; // 超过1000强制为999
- }
- // 补零至3位
- return QString("%1").arg(adjustedNum, 3, 10, QChar('0'));
- }
- void MainWidget::UpdateLogCount()
- {
- //Warinning;
- int nWarinningData=UpdateLogData(ALARM_TYPE_POWER,ALARM_TYPE_SENSOR,ALARM_TYPE_NETWORK);
- //Log;
- int nLogData=UpdateLogData(ALARM_TYPE_RUNNING,ALARM_TYPE_OPRATION);
- QString strShow="000";
- ui->label_WarningNum->setText(formatNumber(nWarinningData));
- ui->label_CtrlNum->setText(formatNumber(nLogData));
- }
- void MainWidget::UpdateIP()
- {
- }
- QString MainWidget::getSysIP()
- {
- 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 MainWidget::slot_TimerUpdate()
- {
- QDateTime currentTime = QDateTime::currentDateTime();
- QString strDate=currentTime.toString("yyyy.MM.dd");
- QString strTime=currentTime.toString("HH:mm:ss");
- if(ui)
- {
- ui->label_Date->setText(strDate);
- ui->label_Time->setText(strTime);
- }
- QString strIP=getSysIP();
- ui->label_IP->setText(strIP);
- slot_UpdateData();
- }
- void MainWidget::slot_BtnHome()
- {
- //ui->label_2->setText(strName[0]);
- ui->stackedWidget->setCurrentIndex(0);
- slot_UpdateData();
- }
- void MainWidget::slot_BtnUp()
- {
- if(nIndex==0)
- {
- nIndex=MaxStack;
- }else
- {
- nIndex-=1;
- }
- //ui->label_2->setText(strName[nIndex]);
- ui->stackedWidget->setCurrentIndex(nIndex);
- }
- void MainWidget::slot_BtnDown()
- {
- if(nIndex==MaxStack)
- {
- nIndex=0;
- }else
- {
- nIndex+=1;
- }
- //ui->label_2->setText(strName[nIndex]);
- ui->stackedWidget->setCurrentIndex(nIndex);
- }
- void MainWidget::slot_SwitchWnd(int nIndex)
- {
- ui->stackedWidget->setCurrentIndex(nIndex);
- if(m_pThreshold&&ui->stackedWidget->currentWidget()==m_pThreshold)
- {
- m_pThreshold->ReadSqlData();
- }else if(m_pLogWidget&&ui->stackedWidget->currentWidget()==m_pLogWidget)
- {
- m_pLogWidget->UpdateLog();
- }
- slot_UpdateData();
- }
- void MainWidget::slot_UpdateData()
- {
- if(nTimerIndex>4)
- {
- nTimerIndex=0;
- UpdateLogCount();
- if(m_pThreshold&&ui->stackedWidget->currentWidget()==m_pThreshold)
- {
- m_pThreshold->ReadSqlData();
- }else if(m_pLogWidget&&ui->stackedWidget->currentWidget()==m_pLogWidget)
- {
- m_pLogWidget->UpdateLog();
- }
- }else
- {
- nTimerIndex++;
- }
- //qDebug()<<"m_pHome="<<m_pHome<<"m_pCH="<<m_pCH<<"m_pThreshold="<<m_pThreshold;
- //qDebug()<<"ui->stackedWidget->currentWidget()="<<ui->stackedWidget->currentWidget();
- if(m_pHome&&ui->stackedWidget->currentWidget()==m_pHome)
- {
- m_pHome->UpdateCHData();
- }
- else if(m_pCH&&ui->stackedWidget->currentWidget()==m_pCH)
- {
- m_pCH->UpdateAllCh();
- }
- else if(m_pThreshold&&ui->stackedWidget->currentWidget()==m_pThreshold)
- {
- m_pThreshold->UpdateTreshold();
- }
- }
|