widget.cpp 10 KB

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