123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 |
- #include "addnormalitem.h"
- #include "ui_addnormalitem.h"
- #include <memory>
- #include <QDebug>
- #include <QListView>
- #include <QFile>
- #include <QApplication>
- #include <QDesktopWidget>
- #include <QPainter>
- #include <QMouseEvent>
- #include "common/combobox/customcombobox.h"
- #include "LHQLogAPI.h"
- #include "TransmitterSwitchInfo.h"
- #include "common/SelectTime/timewidget.h"
- #include "ItemData.h"
- #include "OneShadowEffect.h"
- // #include "lhstylemanager.h"
- AddNormalItem::AddNormalItem(QWidget *parent) :
- QDialog(parent),
- ui(new Ui::AddNormalItem)
- {
- ui->setupUi(this);
- /* 设置无边框 */
- setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint);
- /* 设置底层样式表,让最底层的透明 */
- this->setAttribute(Qt::WA_TranslucentBackground);
- /* 加载QSS */
- // QFile file(":/QSS/QSS/AddNormalItem_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);
- /* 连接信号和槽 */
- 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, &AddNormalItem::do_ok);
- connect(ui->pBtn_cancel, &QPushButton::clicked, this, &AddNormalItem::close);
- /* 设备选择 */
- connect(ui->comBox_devSelect, QOverload<const QString&>::of(&QComboBox::currentTextChanged), this, &AddNormalItem::do_selectDev);
- /* 动作选择 */
- connect(ui->comBox_actionSelect,QOverload<const QString&>::of(&QComboBox::currentTextChanged),this, &AddNormalItem::do_selectAction);
- /* 打开时间选择器 */
- connect(ui->pBtn_selectTime, &QPushButton::clicked, this, &AddNormalItem::do_selectTime);
-
- /* 设置事件过滤器 */
- ui->comBox_actionSelect->installEventFilter(this);
- ui->comBox_devSelect->installEventFilter(this);
- ui->pBtn_Close->installEventFilter(this);
- }
- AddNormalItem::~AddNormalItem()
- {
- delete ui;
- }
- /* 设置父指针,时间选择器需要使用 */
- void AddNormalItem::setParentPointer(QWidget* p)
- {
- m_parent = p;
- // m_parent->installEventFilter(this);
- }
- /* 添加可选设备 */
- void AddNormalItem::setDevice(QMap<QString, DeviceInfo>& 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 AddNormalItem::setWeekDay(int week)
- {
- if(week < 0 || week > 6)
- {
- return;
- }
- m_week = week;
- }
- /* 设置QSS */
- void AddNormalItem::setQSS(const QString& qssPath)
- {
- QString qssFile = qssPath + "/addnormalitem.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 AddNormalItem::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_addNormalItem(m_devName,m_action,m_time);
- close();
- }
- /* 选择了设备,设置其对应的动作 */
- void AddNormalItem::do_selectDev()
- {
- m_devName = ui->comBox_devSelect->currentText();
- setAction(m_devName);
- }
- /* 选择了动作 */
- void AddNormalItem::do_selectAction()
- {
- m_action = ui->comBox_actionSelect->currentText();
- m_actionID = ui->comBox_actionSelect->currentData().toInt();
- }
- /* 点击了时间选择按钮,打开时间选择器 */
- void AddNormalItem::do_selectTime()
- {
- // LH_WRITE_LOG_DEBUG("选择时间");
- std::shared_ptr<TimeWidget> tw = std::make_shared<TimeWidget>(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 AddNormalItem::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 AddNormalItem::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 AddNormalItem::setAction(const QString& devName)
- {
- QMap<int, QString> 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 AddNormalItem::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->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 AddNormalItem::paintEvent(QPaintEvent *event)
- // {
- // QPainter painter(this);
- // painter.setRenderHint(QPainter::Antialiasing);
- // }
- /* 鼠标点击事件 */
- void AddNormalItem::mousePressEvent(QMouseEvent *event)
- {
- m_lastPos = event->globalPos();
- event->accept();
- }
- /* 鼠标移动事件 */
- void AddNormalItem::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 AddNormalItem::mouseReleaseEvent(QMouseEvent *event)
- {
- event->accept();
- }
|