copytoother.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 <tipwidget.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. /* 使用空白的Widget替代弹簧 */
  39. // auto pSpacerItem = new QSpacerItem(20, 20, QSizePolicy::Minimum, QSizePolicy::Expanding);
  40. QWidget* spacerWidget = new QWidget(this);
  41. spacerWidget->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding);
  42. spacerWidget->setObjectName("widget_spacer");
  43. m_layout->addWidget(spacerWidget);
  44. connect(ui->pBtn_close,SIGNAL(clicked()),this,SLOT(close()));
  45. connect(ui->pBtn_cancel,SIGNAL(clicked()),this,SLOT(close()));
  46. connect(ui->pBtn_ok,SIGNAL(clicked()),this,SLOT(do_ok()));
  47. connect(ui->checkBox_list, &QCheckBox::stateChanged, this, &CopyToOther::do_checkBox_list_stateChanged);
  48. /* 注册事件过滤器 */
  49. ui->pBtn_close->installEventFilter(this);
  50. }
  51. CopyToOther::~CopyToOther()
  52. {
  53. delete ui;
  54. }
  55. /**
  56. * @brief 设置频率列表,并去掉当前的频率
  57. *
  58. * @param currChn
  59. * @param list
  60. */
  61. void CopyToOther::setFrequencyList(ChannelInfo& currChn, QMap<int, ChannelInfo>& list)
  62. {
  63. for(auto it : list)
  64. {
  65. if(it.ChannelID == currChn.ChannelID)
  66. {
  67. continue; /* 跳过当前频率 */
  68. }
  69. createRow(it);
  70. }
  71. }
  72. void CopyToOther::do_ok()
  73. {
  74. /* 获取已选中的模版列表 */
  75. for(auto it : m_listCheckBox)
  76. {
  77. if(it->isChecked())
  78. {
  79. auto chnID = it->property(m_propertyChnID.c_str()).toInt();
  80. ChannelInfo info;
  81. info.ChannelID = chnID;
  82. info.ChannelName = it->text();
  83. m_listChannel.append(info);
  84. }
  85. }
  86. /* 判断有没有勾选 */
  87. if(m_listChannel.isEmpty())
  88. {
  89. // LH_WRITE_ERROR("没有选择频率");
  90. TipWidget::display(TipWidget::OPERATOR_WARN, "请选择一个频率", this);
  91. return;
  92. }
  93. m_isOk = true;
  94. emit signal_templateName(m_templateName);
  95. this->close();
  96. }
  97. /* 点击了全选按钮 */
  98. void CopyToOther::do_checkBox_list_stateChanged(int state)
  99. {
  100. auto checkBox = qobject_cast<QCheckBox*>(sender());
  101. if(checkBox == nullptr)
  102. {
  103. return;
  104. }
  105. // LH_WRITE_LOG_DEBUG(QString("全选状态:%1").arg(state));
  106. Qt::CheckState checkState = Qt::Unchecked;
  107. /* 判断是全选还是取消全选 */
  108. if(state == Qt::Checked)
  109. {
  110. checkState = Qt::Checked;
  111. }
  112. else if(state == Qt::Unchecked)
  113. {
  114. checkState = Qt::Unchecked;
  115. }
  116. else if(state == Qt::PartiallyChecked)
  117. {
  118. /* 部分选中,设置为全选 */
  119. ui->checkBox_list->blockSignals(true);
  120. ui->checkBox_list->setCheckState(Qt::Checked);
  121. ui->checkBox_list->blockSignals(false);
  122. checkState = Qt::Checked;
  123. }
  124. for(auto it : m_listCheckBox)
  125. {
  126. it->blockSignals(true);
  127. it->setCheckState(checkState);
  128. it->blockSignals(false);
  129. }
  130. }
  131. /* 点击了一个checkBox */
  132. void CopyToOther::do_checkBox_stateChanged(int state)
  133. {
  134. auto checkBox = qobject_cast<QCheckBox*>(sender());
  135. if(checkBox == nullptr)
  136. {
  137. return;
  138. }
  139. // LH_WRITE_LOG_DEBUG(QString("单个checkBox状态:%1").arg(state));
  140. /* 不是点击了全选,改变全选的状态 */
  141. bool isAllChecked = true;
  142. bool isAllUnchecked = true;
  143. for(auto it : m_listCheckBox)
  144. {
  145. if(it->checkState() == Qt::Checked)
  146. {
  147. isAllUnchecked = false;
  148. }
  149. else if(it->checkState() == Qt::Unchecked)
  150. {
  151. isAllChecked = false;
  152. }
  153. }
  154. Qt::CheckState checkState = Qt::Unchecked;
  155. if(isAllChecked)
  156. {
  157. checkState = Qt::Checked;
  158. }else if(isAllUnchecked)
  159. {
  160. checkState = Qt::Unchecked;
  161. }else {
  162. checkState = Qt::PartiallyChecked;
  163. }
  164. ui->checkBox_list->blockSignals(true);
  165. ui->checkBox_list->setCheckState(checkState);
  166. ui->checkBox_list->blockSignals(false);
  167. }
  168. /* 显示事件 */
  169. void CopyToOther::showEvent(QShowEvent *event)
  170. {
  171. }
  172. /* 设置QSS */
  173. void CopyToOther::setQSSPath(const QString& qssPath)
  174. {
  175. if(qssPath.isEmpty())
  176. {
  177. return;
  178. }
  179. QString qssFile = qssPath + "/copytoother.qss";
  180. QFile file(qssFile);
  181. if(file.open(QFile::ReadOnly))
  182. {
  183. m_qss = file.readAll();
  184. this->setStyleSheet(m_qss);
  185. file.close();
  186. }else
  187. {
  188. LH_WRITE_ERROR(QString("Open %1 failed").arg(qssFile));
  189. }
  190. }
  191. /* 添加一行 */
  192. void CopyToOther::createRow(const ChannelInfo& info)
  193. {
  194. QCheckBox* checkBox = new QCheckBox(this);
  195. checkBox->setText(info.ChannelName);
  196. checkBox->setProperty(m_propertyChnID.c_str(), info.ChannelID);
  197. checkBox->setMinimumHeight(34);
  198. /* 设置只有两种状态 */
  199. checkBox->setTristate(false);
  200. connect(checkBox, &QCheckBox::stateChanged, this, &CopyToOther::do_checkBox_stateChanged);
  201. checkBox->setStyleSheet(m_qss);
  202. /* 插入到最后一个弹簧之前 */
  203. m_layout->insertWidget(m_layout->count() - 1, checkBox);
  204. /* 添加到列表中 */
  205. m_listCheckBox.append(checkBox);
  206. }
  207. /* 事件过滤器 */
  208. bool CopyToOther::eventFilter(QObject *watched, QEvent *event)
  209. {
  210. if(watched == ui->pBtn_close)
  211. {
  212. if(event->type() == QEvent::Enter)
  213. {
  214. ui->pBtn_close->setProperty("Hover", true);
  215. ui->pBtn_close->style()->unpolish(ui->pBtn_close);
  216. ui->pBtn_close->style()->polish(ui->pBtn_close);
  217. return true;
  218. }else if(event->type() == QEvent::Leave)
  219. {
  220. ui->pBtn_close->setProperty("Hover", false);
  221. ui->pBtn_close->style()->unpolish(ui->pBtn_close);
  222. ui->pBtn_close->style()->polish(ui->pBtn_close);
  223. return true;
  224. }
  225. }
  226. return QWidget::eventFilter(watched,event);
  227. }
  228. /* 鼠标点击事件 */
  229. void CopyToOther::mousePressEvent(QMouseEvent *event)
  230. {
  231. m_lastPos = event->globalPos();
  232. event->accept();
  233. }
  234. /* 鼠标移动事件 */
  235. void CopyToOther::mouseMoveEvent(QMouseEvent *event)
  236. {
  237. // QRect rect = this->geometry();
  238. // rect.setBottom(rect.top()+50);
  239. auto point = ui->widget_Top->mapToGlobal(QPoint(0, 0));
  240. QRect rect(point, ui->widget_Top->size());
  241. if(!rect.contains(m_lastPos))
  242. {
  243. event->accept();
  244. return;
  245. }
  246. int dx = event->globalX() - m_lastPos.x();
  247. int dy = event->globalY() - m_lastPos.y();
  248. move(x()+dx, y()+dy);
  249. m_lastPos = event->globalPos();
  250. event->accept();
  251. }
  252. /* 鼠标释放事件 */
  253. void CopyToOther::mouseReleaseEvent(QMouseEvent *event)
  254. {
  255. event->accept();
  256. }