widget.cpp 10.0 KB

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