widget.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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. #ifdef Language_Zh
  119. QString strDate="日期:"+currentTime.toString("yyyy.MM.dd");
  120. #else
  121. QString strDate="Date:"+currentTime.toString("yyyy.MM.dd");
  122. #endif
  123. QString strTime=currentTime.toString("HH:mm:ss");
  124. if(ui)
  125. {
  126. ui->label_4->setText(strDate);
  127. ui->label_5->setText(strTime);
  128. }
  129. #ifdef Language_Zh
  130. QString strIP="IP地址:"+ReadIP();
  131. #else
  132. QString strIP="IP:"+ReadIP();
  133. #endif
  134. ui->label_3->setText(strIP);
  135. if(pdu_Ctrl::instance()->m_nModbusMSType!=1)
  136. {
  137. ui->label_modbus_type->setText(QString("ModbusType: %1").arg( "master"));
  138. }else
  139. {
  140. ui->label_modbus_type->setText(QString("ModbusType: %1").arg("slave"));
  141. }
  142. ui->label_modbus_Addr->setText(QString("ModbusAddress: %1").arg(pdu_Ctrl::instance()->m_nModbusAddress));
  143. }
  144. QString Widget::ReadIP()
  145. {
  146. QProcess process;
  147. QString program = "ip"; // 或者 "ifconfig"
  148. QStringList arguments;
  149. if (program == "ip") {
  150. arguments << "addr" << "show" << "eth0";
  151. } else if (program == "ifconfig") {
  152. arguments << "eth0";
  153. }
  154. process.start(program, arguments);
  155. process.waitForFinished();
  156. QString output = process.readAllStandardOutput();
  157. QRegExp ipRegex("\\binet (\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})/\\d{1,2}\\b");
  158. int pos = 0;
  159. while ((pos = ipRegex.indexIn(output, pos)) != -1) {
  160. return ipRegex.cap(1);
  161. }
  162. return ""; // 没有找到IP地址
  163. }
  164. void Widget::SetData(QLabel *pLab, QString strShow)
  165. {
  166. if(pLab)
  167. {
  168. pLab->setText(strShow);
  169. }
  170. }
  171. void Widget::SetV(QString strShow){SetData(ui->label_8,strShow);SetData(ui->label_phV,strShow);}
  172. void Widget::SetA(QString strShow){SetData(ui->label_13,strShow);SetData(ui->label_A,strShow);SetData(ui->label_phA,strShow);}
  173. void Widget::SetW(QString strShow){SetData(ui->label_16,strShow);SetData(ui->label_W,strShow);SetData(ui->label_phW,strShow);}
  174. void Widget::SetF(QString strShow){SetData(ui->label_15,strShow);SetData(ui->label_F,strShow);SetData(ui->label_phF,strShow);}
  175. void Widget::SetSUM(QString strShow){SetData(ui->label_14,strShow);SetData(ui->label_S,strShow);SetData(ui->label_phKw,strShow);}
  176. void Widget::SetT(QString strShow){SetData(ui->label_Temp,strShow);}
  177. void Widget::SetH(QString strShow){SetData(ui->label_Humidity,strShow+"%");}
  178. void Widget::SetStatus(QString strShow){SetData(ui->label_V,strShow);}
  179. void Widget::SetIP(QString strIP)
  180. {
  181. if(ui)
  182. {
  183. ui->label_3->setText(strIP);
  184. }
  185. }
  186. void Widget::setCH(int nIndex)
  187. {
  188. #ifdef Language_Zh
  189. QString strShow=QString("输出通道CH%1").arg(nIndex);
  190. #else
  191. QString strShow=QString("Output CH%1").arg(nIndex);
  192. #endif
  193. SetData(ui->label_chName,strShow);
  194. }
  195. void Widget::setPH(int nIndex,int nPHType)
  196. {
  197. QString strShow="";
  198. switch (nPHType) {
  199. case 1:
  200. strShow="-L1";
  201. break;
  202. case 2:
  203. strShow="-L2";
  204. break;
  205. case 3:
  206. strShow="-L3";
  207. break;
  208. default:
  209. break;
  210. }
  211. QString strPHShow="";
  212. if(nIndex<=0)
  213. {
  214. #ifdef Language_Zh
  215. strPHShow=QString("输入通道%1").arg(strShow);
  216. #else
  217. strPHShow=QString("Input Vol%1").arg(strShow);
  218. #endif
  219. }else
  220. {
  221. #ifdef Language_Zh
  222. strPHShow=QString("输出通道CH%1%2").arg(nIndex).arg(strShow);
  223. #else
  224. strPHShow=QString("输出通道CH%1%2").arg(nIndex).arg(strShow);
  225. #endif
  226. }
  227. SetData(ui->label_18,strPHShow);
  228. qDebug()<<"CH_Select:"<<nIndex<<"PH_Select:"<<nPHType;
  229. }
  230. /*
  231. void Widget::BtnDown(int nButtonID, int nDownType)
  232. {
  233. switch (nButtonID) {
  234. case GPIO_KEY1:
  235. //1按钮
  236. if(nDownType==BtnDown_C)
  237. {
  238. pageUp();
  239. }
  240. break;
  241. case GPIO_KEY2:
  242. {
  243. }
  244. break;
  245. case GPIO_KEY3:
  246. //3按钮
  247. if(nDownType==BtnDown_C)
  248. {
  249. pageDown();
  250. }
  251. break;
  252. case GPIO_KEY4:
  253. break;
  254. default:
  255. break;
  256. }
  257. qDebug()<<"Key"<<nButtonID<<" Type="<<nDownType;
  258. }
  259. */
  260. void Widget::pageUp()
  261. {
  262. int nIndex=ui->stackedWidget_Main->currentIndex();
  263. switch (nIndex) {
  264. case 1://
  265. {
  266. if(nMAXChildIndex>0)
  267. {
  268. nChidIndex=1;
  269. nChidPHIndex=0;
  270. setCH(nChidIndex);
  271. ui->stackedWidget_Main->setCurrentIndex(2);
  272. }else
  273. {
  274. ui->stackedWidget_Main->setCurrentIndex(3);
  275. }
  276. }
  277. break;
  278. case 2://通道页面
  279. {
  280. if(nChidIndex<nMAXChildIndex)
  281. {
  282. setCH(nChidIndex);
  283. nChidIndex+=1;
  284. nChidPHIndex=0;
  285. }else
  286. {
  287. ui->stackedWidget_Main->setCurrentIndex(3);
  288. nChidIndex=0;
  289. nChidPHIndex=0;
  290. }
  291. }
  292. break;
  293. case 3://传感器页面
  294. {
  295. ui->stackedWidget_Main->setCurrentIndex(0);
  296. }
  297. break;
  298. case 4://ph通道页面
  299. {
  300. if(nChidPHIndex<=2&&nChidPHIndex>=1)
  301. {
  302. nChidPHIndex+=1;
  303. setPH(nChidIndex,nChidPHIndex);
  304. }else if(nChidPHIndex=3||nChidPHIndex==0)
  305. {
  306. nChidPHIndex=1;
  307. setPH(nChidIndex,nChidPHIndex);
  308. }
  309. }
  310. break;
  311. default:
  312. {
  313. ui->stackedWidget_Main->setCurrentIndex(nIndex+1);
  314. }
  315. break;
  316. }
  317. qDebug()<<"Index:"<<nIndex<<"ChID:"<<nChidIndex;
  318. }
  319. void Widget::pageDown()
  320. {
  321. int nIndex=ui->stackedWidget_Main->currentIndex();
  322. if(nIndex==0)
  323. {
  324. ui->stackedWidget_Main->setCurrentIndex(3);
  325. }else if(nIndex==3)
  326. {
  327. if(nMAXChildIndex>0)
  328. {
  329. nChidIndex=nMAXChildIndex;
  330. setCH(nChidIndex);
  331. ui->stackedWidget_Main->setCurrentIndex(2);
  332. }else
  333. {
  334. ui->stackedWidget_Main->setCurrentIndex(1);
  335. }
  336. }
  337. else if(nIndex==2)
  338. {
  339. if(nChidIndex<=1)
  340. {
  341. ui->stackedWidget_Main->setCurrentIndex(1);
  342. nChidIndex=0;
  343. }
  344. else
  345. {
  346. setCH(nChidIndex);
  347. nChidIndex-=1;
  348. }
  349. }else if(nIndex==4)
  350. {
  351. if(nChidPHIndex<=3&&nChidPHIndex>=2)
  352. {
  353. nChidPHIndex-=1;
  354. setPH(nChidIndex,nChidPHIndex);
  355. }else if(nChidPHIndex=1||nChidPHIndex==0)
  356. {
  357. nChidPHIndex=3;
  358. setPH(nChidIndex,nChidPHIndex);
  359. }
  360. }else
  361. {
  362. ui->stackedWidget_Main->setCurrentIndex(nIndex-1);
  363. }
  364. qDebug()<<"Index:"<<nIndex<<"ChID:"<<nChidIndex;
  365. }
  366. void Widget::ChangeView(int widgetType, int nIndex)
  367. {
  368. if(widgetType_Main==widgetType)
  369. {
  370. qDebug()<<"Widget->ChangeView->widgetType_Main index="<<nIndex;
  371. if(nIndex==0)
  372. {
  373. ui->stackedWidget->setCurrentIndex(0);
  374. ui->stackedWidget_Main->setCurrentIndex(0);
  375. nChidIndex=0;
  376. }else
  377. {
  378. ui->stackedWidget->setCurrentIndex(0);
  379. ui->stackedWidget_Main->setCurrentIndex(nIndex);
  380. }
  381. }else if(widgetType_Setting==widgetType)
  382. {
  383. qDebug()<<"Widget->ChangeView->widgetType_Setting index="<<nIndex;
  384. ui->stackedWidget->setCurrentIndex(1);
  385. switch (nIndex) {
  386. case settingMode_Modbus:
  387. {
  388. ui->stackedWidget_Setting->setCurrentIndex(0);
  389. break;
  390. }
  391. case settingMode_IP:
  392. {
  393. ui->stackedWidget_Setting->setCurrentIndex(1);
  394. break;
  395. }
  396. }
  397. }
  398. }
  399. void Widget::setSelectStatus(int nIndex)
  400. {
  401. ui->label_addressName->setStyleSheet(LABEL_Defalut);
  402. ui->label_addressData->setStyleSheet(LABEL_Defalut);
  403. ui->label_MSData->setStyleSheet(LABEL_Defalut);
  404. ui->label_MSName->setStyleSheet(LABEL_Defalut);
  405. ui->label_SaveName->setStyleSheet(LABEL_Defalut);
  406. ui->label_SaveData->setStyleSheet(LABEL_Defalut);
  407. switch (nIndex) {
  408. case 0://address
  409. {
  410. ui->label_addressName->setStyleSheet(LABEL_SELECT);
  411. ui->label_addressData->setStyleSheet(LABEL_SELECT);
  412. }break;
  413. case 1://SAVE
  414. {
  415. ui->label_SaveName->setStyleSheet(LABEL_SELECT);
  416. ui->label_SaveData->setStyleSheet(LABEL_SELECT);
  417. }break;
  418. case 2://主从
  419. {
  420. ui->label_MSData->setStyleSheet(LABEL_SELECT);
  421. ui->label_MSName->setStyleSheet(LABEL_SELECT);
  422. }break;
  423. }
  424. }
  425. void Widget::CtrlBtnSet(int nIndex, int nData)
  426. {
  427. switch (nIndex) {
  428. case 0://address
  429. {
  430. ui->label_addressData->setText(QString::number(nData));
  431. }break;
  432. case 1://SAVE
  433. {
  434. if(nData==1)
  435. {
  436. ui->label_SaveData->setText("YES");
  437. }else
  438. {
  439. ui->label_SaveData->setText("NO");
  440. }
  441. }break;
  442. case 2://主从
  443. {
  444. if(nData!=1)
  445. {
  446. ui->label_MSData->setText("master");
  447. }else
  448. {
  449. ui->label_MSData->setText("slave");
  450. }
  451. }break;
  452. }
  453. }
  454. void Widget::slot_TimerRun()
  455. {
  456. #ifdef JY242
  457. pageUp();
  458. #else
  459. int nIndex=ui->stackedWidget->currentIndex();
  460. int nSubIndex=ui->stackedWidget_Main->currentIndex();
  461. if(nIndex==0&&nSubIndex==2)//通道页面标记
  462. {
  463. if(nChidIndex<nMAXChildIndex)
  464. {
  465. setCH(nChidIndex);
  466. nChidIndex+=1;
  467. nChidPHIndex=0;
  468. }else
  469. {
  470. nChidIndex=1;
  471. nChidPHIndex=0;
  472. setCH(nChidIndex);
  473. }
  474. qDebug()<<"Timer Once ----nChidIndex="<<nChidIndex;
  475. }
  476. #endif
  477. }