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