basicwidget.cpp 15 KB

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