copytoother.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. OneTemplateInfo info;
  78. info.channelInfo.ChannelID = chnID;
  79. info.channelInfo.ChannelName = it->text();
  80. info.templateName = it->text() + QString::number(chnID);
  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. connect(checkBox, &QCheckBox::stateChanged, this, &CopyToOther::do_checkBox_stateChanged);
  145. /* 插入到最后一个弹簧之前 */
  146. m_layout->insertWidget(m_layout->count() - 1, checkBox);
  147. /* 添加到列表中 */
  148. m_listCheckBox.append(checkBox);
  149. }
  150. /* 事件过滤器 */
  151. bool CopyToOther::eventFilter(QObject *watched, QEvent *event)
  152. {
  153. if(watched == ui->pBtn_close)
  154. {
  155. if(event->type() == QEvent::Enter)
  156. {
  157. ui->pBtn_close->setProperty("Hover", true);
  158. ui->pBtn_close->style()->unpolish(ui->pBtn_close);
  159. ui->pBtn_close->style()->polish(ui->pBtn_close);
  160. return true;
  161. }else if(event->type() == QEvent::Leave)
  162. {
  163. ui->pBtn_close->setProperty("Hover", false);
  164. ui->pBtn_close->style()->unpolish(ui->pBtn_close);
  165. ui->pBtn_close->style()->polish(ui->pBtn_close);
  166. return true;
  167. }
  168. }
  169. return QWidget::eventFilter(watched,event);
  170. }
  171. /* 鼠标点击事件 */
  172. void CopyToOther::mousePressEvent(QMouseEvent *event)
  173. {
  174. m_lastPos = event->globalPos();
  175. event->accept();
  176. }
  177. /* 鼠标移动事件 */
  178. void CopyToOther::mouseMoveEvent(QMouseEvent *event)
  179. {
  180. // QRect rect = this->geometry();
  181. // rect.setBottom(rect.top()+50);
  182. auto point = ui->widget_Top->mapToGlobal(QPoint(0, 0));
  183. QRect rect(point, ui->widget_Top->size());
  184. if(!rect.contains(m_lastPos))
  185. {
  186. event->accept();
  187. return;
  188. }
  189. int dx = event->globalX() - m_lastPos.x();
  190. int dy = event->globalY() - m_lastPos.y();
  191. move(x()+dx, y()+dy);
  192. m_lastPos = event->globalPos();
  193. event->accept();
  194. }
  195. /* 鼠标释放事件 */
  196. void CopyToOther::mouseReleaseEvent(QMouseEvent *event)
  197. {
  198. event->accept();
  199. }