#include "copytoother.h" #include "ui_copytoother.h" #include #include #include #include #include #include #include #include #include #include "warning/warning.h" #include "LHQLogAPI.h" #include "OneShadowEffect.h" #include "TransmitterSwitchInfo.h" #include "UIStyleManager.h" #include "template.h" CopyToOther::CopyToOther(QWidget *parent) : QDialog(parent), ui(new Ui::CopyToOther) { ui->setupUi(this); /* 设置隐藏边框 */ this->setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint); /* 设置底层样式表 */ this->setAttribute(Qt::WA_TranslucentBackground); /* 创建阴影 */ QSize size = this->size(); size.setWidth(size.width() - 40); size.setHeight(size.height() - 40); auto pShadow = new OneShadowEffect(this); this->setGraphicsEffect(pShadow); /* 给滚动区域添加一个布局 */ m_layout = new QVBoxLayout(ui->scrollAreaWidgetContents); m_layout->setContentsMargins(0, 0, 0, 0); m_layout->setSpacing(0); ui->scrollAreaWidgetContents->setLayout(m_layout); /* 使用空白的Widget替代弹簧 */ // auto pSpacerItem = new QSpacerItem(20, 20, QSizePolicy::Minimum, QSizePolicy::Expanding); QWidget* spacerWidget = new QWidget(this); spacerWidget->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding); spacerWidget->setObjectName("widget_spacer"); m_layout->addWidget(spacerWidget); connect(ui->pBtn_close,SIGNAL(clicked()),this,SLOT(close())); connect(ui->pBtn_cancel,SIGNAL(clicked()),this,SLOT(close())); connect(ui->pBtn_ok,SIGNAL(clicked()),this,SLOT(do_ok())); connect(ui->checkBox_list, &QCheckBox::stateChanged, this, &CopyToOther::do_checkBox_list_stateChanged); /* 注册事件过滤器 */ ui->pBtn_close->installEventFilter(this); } CopyToOther::~CopyToOther() { delete ui; } /** * @brief 设置频率列表,并去掉当前的频率 * * @param currChn * @param list */ void CopyToOther::setFrequencyList(ChannelInfo& currChn, QMap& list) { for(auto it : list) { if(it.ChannelID == currChn.ChannelID) { continue; /* 跳过当前频率 */ } createRow(it); } } void CopyToOther::do_ok() { /* 获取已选中的模版列表 */ for(auto it : m_listCheckBox) { if(it->isChecked()) { auto chnID = it->property(m_propertyChnID.c_str()).toInt(); ChannelInfo info; info.ChannelID = chnID; info.ChannelName = it->text(); m_listChannel.append(info); } } /* 判断有没有勾选 */ if(m_listChannel.isEmpty()) { // LH_WRITE_ERROR("没有选择频率"); TipWidget::display(TipWidget::OPERATOR_WARN, "请选择一个频率", this); return; } m_isOk = true; emit signal_templateName(m_templateName); this->close(); } /* 点击了全选按钮 */ void CopyToOther::do_checkBox_list_stateChanged(int state) { auto checkBox = qobject_cast(sender()); if(checkBox == nullptr) { return; } // LH_WRITE_LOG_DEBUG(QString("全选状态:%1").arg(state)); Qt::CheckState checkState = Qt::Unchecked; /* 判断是全选还是取消全选 */ if(state == Qt::Checked) { checkState = Qt::Checked; } else if(state == Qt::Unchecked) { checkState = Qt::Unchecked; } else if(state == Qt::PartiallyChecked) { /* 部分选中,设置为全选 */ ui->checkBox_list->blockSignals(true); ui->checkBox_list->setCheckState(Qt::Checked); ui->checkBox_list->blockSignals(false); checkState = Qt::Checked; } for(auto it : m_listCheckBox) { it->blockSignals(true); it->setCheckState(checkState); it->blockSignals(false); } } /* 点击了一个checkBox */ void CopyToOther::do_checkBox_stateChanged(int state) { auto checkBox = qobject_cast(sender()); if(checkBox == nullptr) { return; } // LH_WRITE_LOG_DEBUG(QString("单个checkBox状态:%1").arg(state)); /* 不是点击了全选,改变全选的状态 */ bool isAllChecked = true; bool isAllUnchecked = true; for(auto it : m_listCheckBox) { if(it->checkState() == Qt::Checked) { isAllUnchecked = false; } else if(it->checkState() == Qt::Unchecked) { isAllChecked = false; } } Qt::CheckState checkState = Qt::Unchecked; if(isAllChecked) { checkState = Qt::Checked; }else if(isAllUnchecked) { checkState = Qt::Unchecked; }else { checkState = Qt::PartiallyChecked; } ui->checkBox_list->blockSignals(true); ui->checkBox_list->setCheckState(checkState); ui->checkBox_list->blockSignals(false); } /* 显示事件 */ void CopyToOther::showEvent(QShowEvent *event) { } /* 设置QSS */ void CopyToOther::setQSSPath(const QString& qssPath) { if(qssPath.isEmpty()) { return; } QString qssFile = qssPath + "/copytoother.qss"; QFile file(qssFile); if(file.open(QFile::ReadOnly)) { m_qss = file.readAll(); this->setStyleSheet(m_qss); file.close(); }else { LH_WRITE_ERROR(QString("Open %1 failed").arg(qssFile)); } } /* 添加一行 */ void CopyToOther::createRow(const ChannelInfo& info) { QCheckBox* checkBox = new QCheckBox(this); checkBox->setText(info.ChannelName); checkBox->setProperty(m_propertyChnID.c_str(), info.ChannelID); checkBox->setMinimumHeight(34); /* 设置只有两种状态 */ checkBox->setTristate(false); connect(checkBox, &QCheckBox::stateChanged, this, &CopyToOther::do_checkBox_stateChanged); checkBox->setStyleSheet(m_qss); /* 插入到最后一个弹簧之前 */ m_layout->insertWidget(m_layout->count() - 1, checkBox); /* 添加到列表中 */ m_listCheckBox.append(checkBox); } /* 事件过滤器 */ bool CopyToOther::eventFilter(QObject *watched, QEvent *event) { 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 QWidget::eventFilter(watched,event); } /* 鼠标点击事件 */ void CopyToOther::mousePressEvent(QMouseEvent *event) { m_lastPos = event->globalPos(); event->accept(); } /* 鼠标移动事件 */ void CopyToOther::mouseMoveEvent(QMouseEvent *event) { // QRect rect = this->geometry(); // rect.setBottom(rect.top()+50); auto point = ui->widget_Top->mapToGlobal(QPoint(0, 0)); QRect rect(point, ui->widget_Top->size()); 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); m_lastPos = event->globalPos(); event->accept(); } /* 鼠标释放事件 */ void CopyToOther::mouseReleaseEvent(QMouseEvent *event) { event->accept(); }