basicwidget.cpp 17 KB

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