#include "addspecialitem.h" #include "ui_addspecialitem.h" #include #include #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" #include "OneShadowEffect.h" // #include "lhstylemanager.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())); // } /* 创建弹窗阴影 */ OneShadowEffect *pShadowEffect = new OneShadowEffect(this); ui->widget_background->setGraphicsEffect(pShadowEffect); 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(); ui->dateEdit->setDate(m_date); ui->dateEdit->setDisplayFormat("yyyy-MM-dd"); /* 连接信号和槽 */ 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); connect(ui->pBtn_iconTime, &QPushButton::clicked, this, &AddSpecialItem::do_selectTime); connect(ui->dateEdit, &CalendarDTEdit::dateChanged, this, &AddSpecialItem::do_selectDate); /* 设置事件过滤器 */ ui->comBox_actionSelect->installEventFilter(this); ui->comBox_devSelect->installEventFilter(this); ui->pBtn_Close->installEventFilter(this); ui->dateEdit->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 != 7) { return; } m_week = week; } void AddSpecialItem::setQSS(QString qssPath) { QString qssFile = qssPath + "/addspecialitem.qss"; QFile file(qssFile); 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(); } /* 进行查重和关闭页面 */ 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); } /* 赋值日期 */ m_date = ui->dateEdit->date(); /* 检查时间是否为空 */ if(m_time.isNull()) { ui->label_timeWarn->setText("不能为空!"); ui->label_timeWarn->show(); setTimeEditWarning(true); return; } /* 进行设备查重 */ bool ret = IData.judgeDateTimeRepetitionWithAdd(m_week, m_devName, m_date, 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, 时间:%4").arg(m_devName).arg(m_action).arg(m_date.toString("yyyy-MM-dd")).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(); m_actionID = ui->comBox_actionSelect->currentData().toInt(); } /* 点击了时间选择按钮,打开时间选择器 */ 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::do_selectDate(const QDate &date) { // LH_WRITE_LOG_DEBUG(QString("选择日期:%1").arg(date.toString("yyyy-MM-dd"))); } /* 设置选择框报警 */ 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; } } else if(watched == ui->dateEdit) { if(event->type() == QEvent::Wheel) { return true; } } else if(watched == ui->pBtn_Close) { if(event->type() == QEvent::Enter) { ui->pBtn_Close->setProperty("Hover", true); ui->pBtn_Close->style()->unpolish(ui->pBtn_Close); ui->pBtn_Close->style()->polish(ui->pBtn_Close); return true; }else if(event->type() == QEvent::Leave) { ui->pBtn_Close->setProperty("Hover", false); ui->pBtn_Close->style()->unpolish(ui->pBtn_Close); ui->pBtn_Close->style()->polish(ui->pBtn_Close); return true; } } return QDialog::eventFilter(watched, event); } /* 绘画事件 */ // void AddSpecialItem::paintEvent(QPaintEvent *event) // { // QPainter painter(this); // painter.setRenderHint(QPainter::Antialiasing); // /* 移动到方框下面 */ // QPoint pos = ui->widget_background->pos(); // pos.setX(pos.x() - 16); // pos.setY(pos.y() - 16); // painter.drawImage(pos, m_shadow->image()); // } /* 鼠标点击事件 */ void AddSpecialItem::mousePressEvent(QMouseEvent *event) { m_lastPos = event->globalPos(); event->accept(); } /* 鼠标移动事件 */ void AddSpecialItem::mouseMoveEvent(QMouseEvent *event) { // QRect rect = this->geometry(); auto point = ui->widget_Top->mapToGlobal(QPoint(0, 0)); QRect rect(point, ui->widget_Top->size()); // LH_WRITE_LOG(QString("rect: %1, %2, %3, %4").arg(rect.left()).arg(rect.top()).arg(rect.right()).arg(rect.bottom())); // LH_WRITE_LOG(QString("lastPos: %1, %2").arg(m_lastPos.x()).arg(m_lastPos.y())); // rect.setBottom(rect.top()+50); if(!rect.contains(m_lastPos)) { event->accept(); return; } int dx = event->globalX() - m_lastPos.x(); int dy = event->globalY() - m_lastPos.y(); // move(x()+dx, y()+dy); ui->widget_background->move(ui->widget_background->x() + dx, ui->widget_background->y() + dy); // m_shadow->move(ui->widget_background->pos()); m_lastPos = event->globalPos(); event->accept(); } /* 鼠标释放事件 */ void AddSpecialItem::mouseReleaseEvent(QMouseEvent *event) { event->accept(); }