widget.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. Widget::Widget(QWidget *parent) :
  10. QWidget(parent),
  11. ui(new Ui::Widget)
  12. {
  13. ui->setupUi(this);
  14. //ui->stackedWidget->setCurrentIndex(1);
  15. nChidIndex=0;
  16. nChidPHIndex=0;
  17. nMAXChildIndex=0;
  18. m_nAngle=0;
  19. m_nWidth = 160;
  20. m_nHeight = 128;
  21. m_pAngleCtrl=NULL;
  22. #ifdef RS485Type
  23. #ifdef RS485IpType
  24. //ui->label_MSName->hide();
  25. //ui->label_MSData->hide();
  26. //ui->label_3->hide();
  27. //ui->label_4->hide();
  28. //ui->label_5->hide();
  29. ui->label_modbus_type->hide();
  30. ui->label_modbus_Addr->hide();
  31. #else
  32. ui->label_MSName->hide();
  33. ui->label_MSData->hide();
  34. ui->label_3->hide();
  35. ui->label_4->hide();
  36. ui->label_5->hide();
  37. #endif
  38. #else
  39. //#ifdef hightSmartPDU
  40. ui->label_modbus_type->hide();
  41. ui->label_modbus_Addr->hide();
  42. //#endif
  43. #endif
  44. ui->stackedWidget_Main->setCurrentIndex(0);
  45. //ui->stackedWidget_Main->setCurrentWidget(ui->page_7);
  46. m_pMSGWidget=new MSGWidget(this);
  47. m_pMSGWidget->hide();
  48. }
  49. Widget::~Widget()
  50. {
  51. delete ui;
  52. }
  53. void Widget::setInitData(QGraphicsProxyWidget *pAngleCtrl, QGraphicsView* pView, int nWidth, int nHeight)
  54. {
  55. m_pAngleCtrl=pAngleCtrl;
  56. m_nWidth= nWidth;
  57. m_nHeight = nHeight;
  58. m_pView=pView;
  59. }
  60. int Widget::getShowModeIndex()
  61. {
  62. return ui->stackedWidget->currentIndex();
  63. }
  64. int Widget::getShowPagIndex()
  65. {
  66. return ui->stackedWidget_Main->currentIndex();
  67. }
  68. int Widget::getShowSubIndex()
  69. {
  70. return nChidIndex;
  71. }
  72. int Widget::getShowPHIndex()
  73. {
  74. return nChidPHIndex;
  75. }
  76. void Widget::setShowPHIndex(int nIndex)
  77. {
  78. nChidPHIndex=nIndex;
  79. setPH(nChidIndex,nChidPHIndex);
  80. }
  81. void Widget::UpdateDate()
  82. {
  83. QDateTime currentTime = QDateTime::currentDateTime();
  84. QString strDate="Date:"+currentTime.toString("yyyy.MM.dd");
  85. QString strTime="Time:"+currentTime.toString("HH:mm:ss");
  86. if(ui)
  87. {
  88. ui->label_4->setText(strDate);
  89. ui->label_5->setText(strTime);
  90. }
  91. QString strIP="IP:"+ReadIP();
  92. ui->label_3->setText(strIP);
  93. if(pdu_Ctrl::instance()->m_nModbusMSType!=1)
  94. {
  95. ui->label_modbus_type->setText(QString("ModbusType: %1").arg( "master"));
  96. }else
  97. {
  98. ui->label_modbus_type->setText(QString("ModbusType: %1").arg("slave"));
  99. }
  100. ui->label_modbus_Addr->setText(QString("ModbusAddress: %1").arg(pdu_Ctrl::instance()->m_nModbusAddress));
  101. }
  102. QString Widget::ReadIP()
  103. {
  104. QProcess process;
  105. QString program = "ip"; // 或者 "ifconfig"
  106. QStringList arguments;
  107. if (program == "ip") {
  108. arguments << "addr" << "show" << "eth0";
  109. } else if (program == "ifconfig") {
  110. arguments << "eth0";
  111. }
  112. process.start(program, arguments);
  113. process.waitForFinished();
  114. QString output = process.readAllStandardOutput();
  115. QRegExp ipRegex("\\binet (\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})/\\d{1,2}\\b");
  116. int pos = 0;
  117. while ((pos = ipRegex.indexIn(output, pos)) != -1) {
  118. return ipRegex.cap(1);
  119. }
  120. return ""; // 没有找到IP地址
  121. }
  122. void Widget::SetData(QLabel *pLab, QString strShow)
  123. {
  124. if(pLab)
  125. {
  126. pLab->setText(strShow);
  127. }
  128. }
  129. void Widget::SetV(QString strShow){SetData(ui->label_8,strShow);SetData(ui->label_phV,strShow);}
  130. void Widget::SetA(QString strShow){SetData(ui->label_13,strShow);SetData(ui->label_A,strShow);SetData(ui->label_phA,strShow);}
  131. void Widget::SetW(QString strShow){SetData(ui->label_16,strShow);SetData(ui->label_W,strShow);SetData(ui->label_phW,strShow);}
  132. void Widget::SetF(QString strShow){SetData(ui->label_15,strShow);SetData(ui->label_F,strShow);SetData(ui->label_phF,strShow);}
  133. void Widget::SetSUM(QString strShow){SetData(ui->label_14,strShow);SetData(ui->label_S,strShow);SetData(ui->label_phKw,strShow);}
  134. void Widget::SetT(QString strShow){SetData(ui->label_Temp,strShow+ " ℃");}
  135. void Widget::SetH(QString strShow){SetData(ui->label_Humidity,strShow+" %");}
  136. void Widget::SetCO2(QString strShow){SetData(ui->label_CO2,strShow+" PPM");}
  137. void Widget::SetVer(QString strShow){SetData(ui->label_2,"Version:"+strShow);}
  138. void Widget::SetStatus(QString strShow){SetData(ui->label_V,strShow);}
  139. void Widget::SetIP(QString strIP)
  140. {
  141. if(ui)
  142. {
  143. ui->label_3->setText(strIP);
  144. }
  145. }
  146. void Widget::setCH(int nIndex)
  147. {
  148. QString strShow=QString("Output CH%1").arg(nIndex);
  149. SetData(ui->label_chName,strShow);
  150. }
  151. void Widget::setPH(int nIndex,int nPHType)
  152. {
  153. QString strShow="";
  154. switch (nPHType) {
  155. case 1:
  156. strShow="-L1";
  157. break;
  158. case 2:
  159. strShow="-L2";
  160. break;
  161. case 3:
  162. strShow="-L3";
  163. break;
  164. default:
  165. break;
  166. }
  167. QString strPHShow="";
  168. if(nIndex<=0)
  169. {
  170. strPHShow=QString("Input Vol%1").arg(strShow);
  171. }else
  172. {
  173. strPHShow=QString("Output CH%1%2").arg(nIndex).arg(strShow);
  174. }
  175. SetData(ui->label_18,strPHShow);
  176. qDebug()<<"CH_Select:"<<nIndex<<"PH_Select:"<<nPHType;
  177. }
  178. /*
  179. void Widget::BtnDown(int nButtonID, int nDownType)
  180. {
  181. switch (nButtonID) {
  182. case GPIO_KEY1:
  183. //1按钮
  184. if(nDownType==BtnDown_C)
  185. {
  186. pageUp();
  187. }
  188. break;
  189. case GPIO_KEY2:
  190. {
  191. }
  192. break;
  193. case GPIO_KEY3:
  194. //3按钮
  195. if(nDownType==BtnDown_C)
  196. {
  197. pageDown();
  198. }
  199. break;
  200. case GPIO_KEY4:
  201. break;
  202. default:
  203. break;
  204. }
  205. qDebug()<<"Key"<<nButtonID<<" Type="<<nDownType;
  206. }
  207. */
  208. void Widget::pageUp()
  209. {
  210. int nIndex=ui->stackedWidget_Main->currentIndex();
  211. switch (nIndex) {
  212. case 1://
  213. {
  214. if(nMAXChildIndex>0)
  215. {
  216. nChidIndex=1;
  217. nChidPHIndex=0;
  218. setCH(nChidIndex);
  219. ui->stackedWidget_Main->setCurrentIndex(2);
  220. }else
  221. {
  222. ui->stackedWidget_Main->setCurrentIndex(3);
  223. }
  224. }
  225. break;
  226. case 2://通道页面
  227. {
  228. if(nChidIndex<nMAXChildIndex)
  229. {
  230. setCH(nChidIndex);
  231. nChidIndex+=1;
  232. nChidPHIndex=0;
  233. }else
  234. {
  235. ui->stackedWidget_Main->setCurrentIndex(3);
  236. nChidIndex=0;
  237. nChidPHIndex=0;
  238. }
  239. }
  240. break;
  241. case 3://传感器页面
  242. {
  243. ui->stackedWidget_Main->setCurrentIndex(0);
  244. }
  245. break;
  246. case 4://ph通道页面
  247. {
  248. if(nChidPHIndex<=2&&nChidPHIndex>=1)
  249. {
  250. nChidPHIndex+=1;
  251. setPH(nChidIndex,nChidPHIndex);
  252. }else if(nChidPHIndex==3||nChidPHIndex==0)
  253. {
  254. nChidPHIndex=1;
  255. setPH(nChidIndex,nChidPHIndex);
  256. }
  257. }
  258. break;
  259. default:
  260. {
  261. ui->stackedWidget_Main->setCurrentIndex(nIndex+1);
  262. }
  263. break;
  264. }
  265. qDebug()<<"Index:"<<nIndex<<"ChID:"<<nChidIndex;
  266. }
  267. void Widget::pageDown()
  268. {
  269. int nIndex=ui->stackedWidget_Main->currentIndex();
  270. if(nIndex==0)
  271. {
  272. ui->stackedWidget_Main->setCurrentIndex(3);
  273. }else if(nIndex==3)
  274. {
  275. if(nMAXChildIndex>0)
  276. {
  277. nChidIndex=nMAXChildIndex;
  278. setCH(nChidIndex);
  279. ui->stackedWidget_Main->setCurrentIndex(2);
  280. }else
  281. {
  282. ui->stackedWidget_Main->setCurrentIndex(1);
  283. }
  284. }
  285. else if(nIndex==2)
  286. {
  287. if(nChidIndex<=1)
  288. {
  289. ui->stackedWidget_Main->setCurrentIndex(1);
  290. nChidIndex=0;
  291. }
  292. else
  293. {
  294. setCH(nChidIndex);
  295. nChidIndex-=1;
  296. }
  297. }else if(nIndex==4)
  298. {
  299. if(nChidPHIndex<=3&&nChidPHIndex>=2)
  300. {
  301. nChidPHIndex-=1;
  302. setPH(nChidIndex,nChidPHIndex);
  303. }else if(nChidPHIndex==1||nChidPHIndex==0)
  304. {
  305. nChidPHIndex=3;
  306. setPH(nChidIndex,nChidPHIndex);
  307. }
  308. }else
  309. {
  310. ui->stackedWidget_Main->setCurrentIndex(nIndex-1);
  311. }
  312. qDebug()<<"Index:"<<nIndex<<"ChID:"<<nChidIndex;
  313. }
  314. void Widget::ChangeView(int widgetType, int nIndex)
  315. {
  316. if(widgetType_Main==widgetType)
  317. {
  318. qDebug()<<"Widget->ChangeView->widgetType_Main index="<<nIndex;
  319. if(nIndex==0)
  320. {
  321. ui->stackedWidget->setCurrentIndex(0);
  322. ui->stackedWidget_Main->setCurrentIndex(0);
  323. nChidIndex=0;
  324. }else
  325. {
  326. ui->stackedWidget->setCurrentIndex(0);
  327. ui->stackedWidget_Main->setCurrentIndex(nIndex);
  328. }
  329. }else if(widgetType_Setting==widgetType)
  330. {
  331. qDebug()<<"Widget->ChangeView->widgetType_Setting index="<<nIndex;
  332. ui->stackedWidget->setCurrentIndex(1);
  333. switch (nIndex) {
  334. case settingMode_Modbus:
  335. {
  336. ui->stackedWidget_Setting->setCurrentIndex(0);
  337. break;
  338. }
  339. case settingMode_IP:
  340. {
  341. ui->stackedWidget_Setting->setCurrentIndex(1);
  342. break;
  343. }
  344. }
  345. }
  346. }
  347. void Widget::setSelectStatus(int nIndex)
  348. {
  349. ui->label_addressName->setStyleSheet(LABEL_Defalut);
  350. ui->label_addressData->setStyleSheet(LABEL_Defalut);
  351. ui->label_MSData->setStyleSheet(LABEL_Defalut);
  352. ui->label_MSName->setStyleSheet(LABEL_Defalut);
  353. ui->label_SaveName->setStyleSheet(LABEL_Defalut);
  354. ui->label_SaveData->setStyleSheet(LABEL_Defalut);
  355. switch (nIndex) {
  356. case 0://address
  357. {
  358. ui->label_addressName->setStyleSheet(LABEL_SELECT);
  359. ui->label_addressData->setStyleSheet(LABEL_SELECT);
  360. }break;
  361. case 1://SAVE
  362. {
  363. ui->label_SaveName->setStyleSheet(LABEL_SELECT);
  364. ui->label_SaveData->setStyleSheet(LABEL_SELECT);
  365. }break;
  366. case 2://主从
  367. {
  368. ui->label_MSData->setStyleSheet(LABEL_SELECT);
  369. ui->label_MSName->setStyleSheet(LABEL_SELECT);
  370. }break;
  371. }
  372. }
  373. void Widget::CtrlBtnSet(int nIndex, int nData)
  374. {
  375. switch (nIndex) {
  376. case 0://address
  377. {
  378. ui->label_addressData->setText(QString::number(nData));
  379. }break;
  380. case 1://SAVE
  381. {
  382. if(nData==1)
  383. {
  384. ui->label_SaveData->setText("YES");
  385. }else
  386. {
  387. ui->label_SaveData->setText("NO");
  388. }
  389. }break;
  390. case 2://主从
  391. {
  392. if(nData!=1)
  393. {
  394. ui->label_MSData->setText("master");
  395. }else
  396. {
  397. ui->label_MSData->setText("slave");
  398. }
  399. }break;
  400. }
  401. }
  402. void Widget::ShowMSGWidget(QString strShowData)
  403. {
  404. if(strShowData.isEmpty())
  405. return;
  406. if(m_pMSGWidget&&m_pMSGWidget->isHidden())
  407. {
  408. m_pMSGWidget->SetText(strShowData);
  409. QRect parentRect = this->geometry();
  410. m_pMSGWidget->move(parentRect.center() - m_pMSGWidget->rect().center());
  411. m_pMSGWidget->show();
  412. }
  413. }
  414. void Widget::ClickMSGBtn()
  415. {
  416. if(m_pMSGWidget&&!m_pMSGWidget->isHidden())
  417. {
  418. m_pMSGWidget->hide();
  419. emit signal_ClickMSG();
  420. }
  421. }