EyeMapInfo.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. void AllEyeMapInfo::updateVoltageAndTimeGardInfo(const OneEyeMapInfo& info)
  268. {
  269. for(auto& it : listEyeMapPtr)
  270. {
  271. if(it->getChannelInfo().channel == info.channelInfo.channel)
  272. {
  273. it->setVoltageRange(info.voltageRange);
  274. it->setTimeGridValue(info.tGridValue);
  275. break;
  276. }
  277. }
  278. }
  279. /* 更新初始化数组 */
  280. void AllEyeMapInfo::updateInitEyeMapInfo()
  281. {
  282. listInitEyeMapInfo.clear();
  283. for (const auto& it : listEyeMapPtr)
  284. {
  285. OneEyeMapInfo info;
  286. // info.uid = it->getUID();
  287. info.num = it->getNum();
  288. info.title = it->getTitle();
  289. info.titleBarColor = it->getTitleBarColor();
  290. info.isShow = it->getShow();
  291. info.channelInfo = it->getChannelInfo();
  292. info.voltageRange = it->getVoltageRange();
  293. info.tGridValue = it->getTimeGridValue();
  294. listInitEyeMapInfo.append(info);
  295. }
  296. }
  297. /* 获取通道号对应的通道名称 */
  298. QString AllEyeMapInfo::getChannelName(OscChnNum channel)
  299. {
  300. switch (channel)
  301. {
  302. case OscChnNum::Osc_None:
  303. return QString("请选择通道");
  304. case OscChnNum::Osc1_CHA:
  305. return QString("示波器1通道A");
  306. case OscChnNum::Osc1_CHB:
  307. return QString("示波器1通道B");
  308. case OscChnNum::Osc2_CHA:
  309. return QString("示波器2通道A");
  310. case OscChnNum::Osc2_CHB:
  311. return QString("示波器2通道B");
  312. case OscChnNum::Osc3_CHA:
  313. return QString("示波器3通道A");
  314. case OscChnNum::Osc3_CHB:
  315. return QString("示波器3通道B");
  316. case OscChnNum::Osc4_CHA:
  317. return QString("示波器4通道A");
  318. case OscChnNum::Osc4_CHB:
  319. return QString("示波器4通道B");
  320. default:
  321. return QString("请选择通道");
  322. }
  323. }
  324. /* 判断一个示波器是否有显示的眼图,有一个就返回true */
  325. bool AllEyeMapInfo::hasShowEyeMap(int oscNum)
  326. {
  327. for(const auto& it : listEyeMapPtr)
  328. {
  329. if(it->getChannelInfo().channel == static_cast<OscChnNum>(oscNum) && it->getShow())
  330. {
  331. return true;
  332. }
  333. }
  334. return false;
  335. }
  336. /* 根据示波器编号设置眼图状态 */
  337. void AllEyeMapInfo::setEyeMapOscOpen(int oscNum, bool isOpen)
  338. {
  339. OscChnNum oscChn1 = static_cast<OscChnNum>(oscNum * 2 - 1);
  340. OscChnNum oscChn2 = static_cast<OscChnNum>(oscNum * 2);
  341. for(const auto& it : listEyeMapPtr)
  342. {
  343. if(it->getChannelInfo().channel == oscChn1 || it->getChannelInfo().channel == oscChn2)
  344. {
  345. it->setOpen(isOpen);
  346. }
  347. }
  348. }
  349. /* 获取一项初始化数据,通道号和通道名设置为none */
  350. OneEyeMapInfo AllEyeMapInfo::getOneEyeMapInfo(int num)
  351. {
  352. OneEyeMapInfo info;
  353. // info.uid = static_cast<EyeMapUID>(num);
  354. info.num = num;
  355. // info.title = "通道" + QString::number(num);
  356. info.titleBarColor = QColor("#2D2D31");
  357. info.isShow = false;
  358. info.channelInfo.channel = OscChnNum::Osc_None;
  359. info.channelInfo.channelName = getChannelName(info.channelInfo.channel);
  360. info.title = info.channelInfo.channelName;
  361. info.voltageRange = OscVoltageRange::CR_2V5;
  362. info.tGridValue = OscTimeGridValue::TGV_200NS;
  363. return info;
  364. }
  365. OneEyeMapInfo AllEyeMapInfo::getInitEyeMapInfo(int num)
  366. {
  367. OneEyeMapInfo info;
  368. // info.uid = static_cast<EyeMapUID>(num);
  369. info.num = num;
  370. // info.title = "通道" + QString::number(num);
  371. info.titleBarColor = QColor("#2D2D31");
  372. info.isShow = false;
  373. info.channelInfo.channel = static_cast<OscChnNum>(num);
  374. info.channelInfo.channelName = getChannelName(info.channelInfo.channel);
  375. info.title = info.channelInfo.channelName;
  376. info.voltageRange = OscVoltageRange::CR_2V5;
  377. info.tGridValue = OscTimeGridValue::TGV_200NS;
  378. return info;
  379. }
  380. /* 获取未使用的通道号 */
  381. OscChnNum AllEyeMapInfo::getUnusedChannel()
  382. {
  383. for(int i = 1; i <= 8; i++)
  384. {
  385. bool isUsed = false;
  386. for(const auto& it : listEyeMapPtr)
  387. {
  388. if(it->getChannelInfo().channel == static_cast<OscChnNum>(i))
  389. {
  390. isUsed = true;
  391. break;
  392. }
  393. }
  394. if(!isUsed)
  395. {
  396. return static_cast<OscChnNum>(i);
  397. }
  398. }
  399. return OscChnNum::Osc_None;
  400. }