widget.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. #include <QSettings>
  10. Widget::Widget(QWidget *parent) :
  11. QWidget(parent),
  12. ui(new Ui::Widget)
  13. {
  14. ui->setupUi(this);
  15. //ui->stackedWidget->setCurrentIndex(1);
  16. nChidIndex=0;
  17. nChidPHIndex=0;
  18. nMAXChildIndex=0;
  19. m_nAngle=0;
  20. m_nWidth = 160;
  21. m_nHeight = 128;
  22. m_pAngleCtrl=NULL;
  23. #ifdef RS485Type
  24. ui->label_MSName->hide();
  25. ui->label_MSData->hide();
  26. ui->label_3->hide();
  27. ui->label_4->hide();
  28. ui->label_5->hide();
  29. #else
  30. //ui->label_MSName->hide();
  31. //ui->label_MSData->hide();
  32. //ui->label_3->hide();
  33. //ui->label_4->hide();
  34. //ui->label_5->hide();
  35. ui->label_modbus_type->hide();
  36. ui->label_modbus_Addr->hide();
  37. #endif
  38. #ifdef hightSmartPDU
  39. ui->label_modbus_type->hide();
  40. ui->label_modbus_Addr->hide();
  41. #endif
  42. //定时器初始化读取配置文件:默认5秒一刷新
  43. QString pAddress = QCoreApplication::applicationDirPath() + "/" + "smartPDU.ini";
  44. qDebug()<<"SettingPath:"<<pAddress;
  45. QSettings settings(pAddress, QSettings::IniFormat);
  46. int nTimerData=5000;
  47. switch (settings.status()) {
  48. case QSettings::NoError:
  49. qDebug() << "successed";
  50. break;
  51. case QSettings::FormatError:
  52. qDebug() << "code Error";
  53. break;
  54. case QSettings::AccessError:
  55. qDebug() << "load Error";
  56. break;
  57. }
  58. if(settings.contains("misc/fliptime"))
  59. {
  60. QString strTime=settings.value("misc/fliptime").toString();
  61. nTimerData=strTime.toInt();
  62. nTimerData*=1000;
  63. qDebug()<<"[misc]fliptime="<<strTime <<"Int="<<nTimerData;
  64. }else
  65. {
  66. nTimerData=5000;
  67. QString strTime=settings.value("misc/fliptime").toString();
  68. qDebug()<<"misc/fliptime="<<strTime;
  69. }
  70. if(nTimerData>0)
  71. {
  72. m_Timer = new QTimer;
  73. m_Timer->setInterval(nTimerData);// 设置默认定时时长1000ms
  74. connect(m_Timer, &QTimer::timeout, this,&Widget::slot_TimerRun);
  75. m_Timer->start(nTimerData);
  76. qDebug()<<"Timer Start msec="<<nTimerData;
  77. }else
  78. {
  79. qDebug()<<"Timer Not Run";
  80. m_Timer=nullptr;
  81. }
  82. }
  83. Widget::~Widget()
  84. {
  85. delete ui;
  86. }
  87. void Widget::setInitData(QGraphicsProxyWidget *pAngleCtrl, QGraphicsView* pView, int nWidth, int nHeight)
  88. {
  89. m_pAngleCtrl=pAngleCtrl;
  90. m_nWidth= nWidth;
  91. m_nHeight = nHeight;
  92. m_pView=pView;
  93. }
  94. int Widget::getShowModeIndex()
  95. {
  96. return ui->stackedWidget->currentIndex();
  97. }
  98. int Widget::getShowPagIndex()
  99. {
  100. return ui->stackedWidget_Main->currentIndex();
  101. }
  102. int Widget::getShowSubIndex()
  103. {
  104. return nChidIndex;
  105. }
  106. int Widget::getShowPHIndex()
  107. {
  108. return nChidPHIndex;
  109. }
  110. void Widget::setShowPHIndex(int nIndex)
  111. {
  112. nChidPHIndex=nIndex;
  113. setPH(nChidIndex,nChidPHIndex);
  114. }
  115. void Widget::UpdateDate()
  116. {
  117. QDateTime currentTime = QDateTime::currentDateTime();
  118. QString strDate="Date:"+currentTime.toString("yyyy.MM.dd");
  119. QString strTime="Time:"+currentTime.toString("HH:mm:ss");
  120. if(ui)
  121. {
  122. ui->label_4->setText(strDate);
  123. ui->label_5->setText(strTime);
  124. }
  125. QString strIP="IP:"+ReadIP();
  126. ui->label_3->setText(strIP);
  127. if(pdu_Ctrl::instance()->m_nModbusMSType!=1)
  128. {
  129. ui->label_modbus_type->setText(QString("ModbusType: %1").arg( "master"));
  130. }else
  131. {
  132. ui->label_modbus_type->setText(QString("ModbusType: %1").arg("slave"));
  133. }
  134. ui->label_modbus_Addr->setText(QString("ModbusAddress: %1").arg(pdu_Ctrl::instance()->m_nModbusAddress));
  135. }
  136. QString Widget::ReadIP()
  137. {
  138. QProcess process;
  139. QString program = "ip"; // 或者 "ifconfig"
  140. QStringList arguments;
  141. if (program == "ip") {
  142. arguments << "addr" << "show" << "eth0";
  143. } else if (program == "ifconfig") {
  144. arguments << "eth0";
  145. }
  146. process.start(program, arguments);
  147. process.waitForFinished();
  148. QString output = process.readAllStandardOutput();
  149. QRegExp ipRegex("\\binet (\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})/\\d{1,2}\\b");
  150. int pos = 0;
  151. while ((pos = ipRegex.indexIn(output, pos)) != -1) {
  152. return ipRegex.cap(1);
  153. }
  154. return ""; // 没有找到IP地址
  155. }
  156. void Widget::SetData(QLabel *pLab, QString strShow)
  157. {
  158. if(pLab)
  159. {
  160. pLab->setText(strShow);
  161. }
  162. }
  163. void Widget::SetV(QString strShow){SetData(ui->label_8,strShow);SetData(ui->label_phV,strShow);}
  164. void Widget::SetA(QString strShow){SetData(ui->label_13,strShow);SetData(ui->label_A,strShow);SetData(ui->label_phA,strShow);}
  165. void Widget::SetW(QString strShow){SetData(ui->label_16,strShow);SetData(ui->label_W,strShow);SetData(ui->label_phW,strShow);}
  166. void Widget::SetF(QString strShow){SetData(ui->label_15,strShow);SetData(ui->label_F,strShow);SetData(ui->label_phF,strShow);}
  167. void Widget::SetSUM(QString strShow){SetData(ui->label_14,strShow);SetData(ui->label_S,strShow);SetData(ui->label_phKw,strShow);}
  168. void Widget::SetT(QString strShow){SetData(ui->label_Temp,strShow);}
  169. void Widget::SetH(QString strShow){SetData(ui->label_Humidity,strShow+"%");}
  170. void Widget::SetStatus(QString strShow){SetData(ui->label_V,strShow);}
  171. void Widget::SetIP(QString strIP)
  172. {
  173. if(ui)
  174. {
  175. ui->label_3->setText(strIP);
  176. }
  177. }
  178. void Widget::setCH(int nIndex)
  179. {
  180. QString strShow=QString("Output CH%1").arg(nIndex);
  181. SetData(ui->label_chName,strShow);
  182. }
  183. void Widget::setPH(int nIndex,int nPHType)
  184. {
  185. QString strShow="";
  186. switch (nPHType) {
  187. case 1:
  188. strShow="-A";
  189. break;
  190. case 2:
  191. strShow="-B";
  192. break;
  193. case 3:
  194. strShow="-C";
  195. break;
  196. default:
  197. break;
  198. }
  199. QString strPHShow="";
  200. if(nIndex<=0)
  201. {
  202. strPHShow=QString("Input Vol%1").arg(strShow);
  203. }else
  204. {
  205. strPHShow=QString("Output CH%1%2").arg(nIndex).arg(strShow);
  206. }
  207. SetData(ui->label_18,strPHShow);
  208. qDebug()<<"CH_Select:"<<nIndex<<"PH_Select:"<<nPHType;
  209. }
  210. /*
  211. void Widget::BtnDown(int nButtonID, int nDownType)
  212. {
  213. switch (nButtonID) {
  214. case GPIO_KEY1:
  215. //1按钮
  216. if(nDownType==BtnDown_C)
  217. {
  218. pageUp();
  219. }
  220. break;
  221. case GPIO_KEY2:
  222. {
  223. }
  224. break;
  225. case GPIO_KEY3:
  226. //3按钮
  227. if(nDownType==BtnDown_C)
  228. {
  229. pageDown();
  230. }
  231. break;
  232. case GPIO_KEY4:
  233. break;
  234. default:
  235. break;
  236. }
  237. qDebug()<<"Key"<<nButtonID<<" Type="<<nDownType;
  238. }
  239. */
  240. void Widget::pageUp()
  241. {
  242. int nIndex=ui->stackedWidget_Main->currentIndex();
  243. switch (nIndex) {
  244. case 1://
  245. {
  246. if(nMAXChildIndex>0)
  247. {
  248. nChidIndex=1;
  249. nChidPHIndex=0;
  250. setCH(nChidIndex);
  251. ui->stackedWidget_Main->setCurrentIndex(2);
  252. }else
  253. {
  254. ui->stackedWidget_Main->setCurrentIndex(3);
  255. }
  256. }
  257. break;
  258. case 2://通道页面
  259. {
  260. if(nChidIndex<nMAXChildIndex)
  261. {
  262. setCH(nChidIndex);
  263. nChidIndex+=1;
  264. nChidPHIndex=0;
  265. }else
  266. {
  267. ui->stackedWidget_Main->setCurrentIndex(3);
  268. nChidIndex=0;
  269. nChidPHIndex=0;
  270. }
  271. }
  272. break;
  273. case 3://传感器页面
  274. {
  275. ui->stackedWidget_Main->setCurrentIndex(0);
  276. }
  277. break;
  278. case 4://ph通道页面
  279. {
  280. if(nChidPHIndex<=2&&nChidPHIndex>=1)
  281. {
  282. nChidPHIndex+=1;
  283. setPH(nChidIndex,nChidPHIndex);
  284. }else if(nChidPHIndex=3||nChidPHIndex==0)
  285. {
  286. nChidPHIndex=1;
  287. setPH(nChidIndex,nChidPHIndex);
  288. }
  289. }
  290. break;
  291. default:
  292. {
  293. ui->stackedWidget_Main->setCurrentIndex(nIndex+1);
  294. }
  295. break;
  296. }
  297. qDebug()<<"Index:"<<nIndex<<"ChID:"<<nChidIndex;
  298. }
  299. void Widget::pageDown()
  300. {
  301. int nIndex=ui->stackedWidget_Main->currentIndex();
  302. if(nIndex==0)
  303. {
  304. ui->stackedWidget_Main->setCurrentIndex(3);
  305. }else if(nIndex==3)
  306. {
  307. if(nMAXChildIndex>0)
  308. {
  309. nChidIndex=nMAXChildIndex;
  310. setCH(nChidIndex);
  311. ui->stackedWidget_Main->setCurrentIndex(2);
  312. }else
  313. {
  314. ui->stackedWidget_Main->setCurrentIndex(1);
  315. }
  316. }
  317. else if(nIndex==2)
  318. {
  319. if(nChidIndex<=1)
  320. {
  321. ui->stackedWidget_Main->setCurrentIndex(1);
  322. nChidIndex=0;
  323. }
  324. else
  325. {
  326. setCH(nChidIndex);
  327. nChidIndex-=1;
  328. }
  329. }else if(nIndex==4)
  330. {
  331. if(nChidPHIndex<=3&&nChidPHIndex>=2)
  332. {
  333. nChidPHIndex-=1;
  334. setPH(nChidIndex,nChidPHIndex);
  335. }else if(nChidPHIndex=1||nChidPHIndex==0)
  336. {
  337. nChidPHIndex=3;
  338. setPH(nChidIndex,nChidPHIndex);
  339. }
  340. }else
  341. {
  342. ui->stackedWidget_Main->setCurrentIndex(nIndex-1);
  343. }
  344. qDebug()<<"Index:"<<nIndex<<"ChID:"<<nChidIndex;
  345. }
  346. void Widget::ChangeView(int widgetType, int nIndex)
  347. {
  348. if(widgetType_Main==widgetType)
  349. {
  350. qDebug()<<"Widget->ChangeView->widgetType_Main index="<<nIndex;
  351. if(nIndex==0)
  352. {
  353. ui->stackedWidget->setCurrentIndex(0);
  354. ui->stackedWidget_Main->setCurrentIndex(0);
  355. nChidIndex=0;
  356. }else
  357. {
  358. ui->stackedWidget->setCurrentIndex(0);
  359. ui->stackedWidget_Main->setCurrentIndex(nIndex);
  360. }
  361. }else if(widgetType_Setting==widgetType)
  362. {
  363. qDebug()<<"Widget->ChangeView->widgetType_Setting index="<<nIndex;
  364. ui->stackedWidget->setCurrentIndex(1);
  365. switch (nIndex) {
  366. case settingMode_Modbus:
  367. {
  368. ui->stackedWidget_Setting->setCurrentIndex(0);
  369. break;
  370. }
  371. case settingMode_IP:
  372. {
  373. ui->stackedWidget_Setting->setCurrentIndex(1);
  374. break;
  375. }
  376. }
  377. }
  378. }
  379. void Widget::setSelectStatus(int nIndex)
  380. {
  381. ui->label_addressName->setStyleSheet(LABEL_Defalut);
  382. ui->label_addressData->setStyleSheet(LABEL_Defalut);
  383. ui->label_MSData->setStyleSheet(LABEL_Defalut);
  384. ui->label_MSName->setStyleSheet(LABEL_Defalut);
  385. ui->label_SaveName->setStyleSheet(LABEL_Defalut);
  386. ui->label_SaveData->setStyleSheet(LABEL_Defalut);
  387. switch (nIndex) {
  388. case 0://address
  389. {
  390. ui->label_addressName->setStyleSheet(LABEL_SELECT);
  391. ui->label_addressData->setStyleSheet(LABEL_SELECT);
  392. }break;
  393. case 1://SAVE
  394. {
  395. ui->label_SaveName->setStyleSheet(LABEL_SELECT);
  396. ui->label_SaveData->setStyleSheet(LABEL_SELECT);
  397. }break;
  398. case 2://主从
  399. {
  400. ui->label_MSData->setStyleSheet(LABEL_SELECT);
  401. ui->label_MSName->setStyleSheet(LABEL_SELECT);
  402. }break;
  403. }
  404. }
  405. void Widget::CtrlBtnSet(int nIndex, int nData)
  406. {
  407. switch (nIndex) {
  408. case 0://address
  409. {
  410. ui->label_addressData->setText(QString::number(nData));
  411. }break;
  412. case 1://SAVE
  413. {
  414. if(nData==1)
  415. {
  416. ui->label_SaveData->setText("YES");
  417. }else
  418. {
  419. ui->label_SaveData->setText("NO");
  420. }
  421. }break;
  422. case 2://主从
  423. {
  424. if(nData!=1)
  425. {
  426. ui->label_MSData->setText("master");
  427. }else
  428. {
  429. ui->label_MSData->setText("slave");
  430. }
  431. }break;
  432. }
  433. }
  434. void Widget::slot_TimerRun()
  435. {
  436. int nIndex=ui->stackedWidget->currentIndex();
  437. int nSubIndex=ui->stackedWidget_Main->currentIndex();
  438. if(nIndex==0&&nSubIndex==2)//通道页面标记
  439. {
  440. if(nChidIndex<nMAXChildIndex)
  441. {
  442. setCH(nChidIndex);
  443. nChidIndex+=1;
  444. nChidPHIndex=0;
  445. }else
  446. {
  447. nChidIndex=1;
  448. nChidPHIndex=0;
  449. setCH(nChidIndex);
  450. }
  451. qDebug()<<"Timer Once ----nChidIndex="<<nChidIndex;
  452. }
  453. }