warning.cpp 5.4 KB

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