warning.cpp 5.7 KB

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