warning.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include "warning.h"
  2. #include "ui_warning.h"
  3. #include <QPainter>
  4. #include <QLayout>
  5. #include <QDebug>
  6. #include <QFile>
  7. #include "oneshadow.h"
  8. #include "lhstylemanager.h"
  9. #include "LHQLogAPI.h"
  10. Warning::Warning(QWidget *parent) :
  11. QDialog(parent),
  12. ui(new Ui::Warning)
  13. {
  14. ui->setupUi(this);
  15. /* 设置无边框和背景透明 */
  16. this->setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint);
  17. this->setAttribute(Qt::WA_TranslucentBackground);
  18. /* 加载QSS */
  19. // QFile file(":/QSS/QSS/warning_light.qss");
  20. // if(file.open(QFile::ReadOnly))
  21. // {
  22. // QString styleSheet = file.readAll();
  23. // this->setStyleSheet(styleSheet);
  24. // file.close();
  25. // }
  26. /* 设置QSS */
  27. setQSS();
  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. Warning::~Warning()
  43. {
  44. delete ui;
  45. }
  46. void Warning::setText(const QString &text)
  47. {
  48. /* 根据文本大小设置高度 */
  49. ui->label_Warn->setText(text);
  50. /* 重新设置位置 */
  51. resetLabelSize();
  52. moveWarnICON();
  53. }
  54. /* 只有一个确定按钮 */
  55. void Warning::setTextWithOneButton(const QString &text)
  56. {
  57. ui->pBtn_cancel->hide();
  58. // ui->pBtn_ok->move(ui->pBtn_cancel->x(),ui->pBtn_cancel->y());
  59. ui->label_Warn->setText(text);
  60. /* 重新设置位置 */
  61. resetLabelSize();
  62. moveWarnICON();
  63. }
  64. /* 设置QSS */
  65. void Warning::setQSS()
  66. {
  67. QString appPath = QApplication::applicationDirPath();
  68. QString qssPath;
  69. if(LHStyleManager::Instance()->GetCurSkinStyle() == SkinStyle::eWhiteStyle)
  70. {
  71. qssPath = appPath + "/white/warning.qss";
  72. }else if (LHStyleManager::Instance()->GetCurSkinStyle() == SkinStyle::eBlackStyle)
  73. {
  74. qssPath = appPath + "/black/warning.qss";
  75. }
  76. QFile file(qssPath);
  77. if(file.open(QIODevice::ReadOnly))
  78. {
  79. QString stylesheet = file.readAll();
  80. this->setStyleSheet(stylesheet);
  81. file.close();
  82. } else
  83. {
  84. LH_WRITE_ERROR(QString("打开文件失败:%1").arg(file.fileName()));
  85. }
  86. }
  87. void Warning::paintEvent(QPaintEvent *event)
  88. {
  89. QPainter painter(this);
  90. painter.setRenderHint(QPainter::Antialiasing);
  91. /* 绘制阴影 */
  92. painter.drawImage(QPoint(0,0),m_shadow->image());
  93. }
  94. void Warning::do_ok()
  95. {
  96. emit signal_ok();
  97. m_isOk = true;
  98. this->close();
  99. }
  100. /**
  101. * @brief 重新设置显示文字的区域大小,主要是设置宽度,为计算出左侧的图标位置
  102. * 字号是18,每个汉字的宽度、高度都是18,行高是27。
  103. * 文字显示区域最大宽度是306,显示17个汉字。
  104. * 阴影宽度是16,需要加上阴影宽度的坐标。
  105. *
  106. */
  107. void Warning::resetLabelSize()
  108. {
  109. int TextCount = ui->label_Warn->text().count();
  110. int width = TextCount * 18;
  111. if(width > 306)
  112. {
  113. width = 306;
  114. }
  115. // ui->label_Warn->setFixedWidth(width);
  116. ui->label_Warn->resize(width, ui->label_Warn->height());
  117. /* ui->widget是布局确定的大小,获取它的大小不准确 */
  118. int widgetWidth = this->width();
  119. int x = (widgetWidth - width) / 2;
  120. // int x = (widgetWidth - width - ui->label_warnIcon->width() - 12) / 2;
  121. // x = x + ui->label_warnIcon->width() + 12; /* 加上图标的宽度,和图标之间的间距 */
  122. ui->label_Warn->move(x, ui->label_Warn->y());
  123. }
  124. /* 移动警告图标 */
  125. void Warning::moveWarnICON()
  126. {
  127. int x = ui->label_Warn->x() - ui->label_warnIcon->width() - 12;
  128. int y = ui->label_Warn->y() + ui->label_Warn->height() / 2 - ui->label_warnIcon->height() / 2;
  129. ui->label_warnIcon->move(x,y);
  130. }