basicwidget.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 <string>
  12. BasicWidget::BasicWidget(QWidget *parent) :
  13. QWidget(parent),
  14. ui(new Ui::BasicWidget)
  15. {
  16. ui->setupUi(this);
  17. m_logger = spdlog::get("ACASetting");
  18. if(m_logger == nullptr)
  19. {
  20. fmt::print("BasicWidget: Logger ACASetting not found\n");
  21. return;
  22. }
  23. connect(ui->pBtn_compareItem, &QPushButton::clicked, this, &BasicWidget::do_pBtn_CompareItemClicked);
  24. /* 设置UI */
  25. UIStyle.registerWidget(this);
  26. /* 设置下拉框选项 */
  27. ui->comBox_notConsistency->setViewShadowEffect();
  28. ui->comBox_driverName->setViewShadowEffect();
  29. ui->comBox_recordMode->setViewShadowEffect();
  30. /* 初始化数据 */
  31. /* 设置录音模式 */
  32. for(auto it = g_mapRecordModes.begin(); it != g_mapRecordModes.end(); ++it)
  33. {
  34. ui->comBox_recordMode->addItem(it.value(), it.key());
  35. }
  36. /* 设置不一致判断选项 */
  37. for(auto it = g_mapNotConsistency.begin(); it != g_mapNotConsistency.end(); ++it)
  38. {
  39. ui->comBox_notConsistency->addItem(it.value(), it.key());
  40. }
  41. /* 设置从数据库读取到的选项 */
  42. const BaseConfig_t& baseConfig = SysConfig.getBaseConfigSrc();
  43. ui->lineEdit_serverIP->setText(baseConfig.strServerIP);
  44. ui->comBox_recordMode->setCurrentIndex(ui->comBox_recordMode->findData(baseConfig.nRecordMode));
  45. ui->comBox_driverName->setCurrentText(baseConfig.strDriverName);
  46. ui->comBox_notConsistency->setCurrentIndex(ui->comBox_notConsistency->findData(baseConfig.nNotConsistency));
  47. ui->checkBox_enableMultiCPU->setChecked(baseConfig.isEnableMultiCore);
  48. ui->checkBox_enableDebug->setChecked(baseConfig.isEnableDebugLog);
  49. ui->checkBox_clearHistoryDir->setChecked(baseConfig.isClearDirSystemOn);
  50. ui->checkBox_enableSoundCardName->setChecked(baseConfig.isUsingSoundCardName);
  51. }
  52. BasicWidget::~BasicWidget()
  53. {
  54. UIStyle.unregisterWidget(this);
  55. delete ui;
  56. }
  57. /* 保存数据 */
  58. void BasicWidget::saveBasicInfo()
  59. {
  60. bool isSuccess = true;
  61. /* 保存基础信息 */
  62. if(!saveBasicSettingInfo())
  63. {
  64. TipWidget::display(TipWidget::OPERATOR_WARN, "保存基础信息失败", this);
  65. isSuccess = false;
  66. }
  67. /* 保存对比项信息 */
  68. if(!saveCompareItemInfo())
  69. {
  70. TipWidget::display(TipWidget::OPERATOR_WARN, "保存对比项信息失败", this);
  71. isSuccess = false;
  72. }
  73. if(isSuccess)
  74. {
  75. TipWidget::display(TipWidget::OPERATOR_OK, "保存设置成功", this);
  76. }
  77. m_isModify = false; // 保存成功后,重置修改标志
  78. }
  79. void BasicWidget::do_pBtn_CompareItemClicked()
  80. {
  81. CompareItemListDialog dlg;
  82. /* 设置已有的对比项列表 */
  83. dlg.setCompareItemList(CIData.getCompareItemTableData());
  84. dlg.exec();
  85. }
  86. /* 保存基础信息 */
  87. bool BasicWidget::saveBasicSettingInfo()
  88. {
  89. BaseConfig_t baseConfig;
  90. /* 获取基础配置信息 */
  91. baseConfig.strServerIP = ui->lineEdit_serverIP->text();
  92. baseConfig.nRecordMode = ui->comBox_recordMode->currentData().toInt();
  93. baseConfig.strDriverName = ui->comBox_driverName->currentData().toString();
  94. baseConfig.nNotConsistency = ui->comBox_notConsistency->currentData().toInt();
  95. baseConfig.isEnableMultiCore = ui->checkBox_enableMultiCPU->isChecked();
  96. baseConfig.isEnableDebugLog = ui->checkBox_enableDebug->isChecked();
  97. baseConfig.isClearDirSystemOn = ui->checkBox_clearHistoryDir;
  98. baseConfig.isUsingSoundCardName = ui->checkBox_enableSoundCardName;
  99. if(baseConfig == SysConfig.getBaseConfigSrc())
  100. {
  101. SPDLOG_LOGGER_DEBUG(m_logger, "基础配置信息没有修改,不需要保存");
  102. return true;
  103. }
  104. /* 将结构体转换成json字符串 */
  105. std::string strJson;
  106. SysConfig.setBaseConfigToJson(baseConfig, strJson);
  107. if(!m_fromWebAPI->updateSystemConfig(Config_Base, strJson, SysConfig.mapSysConfigDesc[eSystemConfigType::eSCT_BaseConfig]))
  108. {
  109. SPDLOG_LOGGER_ERROR(m_logger, "更新基础配置信息失败");
  110. return false;
  111. }
  112. SysConfig.setBaseConfig(baseConfig); // 更新本地的基础配置信息
  113. return true;
  114. }
  115. /* 保存对比项信息
  116. * 先获取未修改过的对比项信息,和现有的进行对比一下,找出新增、修改、删除的对比项。
  117. * 新增的对比项直接添加到数据库中,修改的对比项更新到数据库中,删除的对比项从数据库中删除。
  118. */
  119. bool BasicWidget::saveCompareItemInfo()
  120. {
  121. if(m_fromWebAPI == nullptr)
  122. {
  123. SPDLOG_LOGGER_ERROR(m_logger, "WebAPI 未设置指针,无法保存对比项信息");
  124. return false;
  125. }
  126. /* 获取未修改过的对比项信息,不从数据库里获取了 */
  127. const QList<CompareItemInfo_t>& srcCompareItems = CIData.getSrcCompareItemList();
  128. const QList<CompareItemInfo_t>& nowItems = CIData.getCompareItemList();
  129. QList<CompareItemInfo_t> insertItems; // 新增的对比项
  130. QList<int> deleteIDs; // 删除的对比项ID
  131. QList<CompareItemInfo_t> baseUpdateItems; // 修改的对比项基础信息
  132. QList<CompareItemInfo_t> roadUpdateItems; // 修改的对比项通道信息
  133. /* 先找到新增的对比项 */
  134. findInsertCompareItem(srcCompareItems, nowItems, insertItems);
  135. /* 找到删除的对比项 */
  136. findDeleteCompareItem(srcCompareItems, nowItems, deleteIDs);
  137. /* 找到修改的对比项 */
  138. findUpdateCompareItem(srcCompareItems, nowItems, baseUpdateItems, roadUpdateItems);
  139. /* 先删除已经消失的对比项 */
  140. if(!deleteIDs.isEmpty())
  141. {
  142. if(!m_fromWebAPI->deleteCompareItem(deleteIDs))
  143. {
  144. SPDLOG_LOGGER_ERROR(m_logger, "删除对比项信息失败");
  145. return false;
  146. }
  147. }
  148. /* 再插入新增的对比项 */
  149. if(!insertItems.isEmpty())
  150. {
  151. if(!m_fromWebAPI->insertCompareItem(insertItems))
  152. {
  153. SPDLOG_LOGGER_ERROR(m_logger, "插入对比项信息失败");
  154. return false;
  155. }
  156. }
  157. /* 更新修改的对比项基础信息 */
  158. if(!baseUpdateItems.isEmpty())
  159. {
  160. if(!m_fromWebAPI->updateCompareItemOnly(baseUpdateItems))
  161. {
  162. SPDLOG_LOGGER_ERROR(m_logger, "更新对比项基础信息失败");
  163. return false;
  164. }
  165. }
  166. /* 更新修改的对比项通道信息 */
  167. if(!roadUpdateItems.isEmpty())
  168. {
  169. if(!m_fromWebAPI->updateCompareItemRoadOnly(roadUpdateItems))
  170. {
  171. SPDLOG_LOGGER_ERROR(m_logger, "更新对比项通道信息失败");
  172. return false;
  173. }
  174. }
  175. /* 更新数据库的信息 */
  176. QList<CompareItemInfo_t> listNewItems;
  177. if(!m_fromWebAPI->getCompareItemInfo(listNewItems))
  178. {
  179. SPDLOG_LOGGER_ERROR(m_logger, "获取最新对比项信息失败");
  180. return false;
  181. }
  182. /* 更新本地的对比项数据 */
  183. CIData.setCompareItemList(listNewItems);
  184. return true;
  185. }
  186. /* 查找新增的对比项信息 */
  187. void BasicWidget::findInsertCompareItem(const QList<CompareItemInfo_t>& srcItems, const QList<CompareItemInfo_t>& nowItems, QList<CompareItemInfo_t>& insertItems)
  188. {
  189. for(const CompareItemInfo_t& nowItem : nowItems)
  190. {
  191. bool bFound = false;
  192. for(const CompareItemInfo_t& srcItem : srcItems)
  193. {
  194. if(nowItem.nID == srcItem.nID)
  195. {
  196. bFound = true;
  197. break;
  198. }
  199. }
  200. if(!bFound)
  201. {
  202. insertItems.append(nowItem);
  203. }
  204. }
  205. }
  206. /* 查找删除的对比项信息 */
  207. void BasicWidget::findDeleteCompareItem(const QList<CompareItemInfo_t>& srcItems, const QList<CompareItemInfo_t>& nowItems, QList<int>& deleteIDs)
  208. {
  209. for(const CompareItemInfo_t& srcItem : srcItems)
  210. {
  211. bool bFound = false;
  212. for(const CompareItemInfo_t& nowItem : nowItems)
  213. {
  214. if(srcItem.nID == nowItem.nID)
  215. {
  216. bFound = true;
  217. break;
  218. }
  219. }
  220. if(!bFound)
  221. {
  222. deleteIDs.append(srcItem.nID);
  223. }
  224. }
  225. }
  226. /* 查找修改的对比项 */
  227. void BasicWidget::findUpdateCompareItem(const QList<CompareItemInfo_t>& srcItems, const QList<CompareItemInfo_t>& nowItems,
  228. QList<CompareItemInfo_t>& baseUpdateItems, QList<CompareItemInfo_t>& roadUpdateItems)
  229. {
  230. for(const CompareItemInfo_t& nowItem : nowItems)
  231. {
  232. bool bFound = false;
  233. for(const CompareItemInfo_t& srcItem : srcItems)
  234. {
  235. if(nowItem.nID == srcItem.nID)
  236. {
  237. bFound = true;
  238. if(!nowItem.isEqualBase(srcItem))
  239. {
  240. baseUpdateItems.append(nowItem);
  241. }
  242. /* 对比通道信息 */
  243. if(!nowItem.isEqualRoads(srcItem))
  244. {
  245. roadUpdateItems.append(nowItem);
  246. }
  247. break;
  248. }
  249. }
  250. if(!bFound)
  251. {
  252. // SPDLOG_LOGGER_DEBUG(m_logger, "对比项ID {} 在源数据中未找到,可能是新增的对比项", nowItem.nID);
  253. }
  254. }
  255. }