checkperiodwidget.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  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. /* 判断参数是否修改了 */
  152. bool CheckPeriodWidget::isDataChanged()
  153. {
  154. /* 更新静音过载反相的设置 */
  155. m_mapNoDetectApply[m_currNoDetectApply.nID] = m_currNoDetectApply;
  156. /* 生成按照对比项相关的设置计划 */
  157. QMap<int, DetectPeriodConfig_t> mapDetectConfig;
  158. updateCompareItemDetectPeriod(mapDetectConfig);
  159. /* 和已有的对比,只有有修改的地方,直接删除这个对比项ID所有计划,然后插入 */
  160. const auto& oldMap = SysConfig.getDetectPeriodConfig();
  161. QMap<int, DetectPeriodConfig_t> insertMap;
  162. QList<int> deleteList;
  163. /* 先找出需要插入的 */
  164. for(auto it = mapDetectConfig.cbegin(); it != mapDetectConfig.cend(); ++it)
  165. {
  166. int id = it.key();
  167. if(oldMap.contains(id))
  168. {
  169. /* 如果有修改,就删除原来的 */
  170. if(oldMap[id] == it.value())
  171. {
  172. continue;
  173. }else
  174. {
  175. deleteList.append(id);
  176. }
  177. }
  178. /* 无论是没有还是修改了,都需要重新插入 */
  179. insertMap.insert(id, it.value());
  180. }
  181. if(deleteList.isEmpty() && insertMap.isEmpty())
  182. {
  183. return false;
  184. }
  185. return true;
  186. }
  187. /* 选择了一个对比项,id是新对比项的id */
  188. void CheckPeriodWidget::do_comboBox_selectCompareItem_currentIndexChanged(int index)
  189. {
  190. /* 获取当前选择的对比项id */
  191. int id = ui->comboBox_selectCompareItem->itemData(index).value<int>();
  192. if(id == 0)
  193. {
  194. m_pListcurrDetect = nullptr;
  195. SPDLOG_LOGGER_WARN(m_logger, "对比项ID为0,无法获取检测计划列表");
  196. return;
  197. }
  198. /* 如果已经有了,就不需要重新创建 */
  199. if(m_mapDetectPlanList.contains(id))
  200. {
  201. m_pListcurrDetect = m_mapDetectPlanList.value(id);
  202. }else
  203. {
  204. /* 创建一个新的列表 */
  205. m_pListcurrDetect = new QList<OneDetectPlan*>();
  206. m_mapDetectPlanList.insert(id, m_pListcurrDetect);
  207. }
  208. if(m_mapNoDetectPlanList.contains(id))
  209. {
  210. m_pListcurrNoDetect = m_mapNoDetectPlanList.value(id);
  211. } else
  212. {
  213. /* 创建一个新的列表 */
  214. m_pListcurrNoDetect = new QList<OneDetectPlan*>();
  215. m_mapNoDetectPlanList.insert(id, m_pListcurrNoDetect);
  216. }
  217. /* 应用项目需要先保存当前的设置到对应的对比项ID中,然后再切换 */
  218. m_currNoDetectApply.isApplySlient = ui->checkBox_applySlient->isChecked();
  219. m_currNoDetectApply.isApplyOverload = ui->checkBox_applyOverload->isChecked();
  220. m_currNoDetectApply.isApplyPhase = ui->checkBox_applyPhase->isChecked();
  221. m_currNoDetectApply.isApplyNoise = ui->checkBox_applyNoise->isChecked();
  222. /* 这里多次检测,是防止第一次进入这个页面,没有保存好的设置 */
  223. if(m_currNoDetectApply.nID > 0)
  224. {
  225. if(m_mapNoDetectApply.contains(m_currNoDetectApply.nID))
  226. {
  227. m_mapNoDetectApply[m_currNoDetectApply.nID] = m_currNoDetectApply;
  228. } else
  229. {
  230. m_mapNoDetectApply.insert(m_currNoDetectApply.nID, m_currNoDetectApply);
  231. }
  232. }
  233. if(m_mapNoDetectApply.contains(id))
  234. {
  235. m_currNoDetectApply = m_mapNoDetectApply.value(id);
  236. } else
  237. {
  238. /* 清空设置 */
  239. m_currNoDetectApply = NoDetectPlanApply_t();
  240. m_currNoDetectApply.nID = id;
  241. m_mapNoDetectApply.insert(id, m_currNoDetectApply);
  242. }
  243. ui->checkBox_applySlient->setChecked(m_currNoDetectApply.isApplySlient);
  244. ui->checkBox_applyOverload->setChecked(m_currNoDetectApply.isApplyOverload);
  245. ui->checkBox_applyPhase->setChecked(m_currNoDetectApply.isApplyPhase);
  246. ui->checkBox_applyNoise->setChecked(m_currNoDetectApply.isApplyNoise);
  247. /* 清空当前的布局 */
  248. removeAllDetectPlanWidgets();
  249. removeAllNoDetectPlanWidgets();
  250. addDetectPlanToLayout();
  251. addNoDetectPlanToLayout();
  252. }
  253. /* 添加一个检测计划 */
  254. void CheckPeriodWidget::do_pBtn_addDetectPlan_clicked()
  255. {
  256. /* 检查当前有没有对比项选择 */
  257. int id = ui->comboBox_selectCompareItem->currentData().value<int>();
  258. if(id <= 0)
  259. {
  260. TipWidget::display(TipWidget::OPERATOR_WARN, "请先选择一个对比项", GInfo.getTopWindow());
  261. SPDLOG_LOGGER_WARN(m_logger, "没有选择对比项,无法添加检测计划");
  262. return;
  263. }
  264. AddPeriodDialog dlg(PERIOD_WEEK);
  265. /* 更新计划列表 */
  266. updateDetectPlanList(1);
  267. /* 设置计划列表 */
  268. dlg.setPlanList(m_currentPlanList);
  269. dlg.exec();
  270. if(!dlg.isOK())
  271. {
  272. return;
  273. }
  274. OnePlan_t plan = dlg.getPlan();
  275. addDetectPlan(plan);
  276. }
  277. /* 添加一个不检测计划 */
  278. void CheckPeriodWidget::do_pBtn_addNoDetectPlan_clicked()
  279. {
  280. /* 检查当前有没有对比项选择 */
  281. int id = ui->comboBox_selectCompareItem->currentData().value<int>();
  282. if(id <= 0)
  283. {
  284. TipWidget::display(TipWidget::OPERATOR_WARN, "请先选择一个对比项", GInfo.getTopWindow());
  285. SPDLOG_LOGGER_WARN(m_logger, "没有选择对比项,无法添加检测计划");
  286. return;
  287. }
  288. AddPeriodDialog dlg(PERIOD_DATE);
  289. dlg.exec();
  290. if(!dlg.isOK())
  291. {
  292. return;
  293. }
  294. OnePlan_t plan = dlg.getPlan();
  295. addNoDetectPlan(plan);
  296. }
  297. /* 删除一个检测计划 */
  298. void CheckPeriodWidget::do_pBtn_deleteDetectPlan_clicked()
  299. {
  300. /* 获取信号发送者 */
  301. OneDetectPlan *pDetect = qobject_cast<OneDetectPlan*>(sender());
  302. if(pDetect == nullptr)
  303. {
  304. return;
  305. }
  306. m_layoutDetectPlans->removeWidget(pDetect);
  307. m_pListcurrDetect->removeAll(pDetect);
  308. delete pDetect;
  309. /* 重新排序 */
  310. // sortDetectPlanList();
  311. }
  312. /* 删除一个不检测计划 */
  313. void CheckPeriodWidget::do_pBtn_deleteNoDetectPlan_clicked()
  314. {
  315. /* 获取信号发送者 */
  316. OneDetectPlan *pDetect = qobject_cast<OneDetectPlan*>(sender());
  317. if(pDetect == nullptr)
  318. {
  319. return;
  320. }
  321. m_layoutNoDetectPlans->removeWidget(pDetect);
  322. m_pListcurrNoDetect->removeAll(pDetect);
  323. delete pDetect;
  324. /* 重新排序 */
  325. // sortNoDetectPlanList();
  326. }
  327. /* 修改了日期或周几,检测是否冲突 */
  328. void CheckPeriodWidget::do_detectPlanModifiedWeek(OnePlan_t formerPlan, OnePlan_t newPlan)
  329. {
  330. OneDetectPlan *pDetect = qobject_cast<OneDetectPlan*>(sender());
  331. /* 更新计划 */
  332. updateDetectPlanList(1);
  333. /* 检测是否冲突 */
  334. if(isWeekPlanDuplicate(m_currentPlanList, newPlan, newPlan))
  335. {
  336. /* 弹出提示 */
  337. TipWidget::display(TipWidget::OPERATOR_WARN, "与已有计划冲突,请重新修改", GInfo.getTopWindow());
  338. SPDLOG_LOGGER_WARN(m_logger, "检测计划冲突");
  339. /* 恢复原计划 */
  340. pDetect->setPlan(formerPlan);
  341. return;
  342. }
  343. /* 重新排序 */
  344. sortDetectPlanList();
  345. }
  346. /* 点击了检测计划的时间按钮,在这里修改时间 */
  347. void CheckPeriodWidget::do_detectPlanModifiedTime(QPoint pBtnSize, bool isStartTime)
  348. {
  349. /* 获取信号发送者 */
  350. auto one = qobject_cast<OneDetectPlan*>(sender());
  351. OnePlan_t plan = one->getPlan();
  352. /* 创建时间选择控件 */
  353. std::shared_ptr<TimeWidget> tw = std::make_shared<TimeWidget>(this, TimeWidget::ShowType::Dialog);
  354. /* 设置样式表 */
  355. tw->setStyleSheet(m_qssPlan);
  356. /* 设置图标 */
  357. tw->setIcon(":/icon/time.png");
  358. tw->setIconShow(true);
  359. tw->setIconSize(16, 16);
  360. /* 重新设置大小 */
  361. tw->setEditLine(120, 32);
  362. /* 设置选择框大小 */
  363. tw->setTimeAreaWidth(120);
  364. /* 移动位置,覆盖显示时间的按钮,获取的坐标是相对于Dialog的位置 */
  365. auto pos = this->mapFromGlobal(pBtnSize);
  366. // pos.setX(pos.x() - 1); /* 去掉阴影的宽度 */
  367. // pos.setY(pos.y()); /* 去掉阴影的高度 */
  368. tw->move(pos);
  369. // SPDLOG_LOGGER_DEBUG(m_logger, "移动前位置: {}, {}", pBtnSize.x(), pBtnSize.y());
  370. // SPDLOG_LOGGER_DEBUG(m_logger, "移动后位置: {}, {}", pos.x(), pos.y());
  371. /* 设置默认的时间 */
  372. if(isStartTime)
  373. {
  374. tw->setTime(plan.timeStart);
  375. } else
  376. {
  377. tw->setTime(plan.timeEnd);
  378. }
  379. tw->execShow();
  380. auto time = tw->getTime();
  381. /* 判断时间有没有修改 */
  382. if(isStartTime)
  383. {
  384. if(time == plan.timeStart)
  385. {
  386. return; // 没有修改
  387. }
  388. } else
  389. {
  390. if(time == plan.timeEnd)
  391. {
  392. return; // 没有修改
  393. }
  394. }
  395. SPDLOG_LOGGER_DEBUG(m_logger, "修改时间: {}, {}", time.toString("hh:mm:ss").toStdString(), isStartTime ? "开始时间" : "结束时间");
  396. /* 先判断结束时间有没有晚于结束时间 */
  397. if(isStartTime)
  398. {
  399. if(time >= plan.timeEnd)
  400. {
  401. TipWidget::display(TipWidget::OPERATOR_WARN, "结束时间不能早于等于开始时间", GInfo.getTopWindow());
  402. SPDLOG_LOGGER_WARN(m_logger, "结束时间不能早于等于开始时间");
  403. return;
  404. }
  405. }else {
  406. if((time <= plan.timeStart) && (time != QTime(0,0,0)))
  407. {
  408. TipWidget::display(TipWidget::OPERATOR_WARN, "结束时间不能早于等于开始时间", GInfo.getTopWindow());
  409. SPDLOG_LOGGER_WARN(m_logger, "结束时间不能早于等于开始时间");
  410. return;
  411. }
  412. }
  413. OnePlan_t newPlan = plan;
  414. if(isStartTime)
  415. {
  416. newPlan.timeStart = time;
  417. } else
  418. {
  419. newPlan.timeEnd = time;
  420. }
  421. /* 判断时间是否重复 */
  422. updateDetectPlanList(1);
  423. if(isWeekPlanDuplicate(m_currentPlanList, newPlan, plan))
  424. {
  425. /* 设置时间报警 */
  426. TipWidget::display(TipWidget::OPERATOR_WARN, "与已有计划冲突,请重新修改", GInfo.getTopWindow());
  427. SPDLOG_LOGGER_WARN(m_logger, "检测计划冲突");
  428. return;
  429. }
  430. /* 设置时间 */
  431. one->setPlan(newPlan);
  432. /* 重新排序 */
  433. sortDetectPlanList();
  434. }
  435. /* 修改了静音、反相、过载相关的设置 */
  436. void CheckPeriodWidget::do_checkBox_clicked()
  437. {
  438. m_currNoDetectApply.isApplySlient = ui->checkBox_applySlient->isChecked();
  439. m_currNoDetectApply.isApplyOverload = ui->checkBox_applyOverload->isChecked();
  440. m_currNoDetectApply.isApplyPhase = ui->checkBox_applyPhase->isChecked();
  441. m_currNoDetectApply.isApplyNoise = ui->checkBox_applyNoise->isChecked();
  442. }
  443. /* 恢复配置项 */
  444. void CheckPeriodWidget::do_pBtn_restore_clicked()
  445. {
  446. initListWidget();
  447. }
  448. /* 初始化QListWidget */
  449. void CheckPeriodWidget::initListWidget()
  450. {
  451. /* 获取读取到的检测计划 */
  452. const auto& mapDetectConfig = SysConfig.getDetectPeriodConfig();
  453. /* 将计划转换成列表组件 */
  454. for(auto it = mapDetectConfig.cbegin(); it != mapDetectConfig.cend(); ++it)
  455. {
  456. int id = it.key();
  457. const DetectPeriodConfig_t& config = it.value();
  458. /* 如果已经有了,就不需要重新创建 */
  459. if(m_mapDetectPlanList.contains(id))
  460. {
  461. m_pListcurrDetect = m_mapDetectPlanList.value(id);
  462. }else
  463. {
  464. /* 创建一个新的列表 */
  465. m_pListcurrDetect = new QList<OneDetectPlan*>();
  466. m_mapDetectPlanList.insert(id, m_pListcurrDetect);
  467. }
  468. if(m_mapNoDetectPlanList.contains(id))
  469. {
  470. m_pListcurrNoDetect = m_mapNoDetectPlanList.value(id);
  471. } else
  472. {
  473. /* 创建一个新的列表 */
  474. m_pListcurrNoDetect = new QList<OneDetectPlan*>();
  475. m_mapNoDetectPlanList.insert(id, m_pListcurrNoDetect);
  476. }
  477. /* 添加检测计划 */
  478. for(const auto& plan : config.listDetect)
  479. {
  480. addDetectPlan(plan);
  481. }
  482. /* 添加非检测计划 */
  483. for(const auto& plan : config.listNoDetect)
  484. {
  485. addNoDetectPlan(plan);
  486. }
  487. /* 设置应用配置 */
  488. NoDetectPlanApply_t apply;
  489. apply.nID = id;
  490. apply.isApplySlient = config.isApplySlient;
  491. apply.isApplyOverload = config.isApplyOverload;
  492. apply.isApplyPhase = config.isApplyPhase;
  493. apply.isApplyNoise = config.isApplyNoise;
  494. m_mapNoDetectApply[id] = apply;
  495. }
  496. }
  497. /* 添加一个检测计划 */
  498. void CheckPeriodWidget::addDetectPlan(const OnePlan_t& plan)
  499. {
  500. /* 传入的日期类型,不是特殊日就可以 */
  501. OneDetectPlan *detectPlan = new OneDetectPlan(eWeekType::Week_Monday, this);
  502. /* 删除日期选择框 */
  503. detectPlan->setQSS(m_qssPlan);
  504. detectPlan->setPlan(plan);
  505. connect(detectPlan, &OneDetectPlan::signal_oneDetectPlanCloseClicked, this, &CheckPeriodWidget::do_pBtn_deleteDetectPlan_clicked);
  506. connect(detectPlan, &OneDetectPlan::signal_planModifiedWeek, this, &CheckPeriodWidget::do_detectPlanModifiedWeek);
  507. connect(detectPlan, &OneDetectPlan::signal_timeButtonClicked, this, &CheckPeriodWidget::do_detectPlanModifiedTime);
  508. /* 将数据插入到列表中,在排序的时候会将新增加的插入到里面 */
  509. m_pListcurrDetect->append(detectPlan);
  510. /* 排序 */
  511. sortDetectPlanList();
  512. }
  513. /* 添加一个非检测计划 */
  514. void CheckPeriodWidget::addNoDetectPlan(const OnePlan_t& plan)
  515. {
  516. /* 传入特殊日的日期类型 */
  517. OneDetectPlan *detectPlan = new OneDetectPlan(eWeekType::Week_Special, this);
  518. detectPlan->setQSS(m_qssPlan);
  519. detectPlan->setPlan(plan);
  520. connect(detectPlan, &OneDetectPlan::signal_oneDetectPlanCloseClicked, this, &CheckPeriodWidget::do_pBtn_deleteNoDetectPlan_clicked);
  521. m_pListcurrNoDetect->append(detectPlan);
  522. /* 排序并显示 */
  523. sortNoDetectPlanList();
  524. }
  525. /* 重新排序检测计划 */
  526. void CheckPeriodWidget::sortDetectPlanList()
  527. {
  528. /* 清空列表 */
  529. removeAllDetectPlanWidgets();
  530. /* 排序 */
  531. std::sort(m_pListcurrDetect->begin(), m_pListcurrDetect->end(), [](OneDetectPlan *a, OneDetectPlan *b)
  532. {
  533. const OnePlan_t &planA = a->getPlan();
  534. const OnePlan_t &planB = b->getPlan();
  535. /* 按时间从小到大升序 */
  536. return weekTimeIsGerater(planB.weekType, planB.timeStart, planA.weekType, planA.timeStart);
  537. });
  538. /* 重新插入 */
  539. addDetectPlanToLayout();
  540. update();
  541. }
  542. /* 排序非检测日期列表 */
  543. void CheckPeriodWidget::sortNoDetectPlanList()
  544. {
  545. /* 清空列表 */
  546. removeAllNoDetectPlanWidgets();
  547. /* 排序 */
  548. std::sort(m_pListcurrDetect->begin(), m_pListcurrDetect->end(), [](OneDetectPlan *a, OneDetectPlan *b)
  549. {
  550. const OnePlan_t &planA = a->getPlan();
  551. const OnePlan_t &planB = b->getPlan();
  552. /* 按时间从小到大升序 */
  553. return dateTimeIsGerater(planB.date, planB.timeStart, planA.date, planA.timeStart);
  554. });
  555. /* 重新插入 */
  556. addNoDetectPlanToLayout();
  557. update();
  558. }
  559. /* 获取计划列表 */
  560. QList<OnePlan_t> CheckPeriodWidget::updateDetectPlanList(int id)
  561. {
  562. m_currentPlanList.clear();
  563. for(auto& pDetect : *m_pListcurrDetect)
  564. {
  565. OnePlan_t plan = pDetect->getPlan();
  566. m_currentPlanList.append(plan);
  567. }
  568. return m_currentPlanList;
  569. }
  570. /* 清空当前检测计划布局中的控件 */
  571. void CheckPeriodWidget::removeAllDetectPlanWidgets()
  572. {
  573. /* 清空布局中的所有控件,只剩一个弹簧 */
  574. while (m_layoutDetectPlans->count() > 1)
  575. {
  576. QWidget *widget = m_layoutDetectPlans->itemAt(0)->widget();
  577. if(widget)
  578. {
  579. m_layoutDetectPlans->removeWidget(widget);
  580. widget->hide();
  581. }
  582. }
  583. }
  584. /* 清空当前非检测计划布局中的控件 */
  585. void CheckPeriodWidget::removeAllNoDetectPlanWidgets()
  586. {
  587. /* 清空所有的非检测计划日期 */
  588. while( m_layoutNoDetectPlans->count() > 1)
  589. {
  590. QWidget *widget = m_layoutNoDetectPlans->itemAt(0)->widget();
  591. if(widget)
  592. {
  593. m_layoutNoDetectPlans->removeWidget(widget);
  594. widget->hide();
  595. }
  596. }
  597. }
  598. /* 从当前列表中添加控件到布局 */
  599. void CheckPeriodWidget::addDetectPlanToLayout()
  600. {
  601. for(auto& pDetect : *m_pListcurrDetect)
  602. {
  603. pDetect->show();
  604. /* 添加到布局中 */
  605. m_layoutDetectPlans->insertWidget(m_layoutDetectPlans->count() - 1, pDetect);
  606. }
  607. }
  608. /* 从当前列表中添加非检测计划控件到布局 */
  609. void CheckPeriodWidget::addNoDetectPlanToLayout()
  610. {
  611. for(auto& pDetect : *m_pListcurrNoDetect)
  612. {
  613. pDetect->show();
  614. /* 添加到布局中 */
  615. m_layoutNoDetectPlans->insertWidget(m_layoutNoDetectPlans->count() - 1, pDetect);
  616. }
  617. }
  618. /* 更新所有对比项的计划周期 */
  619. void CheckPeriodWidget::updateCompareItemDetectPeriod(QMap<int, DetectPeriodConfig_t>& mapDetectConfig)
  620. {
  621. /* 先更新检测计划 */
  622. for(auto it = m_mapDetectPlanList.cbegin(); it != m_mapDetectPlanList.cend(); ++it)
  623. {
  624. int id = it.key();
  625. QList<OneDetectPlan*>* pList = it.value();
  626. DetectPeriodConfig_t config;
  627. config.nID = id;
  628. /* 更新检测计划 */
  629. for(auto& pDetect : *pList)
  630. {
  631. OnePlan_t plan = pDetect->getPlan();
  632. config.listDetect.append(plan);
  633. }
  634. auto apply = m_mapNoDetectApply.find(id);
  635. if(apply != m_mapNoDetectApply.cend())
  636. {
  637. config.isApplySlient = apply->isApplySlient;
  638. config.isApplyOverload = apply->isApplyOverload;
  639. config.isApplyPhase = apply->isApplyPhase;
  640. config.isApplyNoise = apply->isApplyNoise;
  641. } else
  642. {
  643. config.isApplySlient = false;
  644. config.isApplyOverload = false;
  645. config.isApplyPhase = false;
  646. config.isApplyNoise = false;
  647. }
  648. mapDetectConfig.insert(id, config);
  649. }
  650. /* 更新非检测计划 */
  651. for(auto it = m_mapNoDetectPlanList.cbegin(); it != m_mapNoDetectPlanList.cend(); ++it)
  652. {
  653. int id = it.key();
  654. QList<OneDetectPlan*>* pList = it.value();
  655. /* 查找的时候,如果没有,会自动插入一个key为id的值 */
  656. DetectPeriodConfig_t& config = mapDetectConfig[id];
  657. config.nID = id;
  658. /* 更新非检测计划 */
  659. for(auto& pDetect : *pList)
  660. {
  661. OnePlan_t plan = pDetect->getPlan();
  662. config.listNoDetect.append(plan);
  663. }
  664. /* 这里再查找一遍应用信息是因为防止没有检测计划,只有非检测计划 */
  665. auto apply = m_mapNoDetectApply.find(id);
  666. if(apply != m_mapNoDetectApply.cend())
  667. {
  668. config.isApplySlient = apply->isApplySlient;
  669. config.isApplyOverload = apply->isApplyOverload;
  670. config.isApplyPhase = apply->isApplyPhase;
  671. config.isApplyNoise = apply->isApplyNoise;
  672. } else
  673. {
  674. config.isApplySlient = false;
  675. config.isApplyOverload = false;
  676. config.isApplyPhase = false;
  677. config.isApplyNoise = false;
  678. }
  679. }
  680. }