#include "checkperiodwidget.h" #include "GlobalVariable.h" #include "checkperiodfunc.h" #include "onedetectplan.h" #include "ui_checkperiodwidget.h" #include "addperioddialog.h" #include "UIStyleManager.h" #include "customcombobox.h" #include #include #include #include #include #include "checkperiodfunc.h" #include "tipwidget.h" #include "GlobalInfo.h" #include "timewidget.h" CheckPeriodWidget::CheckPeriodWidget(QWidget *parent) : QWidget(parent), ui(new Ui::CheckPeriodWidget) { ui->setupUi(this); m_logger = spdlog::get("ACASetting"); if(m_logger == nullptr) { fmt::print("CheckPeriodWidget: Logger ACASetting not found\n"); return; } /* 下拉框设置阴影 */ ui->comboBox_selectCompareItem->setViewShadowEffect(); /* 初始化检测计划容器 */ m_layoutDetectPlans = qobject_cast(ui->scrollArea_detectPlans->layout()); connect(ui->pBtn_addDetectPlan, &QPushButton::clicked, this, &CheckPeriodWidget::do_pBtn_addDetectPlan_clicked); connect(ui->pBtn_addNoDetectPlan, &QPushButton::clicked, this, &CheckPeriodWidget::do_pBtn_addNoDetectPlan_clicked); connect(ui->comboBox_selectCompareItem, QOverload::of(&QComboBox::currentIndexChanged), this, &CheckPeriodWidget::do_comboBox_selectCompareItem_currentIndexChanged); /* 设置UI */ UIStyle.registerWidget(this); QString qssPath = UIStyle.getQSSPath() + "/onedetectplan.qss"; QFile file(qssPath); if(file.open(QFile::ReadOnly)) { m_qssPlan = file.readAll(); file.close(); } else { SPDLOG_LOGGER_WARN(m_logger, "打开QSS文件失败: {}", qssPath.toStdString()); } /* 初始化列表 */ initListWidget(); } CheckPeriodWidget::~CheckPeriodWidget() { UIStyle.unregisterWidget(this); delete ui; } /* 更新可选的对比项列表 */ void CheckPeriodWidget::updateCompareItemList(const QList& compareItemList) { /* 先获取当前选择的列表项 */ int id = ui->comboBox_selectCompareItem->currentData().value(); ui->comboBox_selectCompareItem->clear(); m_pListcurrDetect = nullptr; /* 屏蔽comboBox信号 */ ui->comboBox_selectCompareItem->blockSignals(true); for(const auto& item : compareItemList) { /* 添加到下拉框 */ ui->comboBox_selectCompareItem->addItem(item.strName, QVariant::fromValue(item.nID)); } /* 设置当前选择的对比项 */ if(id != 0) { /* 屏蔽comboBox信号 */ ui->comboBox_selectCompareItem->blockSignals(true); for(int i = 0; i < ui->comboBox_selectCompareItem->count(); ++i) { if(ui->comboBox_selectCompareItem->itemData(i).value() == id) { ui->comboBox_selectCompareItem->setCurrentIndex(i); break; } } } else { ui->comboBox_selectCompareItem->setCurrentIndex(0); } /* 恢复comboBox信号 */ ui->comboBox_selectCompareItem->blockSignals(false); /* 手动切换当前的列表 */ do_comboBox_selectCompareItem_currentIndexChanged(ui->comboBox_selectCompareItem->currentIndex()); } /* 设置计划列表 */ void CheckPeriodWidget::setDetectPlanList(const QList& planList) { m_currentPlanList = planList; removeAllDetectPlanWidgets(); for(const auto& plan : m_currentPlanList) { addDetectPlan(plan); } } /* 选择了一个对比项 */ void CheckPeriodWidget::do_comboBox_selectCompareItem_currentIndexChanged(int index) { /* 获取当前选择的对比项id */ int id = ui->comboBox_selectCompareItem->currentData().value(); if(id == 0) { m_pListcurrDetect = nullptr; SPDLOG_LOGGER_WARN(m_logger, "对比项ID为0,无法获取检测计划列表"); return; } /* 如果已经有了,就不需要重新创建 */ if(m_mapDetectPlanList.contains(id)) { m_pListcurrDetect = m_mapDetectPlanList.value(id); }else { /* 创建一个新的列表 */ m_pListcurrDetect = new QList(); m_mapDetectPlanList.insert(id, m_pListcurrDetect); } /* 清空当前的布局 */ removeAllDetectPlanWidgets(); addDetectPlanToLayout(); } /* 添加一个检测计划 */ void CheckPeriodWidget::do_pBtn_addDetectPlan_clicked() { /* 检查当前有没有对比项选择 */ int id = ui->comboBox_selectCompareItem->currentData().value(); if(id <= 0) { TipWidget::display(TipWidget::OPERATOR_WARN, "请先选择一个对比项", GInfo.getTopWindow()); SPDLOG_LOGGER_WARN(m_logger, "没有选择对比项,无法添加检测计划"); return; } AddPeriodDialog dlg(PERIOD_WEEK); /* 更新计划列表 */ updateDetectPlanList(1); /* 设置计划列表 */ dlg.setPlanList(m_currentPlanList); dlg.exec(); if(!dlg.isOK()) { return; } OnePlan_t plan = dlg.getPlan(); addDetectPlan(plan); } /* 添加一个不检测计划 */ void CheckPeriodWidget::do_pBtn_addNoDetectPlan_clicked() { /* 检查当前有没有对比项选择 */ int id = ui->comboBox_selectCompareItem->currentData().value(); // if(id <= 0) // { // TipWidget::display(TipWidget::OPERATOR_WARN, "请先选择一个对比项", GInfo.getTopWindow()); // SPDLOG_LOGGER_WARN(m_logger, "没有选择对比项,无法添加检测计划"); // return; // } AddPeriodDialog dlg(PERIOD_DATE); dlg.exec(); } /* 删除一个检测计划 */ void CheckPeriodWidget::do_pBtn_deleteDetectPlan_clicked() { /* 获取信号发送者 */ OneDetectPlan *pDetect = qobject_cast(sender()); if(pDetect == nullptr) { return; } m_layoutDetectPlans->removeWidget(pDetect); m_pListcurrDetect->removeAll(pDetect); delete pDetect; /* 重新排序 */ // sortDetectPlanList(); } /* 删除一个不检测计划 */ void CheckPeriodWidget::do_pBtn_deleteNoDetectPlan_clicked() { } /* 修改了日期或周几,检测是否冲突 */ void CheckPeriodWidget::do_detectPlanModifiedWeek(OnePlan_t formerPlan, OnePlan_t newPlan) { OneDetectPlan *pDetect = qobject_cast(sender()); /* 更新计划 */ updateDetectPlanList(1); /* 检测是否冲突 */ if(isWeekPlanDuplicate(m_currentPlanList, newPlan, newPlan)) { /* 弹出提示 */ TipWidget::display(TipWidget::OPERATOR_WARN, "与已有计划冲突,请重新修改", GInfo.getTopWindow()); SPDLOG_LOGGER_WARN(m_logger, "检测计划冲突"); /* 恢复原计划 */ pDetect->setPlan(formerPlan); return; } /* 重新排序 */ sortDetectPlanList(); } /* 点击了检测计划的时间按钮,在这里修改时间 */ void CheckPeriodWidget::do_detectPlanModifiedTime(QPoint pBtnSize, bool isStartTime) { /* 获取信号发送者 */ auto one = qobject_cast(sender()); OnePlan_t plan = one->getPlan(); /* 创建时间选择控件 */ std::shared_ptr tw = std::make_shared(this, TimeWidget::ShowType::Dialog); /* 设置样式表 */ tw->setStyleSheet(m_qssPlan); /* 设置图标 */ tw->setIcon(":/icon/time.png"); tw->setIconShow(true); tw->setIconSize(16, 16); /* 重新设置大小 */ tw->setEditLine(112, 32); /* 设置选择框大小 */ tw->setTimeAreaWidth(140); /* 移动位置,覆盖显示时间的按钮,获取的坐标是相对于Dialog的位置 */ auto pos = this->mapFromGlobal(pBtnSize); // pos.setX(pos.x() - 1); /* 去掉阴影的宽度 */ // pos.setY(pos.y()); /* 去掉阴影的高度 */ tw->move(pos); // SPDLOG_LOGGER_DEBUG(m_logger, "移动前位置: {}, {}", pBtnSize.x(), pBtnSize.y()); // SPDLOG_LOGGER_DEBUG(m_logger, "移动后位置: {}, {}", pos.x(), pos.y()); /* 设置默认的时间 */ if(isStartTime) { tw->setTime(plan.timeStart); } else { tw->setTime(plan.timeEnd); } tw->execShow(); auto time = tw->getTime(); /* 判断时间有没有修改 */ if(isStartTime) { if(time == plan.timeStart) { return; // 没有修改 } } else { if(time == plan.timeEnd) { return; // 没有修改 } } SPDLOG_LOGGER_DEBUG(m_logger, "修改时间: {}, {}", time.toString("hh:mm:ss").toStdString(), isStartTime ? "开始时间" : "结束时间"); OnePlan_t newPlan = plan; if(isStartTime) { newPlan.timeStart = time; } else { newPlan.timeEnd = time; } /* 判断时间是否重复 */ updateDetectPlanList(1); if(isWeekPlanDuplicate(m_currentPlanList, newPlan, plan)) { /* 设置时间报警 */ TipWidget::display(TipWidget::OPERATOR_WARN, "与已有计划冲突,请重新修改", GInfo.getTopWindow()); SPDLOG_LOGGER_WARN(m_logger, "检测计划冲突"); return; } /* 设置时间 */ one->setPlan(newPlan); /* 重新排序 */ sortDetectPlanList(); } /* 初始化QListWidget */ void CheckPeriodWidget::initListWidget() { ui->listWidget_noDetectPlan->clear(); /* 禁用横向滚动条 */ /* 设置行间距为16 */ } /* 添加一个检测计划 */ void CheckPeriodWidget::addDetectPlan(const OnePlan_t& plan) { OneDetectPlan *detectPlan = new OneDetectPlan(eWeekType::Week_Monday, this); /* 删除日期选择框 */ detectPlan->setQSS(m_qssPlan); detectPlan->setPlan(plan); connect(detectPlan, &OneDetectPlan::signal_oneDetectPlanCloseClicked, this, &CheckPeriodWidget::do_pBtn_deleteDetectPlan_clicked); connect(detectPlan, &OneDetectPlan::signal_planModifiedWeek, this, &CheckPeriodWidget::do_detectPlanModifiedWeek); connect(detectPlan, &OneDetectPlan::signal_timeButtonClicked, this, &CheckPeriodWidget::do_detectPlanModifiedTime); /* 将数据插入到列表中,在排序的时候会将新增加的插入到里面 */ m_pListcurrDetect->append(detectPlan); /* 排序 */ sortDetectPlanList(); } /* 重新排序检测计划 */ void CheckPeriodWidget::sortDetectPlanList() { /* 清空列表 */ removeAllDetectPlanWidgets(); /* 排序 */ std::sort(m_pListcurrDetect->begin(), m_pListcurrDetect->end(), [](OneDetectPlan *a, OneDetectPlan *b) { const OnePlan_t &planA = a->getPlan(); const OnePlan_t &planB = b->getPlan(); /* 按时间从小到大升序 */ return weekTimeIsGerater(planB.weekType, planB.timeStart, planA.weekType, planA.timeStart); }); /* 重新插入 */ addDetectPlanToLayout(); } /* 获取计划列表 */ QList CheckPeriodWidget::updateDetectPlanList(int id) { m_currentPlanList.clear(); for(auto& pDetect : *m_pListcurrDetect) { OnePlan_t plan = pDetect->getPlan(); m_currentPlanList.append(plan); } return m_currentPlanList; } /* 清空当前检测计划布局中的控件 */ void CheckPeriodWidget::removeAllDetectPlanWidgets() { /* 清空布局中的所有控件,只剩一个弹簧 */ // for(auto& pDetect : *m_pListcurrDetect) // { // m_layoutDetectPlans->removeWidget(pDetect); // } while (m_layoutDetectPlans->count() > 1) { QWidget *widget = m_layoutDetectPlans->itemAt(0)->widget(); if(widget) { m_layoutDetectPlans->removeWidget(widget); widget->hide(); } } update(); } /* 从当前列表中添加控件到布局 */ void CheckPeriodWidget::addDetectPlanToLayout() { for(auto& pDetect : *m_pListcurrDetect) { pDetect->show(); /* 添加到布局中 */ m_layoutDetectPlans->insertWidget(m_layoutDetectPlans->count() - 1, pDetect); } }