widget.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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){SetData(ui->label_8,strShow);SetData(ui->label_phV,strShow);}
  131. void Widget::SetA(QString strShow){SetData(ui->label_13,strShow);SetData(ui->label_A,strShow);SetData(ui->label_phA,strShow);}
  132. void Widget::SetW(QString strShow){SetData(ui->label_16,strShow);SetData(ui->label_W,strShow);SetData(ui->label_phW,strShow);}
  133. void Widget::SetF(QString strShow){SetData(ui->label_15,strShow);SetData(ui->label_F,strShow);SetData(ui->label_phF,strShow);}
  134. void Widget::SetSUM(QString strShow){SetData(ui->label_14,strShow);SetData(ui->label_S,strShow);SetData(ui->label_phKw,strShow);}
  135. void Widget::SetT(QString strShow){SetData(ui->label_Temp,strShow+ " ℃");}
  136. void Widget::SetH(QString strShow){SetData(ui->label_Humidity,strShow+" %");}
  137. void Widget::SetCO2(QString strShow){SetData(ui->label_CO2,strShow+" PPM");}
  138. void Widget::SetVer(QString strShow){SetData(ui->label_2,"Version:"+strShow);}
  139. void Widget::SetStatus(QString strShow){SetData(ui->label_V,strShow);}
  140. void Widget::SetIP(QString strIP)
  141. {
  142. if(ui)
  143. {
  144. ui->label_3->setText(strIP);
  145. }
  146. }
  147. void Widget::setCH(int nIndex)
  148. {
  149. QString strShow=QString("Output CH%1").arg(nIndex);
  150. SetData(ui->label_chName,strShow);
  151. }
  152. void Widget::setPH(int nIndex,int nPHType)
  153. {
  154. QString strShow="";
  155. switch (nPHType) {
  156. case 1:
  157. strShow="-L1";
  158. break;
  159. case 2:
  160. strShow="-L2";
  161. break;
  162. case 3:
  163. strShow="-L3";
  164. break;
  165. default:
  166. break;
  167. }
  168. QString strPHShow="";
  169. if(nIndex<=0)
  170. {
  171. strPHShow=QString("Input Vol%1").arg(strShow);
  172. }else
  173. {
  174. strPHShow=QString("Output CH%1%2").arg(nIndex).arg(strShow);
  175. }
  176. SetData(ui->label_18,strPHShow);
  177. qDebug()<<"CH_Select:"<<nIndex<<"PH_Select:"<<nPHType;
  178. }
  179. /*
  180. void Widget::BtnDown(int nButtonID, int nDownType)
  181. {
  182. switch (nButtonID) {
  183. case GPIO_KEY1:
  184. //1按钮
  185. if(nDownType==BtnDown_C)
  186. {
  187. pageUp();
  188. }
  189. break;
  190. case GPIO_KEY2:
  191. {
  192. }
  193. break;
  194. case GPIO_KEY3:
  195. //3按钮
  196. if(nDownType==BtnDown_C)
  197. {
  198. pageDown();
  199. }
  200. break;
  201. case GPIO_KEY4:
  202. break;
  203. default:
  204. break;
  205. }
  206. qDebug()<<"Key"<<nButtonID<<" Type="<<nDownType;
  207. }
  208. */
  209. void Widget::pageUp()
  210. {
  211. int nIndex=ui->stackedWidget_Main->currentIndex();
  212. switch (nIndex) {
  213. case 1://
  214. {
  215. if(nMAXChildIndex>0)
  216. {
  217. nChidIndex=1;
  218. nChidPHIndex=0;
  219. setCH(nChidIndex);
  220. ui->stackedWidget_Main->setCurrentIndex(2);
  221. }else
  222. {
  223. ui->stackedWidget_Main->setCurrentIndex(3);
  224. }
  225. }
  226. break;
  227. case 2://通道页面
  228. {
  229. if(nChidIndex<nMAXChildIndex)
  230. {
  231. setCH(nChidIndex);
  232. nChidIndex+=1;
  233. nChidPHIndex=0;
  234. }else
  235. {
  236. ui->stackedWidget_Main->setCurrentIndex(3);
  237. nChidIndex=0;
  238. nChidPHIndex=0;
  239. }
  240. }
  241. break;
  242. case 3://传感器页面
  243. {
  244. ui->stackedWidget_Main->setCurrentIndex(0);
  245. }
  246. break;
  247. case 4://ph通道页面
  248. {
  249. if(nChidPHIndex<=2&&nChidPHIndex>=1)
  250. {
  251. nChidPHIndex+=1;
  252. setPH(nChidIndex,nChidPHIndex);
  253. }else if(nChidPHIndex==3||nChidPHIndex==0)
  254. {
  255. nChidPHIndex=1;
  256. setPH(nChidIndex,nChidPHIndex);
  257. }
  258. }
  259. break;
  260. default:
  261. {
  262. ui->stackedWidget_Main->setCurrentIndex(nIndex+1);
  263. }
  264. break;
  265. }
  266. qDebug()<<"Index:"<<nIndex<<"ChID:"<<nChidIndex;
  267. }
  268. void Widget::pageDown()
  269. {
  270. int nIndex=ui->stackedWidget_Main->currentIndex();
  271. if(nIndex==0)
  272. {
  273. ui->stackedWidget_Main->setCurrentIndex(3);
  274. }else if(nIndex==3)
  275. {
  276. if(nMAXChildIndex>0)
  277. {
  278. nChidIndex=nMAXChildIndex;
  279. setCH(nChidIndex);
  280. ui->stackedWidget_Main->setCurrentIndex(2);
  281. }else
  282. {
  283. ui->stackedWidget_Main->setCurrentIndex(1);
  284. }
  285. }
  286. else if(nIndex==2)
  287. {
  288. if(nChidIndex<=1)
  289. {
  290. ui->stackedWidget_Main->setCurrentIndex(1);
  291. nChidIndex=0;
  292. }
  293. else
  294. {
  295. setCH(nChidIndex);
  296. nChidIndex-=1;
  297. }
  298. }else if(nIndex==4)
  299. {
  300. if(nChidPHIndex<=3&&nChidPHIndex>=2)
  301. {
  302. nChidPHIndex-=1;
  303. setPH(nChidIndex,nChidPHIndex);
  304. }else if(nChidPHIndex==1||nChidPHIndex==0)
  305. {
  306. nChidPHIndex=3;
  307. setPH(nChidIndex,nChidPHIndex);
  308. }
  309. }else
  310. {
  311. ui->stackedWidget_Main->setCurrentIndex(nIndex-1);
  312. }
  313. qDebug()<<"Index:"<<nIndex<<"ChID:"<<nChidIndex;
  314. }
  315. void Widget::ChangeView(int widgetType, int nIndex)
  316. {
  317. if(widgetType_Main==widgetType)
  318. {
  319. qDebug()<<"Widget->ChangeView->widgetType_Main index="<<nIndex;
  320. if(nIndex==0)
  321. {
  322. ui->stackedWidget->setCurrentIndex(0);
  323. ui->stackedWidget_Main->setCurrentIndex(0);
  324. nChidIndex=0;
  325. }else
  326. {
  327. ui->stackedWidget->setCurrentIndex(0);
  328. ui->stackedWidget_Main->setCurrentIndex(nIndex);
  329. }
  330. }else if(widgetType_Setting==widgetType)
  331. {
  332. qDebug()<<"Widget->ChangeView->widgetType_Setting index="<<nIndex;
  333. ui->stackedWidget->setCurrentIndex(1);
  334. switch (nIndex) {
  335. case settingMode_Modbus:
  336. {
  337. ui->stackedWidget_Setting->setCurrentIndex(0);
  338. break;
  339. }
  340. case settingMode_IP:
  341. {
  342. ui->stackedWidget_Setting->setCurrentIndex(1);
  343. break;
  344. }
  345. }
  346. }
  347. }
  348. void Widget::setSelectStatus(int nIndex)
  349. {
  350. ui->label_addressName->setStyleSheet(LABEL_Defalut);
  351. ui->label_addressData->setStyleSheet(LABEL_Defalut);
  352. ui->label_MSData->setStyleSheet(LABEL_Defalut);
  353. ui->label_MSName->setStyleSheet(LABEL_Defalut);
  354. ui->label_SaveName->setStyleSheet(LABEL_Defalut);
  355. ui->label_SaveData->setStyleSheet(LABEL_Defalut);
  356. switch (nIndex) {
  357. case 0://address
  358. {
  359. ui->label_addressName->setStyleSheet(LABEL_SELECT);
  360. ui->label_addressData->setStyleSheet(LABEL_SELECT);
  361. }break;
  362. case 1://SAVE
  363. {
  364. ui->label_SaveName->setStyleSheet(LABEL_SELECT);
  365. ui->label_SaveData->setStyleSheet(LABEL_SELECT);
  366. }break;
  367. case 2://主从
  368. {
  369. ui->label_MSData->setStyleSheet(LABEL_SELECT);
  370. ui->label_MSName->setStyleSheet(LABEL_SELECT);
  371. }break;
  372. }
  373. }
  374. void Widget::CtrlBtnSet(int nIndex, int nData)
  375. {
  376. switch (nIndex) {
  377. case 0://address
  378. {
  379. ui->label_addressData->setText(QString::number(nData));
  380. }break;
  381. case 1://SAVE
  382. {
  383. if(nData==1)
  384. {
  385. ui->label_SaveData->setText("YES");
  386. }else
  387. {
  388. ui->label_SaveData->setText("NO");
  389. }
  390. }break;
  391. case 2://主从
  392. {
  393. if(nData!=1)
  394. {
  395. ui->label_MSData->setText("master");
  396. }else
  397. {
  398. ui->label_MSData->setText("slave");
  399. }
  400. }break;
  401. }
  402. }
  403. void Widget::ShowMSGWidget(QString strShowData)
  404. {
  405. if(strShowData.isEmpty())
  406. return;
  407. if(m_pMSGWidget&&m_pMSGWidget->isHidden())
  408. {
  409. m_pMSGWidget->SetText(strShowData);
  410. adjustMSGWidgetPosition(false);
  411. if (m_pMSGWidget->isHidden()) {
  412. m_pMSGWidget->show();
  413. }
  414. qDebug()<<"MSGWidget_Show geometry("<<m_pMSGWidget->geometry().x()<<","<<m_pMSGWidget->geometry().y()<<","<<m_pMSGWidget->geometry().width()<<","<<m_pMSGWidget->geometry().height()<<")";
  415. }
  416. }
  417. void Widget::ClickMSGBtn()
  418. {
  419. if(m_pMSGWidget&&!m_pMSGWidget->isHidden())
  420. {
  421. m_pMSGWidget->hide();
  422. emit signal_ClickMSG();
  423. }
  424. }
  425. void Widget::adjustMSGWidgetPosition(bool isAuto)
  426. {
  427. if(isAuto)
  428. {
  429. if(!(m_pMSGWidget&&!m_pMSGWidget->isHidden()))
  430. {
  431. return;
  432. }
  433. }
  434. // 获取主窗口的逻辑尺寸
  435. QRect mainWindowSize = this->geometry();
  436. int mainWidth = mainWindowSize.width();
  437. int mainHeight = mainWindowSize.height();
  438. qDebug()<<"Widget geometry("<<mainWindowSize.x()<<","<<mainWindowSize.y()<<","<<mainWindowSize.width()<<","<<mainWindowSize.height()<<")";
  439. // 根据旋转角度确定实际显示尺寸
  440. int displayWidth, displayHeight;
  441. displayWidth = mainWidth;
  442. displayHeight = mainHeight;
  443. // 设置弹窗宽度(略小于显示宽度)
  444. m_pMSGWidget->setFW(displayWidth-4);
  445. // 根据旋转角度计算弹窗位置
  446. QPoint popupPos;
  447. int offset=0;
  448. if(m_nAngle==0||m_nAngle==180)
  449. {
  450. if(m_nAngle==0)
  451. {
  452. popupPos = QPoint(offset,(displayHeight - m_pMSGWidget->height()) / 2);
  453. }
  454. else
  455. {
  456. popupPos = QPoint(-offset,-(displayHeight - m_pMSGWidget->height()) / 2);
  457. }
  458. }else
  459. {
  460. if(m_nAngle==90)
  461. {
  462. popupPos = QPoint(-(displayHeight - m_pMSGWidget->height()) / 2,offset);
  463. }else
  464. {
  465. popupPos = QPoint((displayHeight - m_pMSGWidget->height()) / 2,-offset);
  466. }
  467. }
  468. popupPos += QPoint(mainWindowSize.x(), mainWindowSize.y());
  469. // 应用位置
  470. m_pMSGWidget->move(popupPos);
  471. // 调试输出
  472. qDebug() << "Rotation:" << m_nAngle;
  473. qDebug() << "MSGWidget position:" << popupPos;
  474. qDebug() << "MSGWidget geometry:" << m_pMSGWidget->geometry();
  475. }
  476. void Widget::resizeEvent(QResizeEvent *event)
  477. {
  478. if(m_pMSGWidget&&!m_pMSGWidget->isHidden())
  479. {
  480. adjustMSGWidgetPosition();
  481. }
  482. }