#include "addspecialitem.h" #include "ui_addspecialitem.h" #include #include #include #include #include #include #include "common/combobox/customcombobox.h" #include "LHQLogAPI.h" #include "transmitterswitchinfo.h" #include "common/SelectTime/timewidget.h" #include "common/date/calendardtedit.h" #include "ItemData.h" AddSpecialItem::AddSpecialItem(QWidget *parent) : QDialog(parent), ui(new Ui::AddSpecialItem) { ui->setupUi(this); /* 设置无边框 */ setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint); /* 设置底层样式表,让最底层的透明 */ this->setAttribute(Qt::WA_TranslucentBackground); /* 加载QSS */ QFile file(":/QSS/QSS/AddSpecialItem_Light.qss"); if(file.open(QIODevice::ReadOnly)) { QString stylesheet = file.readAll(); this->setStyleSheet(stylesheet); file.close(); } else { LH_WRITE_ERROR(QString("打开文件失败:%1").arg(file.fileName())); } /* 设置comboBox阴影 */ ui->comBox_devSelect->setViewShadowEffect(); ui->comBox_actionSelect->setViewShadowEffect(); ui->label_timeWarn->hide(); ui->label_devWarn->hide(); ui->label_actionWarn->hide(); /* 获取屏幕大小 */ m_rectScreen = QApplication::desktop()->availableGeometry(); this->resize(m_rectScreen.width(), m_rectScreen.height()); /* 设置默认时间 */ m_time.setHMS(0, 0, 0); m_date = QDate::currentDate(); /* 连接信号和槽 */ connect(ui->pBtn_Close, &QPushButton::clicked, this, &QDialog::close); connect(ui->pBtn_cancel, &QPushButton::clicked, this, &QDialog::close); connect(ui->pBtn_ok, &QPushButton::clicked, this, &AddSpecialItem::do_ok); connect(ui->pBtn_cancel, &QPushButton::clicked, this, &AddSpecialItem::close); /* 设备选择 */ connect(ui->comBox_devSelect, QOverload::of(&QComboBox::currentTextChanged), this, &AddSpecialItem::do_selectDev); /* 动作选择 */ connect(ui->comBox_actionSelect,QOverload::of(&QComboBox::currentTextChanged),this, &AddSpecialItem::do_selectAction); /* 打开时间选择器 */ connect(ui->pBtn_selectTime, &QPushButton::clicked, this, &AddSpecialItem::do_selectTime); /* 设置事件过滤器 */ ui->comBox_actionSelect->installEventFilter(this); ui->comBox_devSelect->installEventFilter(this); } AddSpecialItem::~AddSpecialItem() { delete ui; } /* 设置父指针,时间选择器需要使用 */ void AddSpecialItem::setParentPointer(QWidget* p) { m_parent = p; // m_parent->installEventFilter(this); } /* 添加可选设备 */ void AddSpecialItem::setDevice(QMap& mapDev) { ui->comBox_devSelect->clear(); for(const auto& it : mapDev) { ui->comBox_devSelect->addItem(it.devName); } /* 设置显示第一个设备,并设置可选的动作 */ ui->comBox_devSelect->setCurrentIndex(0); m_devName = ui->comBox_devSelect->currentText(); setAction(m_devName); } /* 设置周几 */ void AddSpecialItem::setWeekDay(int week) { if(week < 0 || week > 6) { return; } m_week = week; } /* 进行查重和关闭页面 */ void AddSpecialItem::do_ok() { ui->label_timeWarn->hide(); ui->label_devWarn->hide(); ui->label_actionWarn->hide(); setComboBoxWarning(ui->comBox_devSelect,false); setComboBoxWarning(ui->comBox_actionSelect, false); setTimeEditWarning(false); /* 检查设备是否为空 */ if(ui->comBox_devSelect->currentText().isEmpty()) { ui->label_devWarn->setText("不能为空!"); ui->label_devWarn->show(); setComboBoxWarning(ui->comBox_devSelect, true); return; } /* 检查动作是否为空 */ if(ui->comBox_actionSelect->currentText().isEmpty()) { ui->label_actionWarn->setText("不能为空!"); ui->label_actionWarn->show(); setComboBoxWarning(ui->comBox_actionSelect, true); } /* 检查时间是否为空 */ if(m_time.isNull()) { ui->label_timeWarn->setText("不能为空!"); ui->label_timeWarn->show(); setTimeEditWarning(true); return; } /* 进行设备查重 */ // bool ret = m_p->judgeTimeRepetition(*m_p->m_vecItem[m_p->m_stack->currentIndex()],m_devName,m_time); bool ret = IData.judgeTimeRepetitionWithAdd(m_week, m_devName, m_time); if(ret) { ui->label_timeWarn->setText("一个时间点,单个设备仅能执行一个操作!"); ui->label_timeWarn->show(); setTimeEditWarning(true); return; } m_isAddDev = true; /* 添加一项计划 */ LH_WRITE_LOG_DEBUG(QString("添加一项计划: 设备:%1, 动作:%2, 时间:%3").arg(m_devName).arg(m_action).arg(m_time.toString("hh:mm:ss"))); /* 发送信号 */ emit signal_AddSpecialItem(m_devName, m_action, m_date, m_time); close(); } /* 选择了设备,设置其对应的动作 */ void AddSpecialItem::do_selectDev() { m_devName = ui->comBox_devSelect->currentText(); setAction(m_devName); } /* 选择了动作 */ void AddSpecialItem::do_selectAction() { m_action = ui->comBox_actionSelect->currentText(); } /* 点击了时间选择按钮,打开时间选择器 */ void AddSpecialItem::do_selectTime() { // LH_WRITE_LOG_DEBUG("选择时间"); std::shared_ptr tw = std::make_shared(this, TimeWidget::ShowType::Dialog); /* 设置图标 */ tw->setIcon(":/ICON/ICON/Time.png"); tw->setIconShow(true); tw->setIconSize(16, 16); /* 重新设置大小 */ tw->setEditLine(ui->pBtn_selectTime->width(), ui->pBtn_selectTime->height()); /* 设置选择框大小 */ tw->setTimeAreaWidth(140); /* 移动位置,覆盖显示时间的按钮,获取的坐标是相对于Dialog的位置 */ auto pos = ui->pBtn_selectTime->mapTo(this, QPoint(0, 0)); tw->move(pos); /* 设置默认的时间 */ tw->setTime(m_time); tw->execShow(); m_time = tw->getTime(); // LH_WRITE_LOG_DEBUG(QString("选择时间:%1").arg(m_time.toString("hh:mm:ss"))); ui->pBtn_selectTime->setText(m_time.toString("hh:mm:ss")); } /* 设置选择框报警 */ void AddSpecialItem::setComboBoxWarning(QComboBox* bo, bool flag) { if(flag) { bo->setProperty("Warn", true); } else { bo->setProperty("Warn", false); } bo->style()->unpolish(bo); bo->style()->polish(bo); } /* 设置时间报警 */ void AddSpecialItem::setTimeEditWarning(bool flag) { if(flag) { ui->pBtn_selectTime->setProperty("Warn", true); } else { ui->pBtn_selectTime->setProperty("Warn", false); } ui->pBtn_selectTime->style()->unpolish(ui->pBtn_selectTime); ui->pBtn_selectTime->style()->polish(ui->pBtn_selectTime); } /* 设置动作 */ void AddSpecialItem::setAction(const QString& devName) { QMap devAction; if(!DeviceContainer.getDevAction(devName, devAction)) { return; } ui->comBox_actionSelect->clear(); for(auto it = devAction.begin();it != devAction.end();it++) { ui->comBox_actionSelect->addItem(it.value(), it.key()); } } /* 事件过滤器 */ bool AddSpecialItem::eventFilter(QObject *watched, QEvent *event) { if(watched == ui->comBox_devSelect) { if(event->type() == QEvent::Wheel) { return true; } } else if(watched == ui->comBox_actionSelect) { if(event->type() == QEvent::Wheel) { return true; } } return QDialog::eventFilter(watched, event); }