copytoother.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #include "copytoother.h"
  2. #include "ui_copytoother.h"
  3. #include <QDebug>
  4. #include <QPoint>
  5. #include <QTableWidgetItem>
  6. #include <QFile>
  7. #include <QPainter>
  8. #include <QMouseEvent>
  9. #include <QCheckBox>
  10. #include <QVBoxLayout>
  11. #include <qcheckbox.h>
  12. #include "warning/warning.h"
  13. #include "LHQLogAPI.h"
  14. #include "OneShadowEffect.h"
  15. #include "TransmitterSwitchInfo.h"
  16. #include "UIStyleManager.h"
  17. #include "template.h"
  18. CopyToOther::CopyToOther(QWidget *parent) :
  19. QDialog(parent),
  20. ui(new Ui::CopyToOther)
  21. {
  22. ui->setupUi(this);
  23. /* 设置隐藏边框 */
  24. this->setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint);
  25. /* 设置底层样式表 */
  26. this->setAttribute(Qt::WA_TranslucentBackground);
  27. /* 创建阴影 */
  28. QSize size = this->size();
  29. size.setWidth(size.width() - 40);
  30. size.setHeight(size.height() - 40);
  31. auto pShadow = new OneShadowEffect(this);
  32. this->setGraphicsEffect(pShadow);
  33. /* 给滚动区域添加一个布局 */
  34. m_layout = new QVBoxLayout(ui->scrollAreaWidgetContents);
  35. m_layout->setContentsMargins(0, 0, 0, 0);
  36. m_layout->setSpacing(0);
  37. ui->scrollAreaWidgetContents->setLayout(m_layout);
  38. /* 添加一个竖直弹簧 */
  39. auto pSpacerItem = new QSpacerItem(20, 20, QSizePolicy::Minimum, QSizePolicy::Expanding);
  40. m_layout->addItem(pSpacerItem);
  41. connect(ui->pBtn_close,SIGNAL(clicked()),this,SLOT(close()));
  42. connect(ui->pBtn_cancel,SIGNAL(clicked()),this,SLOT(close()));
  43. connect(ui->pBtn_ok,SIGNAL(clicked()),this,SLOT(do_ok()));
  44. connect(ui->checkBox_list, &QCheckBox::stateChanged, this, &CopyToOther::do_checkBox_stateChanged);
  45. /* 注册事件过滤器 */
  46. ui->pBtn_close->installEventFilter(this);
  47. }
  48. CopyToOther::~CopyToOther()
  49. {
  50. delete ui;
  51. }
  52. /**
  53. * @brief 设置频率列表,并去掉当前的频率
  54. *
  55. * @param currChn
  56. * @param list
  57. */
  58. void CopyToOther::setFrequencyList(ChannelInfo& currChn, QMap<int, ChannelInfo>& list)
  59. {
  60. for(auto it : list)
  61. {
  62. if(it.ChannelID == currChn.ChannelID)
  63. {
  64. continue; /* 跳过当前频率 */
  65. }
  66. createRow(it);
  67. }
  68. }
  69. void CopyToOther::do_ok()
  70. {
  71. /* 获取已选中的模版列表 */
  72. for(auto it : m_listCheckBox)
  73. {
  74. if(it->isChecked())
  75. {
  76. auto chnID = it->property(m_propertyChnID.c_str()).toInt();
  77. ChannelInfo info;
  78. info.ChannelID = chnID;
  79. info.ChannelName = it->text();
  80. m_listChannel.append(info);
  81. }
  82. }
  83. m_isOk = true;
  84. emit signal_templateName(m_templateName);
  85. this->close();
  86. }
  87. /* 点击了一个checkBox,取消其他同频率的checkBox */
  88. void CopyToOther::do_checkBox_stateChanged(int state)
  89. {
  90. auto checkBox = qobject_cast<QCheckBox*>(sender());
  91. if(checkBox == nullptr)
  92. {
  93. return;
  94. }
  95. /* 先判断是不是全选 */
  96. if(checkBox == ui->checkBox_list)
  97. {
  98. /* 判断是全选还是取消全选 */
  99. if(state == Qt::Checked)
  100. {
  101. for(auto it : m_listCheckBox)
  102. {
  103. it->setChecked(true);
  104. }
  105. }else {
  106. for(auto it : m_listCheckBox)
  107. {
  108. it->setChecked(false);
  109. }
  110. }
  111. return;
  112. }
  113. }
  114. /* 显示事件 */
  115. void CopyToOther::showEvent(QShowEvent *event)
  116. {
  117. }
  118. /* 设置QSS */
  119. void CopyToOther::setQSSPath(const QString& qssPath)
  120. {
  121. if(qssPath.isEmpty())
  122. {
  123. return;
  124. }
  125. QString qssFile = qssPath + "/copytoother.qss";
  126. QFile file(qssFile);
  127. if(file.open(QFile::ReadOnly))
  128. {
  129. QString styleSheet = file.readAll();
  130. this->setStyleSheet(styleSheet);
  131. file.close();
  132. }else
  133. {
  134. LH_WRITE_ERROR(QString("Open %1 failed").arg(qssFile));
  135. }
  136. }
  137. /* 添加一行 */
  138. void CopyToOther::createRow(const ChannelInfo& info)
  139. {
  140. QCheckBox* checkBox = new QCheckBox(this);
  141. checkBox->setText(info.ChannelName);
  142. checkBox->setProperty(m_propertyChnID.c_str(), info.ChannelID);
  143. checkBox->setMinimumHeight(34);
  144. checkBox->setStyleSheet(R"(
  145. QCheckBox
  146. {
  147. background: #464649;
  148. font-weight: 400;
  149. font-size: 14px;
  150. color: #B1B3B4;
  151. text-align: left;
  152. font-style: normal;
  153. text-transform: none;
  154. padding-left: 16px;
  155. })"
  156. );
  157. connect(checkBox, &QCheckBox::stateChanged, this, &CopyToOther::do_checkBox_stateChanged);
  158. /* 插入到最后一个弹簧之前 */
  159. m_layout->insertWidget(m_layout->count() - 1, checkBox);
  160. /* 添加到列表中 */
  161. m_listCheckBox.append(checkBox);
  162. }
  163. /* 事件过滤器 */
  164. bool CopyToOther::eventFilter(QObject *watched, QEvent *event)
  165. {
  166. if(watched == ui->pBtn_close)
  167. {
  168. if(event->type() == QEvent::Enter)
  169. {
  170. ui->pBtn_close->setProperty("Hover", true);
  171. ui->pBtn_close->style()->unpolish(ui->pBtn_close);
  172. ui->pBtn_close->style()->polish(ui->pBtn_close);
  173. return true;
  174. }else if(event->type() == QEvent::Leave)
  175. {
  176. ui->pBtn_close->setProperty("Hover", false);
  177. ui->pBtn_close->style()->unpolish(ui->pBtn_close);
  178. ui->pBtn_close->style()->polish(ui->pBtn_close);
  179. return true;
  180. }
  181. }
  182. return QWidget::eventFilter(watched,event);
  183. }
  184. /* 鼠标点击事件 */
  185. void CopyToOther::mousePressEvent(QMouseEvent *event)
  186. {
  187. m_lastPos = event->globalPos();
  188. event->accept();
  189. }
  190. /* 鼠标移动事件 */
  191. void CopyToOther::mouseMoveEvent(QMouseEvent *event)
  192. {
  193. // QRect rect = this->geometry();
  194. // rect.setBottom(rect.top()+50);
  195. auto point = ui->widget_Top->mapToGlobal(QPoint(0, 0));
  196. QRect rect(point, ui->widget_Top->size());
  197. if(!rect.contains(m_lastPos))
  198. {
  199. event->accept();
  200. return;
  201. }
  202. int dx = event->globalX() - m_lastPos.x();
  203. int dy = event->globalY() - m_lastPos.y();
  204. move(x()+dx, y()+dy);
  205. m_lastPos = event->globalPos();
  206. event->accept();
  207. }
  208. /* 鼠标释放事件 */
  209. void CopyToOther::mouseReleaseEvent(QMouseEvent *event)
  210. {
  211. event->accept();
  212. }