basicwidget.cpp 12 KB

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