warning.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. /* 设置QSS */
  81. void Warning::setQSS(const QString& qssPath)
  82. {
  83. QString qssFile = qssPath + "/warning.qss";
  84. QFile file(qssFile);
  85. if(file.open(QIODevice::ReadOnly))
  86. {
  87. QString stylesheet = file.readAll();
  88. this->setStyleSheet(stylesheet);
  89. file.close();
  90. } else
  91. {
  92. LH_WRITE_ERROR(QString("打开文件失败:%1").arg(file.fileName()));
  93. }
  94. }
  95. // void Warning::paintEvent(QPaintEvent *event)
  96. // {
  97. // QPainter painter(this);
  98. // painter.setRenderHint(QPainter::Antialiasing);
  99. // /* 绘制阴影 */
  100. // painter.drawImage(QPoint(0,0),m_shadow->image());
  101. // }
  102. /* 事件过滤器 */
  103. bool Warning::eventFilter(QObject *watched, QEvent *event)
  104. {
  105. if(watched == ui->pBtn_close)
  106. {
  107. if(event->type() == QEvent::Enter)
  108. {
  109. ui->pBtn_close->setProperty("Hover", true);
  110. ui->pBtn_close->style()->unpolish(ui->pBtn_close);
  111. ui->pBtn_close->style()->polish(ui->pBtn_close);
  112. return true;
  113. }else if(event->type() == QEvent::Leave)
  114. {
  115. ui->pBtn_close->setProperty("Hover", false);
  116. ui->pBtn_close->style()->unpolish(ui->pBtn_close);
  117. ui->pBtn_close->style()->polish(ui->pBtn_close);
  118. return true;
  119. }
  120. }
  121. return QWidget::eventFilter(watched,event);
  122. }
  123. /* 鼠标点击事件 */
  124. void Warning::mousePressEvent(QMouseEvent *event)
  125. {
  126. m_lastPos = event->globalPos();
  127. event->accept();
  128. }
  129. /* 鼠标移动事件 */
  130. void Warning::mouseMoveEvent(QMouseEvent *event)
  131. {
  132. // QRect rect = this->geometry();
  133. // rect.setBottom(rect.top()+50);
  134. auto point = ui->widget_Top->mapToGlobal(QPoint(0, 0));
  135. QRect rect(point, ui->widget_Top->size());
  136. if(!rect.contains(m_lastPos))
  137. {
  138. event->accept();
  139. return;
  140. }
  141. int dx = event->globalX() - m_lastPos.x();
  142. int dy = event->globalY() - m_lastPos.y();
  143. move(x()+dx, y()+dy);
  144. m_lastPos = event->globalPos();
  145. event->accept();
  146. }
  147. /* 鼠标释放事件 */
  148. void Warning::mouseReleaseEvent(QMouseEvent *event)
  149. {
  150. event->accept();
  151. }
  152. void Warning::do_ok()
  153. {
  154. emit signal_ok();
  155. m_isOk = true;
  156. this->close();
  157. }
  158. /**
  159. * @brief 重新设置显示文字的区域大小,主要是设置宽度,为计算出左侧的图标位置
  160. * 字号是18,每个汉字的宽度、高度都是18,行高是27。
  161. * 文字显示区域最大宽度是306,显示17个汉字。
  162. * 阴影宽度是16,需要加上阴影宽度的坐标。
  163. *
  164. */
  165. void Warning::resetLabelSize()
  166. {
  167. int TextCount = ui->label_Warn->text().count();
  168. int width = TextCount * 18;
  169. if(width > 306)
  170. {
  171. width = 306;
  172. }
  173. // ui->label_Warn->setFixedWidth(width);
  174. ui->label_Warn->resize(width, ui->label_Warn->height());
  175. /* ui->widget是布局确定的大小,获取它的大小不准确 */
  176. int widgetWidth = this->width();
  177. int x = (widgetWidth - width) / 2;
  178. // int x = (widgetWidth - width - ui->label_warnIcon->width() - 12) / 2;
  179. // x = x + ui->label_warnIcon->width() + 12; /* 加上图标的宽度,和图标之间的间距 */
  180. ui->label_Warn->move(x, ui->label_Warn->y());
  181. }
  182. /* 移动警告图标 */
  183. void Warning::moveWarnICON()
  184. {
  185. int x = ui->label_Warn->x() - ui->label_warnIcon->width() - 12;
  186. int y = ui->label_Warn->y() + ui->label_Warn->height() / 2 - ui->label_warnIcon->height() / 2;
  187. ui->label_warnIcon->move(x,y);
  188. }