basicwidget.cpp 10 KB

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