warning.cpp 5.7 KB

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