onesettingitem.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include "onesettingitem.h"
  2. #include "spdlog.h"
  3. #include "ui_onesettingitem.h"
  4. #include <QDebug>
  5. #include <QFile>
  6. #include <QEvent>
  7. #include <qnamespace.h>
  8. #include "colordialogapi.h"
  9. #include "ColorDelegate.h"
  10. OneSettingItem::OneSettingItem(QWidget *parent) :
  11. QWidget(parent),
  12. ui(new Ui::OneSettingItem)
  13. {
  14. ui->setupUi(this);
  15. m_logger = spdlog::get("EyeMap");
  16. if(m_logger == nullptr)
  17. {
  18. qDebug() << "获取 EyeMap logger 失败";
  19. return;
  20. }
  21. /* 加载QSS */
  22. QFile fileQss(":/qss/SettingNum/OneItem/OneSettingItem.qss");
  23. if(fileQss.open(QFile::ReadOnly))
  24. {
  25. QString qss = fileQss.readAll();
  26. this->setStyleSheet(qss);
  27. fileQss.close();
  28. } else
  29. {
  30. SPDLOG_LOGGER_ERROR(m_logger, "加载QSS文件失败");
  31. }
  32. /* 禁止comboBox滚轮滚动 */
  33. ui->comboBox->installEventFilter(this);
  34. /* 限制自定义名称的字数 */
  35. ui->lineEdit->setMaxLength(10);
  36. /* 链接信号和槽 */
  37. connect(ui->pBtn_background, &QPushButton::clicked, this, &OneSettingItem::do_pBtn_background);
  38. connect(ui->comboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &OneSettingItem::do_select_channel);
  39. }
  40. OneSettingItem::~OneSettingItem()
  41. {
  42. delete ui;
  43. }
  44. /* 设置序号 */
  45. void OneSettingItem::setNum(int num)
  46. {
  47. eyeMapInfo.num = num;
  48. ui->label_num->setText(QString::number(num));
  49. }
  50. /* 设置颜色 */
  51. void OneSettingItem::setColor(const QColor& color)
  52. {
  53. eyeMapInfo.titleBarColor = color;
  54. QString qss = QString("background-color: %1").arg(color.name());
  55. ui->pBtn_background->setStyleSheet(qss);
  56. }
  57. /* 获取所有信息 */
  58. OneEyeMapInfo& OneSettingItem::getEyeMapInfo()
  59. {
  60. /* 更新所有的信息并返回 */
  61. eyeMapInfo.title = ui->lineEdit->text();
  62. eyeMapInfo.channelInfo = getCurrentChannel();
  63. // SPDLOG_LOGGER_DEBUG(m_logger, "当前通道号: {}", eyeMapInfo.channelInfo.channelName.toStdString());
  64. return eyeMapInfo;
  65. }
  66. /* 设置项信息 */
  67. void OneSettingItem::setItemInfo(const OneEyeMapInfo& info)
  68. {
  69. eyeMapInfo = info;
  70. setNum(info.num);
  71. ui->label_num->setText(QString::number(info.num));
  72. ui->lineEdit->setText(info.title);
  73. setColor(info.titleBarColor);
  74. setCurrentChannel(info.channelInfo.channel);
  75. // SPDLOG_LOGGER_DEBUG(m_logger, "当前通道号: {}", eyeMapInfo.channelInfo.channelName.toStdString());
  76. }
  77. /* 设置可选通道列表 */
  78. void OneSettingItem::setChannelList(const QList<OneChannelInfo>& list)
  79. {
  80. ui->comboBox->clear();
  81. ui->comboBox->addItem(GEyeMapInfo.getChannelName(OscChnNum::Osc_None, true), 0);
  82. ui->comboBox->setItemData(ui->comboBox->count() - 1, true, Qt::UserRole + 1);
  83. for(const auto &it : list)
  84. {
  85. /* 设置可选项,用户数据设置为通道号 */
  86. ui->comboBox->addItem(it.channelName, static_cast<int>(it.channel));
  87. ui->comboBox->setItemData(ui->comboBox->count() - 1, it.isConnected, Qt::UserRole + 1);
  88. // SPDLOG_LOGGER_DEBUG(m_logger, "通道号: {}, 通道名: {}", static_cast<int>(it.channel), it.channelName.toStdString());
  89. }
  90. /* 设置自定义委托 */
  91. ui->comboBox->setItemDelegate(new ColorDelegate(ui->comboBox));
  92. }
  93. /* 设置当前通道名 */
  94. void OneSettingItem::setCurrentChannel(const QString& channelName)
  95. {
  96. ui->comboBox->setCurrentText(channelName);
  97. eyeMapInfo.channelInfo.channelName = channelName;
  98. eyeMapInfo.channelInfo.channel = static_cast<OscChnNum>(ui->comboBox->currentData().toInt());
  99. }
  100. /**
  101. * @brief 使用通道号设置当前通道,这里的通道号和下拉列表的index一一对应
  102. *
  103. * @param channel
  104. */
  105. void OneSettingItem::setCurrentChannel(const OscChnNum channel)
  106. {
  107. // SPDLOG_LOGGER_DEBUG(m_logger, "设置通道号: {}", static_cast<int>(channel));
  108. ui->comboBox->setCurrentIndex(static_cast<int>(channel));
  109. // SPDLOG_LOGGER_DEBUG(m_logger, "当前index: {}", ui->comboBox->currentIndex());
  110. // for(int i = 0; i < ui->comboBox->count(); i++)
  111. // {
  112. // SPDLOG_LOGGER_WARN(m_logger, "通道号: {}, 通道名: {}", ui->comboBox->itemData(i).toInt(), ui->comboBox->itemText(i).toStdString());
  113. // }
  114. eyeMapInfo.channelInfo.channelName = ui->comboBox->currentText();
  115. eyeMapInfo.channelInfo.channel = channel;
  116. }
  117. /* 获取当前通道号 */
  118. OneChannelInfo OneSettingItem::getCurrentChannel()
  119. {
  120. OneChannelInfo info;
  121. info.channel = static_cast<OscChnNum>(ui->comboBox->currentData().toInt());
  122. info.channelName = ui->comboBox->currentText();
  123. return info;
  124. }
  125. /* 设置自定义的通道名称栏报警 */
  126. void OneSettingItem::setChannelNameAlarm(bool isAlarm)
  127. {
  128. if(isAlarm)
  129. {
  130. ui->lineEdit->setProperty("Warn", true);
  131. ui->lineEdit->style()->unpolish(ui->lineEdit);
  132. ui->lineEdit->style()->polish(ui->lineEdit);
  133. }else {
  134. ui->lineEdit->setProperty("Warn", false);
  135. ui->lineEdit->style()->unpolish(ui->lineEdit);
  136. ui->lineEdit->style()->polish(ui->lineEdit);
  137. }
  138. }
  139. /* 设置通道选择栏报警 */
  140. void OneSettingItem::setChannelSelectAlarm(bool isAlarm)
  141. {
  142. if(isAlarm)
  143. {
  144. ui->comboBox->setProperty("Warn", true);
  145. ui->comboBox->style()->unpolish(ui->comboBox);
  146. ui->comboBox->style()->polish(ui->comboBox);
  147. }else {
  148. ui->comboBox->setProperty("Warn", false);
  149. ui->comboBox->style()->unpolish(ui->comboBox);
  150. ui->comboBox->style()->polish(ui->comboBox);
  151. }
  152. }
  153. /* 滚轮事件,禁止选择栏滚动 */
  154. // void OneSettingItem::wheelEvent(QWheelEvent *event)
  155. // {
  156. // event->ignore();
  157. // }
  158. /* 事件过滤器 */
  159. bool OneSettingItem::eventFilter(QObject *watched, QEvent *event)
  160. {
  161. if(watched == ui->comboBox)
  162. {
  163. if(event->type() == QEvent::Wheel)
  164. {
  165. return true;
  166. }
  167. }
  168. return QWidget::eventFilter(watched, event);
  169. }
  170. /* 设置颜色 */
  171. void OneSettingItem::do_pBtn_background()
  172. {
  173. // SPDLOG_LOGGER_DEBUG(m_logger, "序号{}点击了背景颜色按钮", eyeMapInfo.num);
  174. QColor color = getColor(nullptr, ColorDlgSkin::DARK);
  175. if(color.isValid())
  176. {
  177. QString qss = QString("background-color: %1").arg(color.name());
  178. ui->pBtn_background->setStyleSheet(qss);
  179. }
  180. eyeMapInfo.titleBarColor = color;
  181. }
  182. /* 选择了通道号,发送信号主要是给外面判断通道号是否冲突 */
  183. void OneSettingItem::do_select_channel(int index)
  184. {
  185. // SPDLOG_LOGGER_DEBUG(m_logger, "序号{}选择了 {}, 通道号: {}", eyeMapInfo.num, ui->comboBox->currentText().toStdString(), ui->comboBox->currentData().toInt());
  186. if(ui->comboBox->currentData().toInt() == 0)
  187. {
  188. return;
  189. }
  190. emit signal_select_channel(static_cast<OscChnNum>(ui->comboBox->currentData().toInt()), ui->comboBox->currentText());
  191. }