EyeMapInfo.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. #include "EyeMapInfo.h"
  2. #include "OneEyeMap.h"
  3. #include <QFile>
  4. #include <QApplication>
  5. #include <QDir>
  6. AllEyeMapInfo::~AllEyeMapInfo()
  7. {
  8. }
  9. /* 初始化函数 */
  10. void AllEyeMapInfo::initEyeMapInfo()
  11. {
  12. m_saveFilePath = QApplication::applicationDirPath() + "/config/";
  13. /* 判断文件夹是否存在 */
  14. QDir dir;
  15. if(!dir.exists(m_saveFilePath))
  16. {
  17. dir.mkpath(m_saveFilePath);
  18. }
  19. /* 判断文件是否存在 */
  20. QString filePath = m_saveFilePath + m_saveFileName;
  21. QFile file(filePath);
  22. if(!file.exists())
  23. {
  24. SPDLOG_INFO("设置数据文件不存在,创建文件");
  25. createSaveFile(m_saveFileName);
  26. } else
  27. {
  28. readSaveFile(m_saveFileName);
  29. }
  30. /* 设置可用通道,给设置页面使用 */
  31. // for(int i = 1; i <= 8; i++)
  32. // {
  33. // OneChannelInfo info;
  34. // info.channel = static_cast<OscChnNum>(i);
  35. // info.channelName = getChannelName(static_cast<OscChnNum>(i));
  36. // GEyeMapInfo.appendChannelInfo(info);
  37. // }
  38. }
  39. /* 读取保存的文件 */
  40. void AllEyeMapInfo::readSaveFile(const QString& fileName)
  41. {
  42. QString filePath = m_saveFilePath + fileName;
  43. QFile file(filePath);
  44. if(!file.open(QIODevice::ReadOnly))
  45. {
  46. SPDLOG_ERROR("打开文件失败, 文件路径: {}", filePath.toStdString());
  47. return;
  48. }
  49. QByteArray data = file.readAll();
  50. file.close();
  51. try
  52. {
  53. listInitEyeMapInfo.clear();
  54. nJson json = nJson::parse(data.toStdString());
  55. nJson jsonArray = json["EyeMapInfo"];
  56. for(const auto& it : jsonArray)
  57. {
  58. OneEyeMapInfo info;
  59. // info.uid = static_cast<EyeMapUID>(it["uid"].is_null() ? 0 : it["uid"].get<int>());
  60. info.num = it["num"];
  61. info.title = QString::fromStdString(it["title"].get<std::string>());
  62. info.titleBarColor = QColor(QString::fromStdString(it["titleBarColor"].get<std::string>()));
  63. info.isShow = it["isShow"];
  64. info.channelInfo.channel = static_cast<OscChnNum>(it["OscChannelNum"].get<int>());
  65. info.channelInfo.channelName = QString::fromStdString(it["OscChannelName"]);
  66. info.voltageRange = static_cast<OscVoltageRange>(it["voltageRange"]);
  67. info.tGridValue = static_cast<OscTimeGridValue>(it["timeGridValue"]);
  68. listInitEyeMapInfo.append(info);
  69. }
  70. nJson jsonBase = json["BaseInfo"];
  71. row = jsonBase["row"];
  72. column = jsonBase["column"];
  73. }
  74. catch (const nJson::parse_error& e) {
  75. SPDLOG_ERROR("解析json文件失败, 错误信息:{}, 文件路径: {}", e.what(), filePath.toStdString());
  76. createSaveFile(m_saveFileName);
  77. }
  78. catch (const nJson::exception& e) {
  79. SPDLOG_ERROR("解析json文件失败, 错误信息:{}, 文件路径: {}", e.what(), filePath.toStdString());
  80. createSaveFile(m_saveFileName);
  81. }
  82. catch(...)
  83. {
  84. SPDLOG_ERROR("解析json文件失败, 文件路径: {}", filePath.toStdString());
  85. createSaveFile(m_saveFileName);
  86. }
  87. }
  88. /* 写入保存的文件 */
  89. void AllEyeMapInfo::createSaveFile(const QString& fileName)
  90. {
  91. QString filePath = m_saveFilePath + fileName;
  92. QFile file(filePath);
  93. if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
  94. {
  95. SPDLOG_ERROR("打开文件失败,文件路径: {}", filePath.toStdString());
  96. return;
  97. }
  98. SPDLOG_INFO("创建数据文件成功,文件路径: {}", filePath.toStdString());
  99. /* 设置初始化的数据 */
  100. listInitEyeMapInfo.clear();
  101. for(int i = 0; i < 8; i++)
  102. {
  103. OneEyeMapInfo info = getInitEyeMapInfo(i + 1);
  104. listInitEyeMapInfo.append(info);
  105. }
  106. row = 4;
  107. column = 2;
  108. nJson json0;
  109. nJson jsonArray1 = json0.array();
  110. for(const auto& it : listInitEyeMapInfo)
  111. {
  112. nJson json2;
  113. // json2["uid"] = static_cast<int>(it.uid);
  114. json2["num"] = it.num;
  115. json2["title"] = it.title.toStdString();
  116. json2["titleBarColor"] = it.titleBarColor.name().toStdString();
  117. json2["isShow"] = it.isShow;
  118. json2["OscChannelNum"] = it.channelInfo.channel;
  119. json2["OscChannelName"] = it.channelInfo.channelName.toStdString();
  120. json2["voltageRange"] = static_cast<int>(it.voltageRange);
  121. json2["timeGridValue"] = static_cast<int>(it.tGridValue);
  122. jsonArray1.push_back(json2);
  123. }
  124. json0["EyeMapInfo"] = jsonArray1;
  125. /* 保存行和列 */
  126. nJson json1;
  127. json1["row"] = 4;
  128. json1["column"] = 2;
  129. json0["BaseInfo"] = json1;
  130. auto data = json0.dump(4);
  131. file.write(data.c_str());
  132. file.close();
  133. }
  134. /* 更新保存的文件 */
  135. void AllEyeMapInfo::updateSaveFile()
  136. {
  137. QString filePath = m_saveFilePath + m_saveFileName;
  138. QFile file(filePath);
  139. if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
  140. {
  141. SPDLOG_ERROR("打开文件失败, 文件路径: {}", filePath.toStdString());
  142. return;
  143. }
  144. nJson json0;
  145. nJson jsonArray1 = json0.array();
  146. for(const auto& it : listInitEyeMapInfo)
  147. {
  148. // SPDLOG_DEBUG("UID: {}", static_cast<int>(it.uid));
  149. nJson json2;
  150. // json2["uid"] = static_cast<int>(it.uid);
  151. json2["num"] = it.num;
  152. json2["title"] = it.title.toStdString();
  153. json2["titleBarColor"] = it.titleBarColor.name().toStdString();
  154. json2["isShow"] = it.isShow;
  155. json2["OscChannelNum"] = it.channelInfo.channel;
  156. json2["OscChannelName"] = it.channelInfo.channelName.toStdString();
  157. json2["voltageRange"] = static_cast<int>(it.voltageRange);
  158. json2["timeGridValue"] = static_cast<int>(it.tGridValue);
  159. jsonArray1.push_back(json2);
  160. }
  161. json0["EyeMapInfo"] = jsonArray1;
  162. nJson json1;
  163. json1["row"] = row;
  164. json1["column"] = column;
  165. json0["BaseInfo"] = json1;
  166. auto data = json0.dump(4);
  167. file.write(data.c_str());
  168. file.close();
  169. }
  170. /* 获取眼图列表 */
  171. const QList<OneEyeMap*>& AllEyeMapInfo::getEyeMapList()
  172. {
  173. return listEyeMapPtr;
  174. }
  175. /* 添加眼图指针 */
  176. void AllEyeMapInfo::addEyeMapPtr(OneEyeMap* ptr)
  177. {
  178. /* 先查找有没有重复的指针 */
  179. for(auto it = listEyeMapPtr.begin(); it != listEyeMapPtr.end(); it++)
  180. {
  181. if(*it == ptr)
  182. {
  183. return;
  184. }
  185. }
  186. listEyeMapPtr.append(ptr);
  187. }
  188. /* 获取每个眼图的序号、标题和颜色 */
  189. QList<OneEyeMapInfo> AllEyeMapInfo::getEyeMapInfo()
  190. {
  191. QList<OneEyeMapInfo> list;
  192. for(const auto& it : listEyeMapPtr)
  193. {
  194. OneEyeMapInfo info;
  195. info.isShow = it->getShow();
  196. info.num = it->getNum();
  197. info.title = it->getTitle();
  198. info.titleBarColor = it->getTitleBarColor();
  199. info.channelInfo = it->getChannelInfo();
  200. info.voltageRange = it->getVoltageRange();
  201. info.tGridValue = it->getTimeGridValue();
  202. list.append(info);
  203. }
  204. /* 打印每个项的信息 */
  205. // for(const auto& it : list)
  206. // {
  207. // SPDLOG_DEBUG("序号: {}, 标题: {}, 通道号: {}", it.num, it.title.toStdString(), it.channelInfo.channelName.toStdString());
  208. // }
  209. return list;
  210. }
  211. /* 根据序号查找指针 */
  212. OneEyeMap* AllEyeMapInfo::findEyeMap(int num)
  213. {
  214. for(const auto& it : listEyeMapPtr)
  215. {
  216. if(it->getNum() == num)
  217. {
  218. return it;
  219. }
  220. }
  221. return nullptr;
  222. }
  223. /* 更新模块信息 */
  224. void AllEyeMapInfo::updateModuleInfo(OneEyeMapInfo& info)
  225. {
  226. for(auto& it : listEyeMapPtr)
  227. {
  228. if(it->getNum() == info.num)
  229. {
  230. it->updateInfo(info);
  231. break;
  232. }
  233. }
  234. }
  235. /* 更新设置组屏页面的信息 */
  236. void AllEyeMapInfo::updateSettingNum(OneEyeMapInfo& info)
  237. {
  238. /* 根据通道号设置眼图模块编号 */
  239. for(auto& it : listEyeMapPtr)
  240. {
  241. /* 根据通道号设置颜色标题等信息,不用重新获取眼图对应的示波器实例了 */
  242. if(it->getChannelInfo().channel == info.channelInfo.channel)
  243. {
  244. SPDLOG_DEBUG("更新设置组屏页面的信息: {}, {}", info.num, info.channelInfo.channelName.toStdString());
  245. it->updateSettingNum(info);
  246. break;
  247. }
  248. }
  249. }
  250. /* 清空所有的编号 */
  251. void AllEyeMapInfo::clearAllNum()
  252. {
  253. for(auto& it : listEyeMapPtr)
  254. {
  255. it->setNum(0);
  256. }
  257. }
  258. /* 重置所有的颜色矩阵 */
  259. void AllEyeMapInfo::resetAllEyeMap()
  260. {
  261. for(auto& it : listEyeMapPtr)
  262. {
  263. it->resetEyeMap();
  264. }
  265. }
  266. /* 获取所有的通道信息,每次都是获取最新的状态 */
  267. QList<OneChannelInfo> AllEyeMapInfo::getChannelInfo()
  268. {
  269. QList<OneChannelInfo> list;
  270. for(auto& it : listEyeMapPtr)
  271. {
  272. list.append(it->getChannelInfo());
  273. }
  274. return list;
  275. }
  276. /* 设置通道号对应的示波器是否连接成功 */
  277. // void AllEyeMapInfo::setChannelConnected(OscChnNum channel, bool isConnected)
  278. // {
  279. // }
  280. /* 更新显示的电压刻度信息,这里只更新电压相关的参数 */
  281. void AllEyeMapInfo::updateVoltageAndTimeGardInfo(const OneEyeMapInfo& info)
  282. {
  283. for(auto& it : listEyeMapPtr)
  284. {
  285. if(it->getChannelInfo().channel == info.channelInfo.channel)
  286. {
  287. it->setVoltageRange(info.voltageRange);
  288. it->setTimeGridValue(info.tGridValue);
  289. break;
  290. }
  291. }
  292. }
  293. /* 更新初始化数组 */
  294. void AllEyeMapInfo::updateInitEyeMapInfo()
  295. {
  296. listInitEyeMapInfo.clear();
  297. for (const auto& it : listEyeMapPtr)
  298. {
  299. OneEyeMapInfo info;
  300. // info.uid = it->getUID();
  301. info.num = it->getNum();
  302. info.title = it->getTitle();
  303. info.titleBarColor = it->getTitleBarColor();
  304. info.isShow = it->getShow();
  305. info.channelInfo = it->getChannelInfo();
  306. info.voltageRange = it->getVoltageRange();
  307. info.tGridValue = it->getTimeGridValue();
  308. listInitEyeMapInfo.append(info);
  309. }
  310. }
  311. /* 获取通道号对应的通道名称 */
  312. QString AllEyeMapInfo::getChannelName(OscChnNum channel, bool isConnected)
  313. {
  314. if(isConnected)
  315. {
  316. switch (channel)
  317. {
  318. case OscChnNum::Osc_None:
  319. return QString("请选择通道");
  320. case OscChnNum::Osc1_CHA:
  321. return QString("示波器1通道A");
  322. case OscChnNum::Osc1_CHB:
  323. return QString("示波器1通道B");
  324. case OscChnNum::Osc2_CHA:
  325. return QString("示波器2通道A");
  326. case OscChnNum::Osc2_CHB:
  327. return QString("示波器2通道B");
  328. case OscChnNum::Osc3_CHA:
  329. return QString("示波器3通道A");
  330. case OscChnNum::Osc3_CHB:
  331. return QString("示波器3通道B");
  332. case OscChnNum::Osc4_CHA:
  333. return QString("示波器4通道A");
  334. case OscChnNum::Osc4_CHB:
  335. return QString("示波器4通道B");
  336. default:
  337. return QString("请选择通道");
  338. }
  339. }
  340. else {
  341. switch (channel)
  342. {
  343. case OscChnNum::Osc_None:
  344. return QString("请选择通道");
  345. case OscChnNum::Osc1_CHA:
  346. return QString("示波器1通道A (未连接)");
  347. case OscChnNum::Osc1_CHB:
  348. return QString("示波器1通道B (未连接)");
  349. case OscChnNum::Osc2_CHA:
  350. return QString("示波器2通道A (未连接)");
  351. case OscChnNum::Osc2_CHB:
  352. return QString("示波器2通道B (未连接)");
  353. case OscChnNum::Osc3_CHA:
  354. return QString("示波器3通道A (未连接)");
  355. case OscChnNum::Osc3_CHB:
  356. return QString("示波器3通道B (未连接)");
  357. case OscChnNum::Osc4_CHA:
  358. return QString("示波器4通道A (未连接)");
  359. case OscChnNum::Osc4_CHB:
  360. return QString("示波器4通道B (未连接)");
  361. default:
  362. return QString("请选择通道");
  363. }
  364. }
  365. }
  366. /* 判断一个示波器是否有显示的眼图,有一个就返回true */
  367. bool AllEyeMapInfo::hasShowEyeMap(int oscNum)
  368. {
  369. for(const auto& it : listEyeMapPtr)
  370. {
  371. if(it->getChannelInfo().channel == static_cast<OscChnNum>(oscNum) && it->getShow())
  372. {
  373. return true;
  374. }
  375. }
  376. return false;
  377. }
  378. /* 根据示波器编号设置眼图状态 */
  379. void AllEyeMapInfo::setEyeMapOscOpen(int oscNum, bool isOpen)
  380. {
  381. OscChnNum oscChn1 = static_cast<OscChnNum>(oscNum * 2 - 1);
  382. OscChnNum oscChn2 = static_cast<OscChnNum>(oscNum * 2);
  383. for(const auto& it : listEyeMapPtr)
  384. {
  385. if(it->getChannelInfo().channel == oscChn1 || it->getChannelInfo().channel == oscChn2)
  386. {
  387. it->setOpen(isOpen);
  388. }
  389. }
  390. }
  391. /* 获取一项初始化数据,通道号和通道名设置为none */
  392. OneEyeMapInfo AllEyeMapInfo::getOneEyeMapInfo(int num)
  393. {
  394. OneEyeMapInfo info;
  395. // info.uid = static_cast<EyeMapUID>(num);
  396. info.num = num;
  397. // info.title = "通道" + QString::number(num);
  398. info.titleBarColor = QColor("#2D2D31");
  399. info.isShow = false;
  400. info.channelInfo.channel = OscChnNum::Osc_None;
  401. info.channelInfo.channelName = getChannelName(info.channelInfo.channel, false);
  402. info.title = info.channelInfo.channelName;
  403. info.voltageRange = OscVoltageRange::CR_2V5;
  404. info.tGridValue = OscTimeGridValue::TGV_200NS;
  405. return info;
  406. }
  407. OneEyeMapInfo AllEyeMapInfo::getInitEyeMapInfo(int num)
  408. {
  409. OneEyeMapInfo info;
  410. // info.uid = static_cast<EyeMapUID>(num);
  411. info.num = num;
  412. // info.title = "通道" + QString::number(num);
  413. info.titleBarColor = QColor("#2D2D31");
  414. info.isShow = false;
  415. info.channelInfo.channel = static_cast<OscChnNum>(num);
  416. info.channelInfo.channelName = getChannelName(info.channelInfo.channel, true);
  417. info.title = info.channelInfo.channelName;
  418. info.voltageRange = OscVoltageRange::CR_2V5;
  419. info.tGridValue = OscTimeGridValue::TGV_200NS;
  420. return info;
  421. }
  422. /* 获取未使用的通道号 */
  423. OscChnNum AllEyeMapInfo::getUnusedChannel()
  424. {
  425. for(int i = 1; i <= 8; i++)
  426. {
  427. bool isUsed = false;
  428. for(const auto& it : listEyeMapPtr)
  429. {
  430. if(it->getChannelInfo().channel == static_cast<OscChnNum>(i))
  431. {
  432. isUsed = true;
  433. break;
  434. }
  435. }
  436. if(!isUsed)
  437. {
  438. return static_cast<OscChnNum>(i);
  439. }
  440. }
  441. return OscChnNum::Osc_None;
  442. }