basicwidget.cpp 15 KB

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