widget.cpp 14 KB

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