checkperiodwidget.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. #include "checkperiodwidget.h"
  2. #include "GlobalVariable.h"
  3. #include "checkperiodfunc.h"
  4. #include "onedetectplan.h"
  5. #include "ui_checkperiodwidget.h"
  6. #include "addperioddialog.h"
  7. #include "UIStyleManager.h"
  8. #include "customcombobox.h"
  9. #include <QFile>
  10. #include <algorithm>
  11. #include <cmath>
  12. #include <qboxlayout.h>
  13. #include "checkperiodfunc.h"
  14. #include "tipwidget.h"
  15. #include "GlobalInfo.h"
  16. #include "timewidget.h"
  17. #include "SystemConfig.h"
  18. CheckPeriodWidget::CheckPeriodWidget(QWidget *parent) :
  19. QWidget(parent),
  20. ui(new Ui::CheckPeriodWidget)
  21. {
  22. ui->setupUi(this);
  23. m_logger = spdlog::get("ACASetting");
  24. if(m_logger == nullptr)
  25. {
  26. fmt::print("CheckPeriodWidget: Logger ACASetting not found\n");
  27. return;
  28. }
  29. /* 下拉框设置阴影 */
  30. ui->comboBox_selectCompareItem->setViewShadowEffect();
  31. /* 获取计划容器的布局指针 */
  32. m_layoutDetectPlans = qobject_cast<QVBoxLayout*>(ui->scrollArea_detectPlans->layout());
  33. m_layoutNoDetectPlans = qobject_cast<QVBoxLayout*>(ui->scrollArea_noDetectPlans->layout());
  34. connect(ui->pBtn_addDetectPlan, &QPushButton::clicked, this, &CheckPeriodWidget::do_pBtn_addDetectPlan_clicked);
  35. connect(ui->pBtn_addNoDetectPlan, &QPushButton::clicked, this, &CheckPeriodWidget::do_pBtn_addNoDetectPlan_clicked);
  36. connect(ui->comboBox_selectCompareItem, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &CheckPeriodWidget::do_comboBox_selectCompareItem_currentIndexChanged);
  37. connect(ui->checkBox_applySlient, &QCheckBox::clicked, this, &CheckPeriodWidget::do_checkBox_clicked);
  38. connect(ui->checkBox_applyOverload, &QCheckBox::clicked, this, &CheckPeriodWidget::do_checkBox_clicked);
  39. connect(ui->checkBox_applyPhase, &QCheckBox::clicked, this, &CheckPeriodWidget::do_checkBox_clicked);
  40. connect(ui->checkBox_applyNoise, &QCheckBox::clicked, this, &CheckPeriodWidget::do_checkBox_clicked);
  41. connect(ui->pBtn_restore, &QPushButton::clicked, this, &CheckPeriodWidget::do_pBtn_restore_clicked);
  42. /* 设置UI */
  43. UIStyle.registerWidget(this);
  44. QString qssPath = UIStyle.getQSSPath() + "/onedetectplan.qss";
  45. QFile file(qssPath);
  46. if(file.open(QFile::ReadOnly))
  47. {
  48. m_qssPlan = file.readAll();
  49. file.close();
  50. } else
  51. {
  52. SPDLOG_LOGGER_WARN(m_logger, "打开QSS文件失败: {}", qssPath.toStdString());
  53. }
  54. /* 初始化列表 */
  55. initListWidget();
  56. }
  57. CheckPeriodWidget::~CheckPeriodWidget()
  58. {
  59. UIStyle.unregisterWidget(this);
  60. delete ui;
  61. }
  62. /* 更新可选的对比项列表 */
  63. void CheckPeriodWidget::updateCompareItemList(const QList<CompareItemInfo_t>& compareItemList)
  64. {
  65. /* 先获取当前选择的列表项 */
  66. int id = ui->comboBox_selectCompareItem->currentData().value<int>();
  67. /* 屏蔽comboBox信号 */
  68. ui->comboBox_selectCompareItem->blockSignals(true);
  69. ui->comboBox_selectCompareItem->clear();
  70. m_pListcurrDetect = nullptr;
  71. for(const auto& item : compareItemList)
  72. {
  73. /* 添加到下拉框 */
  74. ui->comboBox_selectCompareItem->addItem(item.strName, item.nID);
  75. }
  76. /* 设置当前选择的对比项 */
  77. if(id != 0)
  78. {
  79. /* 屏蔽comboBox信号 */
  80. ui->comboBox_selectCompareItem->blockSignals(true);
  81. for(int i = 0; i < ui->comboBox_selectCompareItem->count(); ++i)
  82. {
  83. if(ui->comboBox_selectCompareItem->itemData(i).value<int>() == id)
  84. {
  85. ui->comboBox_selectCompareItem->setCurrentIndex(i);
  86. break;
  87. }
  88. }
  89. } else
  90. {
  91. ui->comboBox_selectCompareItem->setCurrentIndex(0);
  92. }
  93. /* 恢复comboBox信号 */
  94. ui->comboBox_selectCompareItem->blockSignals(false);
  95. /* 手动切换当前的列表 */
  96. do_comboBox_selectCompareItem_currentIndexChanged(ui->comboBox_selectCompareItem->currentIndex());
  97. }
  98. /* 保存设置 */
  99. bool CheckPeriodWidget::saveSettings()
  100. {
  101. /* 更新静音过载反相的设置 */
  102. m_mapNoDetectApply[m_currNoDetectApply.nID] = m_currNoDetectApply;
  103. /* 生成按照对比项相关的设置计划 */
  104. QMap<int, DetectPeriodConfig_t> mapDetectConfig;
  105. updateCompareItemDetectPeriod(mapDetectConfig);
  106. /* 和已有的对比,只有有修改的地方,直接删除这个对比项ID所有计划,然后插入 */
  107. const auto& oldMap = SysConfig.getDetectPeriodConfig();
  108. QMap<int, DetectPeriodConfig_t> insertMap;
  109. QList<int> deleteList;
  110. /* 先找出需要插入的 */
  111. for(auto it = mapDetectConfig.cbegin(); it != mapDetectConfig.cend(); ++it)
  112. {
  113. int id = it.key();
  114. if(oldMap.contains(id))
  115. {
  116. /* 如果有修改,就删除原来的 */
  117. if(oldMap[id] == it.value())
  118. {
  119. continue;
  120. }else
  121. {
  122. deleteList.append(id);
  123. }
  124. }
  125. /* 无论是没有还是修改了,都需要重新插入 */
  126. insertMap.insert(id, it.value());
  127. }
  128. /* 先删除,再插入新数据 */
  129. if(!deleteList.isEmpty())
  130. {
  131. if(!m_fromWebAPI->deleteDetectPeriodConfig(deleteList))
  132. {
  133. SPDLOG_LOGGER_ERROR(m_logger, "删除对比项检测时段配置失败");
  134. TipWidget::display(TipWidget::OPERATOR_WARN, "删除对比项检测时段配置失败", GInfo.getTopWindow());
  135. return false;
  136. }
  137. }
  138. if(!insertMap.isEmpty())
  139. {
  140. if(!m_fromWebAPI->insertDetectPeriodConfig(insertMap))
  141. {
  142. SPDLOG_LOGGER_ERROR(m_logger, "插入对比项检测时段配置失败");
  143. TipWidget::display(TipWidget::OPERATOR_WARN, "插入对比项检测时段配置失败", GInfo.getTopWindow());
  144. return false;
  145. }
  146. }
  147. /* 更新系统配置 */
  148. SysConfig.setDetectPeriodConfig(mapDetectConfig);
  149. return true;
  150. }
  151. /* 选择了一个对比项,id是新对比项的id */
  152. void CheckPeriodWidget::do_comboBox_selectCompareItem_currentIndexChanged(int index)
  153. {
  154. /* 获取当前选择的对比项id */
  155. int id = ui->comboBox_selectCompareItem->itemData(index).value<int>();
  156. if(id == 0)
  157. {
  158. m_pListcurrDetect = nullptr;
  159. SPDLOG_LOGGER_WARN(m_logger, "对比项ID为0,无法获取检测计划列表");
  160. return;
  161. }
  162. /* 如果已经有了,就不需要重新创建 */
  163. if(m_mapDetectPlanList.contains(id))
  164. {
  165. m_pListcurrDetect = m_mapDetectPlanList.value(id);
  166. }else
  167. {
  168. /* 创建一个新的列表 */
  169. m_pListcurrDetect = new QList<OneDetectPlan*>();
  170. m_mapDetectPlanList.insert(id, m_pListcurrDetect);
  171. }
  172. if(m_mapNoDetectPlanList.contains(id))
  173. {
  174. m_pListcurrNoDetect = m_mapNoDetectPlanList.value(id);
  175. } else
  176. {
  177. /* 创建一个新的列表 */
  178. m_pListcurrNoDetect = new QList<OneDetectPlan*>();
  179. m_mapNoDetectPlanList.insert(id, m_pListcurrNoDetect);
  180. }
  181. /* 应用项目需要先保存当前的设置到对应的对比项ID中,然后再切换 */
  182. m_currNoDetectApply.isApplySlient = ui->checkBox_applySlient->isChecked();
  183. m_currNoDetectApply.isApplyOverload = ui->checkBox_applyOverload->isChecked();
  184. m_currNoDetectApply.isApplyPhase = ui->checkBox_applyPhase->isChecked();
  185. m_currNoDetectApply.isApplyNoise = ui->checkBox_applyNoise->isChecked();
  186. /* 这里多次检测,是防止第一次进入这个页面,没有保存好的设置 */
  187. if(m_currNoDetectApply.nID > 0)
  188. {
  189. if(m_mapNoDetectApply.contains(m_currNoDetectApply.nID))
  190. {
  191. m_mapNoDetectApply[m_currNoDetectApply.nID] = m_currNoDetectApply;
  192. } else
  193. {
  194. m_mapNoDetectApply.insert(m_currNoDetectApply.nID, m_currNoDetectApply);
  195. }
  196. }
  197. if(m_mapNoDetectApply.contains(id))
  198. {
  199. m_currNoDetectApply = m_mapNoDetectApply.value(id);
  200. } else
  201. {
  202. /* 清空设置 */
  203. m_currNoDetectApply = NoDetectPlanApply_t();
  204. m_currNoDetectApply.nID = id;
  205. m_mapNoDetectApply.insert(id, m_currNoDetectApply);
  206. }
  207. ui->checkBox_applySlient->setChecked(m_currNoDetectApply.isApplySlient);
  208. ui->checkBox_applyOverload->setChecked(m_currNoDetectApply.isApplyOverload);
  209. ui->checkBox_applyPhase->setChecked(m_currNoDetectApply.isApplyPhase);
  210. ui->checkBox_applyNoise->setChecked(m_currNoDetectApply.isApplyNoise);
  211. /* 清空当前的布局 */
  212. removeAllDetectPlanWidgets();
  213. removeAllNoDetectPlanWidgets();
  214. addDetectPlanToLayout();
  215. addNoDetectPlanToLayout();
  216. }
  217. /* 添加一个检测计划 */
  218. void CheckPeriodWidget::do_pBtn_addDetectPlan_clicked()
  219. {
  220. /* 检查当前有没有对比项选择 */
  221. int id = ui->comboBox_selectCompareItem->currentData().value<int>();
  222. if(id <= 0)
  223. {
  224. TipWidget::display(TipWidget::OPERATOR_WARN, "请先选择一个对比项", GInfo.getTopWindow());
  225. SPDLOG_LOGGER_WARN(m_logger, "没有选择对比项,无法添加检测计划");
  226. return;
  227. }
  228. AddPeriodDialog dlg(PERIOD_WEEK);
  229. /* 更新计划列表 */
  230. updateDetectPlanList(1);
  231. /* 设置计划列表 */
  232. dlg.setPlanList(m_currentPlanList);
  233. dlg.exec();
  234. if(!dlg.isOK())
  235. {
  236. return;
  237. }
  238. OnePlan_t plan = dlg.getPlan();
  239. addDetectPlan(plan);
  240. }
  241. /* 添加一个不检测计划 */
  242. void CheckPeriodWidget::do_pBtn_addNoDetectPlan_clicked()
  243. {
  244. /* 检查当前有没有对比项选择 */
  245. int id = ui->comboBox_selectCompareItem->currentData().value<int>();
  246. if(id <= 0)
  247. {
  248. TipWidget::display(TipWidget::OPERATOR_WARN, "请先选择一个对比项", GInfo.getTopWindow());
  249. SPDLOG_LOGGER_WARN(m_logger, "没有选择对比项,无法添加检测计划");
  250. return;
  251. }
  252. AddPeriodDialog dlg(PERIOD_DATE);
  253. dlg.exec();
  254. if(!dlg.isOK())
  255. {
  256. return;
  257. }
  258. OnePlan_t plan = dlg.getPlan();
  259. addNoDetectPlan(plan);
  260. }
  261. /* 删除一个检测计划 */
  262. void CheckPeriodWidget::do_pBtn_deleteDetectPlan_clicked()
  263. {
  264. /* 获取信号发送者 */
  265. OneDetectPlan *pDetect = qobject_cast<OneDetectPlan*>(sender());
  266. if(pDetect == nullptr)
  267. {
  268. return;
  269. }
  270. m_layoutDetectPlans->removeWidget(pDetect);
  271. m_pListcurrDetect->removeAll(pDetect);
  272. delete pDetect;
  273. /* 重新排序 */
  274. // sortDetectPlanList();
  275. }
  276. /* 删除一个不检测计划 */
  277. void CheckPeriodWidget::do_pBtn_deleteNoDetectPlan_clicked()
  278. {
  279. /* 获取信号发送者 */
  280. OneDetectPlan *pDetect = qobject_cast<OneDetectPlan*>(sender());
  281. if(pDetect == nullptr)
  282. {
  283. return;
  284. }
  285. m_layoutNoDetectPlans->removeWidget(pDetect);
  286. m_pListcurrNoDetect->removeAll(pDetect);
  287. delete pDetect;
  288. /* 重新排序 */
  289. // sortNoDetectPlanList();
  290. }
  291. /* 修改了日期或周几,检测是否冲突 */
  292. void CheckPeriodWidget::do_detectPlanModifiedWeek(OnePlan_t formerPlan, OnePlan_t newPlan)
  293. {
  294. OneDetectPlan *pDetect = qobject_cast<OneDetectPlan*>(sender());
  295. /* 更新计划 */
  296. updateDetectPlanList(1);
  297. /* 检测是否冲突 */
  298. if(isWeekPlanDuplicate(m_currentPlanList, newPlan, newPlan))
  299. {
  300. /* 弹出提示 */
  301. TipWidget::display(TipWidget::OPERATOR_WARN, "与已有计划冲突,请重新修改", GInfo.getTopWindow());
  302. SPDLOG_LOGGER_WARN(m_logger, "检测计划冲突");
  303. /* 恢复原计划 */
  304. pDetect->setPlan(formerPlan);
  305. return;
  306. }
  307. /* 重新排序 */
  308. sortDetectPlanList();
  309. }
  310. /* 点击了检测计划的时间按钮,在这里修改时间 */
  311. void CheckPeriodWidget::do_detectPlanModifiedTime(QPoint pBtnSize, bool isStartTime)
  312. {
  313. /* 获取信号发送者 */
  314. auto one = qobject_cast<OneDetectPlan*>(sender());
  315. OnePlan_t plan = one->getPlan();
  316. /* 创建时间选择控件 */
  317. std::shared_ptr<TimeWidget> tw = std::make_shared<TimeWidget>(this, TimeWidget::ShowType::Dialog);
  318. /* 设置样式表 */
  319. tw->setStyleSheet(m_qssPlan);
  320. /* 设置图标 */
  321. tw->setIcon(":/icon/time.png");
  322. tw->setIconShow(true);
  323. tw->setIconSize(16, 16);
  324. /* 重新设置大小 */
  325. tw->setEditLine(112, 32);
  326. /* 设置选择框大小 */
  327. tw->setTimeAreaWidth(140);
  328. /* 移动位置,覆盖显示时间的按钮,获取的坐标是相对于Dialog的位置 */
  329. auto pos = this->mapFromGlobal(pBtnSize);
  330. // pos.setX(pos.x() - 1); /* 去掉阴影的宽度 */
  331. // pos.setY(pos.y()); /* 去掉阴影的高度 */
  332. tw->move(pos);
  333. // SPDLOG_LOGGER_DEBUG(m_logger, "移动前位置: {}, {}", pBtnSize.x(), pBtnSize.y());
  334. // SPDLOG_LOGGER_DEBUG(m_logger, "移动后位置: {}, {}", pos.x(), pos.y());
  335. /* 设置默认的时间 */
  336. if(isStartTime)
  337. {
  338. tw->setTime(plan.timeStart);
  339. } else
  340. {
  341. tw->setTime(plan.timeEnd);
  342. }
  343. tw->execShow();
  344. auto time = tw->getTime();
  345. /* 判断时间有没有修改 */
  346. if(isStartTime)
  347. {
  348. if(time == plan.timeStart)
  349. {
  350. return; // 没有修改
  351. }
  352. } else
  353. {
  354. if(time == plan.timeEnd)
  355. {
  356. return; // 没有修改
  357. }
  358. }
  359. SPDLOG_LOGGER_DEBUG(m_logger, "修改时间: {}, {}", time.toString("hh:mm:ss").toStdString(), isStartTime ? "开始时间" : "结束时间");
  360. OnePlan_t newPlan = plan;
  361. if(isStartTime)
  362. {
  363. newPlan.timeStart = time;
  364. } else
  365. {
  366. newPlan.timeEnd = time;
  367. }
  368. /* 判断时间是否重复 */
  369. updateDetectPlanList(1);
  370. if(isWeekPlanDuplicate(m_currentPlanList, newPlan, plan))
  371. {
  372. /* 设置时间报警 */
  373. TipWidget::display(TipWidget::OPERATOR_WARN, "与已有计划冲突,请重新修改", GInfo.getTopWindow());
  374. SPDLOG_LOGGER_WARN(m_logger, "检测计划冲突");
  375. return;
  376. }
  377. /* 设置时间 */
  378. one->setPlan(newPlan);
  379. /* 重新排序 */
  380. sortDetectPlanList();
  381. }
  382. /* 修改了静音、反相、过载相关的设置 */
  383. void CheckPeriodWidget::do_checkBox_clicked()
  384. {
  385. m_currNoDetectApply.isApplySlient = ui->checkBox_applySlient->isChecked();
  386. m_currNoDetectApply.isApplyOverload = ui->checkBox_applyOverload->isChecked();
  387. m_currNoDetectApply.isApplyPhase = ui->checkBox_applyPhase->isChecked();
  388. m_currNoDetectApply.isApplyNoise = ui->checkBox_applyNoise->isChecked();
  389. }
  390. /* 恢复配置项 */
  391. void CheckPeriodWidget::do_pBtn_restore_clicked()
  392. {
  393. initListWidget();
  394. }
  395. /* 初始化QListWidget */
  396. void CheckPeriodWidget::initListWidget()
  397. {
  398. /* 获取读取到的检测计划 */
  399. const auto& mapDetectConfig = SysConfig.getDetectPeriodConfig();
  400. /* 将计划转换成列表组件 */
  401. for(auto it = mapDetectConfig.cbegin(); it != mapDetectConfig.cend(); ++it)
  402. {
  403. int id = it.key();
  404. const DetectPeriodConfig_t& config = it.value();
  405. /* 如果已经有了,就不需要重新创建 */
  406. if(m_mapDetectPlanList.contains(id))
  407. {
  408. m_pListcurrDetect = m_mapDetectPlanList.value(id);
  409. }else
  410. {
  411. /* 创建一个新的列表 */
  412. m_pListcurrDetect = new QList<OneDetectPlan*>();
  413. m_mapDetectPlanList.insert(id, m_pListcurrDetect);
  414. }
  415. if(m_mapNoDetectPlanList.contains(id))
  416. {
  417. m_pListcurrNoDetect = m_mapNoDetectPlanList.value(id);
  418. } else
  419. {
  420. /* 创建一个新的列表 */
  421. m_pListcurrNoDetect = new QList<OneDetectPlan*>();
  422. m_mapNoDetectPlanList.insert(id, m_pListcurrNoDetect);
  423. }
  424. /* 添加检测计划 */
  425. for(const auto& plan : config.listDetect)
  426. {
  427. addDetectPlan(plan);
  428. }
  429. /* 添加非检测计划 */
  430. for(const auto& plan : config.listNoDetect)
  431. {
  432. addNoDetectPlan(plan);
  433. }
  434. /* 设置应用配置 */
  435. NoDetectPlanApply_t apply;
  436. apply.nID = id;
  437. apply.isApplySlient = config.isApplySlient;
  438. apply.isApplyOverload = config.isApplyOverload;
  439. apply.isApplyPhase = config.isApplyPhase;
  440. apply.isApplyNoise = config.isApplyNoise;
  441. m_mapNoDetectApply[id] = apply;
  442. }
  443. }
  444. /* 添加一个检测计划 */
  445. void CheckPeriodWidget::addDetectPlan(const OnePlan_t& plan)
  446. {
  447. /* 传入的日期类型,不是特殊日就可以 */
  448. OneDetectPlan *detectPlan = new OneDetectPlan(eWeekType::Week_Monday, this);
  449. /* 删除日期选择框 */
  450. detectPlan->setQSS(m_qssPlan);
  451. detectPlan->setPlan(plan);
  452. connect(detectPlan, &OneDetectPlan::signal_oneDetectPlanCloseClicked, this, &CheckPeriodWidget::do_pBtn_deleteDetectPlan_clicked);
  453. connect(detectPlan, &OneDetectPlan::signal_planModifiedWeek, this, &CheckPeriodWidget::do_detectPlanModifiedWeek);
  454. connect(detectPlan, &OneDetectPlan::signal_timeButtonClicked, this, &CheckPeriodWidget::do_detectPlanModifiedTime);
  455. /* 将数据插入到列表中,在排序的时候会将新增加的插入到里面 */
  456. m_pListcurrDetect->append(detectPlan);
  457. /* 排序 */
  458. sortDetectPlanList();
  459. }
  460. /* 添加一个非检测计划 */
  461. void CheckPeriodWidget::addNoDetectPlan(const OnePlan_t& plan)
  462. {
  463. /* 传入特殊日的日期类型 */
  464. OneDetectPlan *detectPlan = new OneDetectPlan(eWeekType::Week_Special, this);
  465. detectPlan->setQSS(m_qssPlan);
  466. detectPlan->setPlan(plan);
  467. connect(detectPlan, &OneDetectPlan::signal_oneDetectPlanCloseClicked, this, &CheckPeriodWidget::do_pBtn_deleteNoDetectPlan_clicked);
  468. m_pListcurrNoDetect->append(detectPlan);
  469. /* 排序并显示 */
  470. sortNoDetectPlanList();
  471. }
  472. /* 重新排序检测计划 */
  473. void CheckPeriodWidget::sortDetectPlanList()
  474. {
  475. /* 清空列表 */
  476. removeAllDetectPlanWidgets();
  477. /* 排序 */
  478. std::sort(m_pListcurrDetect->begin(), m_pListcurrDetect->end(), [](OneDetectPlan *a, OneDetectPlan *b)
  479. {
  480. const OnePlan_t &planA = a->getPlan();
  481. const OnePlan_t &planB = b->getPlan();
  482. /* 按时间从小到大升序 */
  483. return weekTimeIsGerater(planB.weekType, planB.timeStart, planA.weekType, planA.timeStart);
  484. });
  485. /* 重新插入 */
  486. addDetectPlanToLayout();
  487. update();
  488. }
  489. /* 排序非检测日期列表 */
  490. void CheckPeriodWidget::sortNoDetectPlanList()
  491. {
  492. /* 清空列表 */
  493. removeAllNoDetectPlanWidgets();
  494. /* 排序 */
  495. std::sort(m_pListcurrDetect->begin(), m_pListcurrDetect->end(), [](OneDetectPlan *a, OneDetectPlan *b)
  496. {
  497. const OnePlan_t &planA = a->getPlan();
  498. const OnePlan_t &planB = b->getPlan();
  499. /* 按时间从小到大升序 */
  500. return dateTimeIsGerater(planB.date, planB.timeStart, planA.date, planA.timeStart);
  501. });
  502. /* 重新插入 */
  503. addNoDetectPlanToLayout();
  504. update();
  505. }
  506. /* 获取计划列表 */
  507. QList<OnePlan_t> CheckPeriodWidget::updateDetectPlanList(int id)
  508. {
  509. m_currentPlanList.clear();
  510. for(auto& pDetect : *m_pListcurrDetect)
  511. {
  512. OnePlan_t plan = pDetect->getPlan();
  513. m_currentPlanList.append(plan);
  514. }
  515. return m_currentPlanList;
  516. }
  517. /* 清空当前检测计划布局中的控件 */
  518. void CheckPeriodWidget::removeAllDetectPlanWidgets()
  519. {
  520. /* 清空布局中的所有控件,只剩一个弹簧 */
  521. while (m_layoutDetectPlans->count() > 1)
  522. {
  523. QWidget *widget = m_layoutDetectPlans->itemAt(0)->widget();
  524. if(widget)
  525. {
  526. m_layoutDetectPlans->removeWidget(widget);
  527. widget->hide();
  528. }
  529. }
  530. }
  531. /* 清空当前非检测计划布局中的控件 */
  532. void CheckPeriodWidget::removeAllNoDetectPlanWidgets()
  533. {
  534. /* 清空所有的非检测计划日期 */
  535. while( m_layoutNoDetectPlans->count() > 1)
  536. {
  537. QWidget *widget = m_layoutNoDetectPlans->itemAt(0)->widget();
  538. if(widget)
  539. {
  540. m_layoutNoDetectPlans->removeWidget(widget);
  541. widget->hide();
  542. }
  543. }
  544. }
  545. /* 从当前列表中添加控件到布局 */
  546. void CheckPeriodWidget::addDetectPlanToLayout()
  547. {
  548. for(auto& pDetect : *m_pListcurrDetect)
  549. {
  550. pDetect->show();
  551. /* 添加到布局中 */
  552. m_layoutDetectPlans->insertWidget(m_layoutDetectPlans->count() - 1, pDetect);
  553. }
  554. }
  555. /* 从当前列表中添加非检测计划控件到布局 */
  556. void CheckPeriodWidget::addNoDetectPlanToLayout()
  557. {
  558. for(auto& pDetect : *m_pListcurrNoDetect)
  559. {
  560. pDetect->show();
  561. /* 添加到布局中 */
  562. m_layoutNoDetectPlans->insertWidget(m_layoutNoDetectPlans->count() - 1, pDetect);
  563. }
  564. }
  565. /* 更新所有对比项的计划周期 */
  566. void CheckPeriodWidget::updateCompareItemDetectPeriod(QMap<int, DetectPeriodConfig_t>& mapDetectConfig)
  567. {
  568. /* 先更新检测计划 */
  569. for(auto it = m_mapDetectPlanList.cbegin(); it != m_mapDetectPlanList.cend(); ++it)
  570. {
  571. int id = it.key();
  572. QList<OneDetectPlan*>* pList = it.value();
  573. DetectPeriodConfig_t config;
  574. config.nID = id;
  575. /* 更新检测计划 */
  576. for(auto& pDetect : *pList)
  577. {
  578. OnePlan_t plan = pDetect->getPlan();
  579. config.listDetect.append(plan);
  580. }
  581. auto apply = m_mapNoDetectApply.find(id);
  582. if(apply != m_mapNoDetectApply.cend())
  583. {
  584. config.isApplySlient = apply->isApplySlient;
  585. config.isApplyOverload = apply->isApplyOverload;
  586. config.isApplyPhase = apply->isApplyPhase;
  587. config.isApplyNoise = apply->isApplyNoise;
  588. } else
  589. {
  590. config.isApplySlient = false;
  591. config.isApplyOverload = false;
  592. config.isApplyPhase = false;
  593. config.isApplyNoise = false;
  594. }
  595. mapDetectConfig.insert(id, config);
  596. }
  597. /* 更新非检测计划 */
  598. for(auto it = m_mapNoDetectPlanList.cbegin(); it != m_mapNoDetectPlanList.cend(); ++it)
  599. {
  600. int id = it.key();
  601. QList<OneDetectPlan*>* pList = it.value();
  602. /* 查找的时候,如果没有,会自动插入一个key为id的值 */
  603. DetectPeriodConfig_t& config = mapDetectConfig[id];
  604. config.nID = id;
  605. /* 更新非检测计划 */
  606. for(auto& pDetect : *pList)
  607. {
  608. OnePlan_t plan = pDetect->getPlan();
  609. config.listNoDetect.append(plan);
  610. }
  611. /* 这里再查找一遍应用信息是因为防止没有检测计划,只有非检测计划 */
  612. auto apply = m_mapNoDetectApply.find(id);
  613. if(apply != m_mapNoDetectApply.cend())
  614. {
  615. config.isApplySlient = apply->isApplySlient;
  616. config.isApplyOverload = apply->isApplyOverload;
  617. config.isApplyPhase = apply->isApplyPhase;
  618. config.isApplyNoise = apply->isApplyNoise;
  619. } else
  620. {
  621. config.isApplySlient = false;
  622. config.isApplyOverload = false;
  623. config.isApplyPhase = false;
  624. config.isApplyNoise = false;
  625. }
  626. }
  627. }