basicwidget.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. /* 隐藏不一致判断选项,这个大部分没用了,有用的搬到AI对比中了 */
  48. ui->label_5->hide();
  49. ui->comBox_notConsistency->hide();
  50. /* 设置从数据库读取到的选项 */
  51. restoreBasicSettingInfo();
  52. /* 连接信号和槽 */
  53. connect(ui->comBox_driverName, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &BasicWidget::do_soundCardChanged);
  54. connect(ui->pBtn_restore, &QPushButton::clicked, this, &BasicWidget::do_pBtn_restore_clicked);
  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::do_pBtn_restore_clicked()
  107. {
  108. /* 从全局设置中恢复已修改的设置 */
  109. restoreBasicSettingInfo();
  110. /* 恢复对比项信息 */
  111. restoreCompareItemInfo();
  112. }
  113. /* 设置声卡信息 */
  114. void BasicWidget::setSoundCardInfo()
  115. {
  116. const auto& soundCardInfo = SoundCards.getSoundCardInfo();
  117. /* 清空现有的声卡信息 */
  118. ui->comBox_driverName->clear();
  119. /* 添加声卡信息到下拉框 */
  120. for(const auto& it : soundCardInfo)
  121. {
  122. ui->comBox_driverName->addItem(it.strSoundCardName, it.nSoundCardNum);
  123. }
  124. /* 设置当前信息 */
  125. ui->comBox_driverName->setCurrentIndex(0);
  126. }
  127. /* 保存基础信息 */
  128. bool BasicWidget::saveBasicSettingInfo()
  129. {
  130. BaseConfig_t baseConfig;
  131. const BaseConfig_t& srcBaseConfig = SysConfig.getBaseConfigSrc();
  132. /* 获取基础配置信息 */
  133. baseConfig.strServerIP = ui->lineEdit_serverIP->text();
  134. baseConfig.nRecordMode = ui->comBox_recordMode->currentData().toInt();
  135. baseConfig.strDriverName = ui->comBox_driverName->currentData().toString();
  136. baseConfig.nNotConsistency = ui->comBox_notConsistency->currentData().toInt();
  137. baseConfig.isEnableMultiCore = ui->checkBox_enableMultiCPU->isChecked();
  138. baseConfig.isEnableDebugLog = ui->checkBox_enableDebug->isChecked();
  139. baseConfig.isClearDirSystemOn = ui->checkBox_clearHistoryDir->isChecked();
  140. baseConfig.isUsingSoundCardName = ui->checkBox_enableSoundCardName->isChecked();
  141. // SPDLOG_LOGGER_DEBUG(m_logger, "修改后的基础配置信息: ServerIP: {}, RecordMode: {}, DriverName: {}, NotConsistency: {}, EnableMultiCore: {}, EnableDebugLog: {}, ClearHistoryDirOnStart: {}, UseSoundCardName: {}",
  142. // baseConfig.strServerIP.toStdString(), baseConfig.nRecordMode, baseConfig.strDriverName.toStdString(),
  143. // baseConfig.nNotConsistency, baseConfig.isEnableMultiCore, baseConfig.isEnableDebugLog,
  144. // baseConfig.isClearDirSystemOn, baseConfig.isUsingSoundCardName);
  145. // SPDLOG_LOGGER_DEBUG(m_logger, "现有的基础配置信息: ServerIP: {}, RecordMode: {}, DriverName: {}, NotConsistency: {}, EnableMultiCore: {}, EnableDebugLog: {}, ClearHistoryDirOnStart: {}, UseSoundCardName: {}",
  146. // srcBaseConfig.strServerIP.toStdString(), srcBaseConfig.nRecordMode, srcBaseConfig.strDriverName.toStdString(),
  147. // srcBaseConfig.nNotConsistency, srcBaseConfig.isEnableMultiCore, srcBaseConfig.isEnableDebugLog,
  148. // srcBaseConfig.isClearDirSystemOn, srcBaseConfig.isUsingSoundCardName);
  149. if(baseConfig == srcBaseConfig)
  150. {
  151. SPDLOG_LOGGER_DEBUG(m_logger, "基础配置信息没有修改,不需要保存");
  152. return true;
  153. }
  154. /* 将结构体转换成json字符串 */
  155. std::string strJson;
  156. SysConfig.setBaseConfigToJson(baseConfig, strJson);
  157. if(!m_fromWebAPI->updateSystemConfig(Config_Base, strJson, SysConfig.mapSysConfigDesc[eSystemConfigType::eSCT_BaseConfig]))
  158. {
  159. SPDLOG_LOGGER_ERROR(m_logger, "更新基础配置信息失败");
  160. return false;
  161. }
  162. SysConfig.setBaseConfig(baseConfig); // 更新本地的基础配置信息
  163. return true;
  164. }
  165. /* 保存对比项信息
  166. * 先获取未修改过的对比项信息,和现有的进行对比一下,找出新增、修改、删除的对比项。
  167. * 新增的对比项直接添加到数据库中,修改的对比项更新到数据库中,删除的对比项从数据库中删除。
  168. */
  169. bool BasicWidget::saveCompareItemInfo()
  170. {
  171. if(m_fromWebAPI == nullptr)
  172. {
  173. SPDLOG_LOGGER_ERROR(m_logger, "WebAPI 未设置指针,无法保存对比项信息");
  174. return false;
  175. }
  176. /* 获取未修改过的对比项信息,不从数据库里获取了 */
  177. const QList<CompareItemInfo_t>& srcCompareItems = CIData.getSrcCompareItemList();
  178. const QList<CompareItemInfo_t>& nowItems = CIData.getCompareItemList();
  179. QList<CompareItemInfo_t> insertItems; // 新增的对比项
  180. QList<int> deleteIDs; // 删除的对比项ID
  181. QList<CompareItemInfo_t> baseUpdateItems; // 修改的对比项基础信息
  182. QList<CompareItemInfo_t> roadUpdateItems; // 修改的对比项通道信息
  183. /* 先找到新增的对比项 */
  184. findInsertCompareItem(srcCompareItems, nowItems, insertItems);
  185. /* 找到删除的对比项 */
  186. findDeleteCompareItem(srcCompareItems, nowItems, deleteIDs);
  187. /* 找到修改的对比项 */
  188. findUpdateCompareItem(srcCompareItems, nowItems, baseUpdateItems, roadUpdateItems);
  189. /* 先删除已经消失的对比项 */
  190. if(!deleteIDs.isEmpty())
  191. {
  192. if(!m_fromWebAPI->deleteCompareItem(deleteIDs))
  193. {
  194. SPDLOG_LOGGER_ERROR(m_logger, "删除对比项信息失败");
  195. return false;
  196. }
  197. }
  198. /* 再插入新增的对比项 */
  199. if(!insertItems.isEmpty())
  200. {
  201. if(!m_fromWebAPI->insertCompareItem(insertItems))
  202. {
  203. SPDLOG_LOGGER_ERROR(m_logger, "插入对比项信息失败");
  204. return false;
  205. }
  206. }
  207. /* 更新修改的对比项基础信息 */
  208. if(!baseUpdateItems.isEmpty())
  209. {
  210. if(!m_fromWebAPI->updateCompareItemOnly(baseUpdateItems))
  211. {
  212. SPDLOG_LOGGER_ERROR(m_logger, "更新对比项基础信息失败");
  213. return false;
  214. }
  215. }
  216. /* 更新修改的对比项通道信息 */
  217. if(!roadUpdateItems.isEmpty())
  218. {
  219. if(!m_fromWebAPI->updateCompareItemRoadOnly(roadUpdateItems))
  220. {
  221. SPDLOG_LOGGER_ERROR(m_logger, "更新对比项通道信息失败");
  222. return false;
  223. }
  224. }
  225. /* 获取更新后的数据库信息 */
  226. QList<CompareItemInfo_t> listNewItems;
  227. if(!m_fromWebAPI->getCompareItemInfo(listNewItems))
  228. {
  229. SPDLOG_LOGGER_ERROR(m_logger, "获取最新对比项信息失败");
  230. return false;
  231. }
  232. /* 更新本地的对比项数据 */
  233. CIData.setCompareItemList(listNewItems);
  234. return true;
  235. }
  236. /* 查找新增的对比项信息 */
  237. void BasicWidget::findInsertCompareItem(const QList<CompareItemInfo_t>& srcItems, const QList<CompareItemInfo_t>& nowItems, QList<CompareItemInfo_t>& insertItems)
  238. {
  239. for(const CompareItemInfo_t& nowItem : nowItems)
  240. {
  241. bool bFound = false;
  242. for(const CompareItemInfo_t& srcItem : srcItems)
  243. {
  244. if(nowItem.nID == srcItem.nID)
  245. {
  246. bFound = true;
  247. break;
  248. }
  249. }
  250. if(!bFound)
  251. {
  252. insertItems.append(nowItem);
  253. }
  254. }
  255. }
  256. /* 查找删除的对比项信息 */
  257. void BasicWidget::findDeleteCompareItem(const QList<CompareItemInfo_t>& srcItems, const QList<CompareItemInfo_t>& nowItems, QList<int>& deleteIDs)
  258. {
  259. for(const CompareItemInfo_t& srcItem : srcItems)
  260. {
  261. bool bFound = false;
  262. for(const CompareItemInfo_t& nowItem : nowItems)
  263. {
  264. if(srcItem.nID == nowItem.nID)
  265. {
  266. bFound = true;
  267. break;
  268. }
  269. }
  270. if(!bFound)
  271. {
  272. deleteIDs.append(srcItem.nID);
  273. }
  274. }
  275. }
  276. /* 查找修改的对比项 */
  277. void BasicWidget::findUpdateCompareItem(const QList<CompareItemInfo_t>& srcItems, const QList<CompareItemInfo_t>& nowItems,
  278. QList<CompareItemInfo_t>& baseUpdateItems, QList<CompareItemInfo_t>& roadUpdateItems)
  279. {
  280. for(const CompareItemInfo_t& nowItem : nowItems)
  281. {
  282. bool bFound = false;
  283. for(const CompareItemInfo_t& srcItem : srcItems)
  284. {
  285. if(nowItem.nID == srcItem.nID)
  286. {
  287. bFound = true;
  288. if(!nowItem.isEqualBase(srcItem))
  289. {
  290. baseUpdateItems.append(nowItem);
  291. }
  292. /* 对比通道信息 */
  293. if(!nowItem.isEqualRoads(srcItem))
  294. {
  295. roadUpdateItems.append(nowItem);
  296. }
  297. break;
  298. }
  299. }
  300. if(!bFound)
  301. {
  302. // SPDLOG_LOGGER_DEBUG(m_logger, "对比项ID {} 在源数据中未找到,可能是新增的对比项", nowItem.nID);
  303. }
  304. }
  305. }
  306. /* 恢复基础信息 */
  307. void BasicWidget::restoreBasicSettingInfo()
  308. {
  309. const BaseConfig_t& baseConfig = SysConfig.getBaseConfigSrc();
  310. ui->lineEdit_serverIP->setText(baseConfig.strServerIP);
  311. ui->comBox_recordMode->setCurrentIndex(ui->comBox_recordMode->findData(baseConfig.nRecordMode));
  312. ui->comBox_driverName->setCurrentText(baseConfig.strDriverName);
  313. ui->comBox_notConsistency->setCurrentIndex(ui->comBox_notConsistency->findData(baseConfig.nNotConsistency));
  314. ui->checkBox_enableMultiCPU->setChecked(baseConfig.isEnableMultiCore);
  315. ui->checkBox_enableDebug->setChecked(baseConfig.isEnableDebugLog);
  316. ui->checkBox_clearHistoryDir->setChecked(baseConfig.isClearDirSystemOn);
  317. ui->checkBox_enableSoundCardName->setChecked(baseConfig.isUsingSoundCardName);
  318. }
  319. /* 恢复对比项信息 */
  320. bool BasicWidget::restoreCompareItemInfo()
  321. {
  322. const auto& srcCompareItems = CIData.getSrcCompareItemList();
  323. /* 恢复对比项信息 */
  324. CIData.setCompareItemList(srcCompareItems);
  325. return true;
  326. }