#include "onesettingitem.h" #include "spdlog.h" #include "ui_onesettingitem.h" #include #include #include #include #include "colordialogapi.h" #include "ColorDelegate.h" OneSettingItem::OneSettingItem(QWidget *parent) : QWidget(parent), ui(new Ui::OneSettingItem) { ui->setupUi(this); m_logger = spdlog::get("EyeMap"); if(m_logger == nullptr) { qDebug() << "获取 EyeMap logger 失败"; return; } /* 加载QSS */ QFile fileQss(":/qss/SettingNum/OneItem/OneSettingItem.qss"); if(fileQss.open(QFile::ReadOnly)) { QString qss = fileQss.readAll(); this->setStyleSheet(qss); fileQss.close(); } else { SPDLOG_LOGGER_ERROR(m_logger, "加载QSS文件失败"); } /* 禁止comboBox滚轮滚动 */ ui->comboBox->installEventFilter(this); /* 限制自定义名称的字数 */ ui->lineEdit->setMaxLength(10); /* 链接信号和槽 */ connect(ui->pBtn_background, &QPushButton::clicked, this, &OneSettingItem::do_pBtn_background); connect(ui->comboBox, QOverload::of(&QComboBox::currentIndexChanged), this, &OneSettingItem::do_select_channel); } OneSettingItem::~OneSettingItem() { delete ui; } /* 设置序号 */ void OneSettingItem::setNum(int num) { eyeMapInfo.num = num; ui->label_num->setText(QString::number(num)); } /* 设置颜色 */ void OneSettingItem::setColor(const QColor& color) { eyeMapInfo.titleBarColor = color; QString qss = QString("background-color: %1").arg(color.name()); ui->pBtn_background->setStyleSheet(qss); } /* 获取所有信息 */ OneEyeMapInfo& OneSettingItem::getEyeMapInfo() { /* 更新所有的信息并返回 */ eyeMapInfo.title = ui->lineEdit->text(); eyeMapInfo.channelInfo = getCurrentChannel(); // SPDLOG_LOGGER_DEBUG(m_logger, "当前通道号: {}", eyeMapInfo.channelInfo.channelName.toStdString()); return eyeMapInfo; } /* 设置项信息 */ void OneSettingItem::setItemInfo(const OneEyeMapInfo& info) { eyeMapInfo = info; setNum(info.num); ui->label_num->setText(QString::number(info.num)); ui->lineEdit->setText(info.title); setColor(info.titleBarColor); setCurrentChannel(info.channelInfo.channel); // SPDLOG_LOGGER_DEBUG(m_logger, "当前通道号: {}", eyeMapInfo.channelInfo.channelName.toStdString()); } /* 设置可选通道列表 */ void OneSettingItem::setChannelList(const QList& list) { ui->comboBox->clear(); ui->comboBox->addItem(GEyeMapInfo.getChannelName(OscChnNum::Osc_None, true), 0); ui->comboBox->setItemData(ui->comboBox->count() - 1, true, Qt::UserRole + 1); for(const auto &it : list) { /* 设置可选项,用户数据设置为通道号 */ ui->comboBox->addItem(it.channelName, static_cast(it.channel)); ui->comboBox->setItemData(ui->comboBox->count() - 1, it.isConnected, Qt::UserRole + 1); // SPDLOG_LOGGER_DEBUG(m_logger, "通道号: {}, 通道名: {}", static_cast(it.channel), it.channelName.toStdString()); } /* 设置自定义委托 */ ui->comboBox->setItemDelegate(new ColorDelegate(ui->comboBox)); } /* 设置当前通道名 */ void OneSettingItem::setCurrentChannel(const QString& channelName) { ui->comboBox->setCurrentText(channelName); eyeMapInfo.channelInfo.channelName = channelName; eyeMapInfo.channelInfo.channel = static_cast(ui->comboBox->currentData().toInt()); } /** * @brief 使用通道号设置当前通道,这里的通道号和下拉列表的index一一对应 * * @param channel */ void OneSettingItem::setCurrentChannel(const OscChnNum channel) { // SPDLOG_LOGGER_DEBUG(m_logger, "设置通道号: {}", static_cast(channel)); ui->comboBox->setCurrentIndex(static_cast(channel)); // SPDLOG_LOGGER_DEBUG(m_logger, "当前index: {}", ui->comboBox->currentIndex()); // for(int i = 0; i < ui->comboBox->count(); i++) // { // SPDLOG_LOGGER_WARN(m_logger, "通道号: {}, 通道名: {}", ui->comboBox->itemData(i).toInt(), ui->comboBox->itemText(i).toStdString()); // } eyeMapInfo.channelInfo.channelName = ui->comboBox->currentText(); eyeMapInfo.channelInfo.channel = channel; } /* 获取当前通道号 */ OneChannelInfo OneSettingItem::getCurrentChannel() { OneChannelInfo info; info.channel = static_cast(ui->comboBox->currentData().toInt()); info.channelName = ui->comboBox->currentText(); return info; } /* 设置自定义的通道名称栏报警 */ void OneSettingItem::setChannelNameAlarm(bool isAlarm) { if(isAlarm) { ui->lineEdit->setProperty("Warn", true); ui->lineEdit->style()->unpolish(ui->lineEdit); ui->lineEdit->style()->polish(ui->lineEdit); }else { ui->lineEdit->setProperty("Warn", false); ui->lineEdit->style()->unpolish(ui->lineEdit); ui->lineEdit->style()->polish(ui->lineEdit); } } /* 设置通道选择栏报警 */ void OneSettingItem::setChannelSelectAlarm(bool isAlarm) { if(isAlarm) { ui->comboBox->setProperty("Warn", true); ui->comboBox->style()->unpolish(ui->comboBox); ui->comboBox->style()->polish(ui->comboBox); }else { ui->comboBox->setProperty("Warn", false); ui->comboBox->style()->unpolish(ui->comboBox); ui->comboBox->style()->polish(ui->comboBox); } } /* 滚轮事件,禁止选择栏滚动 */ // void OneSettingItem::wheelEvent(QWheelEvent *event) // { // event->ignore(); // } /* 事件过滤器 */ bool OneSettingItem::eventFilter(QObject *watched, QEvent *event) { if(watched == ui->comboBox) { if(event->type() == QEvent::Wheel) { return true; } } return QWidget::eventFilter(watched, event); } /* 设置颜色 */ void OneSettingItem::do_pBtn_background() { // SPDLOG_LOGGER_DEBUG(m_logger, "序号{}点击了背景颜色按钮", eyeMapInfo.num); QColor color = getColor(nullptr, ColorDlgSkin::DARK); if(color.isValid()) { QString qss = QString("background-color: %1").arg(color.name()); ui->pBtn_background->setStyleSheet(qss); } eyeMapInfo.titleBarColor = color; } /* 选择了通道号,发送信号主要是给外面判断通道号是否冲突 */ void OneSettingItem::do_select_channel(int index) { // SPDLOG_LOGGER_DEBUG(m_logger, "序号{}选择了 {}, 通道号: {}", eyeMapInfo.num, ui->comboBox->currentText().toStdString(), ui->comboBox->currentData().toInt()); if(ui->comboBox->currentData().toInt() == 0) { return; } emit signal_select_channel(static_cast(ui->comboBox->currentData().toInt()), ui->comboBox->currentText()); }