warning.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #include "warning.h"
  2. #include "ui_warning.h"
  3. #include <QPainter>
  4. #include <QLayout>
  5. #include <QDebug>
  6. #include <QFile>
  7. #include <QStyle>
  8. #include <QMouseEvent>
  9. #include "OneShadowEffect.h"
  10. #include "LHQLogAPI.h"
  11. #include "UIStyleManager.h"
  12. Warning::Warning(QWidget *parent) :
  13. QDialog(parent),
  14. ui(new Ui::Warning)
  15. {
  16. ui->setupUi(this);
  17. /* 设置无边框和背景透明 */
  18. this->setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint);
  19. this->setAttribute(Qt::WA_TranslucentBackground);
  20. /* 设置文字自动换行 */
  21. ui->label_Warn->setWordWrap(true);
  22. /* 设置文本居中 */
  23. // ui->label_Warn->setAlignment(Qt::AlignCenter);
  24. ui->label_Warn->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
  25. /* 加载警告图标 */
  26. // ui->label_warnIcon->setStyleSheet(R"(border-image: url(:/ESM-8C_ICON/Tip/Tips2x.png);)");
  27. /* 阴影宽度是16 */
  28. this->layout()->setMargin(SHADOW_W);
  29. // m_shadow = new OneShadow(QSize(width() - SHADOW_W*2, height() - SHADOW_W*2),SHADOW_W);
  30. auto pShadow = new OneShadowEffect(this);
  31. this->setGraphicsEffect(pShadow);
  32. connect(ui->pBtn_close,SIGNAL(clicked()),this,SLOT(close()));
  33. connect(ui->pBtn_cancel,SIGNAL(clicked()),this,SLOT(close()));
  34. connect(ui->pBtn_ok,SIGNAL(clicked()),this,SLOT(do_ok()));
  35. /* 注册事件过滤器 */
  36. ui->pBtn_close->installEventFilter(this);
  37. /* 加载QSS */
  38. QString qssPath;
  39. if(UIStyle.getUIStyle() == EUIStyle::UI_Light)
  40. {
  41. qssPath = ":/qss/warning_light.qss";
  42. } else
  43. {
  44. qssPath = ":/qss/warning_dark.qss";
  45. }
  46. QFile file(qssPath);
  47. if(file.open(QFile::ReadOnly))
  48. {
  49. QString styleSheet = file.readAll();
  50. this->setStyleSheet(styleSheet);
  51. file.close();
  52. }
  53. }
  54. Warning::~Warning()
  55. {
  56. delete ui;
  57. }
  58. void Warning::setTitle(const QString& title)
  59. {
  60. ui->label_title->setText(title);
  61. }
  62. void Warning::setText(const QString &text)
  63. {
  64. /* 根据文本大小设置高度 */
  65. ui->label_Warn->setText(text);
  66. /* 重新设置位置 */
  67. resetLabelSize();
  68. moveWarnICON();
  69. }
  70. /* 只有一个确定按钮 */
  71. void Warning::setTextWithOneButton(const QString &text)
  72. {
  73. ui->pBtn_cancel->hide();
  74. // ui->pBtn_ok->move(ui->pBtn_cancel->x(),ui->pBtn_cancel->y());
  75. ui->label_Warn->setText(text);
  76. /* 重新设置位置 */
  77. resetLabelSize();
  78. moveWarnICON();
  79. }
  80. /* 对调确定按钮和取消按钮 */
  81. void Warning::swapOkCancelButton()
  82. {
  83. QPoint tempPos = ui->pBtn_ok->pos();
  84. ui->pBtn_ok->move(ui->pBtn_cancel->x(), ui->pBtn_cancel->y());
  85. ui->pBtn_cancel->move(tempPos);
  86. }
  87. /* 设置QSS */
  88. void Warning::setQSS(const QString& qssPath)
  89. {
  90. QString qssFile = qssPath + "/warning.qss";
  91. QFile file(qssFile);
  92. if(file.open(QIODevice::ReadOnly))
  93. {
  94. QString stylesheet = file.readAll();
  95. this->setStyleSheet(stylesheet);
  96. file.close();
  97. } else
  98. {
  99. LH_WRITE_ERROR(QString("打开文件失败:%1").arg(file.fileName()));
  100. }
  101. }
  102. // void Warning::paintEvent(QPaintEvent *event)
  103. // {
  104. // QPainter painter(this);
  105. // painter.setRenderHint(QPainter::Antialiasing);
  106. // /* 绘制阴影 */
  107. // painter.drawImage(QPoint(0,0),m_shadow->image());
  108. // }
  109. /* 事件过滤器 */
  110. bool Warning::eventFilter(QObject *watched, QEvent *event)
  111. {
  112. if(watched == ui->pBtn_close)
  113. {
  114. if(event->type() == QEvent::Enter)
  115. {
  116. ui->pBtn_close->setProperty("Hover", true);
  117. ui->pBtn_close->style()->unpolish(ui->pBtn_close);
  118. ui->pBtn_close->style()->polish(ui->pBtn_close);
  119. return true;
  120. }else if(event->type() == QEvent::Leave)
  121. {
  122. ui->pBtn_close->setProperty("Hover", false);
  123. ui->pBtn_close->style()->unpolish(ui->pBtn_close);
  124. ui->pBtn_close->style()->polish(ui->pBtn_close);
  125. return true;
  126. }
  127. }
  128. return QWidget::eventFilter(watched,event);
  129. }
  130. /* 鼠标点击事件 */
  131. void Warning::mousePressEvent(QMouseEvent *event)
  132. {
  133. m_lastPos = event->globalPos();
  134. event->accept();
  135. }
  136. /* 鼠标移动事件 */
  137. void Warning::mouseMoveEvent(QMouseEvent *event)
  138. {
  139. // QRect rect = this->geometry();
  140. // rect.setBottom(rect.top()+50);
  141. auto point = ui->widget_Top->mapToGlobal(QPoint(0, 0));
  142. QRect rect(point, ui->widget_Top->size());
  143. if(!rect.contains(m_lastPos))
  144. {
  145. event->accept();
  146. return;
  147. }
  148. int dx = event->globalX() - m_lastPos.x();
  149. int dy = event->globalY() - m_lastPos.y();
  150. move(x()+dx, y()+dy);
  151. m_lastPos = event->globalPos();
  152. event->accept();
  153. }
  154. /* 鼠标释放事件 */
  155. void Warning::mouseReleaseEvent(QMouseEvent *event)
  156. {
  157. event->accept();
  158. }
  159. void Warning::do_ok()
  160. {
  161. emit signal_ok();
  162. m_isOk = true;
  163. this->close();
  164. }
  165. /**
  166. * @brief 重新设置显示文字的区域大小,主要是设置宽度,为计算出左侧的图标位置
  167. * 字号是18,每个汉字的宽度、高度都是18,行高是27。
  168. * 文字显示区域最大宽度是306,显示17个汉字。
  169. * 阴影宽度是16,需要加上阴影宽度的坐标。
  170. *
  171. */
  172. void Warning::resetLabelSize()
  173. {
  174. int TextCount = ui->label_Warn->text().count();
  175. int width = TextCount * 18;
  176. if(width > 306)
  177. {
  178. width = 306;
  179. }
  180. // ui->label_Warn->setFixedWidth(width);
  181. ui->label_Warn->resize(width, ui->label_Warn->height());
  182. /* ui->widget是布局确定的大小,获取它的大小不准确 */
  183. int widgetWidth = this->width();
  184. int x = (widgetWidth - width) / 2;
  185. // int x = (widgetWidth - width - ui->label_warnIcon->width() - 12) / 2;
  186. // x = x + ui->label_warnIcon->width() + 12; /* 加上图标的宽度,和图标之间的间距 */
  187. ui->label_Warn->move(x, ui->label_Warn->y());
  188. }
  189. /* 移动警告图标 */
  190. void Warning::moveWarnICON()
  191. {
  192. int x = ui->label_Warn->x() - ui->label_warnIcon->width() - 12;
  193. int y = ui->label_Warn->y() + ui->label_Warn->height() / 2 - ui->label_warnIcon->height() / 2;
  194. ui->label_warnIcon->move(x,y);
  195. }