widget.cpp 9.9 KB

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