widget.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. #include "widget.h"
  2. #include "ui_widget.h"
  3. #include<QNetworkInterface>
  4. #include<QDateTime>
  5. #include<QProcess>
  6. #include "buttonclickthread.h"
  7. #include <QPainter>
  8. #include "pdu_ctrl.h"
  9. #include <QResizeEvent>
  10. Widget::Widget(QWidget *parent) :
  11. QWidget(parent),
  12. ui(new Ui::Widget)
  13. {
  14. ui->setupUi(this);
  15. //ui->stackedWidget->setCurrentIndex(1);
  16. nChidIndex=0;
  17. nChidPHIndex=0;
  18. nMAXChildIndex=0;
  19. m_nAngle=0;
  20. m_nWidth = 160;
  21. m_nHeight = 128;
  22. m_pAngleCtrl=NULL;
  23. #ifdef RS485Type
  24. #ifdef RS485IpType
  25. //ui->label_MSName->hide();
  26. //ui->label_MSData->hide();
  27. //ui->label_3->hide();
  28. //ui->label_4->hide();
  29. //ui->label_5->hide();
  30. ui->label_modbus_type->hide();
  31. ui->label_modbus_Addr->hide();
  32. #else
  33. ui->label_MSName->hide();
  34. ui->label_MSData->hide();
  35. ui->label_3->hide();
  36. ui->label_4->hide();
  37. ui->label_5->hide();
  38. #endif
  39. #else
  40. //#ifdef hightSmartPDU
  41. ui->label_modbus_type->hide();
  42. ui->label_modbus_Addr->hide();
  43. //#endif
  44. #endif
  45. ui->stackedWidget_Main->setCurrentIndex(0);
  46. //ui->stackedWidget_Main->setCurrentWidget(ui->page_7);
  47. m_pMSGWidget=new MSGWidget(this);
  48. m_pMSGWidget->hide();
  49. bool bDataSet=connect(this,SIGNAL(signal_SetData(QLabel*,QString)),this,SLOT(SetData(QLabel*,QString)),Qt::QueuedConnection);
  50. if(bDataSet)
  51. {
  52. qDebug()<<"Connect Link Success";
  53. }else
  54. {
  55. qDebug()<<"Connect Link Error";
  56. }
  57. }
  58. Widget::~Widget()
  59. {
  60. delete ui;
  61. }
  62. void Widget::setInitData(QGraphicsProxyWidget *pAngleCtrl, QGraphicsView* pView, int nWidth, int nHeight)
  63. {
  64. m_pAngleCtrl=pAngleCtrl;
  65. m_nWidth= nWidth;
  66. m_nHeight = nHeight;
  67. m_pView=pView;
  68. }
  69. int Widget::getShowModeIndex()
  70. {
  71. return ui->stackedWidget->currentIndex();
  72. }
  73. int Widget::getShowPagIndex()
  74. {
  75. return ui->stackedWidget_Main->currentIndex();
  76. }
  77. int Widget::getShowSubIndex()
  78. {
  79. return nChidIndex;
  80. }
  81. int Widget::getShowPHIndex()
  82. {
  83. return nChidPHIndex;
  84. }
  85. void Widget::setShowPHIndex(int nIndex)
  86. {
  87. nChidPHIndex=nIndex;
  88. setPH(nChidIndex,nChidPHIndex);
  89. }
  90. void Widget::UpdateDate()
  91. {
  92. QDateTime currentTime = QDateTime::currentDateTime();
  93. QString strDate="Date:"+currentTime.toString("yyyy.MM.dd");
  94. QString strTime="Time:"+currentTime.toString("HH:mm:ss");
  95. if(ui)
  96. {
  97. ui->label_4->setText(strDate);
  98. ui->label_5->setText(strTime);
  99. }
  100. QString strIP="IP:"+ReadIP();
  101. ui->label_3->setText(strIP);
  102. if(pdu_Ctrl::instance()->m_nModbusMSType!=1)
  103. {
  104. ui->label_modbus_type->setText(QString("ModbusType: %1").arg( "master"));
  105. }else
  106. {
  107. ui->label_modbus_type->setText(QString("ModbusType: %1").arg("slave"));
  108. }
  109. ui->label_modbus_Addr->setText(QString("ModbusAddress: %1").arg(pdu_Ctrl::instance()->m_nModbusAddress));
  110. }
  111. QString Widget::ReadIP()
  112. {
  113. QProcess process;
  114. QString program = "ip"; // 或者 "ifconfig"
  115. QStringList arguments;
  116. if (program == "ip") {
  117. arguments << "addr" << "show" << "eth0";
  118. } else if (program == "ifconfig") {
  119. arguments << "eth0";
  120. }
  121. process.start(program, arguments);
  122. process.waitForFinished();
  123. QString output = process.readAllStandardOutput();
  124. QRegExp ipRegex("\\binet (\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})/\\d{1,2}\\b");
  125. int pos = 0;
  126. while ((pos = ipRegex.indexIn(output, pos)) != -1) {
  127. return ipRegex.cap(1);
  128. }
  129. return ""; // 没有找到IP地址
  130. }
  131. void Widget::SetData(QLabel *pLab, QString strShow)
  132. {
  133. if(pLab)
  134. {
  135. if(pLab==ui->label_V)
  136. {
  137. if(ui->label_V->text()!=strShow)
  138. {
  139. if(strShow=="OFF")
  140. {
  141. ui->label_V->setStyleSheet(LABEL_RED);
  142. }else
  143. {
  144. ui->label_V->setStyleSheet("QLabel{color: rgb(0, 255, 0);}");
  145. }
  146. }
  147. }
  148. pLab->setText(strShow);
  149. }
  150. }
  151. void Widget::SetV(QString strShow)
  152. {
  153. emit signal_SetData(ui->label_8,strShow);emit signal_SetData(ui->label_phV,strShow);
  154. if(m_nMonwayType==3)
  155. {
  156. emit signal_SetData(ui->label_V,strShow);
  157. }
  158. }
  159. 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);}
  160. 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);}
  161. 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);}
  162. 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);}
  163. void Widget::SetT(QString strShow){emit signal_SetData(ui->label_Temp,strShow+ " ℃");}
  164. void Widget::SetH(QString strShow){emit signal_SetData(ui->label_Humidity,strShow+" %");}
  165. void Widget::SetCO2(QString strShow){emit signal_SetData(ui->label_CO2,strShow+" PPM");}
  166. void Widget::SetVer(QString strShow){emit signal_SetData(ui->label_2,"Version:"+strShow);}
  167. void Widget::SetStatus(QString strShow){
  168. if(m_nMonwayType==2)
  169. {
  170. emit signal_SetData(ui->label_V,strShow);
  171. }
  172. }
  173. void Widget::SetIP(QString strIP)
  174. {
  175. if(ui)
  176. {
  177. ui->label_3->setText(strIP);
  178. }
  179. }
  180. void Widget::setCH(int nIndex)
  181. {
  182. QString strShow=QString("Output CH%1").arg(nIndex);
  183. emit signal_SetData(ui->label_chName,strShow);
  184. }
  185. void Widget::setPH(int nIndex,int nPHType)
  186. {
  187. QString strShow="";
  188. switch (nPHType) {
  189. case 1:
  190. strShow="-L1";
  191. break;
  192. case 2:
  193. strShow="-L2";
  194. break;
  195. case 3:
  196. strShow="-L3";
  197. break;
  198. default:
  199. break;
  200. }
  201. QString strPHShow="";
  202. if(nIndex<=0)
  203. {
  204. strPHShow=QString("Input Vol%1").arg(strShow);
  205. }else
  206. {
  207. strPHShow=QString("Output CH%1%2").arg(nIndex).arg(strShow);
  208. }
  209. emit signal_SetData(ui->label_18,strPHShow);
  210. qDebug()<<"CH_Select:"<<nIndex<<"PH_Select:"<<nPHType;
  211. }
  212. /*
  213. void Widget::BtnDown(int nButtonID, int nDownType)
  214. {
  215. switch (nButtonID) {
  216. case GPIO_KEY1:
  217. //1按钮
  218. if(nDownType==BtnDown_C)
  219. {
  220. pageUp();
  221. }
  222. break;
  223. case GPIO_KEY2:
  224. {
  225. }
  226. break;
  227. case GPIO_KEY3:
  228. //3按钮
  229. if(nDownType==BtnDown_C)
  230. {
  231. pageDown();
  232. }
  233. break;
  234. case GPIO_KEY4:
  235. break;
  236. default:
  237. break;
  238. }
  239. qDebug()<<"Key"<<nButtonID<<" Type="<<nDownType;
  240. }
  241. */
  242. void Widget::pageUp()
  243. {
  244. int nIndex=ui->stackedWidget_Main->currentIndex();
  245. switch (nIndex) {
  246. case 1://
  247. {
  248. if(nMAXChildIndex>0)
  249. {
  250. nChidIndex=1;
  251. nChidPHIndex=0;
  252. setCH(nChidIndex);
  253. ui->stackedWidget_Main->setCurrentIndex(2);
  254. }else
  255. {
  256. ui->stackedWidget_Main->setCurrentIndex(3);
  257. }
  258. }
  259. break;
  260. case 2://通道页面
  261. {
  262. if(nChidIndex<nMAXChildIndex)
  263. {
  264. setCH(nChidIndex);
  265. nChidIndex+=1;
  266. nChidPHIndex=0;
  267. }else
  268. {
  269. ui->stackedWidget_Main->setCurrentIndex(3);
  270. nChidIndex=0;
  271. nChidPHIndex=0;
  272. }
  273. }
  274. break;
  275. case 3://传感器页面
  276. {
  277. ui->stackedWidget_Main->setCurrentIndex(0);
  278. }
  279. break;
  280. case 4://ph通道页面
  281. {
  282. if(nChidPHIndex<=2&&nChidPHIndex>=1)
  283. {
  284. nChidPHIndex+=1;
  285. setPH(nChidIndex,nChidPHIndex);
  286. }else if(nChidPHIndex==3||nChidPHIndex==0)
  287. {
  288. nChidPHIndex=1;
  289. setPH(nChidIndex,nChidPHIndex);
  290. }
  291. }
  292. break;
  293. default:
  294. {
  295. ui->stackedWidget_Main->setCurrentIndex(nIndex+1);
  296. }
  297. break;
  298. }
  299. int nMainIndex= ui->stackedWidget_Main->currentIndex();
  300. int nbaseIndex= ui->stackedWidget->currentIndex();
  301. int nSetingIndex= ui->stackedWidget_Setting->currentIndex();
  302. qDebug()<<"Index:"<<nIndex<<"ChID:"<<nChidIndex;
  303. qDebug()<<"QTStackedIndex Base="<<nbaseIndex<<" Main="<<nMainIndex<<" Set="<<nSetingIndex;
  304. }
  305. void Widget::pageDown()
  306. {
  307. int nIndex=ui->stackedWidget_Main->currentIndex();
  308. if(nIndex==0)
  309. {
  310. ui->stackedWidget_Main->setCurrentIndex(3);
  311. }else if(nIndex==3)
  312. {
  313. if(nMAXChildIndex>0)
  314. {
  315. nChidIndex=nMAXChildIndex;
  316. setCH(nChidIndex);
  317. ui->stackedWidget_Main->setCurrentIndex(2);
  318. }else
  319. {
  320. ui->stackedWidget_Main->setCurrentIndex(1);
  321. }
  322. }
  323. else if(nIndex==2)
  324. {
  325. if(nChidIndex<=1)
  326. {
  327. ui->stackedWidget_Main->setCurrentIndex(1);
  328. nChidIndex=0;
  329. }
  330. else
  331. {
  332. setCH(nChidIndex);
  333. nChidIndex-=1;
  334. }
  335. }else if(nIndex==4)
  336. {
  337. if(nChidPHIndex<=3&&nChidPHIndex>=2)
  338. {
  339. nChidPHIndex-=1;
  340. setPH(nChidIndex,nChidPHIndex);
  341. }else if(nChidPHIndex==1||nChidPHIndex==0)
  342. {
  343. nChidPHIndex=3;
  344. setPH(nChidIndex,nChidPHIndex);
  345. }
  346. }else
  347. {
  348. ui->stackedWidget_Main->setCurrentIndex(nIndex-1);
  349. }
  350. qDebug()<<"Index:"<<nIndex<<"ChID:"<<nChidIndex;
  351. int nMainIndex= ui->stackedWidget_Main->currentIndex();
  352. int nbaseIndex= ui->stackedWidget->currentIndex();
  353. int nSetingIndex= ui->stackedWidget_Setting->currentIndex();
  354. qDebug()<<"QTStackedIndex Base="<<nbaseIndex<<" Main="<<nMainIndex<<" Set="<<nSetingIndex;
  355. }
  356. void Widget::ChangeView(int widgetType, int nIndex)
  357. {
  358. if(widgetType_Main==widgetType)
  359. {
  360. qDebug()<<"Widget->ChangeView->widgetType_Main index="<<nIndex;
  361. if(nIndex==0)
  362. {
  363. ui->stackedWidget->setCurrentIndex(0);
  364. ui->stackedWidget_Main->setCurrentIndex(0);
  365. nChidIndex=0;
  366. }else
  367. {
  368. ui->stackedWidget->setCurrentIndex(0);
  369. ui->stackedWidget_Main->setCurrentIndex(nIndex);
  370. }
  371. }else if(widgetType_Setting==widgetType)
  372. {
  373. qDebug()<<"Widget->ChangeView->widgetType_Setting index="<<nIndex;
  374. ui->stackedWidget->setCurrentIndex(1);
  375. switch (nIndex) {
  376. case settingMode_Modbus:
  377. {
  378. ui->stackedWidget_Setting->setCurrentIndex(0);
  379. break;
  380. }
  381. case settingMode_IP:
  382. {
  383. ui->stackedWidget_Setting->setCurrentIndex(1);
  384. break;
  385. }
  386. }
  387. }
  388. }
  389. void Widget::setSelectStatus(int nIndex)
  390. {
  391. ui->label_addressName->setStyleSheet(LABEL_Defalut);
  392. ui->label_addressData->setStyleSheet(LABEL_Defalut);
  393. ui->label_MSData->setStyleSheet(LABEL_Defalut);
  394. ui->label_MSName->setStyleSheet(LABEL_Defalut);
  395. ui->label_SaveName->setStyleSheet(LABEL_Defalut);
  396. ui->label_SaveData->setStyleSheet(LABEL_Defalut);
  397. switch (nIndex) {
  398. case 0://address
  399. {
  400. ui->label_addressName->setStyleSheet(LABEL_SELECT);
  401. ui->label_addressData->setStyleSheet(LABEL_SELECT);
  402. }break;
  403. case 1://SAVE
  404. {
  405. ui->label_SaveName->setStyleSheet(LABEL_SELECT);
  406. ui->label_SaveData->setStyleSheet(LABEL_SELECT);
  407. }break;
  408. case 2://主从
  409. {
  410. ui->label_MSData->setStyleSheet(LABEL_SELECT);
  411. ui->label_MSName->setStyleSheet(LABEL_SELECT);
  412. }break;
  413. }
  414. }
  415. void Widget::CtrlBtnSet(int nIndex, int nData)
  416. {
  417. switch (nIndex) {
  418. case 0://address
  419. {
  420. ui->label_addressData->setText(QString::number(nData));
  421. }break;
  422. case 1://SAVE
  423. {
  424. if(nData==1)
  425. {
  426. ui->label_SaveData->setText("YES");
  427. }else
  428. {
  429. ui->label_SaveData->setText("NO");
  430. }
  431. }break;
  432. case 2://主从
  433. {
  434. if(nData!=1)
  435. {
  436. ui->label_MSData->setText("master");
  437. }else
  438. {
  439. ui->label_MSData->setText("slave");
  440. }
  441. }break;
  442. }
  443. }
  444. void Widget::setMAXChildIndex(int nIndex)
  445. {
  446. if(m_nMonwayType==1)
  447. {
  448. nMAXChildIndex=0;
  449. }else
  450. {
  451. nMAXChildIndex=nIndex;
  452. }
  453. }
  454. void Widget::ShowMSGWidget(QString strShowData)
  455. {
  456. if(strShowData.isEmpty())
  457. return;
  458. if(m_pMSGWidget&&m_pMSGWidget->isHidden())
  459. {
  460. m_pMSGWidget->SetText(strShowData);
  461. adjustMSGWidgetPosition(false);
  462. if (m_pMSGWidget->isHidden()) {
  463. m_pMSGWidget->show();
  464. }
  465. qDebug()<<"MSGWidget_Show geometry("<<m_pMSGWidget->geometry().x()<<","<<m_pMSGWidget->geometry().y()<<","<<m_pMSGWidget->geometry().width()<<","<<m_pMSGWidget->geometry().height()<<")";
  466. }
  467. }
  468. void Widget::ClickMSGBtn()
  469. {
  470. if(m_pMSGWidget&&!m_pMSGWidget->isHidden())
  471. {
  472. m_pMSGWidget->hide();
  473. emit signal_ClickMSG();
  474. }
  475. }
  476. void Widget::adjustMSGWidgetPosition(bool isAuto)
  477. {
  478. if(isAuto)
  479. {
  480. if(!(m_pMSGWidget&&!m_pMSGWidget->isHidden()))
  481. {
  482. return;
  483. }
  484. }
  485. // 获取主窗口的逻辑尺寸
  486. QRect mainWindowSize = this->geometry();
  487. int mainWidth = mainWindowSize.width();
  488. int mainHeight = mainWindowSize.height();
  489. qDebug()<<"Widget geometry("<<mainWindowSize.x()<<","<<mainWindowSize.y()<<","<<mainWindowSize.width()<<","<<mainWindowSize.height()<<")";
  490. // 根据旋转角度确定实际显示尺寸
  491. int displayWidth, displayHeight;
  492. displayWidth = mainWidth;
  493. displayHeight = mainHeight;
  494. // 设置弹窗宽度(略小于显示宽度)
  495. m_pMSGWidget->setFW(displayWidth-4);
  496. // 根据旋转角度计算弹窗位置
  497. QPoint popupPos;
  498. int offset=0;
  499. if(m_nAngle==0||m_nAngle==180)
  500. {
  501. if(m_nAngle==0)
  502. {
  503. popupPos = QPoint(offset,(displayHeight - m_pMSGWidget->height()) / 2);
  504. }
  505. else
  506. {
  507. popupPos = QPoint(-offset,-(displayHeight - m_pMSGWidget->height()) / 2);
  508. }
  509. }else
  510. {
  511. if(m_nAngle==90)
  512. {
  513. popupPos = QPoint(-(displayHeight - m_pMSGWidget->height()) / 2,offset);
  514. }else
  515. {
  516. popupPos = QPoint((displayHeight - m_pMSGWidget->height()) / 2,-offset);
  517. }
  518. }
  519. popupPos += QPoint(mainWindowSize.x(), mainWindowSize.y());
  520. // 应用位置
  521. m_pMSGWidget->move(popupPos);
  522. // 调试输出
  523. qDebug() << "Rotation:" << m_nAngle;
  524. qDebug() << "MSGWidget position:" << popupPos;
  525. qDebug() << "MSGWidget geometry:" << m_pMSGWidget->geometry();
  526. }
  527. void Widget::setMonwayType(int nType)
  528. {
  529. m_nMonwayType= nType;
  530. switch (m_nMonwayType) {
  531. case 1://总监控
  532. {
  533. nMAXChildIndex=0;
  534. }
  535. break;
  536. case 3://分监视不控制
  537. {
  538. ui->label_34->setText("Volt(V):");
  539. ui->label_V->setStyleSheet("");
  540. emit signal_SetData(ui->label_V,"0.00");
  541. }
  542. break;
  543. default:
  544. {
  545. ui->label_34->setText("Switch");
  546. emit signal_SetData(ui->label_V,"OFF");
  547. #ifdef RS485Type
  548. ui->label_V->setStyleSheet("QLabel {color: rgb(0,255,255);}");
  549. #else
  550. ui->label_V->setStyleSheet("QLabel {color: rgb(0,0,255);}");
  551. #endif
  552. }
  553. break;
  554. }
  555. }
  556. void Widget::resizeEvent(QResizeEvent *event)
  557. {
  558. if(m_pMSGWidget&&!m_pMSGWidget->isHidden())
  559. {
  560. adjustMSGWidgetPosition();
  561. }
  562. }