warning.cpp 4.6 KB

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