EyeMapInfo.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 = i;
  35. info.channelName = "通道" + QString::number(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.num = it["num"];
  60. info.title = QString::fromStdString(it["title"].get<std::string>());
  61. info.titleBarColor = QColor(QString::fromStdString(it["titleBarColor"].get<std::string>()));
  62. info.isShow = it["isShow"];
  63. info.channelInfo.channel = it["OscChannelNum"].get<int>();
  64. info.channelInfo.channelName = QString::fromStdString(it["OscChannelName"]);
  65. info.voltageRange = static_cast<OscVoltageRange>(it["voltageRange"]);
  66. info.tGridValue = static_cast<OscTimeGridValue>(it["timeGridValue"]);
  67. listInitEyeMapInfo.append(info);
  68. }
  69. nJson jsonBase = json["BaseInfo"];
  70. row = jsonBase["row"];
  71. column = jsonBase["column"];
  72. }
  73. catch (const nJson::parse_error& e) {
  74. SPDLOG_ERROR("解析json文件失败, 错误信息:{}, 文件路径: {}", e.what(), filePath.toStdString());
  75. createSaveFile(m_saveFileName);
  76. }
  77. catch (const nJson::exception& e) {
  78. SPDLOG_ERROR("解析json文件失败, 错误信息:{}, 文件路径: {}", e.what(), filePath.toStdString());
  79. createSaveFile(m_saveFileName);
  80. }
  81. }
  82. /* 写入保存的文件 */
  83. void AllEyeMapInfo::createSaveFile(const QString& fileName)
  84. {
  85. QString filePath = m_saveFilePath + fileName;
  86. QFile file(filePath);
  87. if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
  88. {
  89. SPDLOG_ERROR("打开文件失败,文件路径: {}", filePath.toStdString());
  90. return;
  91. }
  92. SPDLOG_INFO("创建数据文件成功,文件路径: {}", filePath.toStdString());
  93. /* 设置初始化的数据 */
  94. listInitEyeMapInfo.clear();
  95. for(int i = 0; i < 8; i++)
  96. {
  97. OneEyeMapInfo info;
  98. info.num = i + 1;
  99. info.title = "通道" + QString::number(i + 1);
  100. info.titleBarColor = QColor("#2D2D31");
  101. info.isShow = false;
  102. info.channelInfo.channel = 0;
  103. info.channelInfo.channelName = getChannelName(0);
  104. info.voltageRange = OscVoltageRange::CR_2V5;
  105. info.tGridValue = OscTimeGridValue::TGV_200NS;
  106. listInitEyeMapInfo.append(info);
  107. }
  108. row = 4;
  109. column = 2;
  110. nJson json0;
  111. nJson jsonArray1 = json0.array();
  112. for(const auto& it : listInitEyeMapInfo)
  113. {
  114. nJson json2;
  115. json2["num"] = it.num;
  116. json2["title"] = it.title.toStdString();
  117. json2["titleBarColor"] = it.titleBarColor.name().toStdString();
  118. json2["isShow"] = it.isShow;
  119. json2["OscChannelNum"] = it.channelInfo.channel;
  120. json2["OscChannelName"] = it.channelInfo.channelName.toStdString();
  121. json2["voltageRange"] = static_cast<int>(it.voltageRange);
  122. json2["timeGridValue"] = static_cast<int>(it.tGridValue);
  123. jsonArray1.push_back(json2);
  124. }
  125. json0["EyeMapInfo"] = jsonArray1;
  126. /* 保存行和列 */
  127. nJson json1;
  128. json1["row"] = 4;
  129. json1["column"] = 2;
  130. json0["BaseInfo"] = json1;
  131. auto data = json0.dump(4);
  132. file.write(data.c_str());
  133. file.close();
  134. }
  135. /* 更新保存的文件 */
  136. void AllEyeMapInfo::updateSaveFile()
  137. {
  138. QString filePath = m_saveFilePath + m_saveFileName;
  139. QFile file(filePath);
  140. if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
  141. {
  142. SPDLOG_ERROR("打开文件失败, 文件路径: {}", filePath.toStdString());
  143. return;
  144. }
  145. nJson json0;
  146. nJson jsonArray1 = json0.array();
  147. for(const auto& it : listInitEyeMapInfo)
  148. {
  149. nJson json2;
  150. json2["num"] = it.num;
  151. json2["title"] = it.title.toStdString();
  152. json2["titleBarColor"] = it.titleBarColor.name().toStdString();
  153. json2["isShow"] = it.isShow;
  154. json2["OscChannelNum"] = it.channelInfo.channel;
  155. json2["OscChannelName"] = it.channelInfo.channelName.toStdString();
  156. json2["voltageRange"] = static_cast<int>(it.voltageRange);
  157. json2["timeGridValue"] = static_cast<int>(it.tGridValue);
  158. jsonArray1.push_back(json2);
  159. }
  160. json0["EyeMapInfo"] = jsonArray1;
  161. nJson json1;
  162. json1["row"] = row;
  163. json1["column"] = column;
  164. json0["BaseInfo"] = json1;
  165. auto data = json0.dump(4);
  166. file.write(data.c_str());
  167. file.close();
  168. }
  169. /* 获取眼图列表 */
  170. const QList<OneEyeMap*>& AllEyeMapInfo::getEyeMapList()
  171. {
  172. return listEyeMapPtr;
  173. }
  174. /* 添加眼图指针 */
  175. void AllEyeMapInfo::addEyeMapPtr(OneEyeMap* ptr)
  176. {
  177. /* 先查找有没有重复的指针 */
  178. for(auto it = listEyeMapPtr.begin(); it != listEyeMapPtr.end(); it++)
  179. {
  180. if(*it == ptr)
  181. {
  182. return;
  183. }
  184. }
  185. listEyeMapPtr.append(ptr);
  186. }
  187. /* 获取每个眼图的序号、标题和颜色 */
  188. QList<OneEyeMapInfo> AllEyeMapInfo::getEyeMapInfo()
  189. {
  190. QList<OneEyeMapInfo> list;
  191. for(const auto& it : listEyeMapPtr)
  192. {
  193. OneEyeMapInfo info;
  194. info.isShow = it->getShow();
  195. info.num = it->getNum();
  196. info.title = it->getTitle();
  197. info.titleBarColor = it->getTitleBarColor();
  198. info.channelInfo = it->getChannelInfo();
  199. info.voltageRange = it->getVoltageRange();
  200. info.tGridValue = it->getTimeGridValue();
  201. list.append(info);
  202. }
  203. return list;
  204. }
  205. /* 根据序号查找指针 */
  206. OneEyeMap* AllEyeMapInfo::findEyeMap(int num)
  207. {
  208. for(const auto& it : listEyeMapPtr)
  209. {
  210. if(it->getNum() == num)
  211. {
  212. return it;
  213. }
  214. }
  215. return nullptr;
  216. }
  217. /* 更新模块信息 */
  218. void AllEyeMapInfo::updateModuleInfo(OneEyeMapInfo& info)
  219. {
  220. for(auto& it : listEyeMapPtr)
  221. {
  222. if(it->getNum() == info.num)
  223. {
  224. it->updateInfo(info);
  225. break;
  226. }
  227. }
  228. }
  229. /* 更新设置组屏页面的信息 */
  230. void AllEyeMapInfo::updateSettingNum(OneEyeMapInfo& info)
  231. {
  232. for(auto& it : listEyeMapPtr)
  233. {
  234. if(it->getNum() == info.num)
  235. {
  236. it->updateSettingNum(info);
  237. break;
  238. }
  239. }
  240. }
  241. /* 更新显示的电压刻度信息,这里只更新电压相关的参数 */
  242. void AllEyeMapInfo::updateVoltageAndTimeGardInfo(const OneEyeMapInfo& info)
  243. {
  244. for(auto& it : listEyeMapPtr)
  245. {
  246. if(it->getNum() == info.num)
  247. {
  248. it->setVoltageRange(info.voltageRange);
  249. it->setTimeGridValue(info.tGridValue);
  250. break;
  251. }
  252. }
  253. }
  254. /* 更新初始化数组 */
  255. void AllEyeMapInfo::updateInitEyeMapInfo()
  256. {
  257. listInitEyeMapInfo.clear();
  258. for (const auto& it : listEyeMapPtr)
  259. {
  260. OneEyeMapInfo info;
  261. info.num = it->getNum();
  262. info.title = it->getTitle();
  263. info.titleBarColor = it->getTitleBarColor();
  264. info.isShow = it->getShow();
  265. info.channelInfo = it->getChannelInfo();
  266. info.voltageRange = it->getVoltageRange();
  267. info.tGridValue = it->getTimeGridValue();
  268. listInitEyeMapInfo.append(info);
  269. }
  270. }
  271. /* 获取通道号对应的通道名称 */
  272. QString AllEyeMapInfo::getChannelName(int channel)
  273. {
  274. switch (channel)
  275. {
  276. case 0:
  277. return QString("请选择通道");
  278. case 1:
  279. return QString("通道1");
  280. case 2:
  281. return QString("通道2");
  282. case 3:
  283. return QString("通道3");
  284. case 4:
  285. return QString("通道4");
  286. case 5:
  287. return QString("通道5");
  288. case 6:
  289. return QString("通道6");
  290. case 7:
  291. return QString("通道7");
  292. case 8:
  293. return QString("通道8");
  294. default:
  295. return QString("请选择通道");
  296. }
  297. }