basicwidget.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. #include "basicwidget.h"
  2. #include "ui_basicwidget.h"
  3. #include "compareitemlistdialog.h"
  4. #include "DialogBase.h"
  5. #include "UIStyleManager.h"
  6. #include "customcombobox.h"
  7. #include "CompareItemData.h"
  8. #include "tipwidget.h"
  9. #include "SystemConfigStruct.h"
  10. #include "SystemConfig.h"
  11. #include "GlobalInfo.h"
  12. #include "SoundCardData.h"
  13. #include <string>
  14. BasicWidget::BasicWidget(QWidget *parent) :
  15. QWidget(parent),
  16. ui(new Ui::BasicWidget)
  17. {
  18. ui->setupUi(this);
  19. m_logger = spdlog::get("ACASetting");
  20. if(m_logger == nullptr)
  21. {
  22. fmt::print("BasicWidget: Logger ACASetting not found\n");
  23. return;
  24. }
  25. connect(ui->pBtn_compareItem, &QPushButton::clicked, this, &BasicWidget::do_pBtn_CompareItemClicked);
  26. /* 设置UI */
  27. UIStyle.registerWidget(this);
  28. /* 设置下拉框选项 */
  29. ui->comBox_notConsistency->setViewShadowEffect();
  30. ui->comBox_driverName->setViewShadowEffect();
  31. ui->comBox_recordMode->setViewShadowEffect();
  32. /* 设置IP地址格式限制 */
  33. QRegularExpression ipRegex(R"(^(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)){3}$)");
  34. QValidator *ipValidator = new QRegularExpressionValidator(ipRegex, this);
  35. ui->lineEdit_serverIP->setValidator(ipValidator);
  36. /* 初始化数据 */
  37. /* 设置录音模式 */
  38. for(auto it = g_mapRecordModes.begin(); it != g_mapRecordModes.end(); ++it)
  39. {
  40. ui->comBox_recordMode->addItem(it.value(), it.key());
  41. }
  42. /* 设置不一致判断选项 */
  43. for(auto it = g_mapNotConsistency.begin(); it != g_mapNotConsistency.end(); ++it)
  44. {
  45. ui->comBox_notConsistency->addItem(it.value(), it.key());
  46. }
  47. /* 设置从数据库读取到的选项 */
  48. const BaseConfig_t& baseConfig = SysConfig.getBaseConfigSrc();
  49. ui->lineEdit_serverIP->setText(baseConfig.strServerIP);
  50. ui->comBox_recordMode->setCurrentIndex(ui->comBox_recordMode->findData(baseConfig.nRecordMode));
  51. ui->comBox_driverName->setCurrentText(baseConfig.strDriverName);
  52. ui->comBox_notConsistency->setCurrentIndex(ui->comBox_notConsistency->findData(baseConfig.nNotConsistency));
  53. ui->checkBox_enableMultiCPU->setChecked(baseConfig.isEnableMultiCore);
  54. ui->checkBox_enableDebug->setChecked(baseConfig.isEnableDebugLog);
  55. ui->checkBox_clearHistoryDir->setChecked(baseConfig.isClearDirSystemOn);
  56. ui->checkBox_enableSoundCardName->setChecked(baseConfig.isUsingSoundCardName);
  57. /* 连接信号和槽 */
  58. connect(ui->comBox_driverName, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &BasicWidget::do_soundCardChanged);
  59. /* 设置声卡信息 */
  60. setSoundCardInfo();
  61. }
  62. BasicWidget::~BasicWidget()
  63. {
  64. UIStyle.unregisterWidget(this);
  65. delete ui;
  66. }
  67. /* 保存数据 */
  68. bool BasicWidget::saveBasicInfo()
  69. {
  70. bool isSuccess = true;
  71. /* 保存基础信息 */
  72. if(!saveBasicSettingInfo())
  73. {
  74. TipWidget::display(TipWidget::OPERATOR_WARN, "保存基础信息失败", GInfo.getTopWindow());
  75. isSuccess = false;
  76. }
  77. /* 保存对比项信息 */
  78. if(!saveCompareItemInfo())
  79. {
  80. TipWidget::display(TipWidget::OPERATOR_WARN, "保存对比项信息失败", GInfo.getTopWindow());
  81. isSuccess = false;
  82. }
  83. m_isModify = false; // 保存成功后,重置修改标志
  84. return isSuccess;
  85. }
  86. void BasicWidget::do_pBtn_CompareItemClicked()
  87. {
  88. CompareItemListDialog dlg;
  89. /* 设置已有的对比项列表 */
  90. dlg.setCompareItemList(CIData.getCompareItemTableData());
  91. dlg.exec();
  92. }
  93. /* 修改了选择了声卡 */
  94. void BasicWidget::do_soundCardChanged(int nIndex)
  95. {
  96. int nSoundCardNum = ui->comBox_driverName->currentData().toInt();
  97. if(nSoundCardNum < 0)
  98. {
  99. SPDLOG_LOGGER_ERROR(m_logger, "选择的声卡无效,无法设置通道信息");
  100. return;
  101. }
  102. /* 设置当前声卡信息 */
  103. if(!SoundCards.setCurrentSoundCard(nSoundCardNum))
  104. {
  105. SPDLOG_LOGGER_ERROR(m_logger, "设置当前声卡失败,声卡编号: {}", nSoundCardNum);
  106. return;
  107. }
  108. }
  109. /* 设置声卡信息 */
  110. void BasicWidget::setSoundCardInfo()
  111. {
  112. const auto& soundCardInfo = SoundCards.getSoundCardInfo();
  113. /* 清空现有的声卡信息 */
  114. ui->comBox_driverName->clear();
  115. /* 添加声卡信息到下拉框 */
  116. for(const auto& it : soundCardInfo)
  117. {
  118. ui->comBox_driverName->addItem(it.strSoundCardName, it.nSoundCardNum);
  119. }
  120. /* 设置当前信息 */
  121. ui->comBox_driverName->setCurrentIndex(0);
  122. }
  123. /* 保存基础信息 */
  124. bool BasicWidget::saveBasicSettingInfo()
  125. {
  126. BaseConfig_t baseConfig;
  127. const BaseConfig_t& srcBaseConfig = SysConfig.getBaseConfigSrc();
  128. /* 获取基础配置信息 */
  129. baseConfig.strServerIP = ui->lineEdit_serverIP->text();
  130. baseConfig.nRecordMode = ui->comBox_recordMode->currentData().toInt();
  131. baseConfig.strDriverName = ui->comBox_driverName->currentData().toString();
  132. baseConfig.nNotConsistency = ui->comBox_notConsistency->currentData().toInt();
  133. baseConfig.isEnableMultiCore = ui->checkBox_enableMultiCPU->isChecked();
  134. baseConfig.isEnableDebugLog = ui->checkBox_enableDebug->isChecked();
  135. baseConfig.isClearDirSystemOn = ui->checkBox_clearHistoryDir->isChecked();
  136. baseConfig.isUsingSoundCardName = ui->checkBox_enableSoundCardName->isChecked();
  137. // SPDLOG_LOGGER_DEBUG(m_logger, "修改后的基础配置信息: ServerIP: {}, RecordMode: {}, DriverName: {}, NotConsistency: {}, EnableMultiCore: {}, EnableDebugLog: {}, ClearHistoryDirOnStart: {}, UseSoundCardName: {}",
  138. // baseConfig.strServerIP.toStdString(), baseConfig.nRecordMode, baseConfig.strDriverName.toStdString(),
  139. // baseConfig.nNotConsistency, baseConfig.isEnableMultiCore, baseConfig.isEnableDebugLog,
  140. // baseConfig.isClearDirSystemOn, baseConfig.isUsingSoundCardName);
  141. // SPDLOG_LOGGER_DEBUG(m_logger, "现有的基础配置信息: ServerIP: {}, RecordMode: {}, DriverName: {}, NotConsistency: {}, EnableMultiCore: {}, EnableDebugLog: {}, ClearHistoryDirOnStart: {}, UseSoundCardName: {}",
  142. // srcBaseConfig.strServerIP.toStdString(), srcBaseConfig.nRecordMode, srcBaseConfig.strDriverName.toStdString(),
  143. // srcBaseConfig.nNotConsistency, srcBaseConfig.isEnableMultiCore, srcBaseConfig.isEnableDebugLog,
  144. // srcBaseConfig.isClearDirSystemOn, srcBaseConfig.isUsingSoundCardName);
  145. if(baseConfig == srcBaseConfig)
  146. {
  147. SPDLOG_LOGGER_DEBUG(m_logger, "基础配置信息没有修改,不需要保存");
  148. return true;
  149. }
  150. /* 将结构体转换成json字符串 */
  151. std::string strJson;
  152. SysConfig.setBaseConfigToJson(baseConfig, strJson);
  153. if(!m_fromWebAPI->updateSystemConfig(Config_Base, strJson, SysConfig.mapSysConfigDesc[eSystemConfigType::eSCT_BaseConfig]))
  154. {
  155. SPDLOG_LOGGER_ERROR(m_logger, "更新基础配置信息失败");
  156. return false;
  157. }
  158. SysConfig.setBaseConfig(baseConfig); // 更新本地的基础配置信息
  159. return true;
  160. }
  161. /* 保存对比项信息
  162. * 先获取未修改过的对比项信息,和现有的进行对比一下,找出新增、修改、删除的对比项。
  163. * 新增的对比项直接添加到数据库中,修改的对比项更新到数据库中,删除的对比项从数据库中删除。
  164. */
  165. bool BasicWidget::saveCompareItemInfo()
  166. {
  167. if(m_fromWebAPI == nullptr)
  168. {
  169. SPDLOG_LOGGER_ERROR(m_logger, "WebAPI 未设置指针,无法保存对比项信息");
  170. return false;
  171. }
  172. /* 获取未修改过的对比项信息,不从数据库里获取了 */
  173. const QList<CompareItemInfo_t>& srcCompareItems = CIData.getSrcCompareItemList();
  174. const QList<CompareItemInfo_t>& nowItems = CIData.getCompareItemList();
  175. QList<CompareItemInfo_t> insertItems; // 新增的对比项
  176. QList<int> deleteIDs; // 删除的对比项ID
  177. QList<CompareItemInfo_t> baseUpdateItems; // 修改的对比项基础信息
  178. QList<CompareItemInfo_t> roadUpdateItems; // 修改的对比项通道信息
  179. /* 先找到新增的对比项 */
  180. findInsertCompareItem(srcCompareItems, nowItems, insertItems);
  181. /* 找到删除的对比项 */
  182. findDeleteCompareItem(srcCompareItems, nowItems, deleteIDs);
  183. /* 找到修改的对比项 */
  184. findUpdateCompareItem(srcCompareItems, nowItems, baseUpdateItems, roadUpdateItems);
  185. /* 先删除已经消失的对比项 */
  186. if(!deleteIDs.isEmpty())
  187. {
  188. if(!m_fromWebAPI->deleteCompareItem(deleteIDs))
  189. {
  190. SPDLOG_LOGGER_ERROR(m_logger, "删除对比项信息失败");
  191. return false;
  192. }
  193. }
  194. /* 再插入新增的对比项 */
  195. if(!insertItems.isEmpty())
  196. {
  197. if(!m_fromWebAPI->insertCompareItem(insertItems))
  198. {
  199. SPDLOG_LOGGER_ERROR(m_logger, "插入对比项信息失败");
  200. return false;
  201. }
  202. }
  203. /* 更新修改的对比项基础信息 */
  204. if(!baseUpdateItems.isEmpty())
  205. {
  206. if(!m_fromWebAPI->updateCompareItemOnly(baseUpdateItems))
  207. {
  208. SPDLOG_LOGGER_ERROR(m_logger, "更新对比项基础信息失败");
  209. return false;
  210. }
  211. }
  212. /* 更新修改的对比项通道信息 */
  213. if(!roadUpdateItems.isEmpty())
  214. {
  215. if(!m_fromWebAPI->updateCompareItemRoadOnly(roadUpdateItems))
  216. {
  217. SPDLOG_LOGGER_ERROR(m_logger, "更新对比项通道信息失败");
  218. return false;
  219. }
  220. }
  221. /* 更新数据库的信息 */
  222. QList<CompareItemInfo_t> listNewItems;
  223. if(!m_fromWebAPI->getCompareItemInfo(listNewItems))
  224. {
  225. SPDLOG_LOGGER_ERROR(m_logger, "获取最新对比项信息失败");
  226. return false;
  227. }
  228. /* 更新本地的对比项数据 */
  229. CIData.setCompareItemList(listNewItems);
  230. return true;
  231. }
  232. /* 查找新增的对比项信息 */
  233. void BasicWidget::findInsertCompareItem(const QList<CompareItemInfo_t>& srcItems, const QList<CompareItemInfo_t>& nowItems, QList<CompareItemInfo_t>& insertItems)
  234. {
  235. for(const CompareItemInfo_t& nowItem : nowItems)
  236. {
  237. bool bFound = false;
  238. for(const CompareItemInfo_t& srcItem : srcItems)
  239. {
  240. if(nowItem.nID == srcItem.nID)
  241. {
  242. bFound = true;
  243. break;
  244. }
  245. }
  246. if(!bFound)
  247. {
  248. insertItems.append(nowItem);
  249. }
  250. }
  251. }
  252. /* 查找删除的对比项信息 */
  253. void BasicWidget::findDeleteCompareItem(const QList<CompareItemInfo_t>& srcItems, const QList<CompareItemInfo_t>& nowItems, QList<int>& deleteIDs)
  254. {
  255. for(const CompareItemInfo_t& srcItem : srcItems)
  256. {
  257. bool bFound = false;
  258. for(const CompareItemInfo_t& nowItem : nowItems)
  259. {
  260. if(srcItem.nID == nowItem.nID)
  261. {
  262. bFound = true;
  263. break;
  264. }
  265. }
  266. if(!bFound)
  267. {
  268. deleteIDs.append(srcItem.nID);
  269. }
  270. }
  271. }
  272. /* 查找修改的对比项 */
  273. void BasicWidget::findUpdateCompareItem(const QList<CompareItemInfo_t>& srcItems, const QList<CompareItemInfo_t>& nowItems,
  274. QList<CompareItemInfo_t>& baseUpdateItems, QList<CompareItemInfo_t>& roadUpdateItems)
  275. {
  276. for(const CompareItemInfo_t& nowItem : nowItems)
  277. {
  278. bool bFound = false;
  279. for(const CompareItemInfo_t& srcItem : srcItems)
  280. {
  281. if(nowItem.nID == srcItem.nID)
  282. {
  283. bFound = true;
  284. if(!nowItem.isEqualBase(srcItem))
  285. {
  286. baseUpdateItems.append(nowItem);
  287. }
  288. /* 对比通道信息 */
  289. if(!nowItem.isEqualRoads(srcItem))
  290. {
  291. roadUpdateItems.append(nowItem);
  292. }
  293. break;
  294. }
  295. }
  296. if(!bFound)
  297. {
  298. // SPDLOG_LOGGER_DEBUG(m_logger, "对比项ID {} 在源数据中未找到,可能是新增的对比项", nowItem.nID);
  299. }
  300. }
  301. }