EyeMapInfo.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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;
  104. info.uid = static_cast<EyeMapUID>(i + 1);
  105. info.num = i + 1;
  106. info.title = "通道" + QString::number(i + 1);
  107. info.titleBarColor = QColor("#2D2D31");
  108. info.isShow = false;
  109. info.channelInfo.channel = static_cast<OscChnNum>(i + 1);
  110. info.channelInfo.channelName = getChannelName(static_cast<OscChnNum>(i + 1));
  111. info.voltageRange = OscVoltageRange::CR_2V5;
  112. info.tGridValue = OscTimeGridValue::TGV_200NS;
  113. listInitEyeMapInfo.append(info);
  114. }
  115. row = 4;
  116. column = 2;
  117. nJson json0;
  118. nJson jsonArray1 = json0.array();
  119. for(const auto& it : listInitEyeMapInfo)
  120. {
  121. nJson json2;
  122. json2["uid"] = static_cast<int>(it.uid);
  123. json2["num"] = it.num;
  124. json2["title"] = it.title.toStdString();
  125. json2["titleBarColor"] = it.titleBarColor.name().toStdString();
  126. json2["isShow"] = it.isShow;
  127. json2["OscChannelNum"] = it.channelInfo.channel;
  128. json2["OscChannelName"] = it.channelInfo.channelName.toStdString();
  129. json2["voltageRange"] = static_cast<int>(it.voltageRange);
  130. json2["timeGridValue"] = static_cast<int>(it.tGridValue);
  131. jsonArray1.push_back(json2);
  132. }
  133. json0["EyeMapInfo"] = jsonArray1;
  134. /* 保存行和列 */
  135. nJson json1;
  136. json1["row"] = 4;
  137. json1["column"] = 2;
  138. json0["BaseInfo"] = json1;
  139. auto data = json0.dump(4);
  140. file.write(data.c_str());
  141. file.close();
  142. }
  143. /* 更新保存的文件 */
  144. void AllEyeMapInfo::updateSaveFile()
  145. {
  146. QString filePath = m_saveFilePath + m_saveFileName;
  147. QFile file(filePath);
  148. if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
  149. {
  150. SPDLOG_ERROR("打开文件失败, 文件路径: {}", filePath.toStdString());
  151. return;
  152. }
  153. nJson json0;
  154. nJson jsonArray1 = json0.array();
  155. for(const auto& it : listInitEyeMapInfo)
  156. {
  157. nJson json2;
  158. json2["uid"] = static_cast<int>(it.uid);
  159. json2["num"] = it.num;
  160. json2["title"] = it.title.toStdString();
  161. json2["titleBarColor"] = it.titleBarColor.name().toStdString();
  162. json2["isShow"] = it.isShow;
  163. json2["OscChannelNum"] = it.channelInfo.channel;
  164. json2["OscChannelName"] = it.channelInfo.channelName.toStdString();
  165. json2["voltageRange"] = static_cast<int>(it.voltageRange);
  166. json2["timeGridValue"] = static_cast<int>(it.tGridValue);
  167. jsonArray1.push_back(json2);
  168. }
  169. json0["EyeMapInfo"] = jsonArray1;
  170. nJson json1;
  171. json1["row"] = row;
  172. json1["column"] = column;
  173. json0["BaseInfo"] = json1;
  174. auto data = json0.dump(4);
  175. file.write(data.c_str());
  176. file.close();
  177. }
  178. /* 获取眼图列表 */
  179. const QList<OneEyeMap*>& AllEyeMapInfo::getEyeMapList()
  180. {
  181. return listEyeMapPtr;
  182. }
  183. /* 添加眼图指针 */
  184. void AllEyeMapInfo::addEyeMapPtr(OneEyeMap* ptr)
  185. {
  186. /* 先查找有没有重复的指针 */
  187. for(auto it = listEyeMapPtr.begin(); it != listEyeMapPtr.end(); it++)
  188. {
  189. if(*it == ptr)
  190. {
  191. return;
  192. }
  193. }
  194. listEyeMapPtr.append(ptr);
  195. }
  196. /* 获取每个眼图的序号、标题和颜色 */
  197. QList<OneEyeMapInfo> AllEyeMapInfo::getEyeMapInfo()
  198. {
  199. QList<OneEyeMapInfo> list;
  200. for(const auto& it : listEyeMapPtr)
  201. {
  202. OneEyeMapInfo info;
  203. info.isShow = it->getShow();
  204. info.num = it->getNum();
  205. info.title = it->getTitle();
  206. info.titleBarColor = it->getTitleBarColor();
  207. info.channelInfo = it->getChannelInfo();
  208. info.voltageRange = it->getVoltageRange();
  209. info.tGridValue = it->getTimeGridValue();
  210. list.append(info);
  211. }
  212. return list;
  213. }
  214. /* 根据序号查找指针 */
  215. OneEyeMap* AllEyeMapInfo::findEyeMap(int num)
  216. {
  217. for(const auto& it : listEyeMapPtr)
  218. {
  219. if(it->getNum() == num)
  220. {
  221. return it;
  222. }
  223. }
  224. return nullptr;
  225. }
  226. /* 更新模块信息 */
  227. void AllEyeMapInfo::updateModuleInfo(OneEyeMapInfo& info)
  228. {
  229. for(auto& it : listEyeMapPtr)
  230. {
  231. if(it->getNum() == info.num)
  232. {
  233. it->updateInfo(info);
  234. break;
  235. }
  236. }
  237. }
  238. /* 更新设置组屏页面的信息 */
  239. void AllEyeMapInfo::updateSettingNum(OneEyeMapInfo& info)
  240. {
  241. for(auto& it : listEyeMapPtr)
  242. {
  243. if(it->getNum() == info.num)
  244. {
  245. it->updateSettingNum(info);
  246. break;
  247. }
  248. }
  249. }
  250. /* 更新显示的电压刻度信息,这里只更新电压相关的参数 */
  251. void AllEyeMapInfo::updateVoltageAndTimeGardInfo(const OneEyeMapInfo& info)
  252. {
  253. for(auto& it : listEyeMapPtr)
  254. {
  255. if(it->getNum() == info.num)
  256. {
  257. it->setVoltageRange(info.voltageRange);
  258. it->setTimeGridValue(info.tGridValue);
  259. break;
  260. }
  261. }
  262. }
  263. /* 更新初始化数组 */
  264. void AllEyeMapInfo::updateInitEyeMapInfo()
  265. {
  266. listInitEyeMapInfo.clear();
  267. for (const auto& it : listEyeMapPtr)
  268. {
  269. OneEyeMapInfo info;
  270. info.num = it->getNum();
  271. info.title = it->getTitle();
  272. info.titleBarColor = it->getTitleBarColor();
  273. info.isShow = it->getShow();
  274. info.channelInfo = it->getChannelInfo();
  275. info.voltageRange = it->getVoltageRange();
  276. info.tGridValue = it->getTimeGridValue();
  277. listInitEyeMapInfo.append(info);
  278. }
  279. }
  280. /* 获取通道号对应的通道名称 */
  281. QString AllEyeMapInfo::getChannelName(OscChnNum channel)
  282. {
  283. switch (channel)
  284. {
  285. case OscChnNum::Osc_None:
  286. return QString("请选择通道");
  287. case OscChnNum::Osc1_CHA:
  288. return QString("示波器1通道A");
  289. case OscChnNum::Osc1_CHB:
  290. return QString("示波器1通道B");
  291. case OscChnNum::Osc2_CHA:
  292. return QString("示波器2通道A");
  293. case OscChnNum::Osc2_CHB:
  294. return QString("示波器2通道B");
  295. case OscChnNum::Osc3_CHA:
  296. return QString("示波器3通道A");
  297. case OscChnNum::Osc3_CHB:
  298. return QString("示波器3通道B");
  299. case OscChnNum::Osc4_CHA:
  300. return QString("示波器4通道A");
  301. case OscChnNum::Osc4_CHB:
  302. return QString("示波器4通道B");
  303. default:
  304. return QString("请选择通道");
  305. }
  306. }