mainwidget.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #include "mainwidget.h"
  2. #include "ui_mainwidget.h"
  3. #include <QDateTime>
  4. #include "global.h"
  5. #include "pdu_ctrl.h"
  6. #define MaxStack 3
  7. const QString strName[]={
  8. "系统信息","通道信息","告警信息","运行日志"
  9. };
  10. MainWidget::MainWidget(QWidget *parent) :
  11. QWidget(parent),
  12. ui(new Ui::MainWidget)
  13. {
  14. ui->setupUi(this);
  15. InitWidget();
  16. nTimerIndex=4;
  17. }
  18. MainWidget::~MainWidget()
  19. {
  20. delete ui;
  21. }
  22. void MainWidget::InitWidget()
  23. {
  24. //图片设置
  25. QString strPath=QCoreApplication::applicationDirPath() + QDir::separator()+"image_qt" + QDir::separator();
  26. QImage rgbImage=QImage(strPath+"Home.png");
  27. QImage bgrImage = rgbImage.rgbSwapped();
  28. ui->toolButton->setIcon(QIcon(QPixmap::fromImage(bgrImage)));
  29. rgbImage=QImage(strPath+"T.png");
  30. bgrImage = rgbImage.rgbSwapped();
  31. ui->label_5->setPixmap(QPixmap::fromImage(bgrImage));
  32. rgbImage=QImage(strPath+"humidity.png");
  33. bgrImage = rgbImage.rgbSwapped();
  34. ui->label_6->setPixmap(QPixmap::fromImage(bgrImage));
  35. rgbImage=QImage(strPath+"error.png");
  36. bgrImage = rgbImage.rgbSwapped();
  37. ui->label_12->setPixmap(QPixmap::fromImage(bgrImage));
  38. rgbImage=QImage(strPath+"warning-fill.png");
  39. bgrImage = rgbImage.rgbSwapped();
  40. ui->label_10->setPixmap(QPixmap::fromImage(bgrImage));
  41. rgbImage=QImage(strPath+"log.png");
  42. bgrImage = rgbImage.rgbSwapped();
  43. ui->label_14->setPixmap(QPixmap::fromImage(bgrImage));
  44. ui->label_12->hide();
  45. ui->label_ErrorNum->hide();
  46. m_pTimer=new QTimer(this);
  47. connect(m_pTimer,SIGNAL(timeout()),this,SLOT(slot_TimerUpdate()));
  48. m_pTimer->start(1000);
  49. m_pHome = new HomeWidget(this);
  50. m_pCH=new RunChWiget(this);
  51. m_pThreshold=new ThresholdListWidget(this);
  52. m_pLogWidget=new RunLogWidget();
  53. ui->stackedWidget->addWidget(m_pHome);
  54. ui->stackedWidget->addWidget(m_pCH);
  55. ui->stackedWidget->addWidget(m_pThreshold);
  56. ui->stackedWidget->addWidget(m_pLogWidget);
  57. nIndex=0;
  58. connect(ui->toolButton,SIGNAL(clicked()),this,SLOT(slot_BtnHome()));
  59. connect(m_pHome,SIGNAL(signal_setPage(int)),this,SLOT(slot_SwitchWnd(int)));
  60. //
  61. // connect(ui->pushButton_DownPage,SIGNAL(clicked()),this,SLOT(slot_BtnDown()));
  62. }
  63. int MainWidget::UpdateLogData(int Status,int Status2,int Status3)
  64. {
  65. QSqlQuery query(pdu_Ctrl::instance()->getDB());
  66. QDateTime pdateTime=QDateTime::currentDateTime();
  67. QString strBegin = pdateTime.toString("yyyy-MM-dd ")+"00:00:00";
  68. QString strEnd=pdateTime.toString("yyyy-MM-dd ")+"23:59:59";
  69. QString strExec=QString("SELECT COUNT(*) FROM Table_AlarmManage WHERE (alarmDate BETWEEN '%1' AND '%2') and (alarmType=%3 ").arg(strBegin).arg(strEnd).arg(Status);
  70. if(Status2>=0)
  71. {
  72. strExec.append(QString(" or alarmType=%1 ").arg(Status2));
  73. }
  74. if(Status3)
  75. {
  76. strExec.append(QString(" or alarmType=%1 ").arg(Status3));
  77. }
  78. strExec.append(");");
  79. //qDebug()<<strExec;
  80. if (query.exec(strExec) && query.next()) {
  81. int totalRecords = query.value(0).toInt();
  82. return totalRecords;
  83. }
  84. return 0;
  85. }
  86. void MainWidget::UpdateTemp(QString strT, QString strH)
  87. {
  88. ui->label_Temp->setText(strT);
  89. ui->label_Temp2->setText(strH);
  90. update();
  91. }
  92. QString formatNumber(int num) {
  93. // 边界条件处理
  94. int adjustedNum = num;
  95. if (num < 0) {
  96. adjustedNum = 0; // 负数强制为0 → 后续补零为"000"
  97. } else if (num >= 1000) {
  98. adjustedNum = 999; // 超过1000强制为999
  99. }
  100. // 补零至3位
  101. return QString("%1").arg(adjustedNum, 3, 10, QChar('0'));
  102. }
  103. void MainWidget::UpdateLogCount()
  104. {
  105. //Warinning;
  106. int nWarinningData=UpdateLogData(ALARM_TYPE_POWER,ALARM_TYPE_SENSOR,ALARM_TYPE_NETWORK);
  107. //Log;
  108. int nLogData=UpdateLogData(ALARM_TYPE_RUNNING,ALARM_TYPE_OPRATION);
  109. QString strShow="000";
  110. ui->label_WarningNum->setText(formatNumber(nWarinningData));
  111. ui->label_CtrlNum->setText(formatNumber(nLogData));
  112. }
  113. void MainWidget::UpdateIP()
  114. {
  115. }
  116. QString MainWidget::getSysIP()
  117. {
  118. QProcess process;
  119. QString program = "ip"; // 或者 "ifconfig"
  120. QStringList arguments;
  121. if (program == "ip") {
  122. arguments << "addr" << "show" << "eth0";
  123. } else if (program == "ifconfig") {
  124. arguments << "eth0";
  125. }
  126. process.start(program, arguments);
  127. process.waitForFinished();
  128. QString output = process.readAllStandardOutput();
  129. QRegExp ipRegex("\\binet (\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})/\\d{1,2}\\b");
  130. int pos = 0;
  131. while ((pos = ipRegex.indexIn(output, pos)) != -1) {
  132. return ipRegex.cap(1);
  133. }
  134. return ""; // 没有找到IP地址
  135. }
  136. void MainWidget::slot_TimerUpdate()
  137. {
  138. QDateTime currentTime = QDateTime::currentDateTime();
  139. QString strDate=currentTime.toString("yyyy.MM.dd");
  140. QString strTime=currentTime.toString("HH:mm:ss");
  141. if(ui)
  142. {
  143. ui->label_Date->setText(strDate);
  144. ui->label_Time->setText(strTime);
  145. }
  146. QString strIP=getSysIP();
  147. ui->label_IP->setText(strIP);
  148. slot_UpdateData();
  149. }
  150. void MainWidget::slot_BtnHome()
  151. {
  152. //ui->label_2->setText(strName[0]);
  153. ui->stackedWidget->setCurrentIndex(0);
  154. slot_UpdateData();
  155. }
  156. void MainWidget::slot_BtnUp()
  157. {
  158. if(nIndex==0)
  159. {
  160. nIndex=MaxStack;
  161. }else
  162. {
  163. nIndex-=1;
  164. }
  165. //ui->label_2->setText(strName[nIndex]);
  166. ui->stackedWidget->setCurrentIndex(nIndex);
  167. }
  168. void MainWidget::slot_BtnDown()
  169. {
  170. if(nIndex==MaxStack)
  171. {
  172. nIndex=0;
  173. }else
  174. {
  175. nIndex+=1;
  176. }
  177. //ui->label_2->setText(strName[nIndex]);
  178. ui->stackedWidget->setCurrentIndex(nIndex);
  179. }
  180. void MainWidget::slot_SwitchWnd(int nIndex)
  181. {
  182. ui->stackedWidget->setCurrentIndex(nIndex);
  183. if(m_pThreshold&&ui->stackedWidget->currentWidget()==m_pThreshold)
  184. {
  185. m_pThreshold->ReadSqlData();
  186. }else if(m_pLogWidget&&ui->stackedWidget->currentWidget()==m_pLogWidget)
  187. {
  188. m_pLogWidget->UpdateLog();
  189. }
  190. slot_UpdateData();
  191. }
  192. void MainWidget::slot_UpdateData()
  193. {
  194. if(nTimerIndex>4)
  195. {
  196. nTimerIndex=0;
  197. UpdateLogCount();
  198. if(m_pThreshold&&ui->stackedWidget->currentWidget()==m_pThreshold)
  199. {
  200. m_pThreshold->ReadSqlData();
  201. }else if(m_pLogWidget&&ui->stackedWidget->currentWidget()==m_pLogWidget)
  202. {
  203. m_pLogWidget->UpdateLog();
  204. }
  205. }else
  206. {
  207. nTimerIndex++;
  208. }
  209. //qDebug()<<"m_pHome="<<m_pHome<<"m_pCH="<<m_pCH<<"m_pThreshold="<<m_pThreshold;
  210. //qDebug()<<"ui->stackedWidget->currentWidget()="<<ui->stackedWidget->currentWidget();
  211. if(m_pHome&&ui->stackedWidget->currentWidget()==m_pHome)
  212. {
  213. m_pHome->UpdateCHData();
  214. }
  215. else if(m_pCH&&ui->stackedWidget->currentWidget()==m_pCH)
  216. {
  217. m_pCH->UpdateAllCh();
  218. }
  219. else if(m_pThreshold&&ui->stackedWidget->currentWidget()==m_pThreshold)
  220. {
  221. m_pThreshold->UpdateTreshold();
  222. }
  223. }