widget.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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::SetStatus(QString strShow){SetData(ui->label_V,strShow);}
  133. void Widget::SetIP(QString strIP)
  134. {
  135. if(ui)
  136. {
  137. ui->label_3->setText(strIP);
  138. }
  139. }
  140. void Widget::setCH(int nIndex)
  141. {
  142. QString strShow=QString("Output CH%1").arg(nIndex);
  143. SetData(ui->label_chName,strShow);
  144. }
  145. void Widget::setPH(int nIndex,int nPHType)
  146. {
  147. QString strShow="";
  148. switch (nPHType) {
  149. case 1:
  150. strShow="-L1";
  151. break;
  152. case 2:
  153. strShow="-L2";
  154. break;
  155. case 3:
  156. strShow="-L3";
  157. break;
  158. default:
  159. break;
  160. }
  161. QString strPHShow="";
  162. if(nIndex<=0)
  163. {
  164. strPHShow=QString("Input Vol%1").arg(strShow);
  165. }else
  166. {
  167. strPHShow=QString("Output CH%1%2").arg(nIndex).arg(strShow);
  168. }
  169. SetData(ui->label_18,strPHShow);
  170. qDebug()<<"CH_Select:"<<nIndex<<"PH_Select:"<<nPHType;
  171. }
  172. /*
  173. void Widget::BtnDown(int nButtonID, int nDownType)
  174. {
  175. switch (nButtonID) {
  176. case GPIO_KEY1:
  177. //1按钮
  178. if(nDownType==BtnDown_C)
  179. {
  180. pageUp();
  181. }
  182. break;
  183. case GPIO_KEY2:
  184. {
  185. }
  186. break;
  187. case GPIO_KEY3:
  188. //3按钮
  189. if(nDownType==BtnDown_C)
  190. {
  191. pageDown();
  192. }
  193. break;
  194. case GPIO_KEY4:
  195. break;
  196. default:
  197. break;
  198. }
  199. qDebug()<<"Key"<<nButtonID<<" Type="<<nDownType;
  200. }
  201. */
  202. void Widget::pageUp()
  203. {
  204. int nIndex=ui->stackedWidget_Main->currentIndex();
  205. switch (nIndex) {
  206. case 1://
  207. {
  208. if(nMAXChildIndex>0)
  209. {
  210. nChidIndex=1;
  211. nChidPHIndex=0;
  212. setCH(nChidIndex);
  213. ui->stackedWidget_Main->setCurrentIndex(2);
  214. }else
  215. {
  216. ui->stackedWidget_Main->setCurrentIndex(3);
  217. }
  218. }
  219. break;
  220. case 2://通道页面
  221. {
  222. if(nChidIndex<nMAXChildIndex)
  223. {
  224. setCH(nChidIndex);
  225. nChidIndex+=1;
  226. nChidPHIndex=0;
  227. }else
  228. {
  229. ui->stackedWidget_Main->setCurrentIndex(3);
  230. nChidIndex=0;
  231. nChidPHIndex=0;
  232. }
  233. }
  234. break;
  235. case 3://传感器页面
  236. {
  237. ui->stackedWidget_Main->setCurrentIndex(0);
  238. }
  239. break;
  240. case 4://ph通道页面
  241. {
  242. if(nChidPHIndex<=2&&nChidPHIndex>=1)
  243. {
  244. nChidPHIndex+=1;
  245. setPH(nChidIndex,nChidPHIndex);
  246. }else if(nChidPHIndex=3||nChidPHIndex==0)
  247. {
  248. nChidPHIndex=1;
  249. setPH(nChidIndex,nChidPHIndex);
  250. }
  251. }
  252. break;
  253. default:
  254. {
  255. ui->stackedWidget_Main->setCurrentIndex(nIndex+1);
  256. }
  257. break;
  258. }
  259. qDebug()<<"Index:"<<nIndex<<"ChID:"<<nChidIndex;
  260. }
  261. void Widget::pageDown()
  262. {
  263. int nIndex=ui->stackedWidget_Main->currentIndex();
  264. if(nIndex==0)
  265. {
  266. ui->stackedWidget_Main->setCurrentIndex(3);
  267. }else if(nIndex==3)
  268. {
  269. if(nMAXChildIndex>0)
  270. {
  271. nChidIndex=nMAXChildIndex;
  272. setCH(nChidIndex);
  273. ui->stackedWidget_Main->setCurrentIndex(2);
  274. }else
  275. {
  276. ui->stackedWidget_Main->setCurrentIndex(1);
  277. }
  278. }
  279. else if(nIndex==2)
  280. {
  281. if(nChidIndex<=1)
  282. {
  283. ui->stackedWidget_Main->setCurrentIndex(1);
  284. nChidIndex=0;
  285. }
  286. else
  287. {
  288. setCH(nChidIndex);
  289. nChidIndex-=1;
  290. }
  291. }else if(nIndex==4)
  292. {
  293. if(nChidPHIndex<=3&&nChidPHIndex>=2)
  294. {
  295. nChidPHIndex-=1;
  296. setPH(nChidIndex,nChidPHIndex);
  297. }else if(nChidPHIndex=1||nChidPHIndex==0)
  298. {
  299. nChidPHIndex=3;
  300. setPH(nChidIndex,nChidPHIndex);
  301. }
  302. }else
  303. {
  304. ui->stackedWidget_Main->setCurrentIndex(nIndex-1);
  305. }
  306. qDebug()<<"Index:"<<nIndex<<"ChID:"<<nChidIndex;
  307. }
  308. void Widget::ChangeView(int widgetType, int nIndex)
  309. {
  310. if(widgetType_Main==widgetType)
  311. {
  312. qDebug()<<"Widget->ChangeView->widgetType_Main index="<<nIndex;
  313. if(nIndex==0)
  314. {
  315. ui->stackedWidget->setCurrentIndex(0);
  316. ui->stackedWidget_Main->setCurrentIndex(0);
  317. nChidIndex=0;
  318. }else
  319. {
  320. ui->stackedWidget->setCurrentIndex(0);
  321. ui->stackedWidget_Main->setCurrentIndex(nIndex);
  322. }
  323. }else if(widgetType_Setting==widgetType)
  324. {
  325. qDebug()<<"Widget->ChangeView->widgetType_Setting index="<<nIndex;
  326. ui->stackedWidget->setCurrentIndex(1);
  327. switch (nIndex) {
  328. case settingMode_Modbus:
  329. {
  330. ui->stackedWidget_Setting->setCurrentIndex(0);
  331. break;
  332. }
  333. case settingMode_IP:
  334. {
  335. ui->stackedWidget_Setting->setCurrentIndex(1);
  336. break;
  337. }
  338. }
  339. }
  340. }
  341. void Widget::setSelectStatus(int nIndex)
  342. {
  343. ui->label_addressName->setStyleSheet(LABEL_Defalut);
  344. ui->label_addressData->setStyleSheet(LABEL_Defalut);
  345. ui->label_MSData->setStyleSheet(LABEL_Defalut);
  346. ui->label_MSName->setStyleSheet(LABEL_Defalut);
  347. ui->label_SaveName->setStyleSheet(LABEL_Defalut);
  348. ui->label_SaveData->setStyleSheet(LABEL_Defalut);
  349. switch (nIndex) {
  350. case 0://address
  351. {
  352. ui->label_addressName->setStyleSheet(LABEL_SELECT);
  353. ui->label_addressData->setStyleSheet(LABEL_SELECT);
  354. }break;
  355. case 1://SAVE
  356. {
  357. ui->label_SaveName->setStyleSheet(LABEL_SELECT);
  358. ui->label_SaveData->setStyleSheet(LABEL_SELECT);
  359. }break;
  360. case 2://主从
  361. {
  362. ui->label_MSData->setStyleSheet(LABEL_SELECT);
  363. ui->label_MSName->setStyleSheet(LABEL_SELECT);
  364. }break;
  365. }
  366. }
  367. void Widget::CtrlBtnSet(int nIndex, int nData)
  368. {
  369. switch (nIndex) {
  370. case 0://address
  371. {
  372. ui->label_addressData->setText(QString::number(nData));
  373. }break;
  374. case 1://SAVE
  375. {
  376. if(nData==1)
  377. {
  378. ui->label_SaveData->setText("YES");
  379. }else
  380. {
  381. ui->label_SaveData->setText("NO");
  382. }
  383. }break;
  384. case 2://主从
  385. {
  386. if(nData!=1)
  387. {
  388. ui->label_MSData->setText("master");
  389. }else
  390. {
  391. ui->label_MSData->setText("slave");
  392. }
  393. }break;
  394. }
  395. }