tipwidget.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #include "tipwidget.h"
  2. #include "ui_tipwidget.h"
  3. #include <QPainter>
  4. #include <QTimer>
  5. #include <QPropertyAnimation>
  6. #include <QGraphicsOpacityEffect>
  7. #include <QPixmap>
  8. #include <QElapsedTimer>
  9. #include <QTime>
  10. #include <QDebug>
  11. static QList<TipWidget*> sl_tipWdgs{Q_NULLPTR, Q_NULLPTR, Q_NULLPTR, Q_NULLPTR};
  12. void TipWidget::display(FormType type, QWidget* parent, int nTitleHeight)
  13. {
  14. if (Q_NULLPTR == parent || sl_tipWdgs.count() > 4)
  15. return;
  16. static QElapsedTimer s_timer;
  17. if (s_timer.elapsed() > 5 && s_timer.restart() < 500)
  18. {
  19. return;
  20. }
  21. int nPosX = (parent->width() - WIDTH) / 2;
  22. int nPosY = nTitleHeight;
  23. TipWidget* obj = Q_NULLPTR;
  24. for (int i = 0; i < sl_tipWdgs.count(); ++i)
  25. {
  26. if (sl_tipWdgs.at(i) == Q_NULLPTR)
  27. {
  28. obj = new TipWidget(type, parent);
  29. obj->setEndPos(nPosX, nPosY + i * HEIGHT + 10);
  30. if (!obj->isVisible())
  31. obj->show();
  32. obj->run();
  33. sl_tipWdgs[i] = obj;
  34. break;
  35. }
  36. }
  37. }
  38. /* 这个是上面的重载函数,可以自定义文本 */
  39. void TipWidget::display(FormType type, QString text, QWidget *parent, int nTitleHeight)
  40. {
  41. if (Q_NULLPTR == parent || sl_tipWdgs.count() > 4)
  42. return;
  43. static QElapsedTimer s_timer;
  44. if (s_timer.elapsed() > 5 && s_timer.restart() < 500)
  45. {
  46. return;
  47. }
  48. int nPosX = (parent->width() - WIDTH) / 2;
  49. int nPosY = nTitleHeight;
  50. TipWidget* obj = Q_NULLPTR;
  51. for (int i = 0; i < sl_tipWdgs.count(); ++i)
  52. {
  53. if (sl_tipWdgs.at(i) == Q_NULLPTR)
  54. {
  55. obj = new TipWidget(type, text, parent);
  56. obj->setEndPos(nPosX, nPosY + i * HEIGHT + 10);
  57. if (!obj->isVisible())
  58. obj->show();
  59. obj->run();
  60. sl_tipWdgs[i] = obj;
  61. break;
  62. }
  63. }
  64. }
  65. TipWidget::TipWidget(FormType type, QWidget *parent) :
  66. QWidget(parent),
  67. ui(new Ui::TipWidget)
  68. {
  69. ui->setupUi(this);
  70. setWindowFlag(Qt::FramelessWindowHint);
  71. _endRect = rect();
  72. setFormType(type);
  73. // ui->label_tipIcon->hide();
  74. m_customText = false;
  75. }
  76. TipWidget::TipWidget(FormType type, QString& text, QWidget *parent) :
  77. QWidget(parent),
  78. ui(new Ui::TipWidget),
  79. m_tipText(text)
  80. {
  81. ui->setupUi(this);
  82. setWindowFlag(Qt::FramelessWindowHint);
  83. _endRect = rect();
  84. m_customText = true;
  85. setFormType(type);
  86. /* 设置字体居中 */
  87. ui->label_tipTitle->setAlignment(Qt::AlignCenter);
  88. if(text.count() > 5)
  89. {
  90. QFontMetrics fm(ui->label_tipTitle->font());
  91. auto textWidth = fm.width(text);
  92. // int width = textWidth + this->width() - ui->label_tipTitle->width();
  93. int width = textWidth + this->width() - 60;
  94. this->setFixedWidth(width);
  95. }
  96. }
  97. TipWidget::~TipWidget()
  98. {
  99. delete ui;
  100. }
  101. void TipWidget::setFormType(FormType type)
  102. {
  103. QPixmap img;
  104. switch(type)
  105. {
  106. case OPERATOR_OK:
  107. _backgroundColor = QColor(246, 255, 237);
  108. _borderColor = QColor(183, 235, 143);
  109. img.load(R"(:/Tip/Complete2x.png)");
  110. if(m_customText)
  111. {
  112. ui->label_tipTitle->setText(m_tipText);
  113. }else
  114. {
  115. ui->label_tipTitle->setText("操作成功!");
  116. }
  117. break;
  118. case OPERATOR_FAIL:
  119. _backgroundColor = QColor(255, 241, 240);
  120. _borderColor = QColor(255, 163, 158);
  121. img.load(":/Tip/Failed2x.png");
  122. if(m_customText)
  123. {
  124. ui->label_tipTitle->setText(m_tipText);
  125. }else
  126. {
  127. ui->label_tipTitle->setText("操作失败!");
  128. }
  129. break;
  130. case OPERATOR_TIP:
  131. _backgroundColor = QColor(236, 238, 254);
  132. _borderColor = QColor(68, 88, 254);
  133. img.load(":/Tip/Wait2x.png");
  134. if(m_customText)
  135. {
  136. ui->label_tipTitle->setText(m_tipText);
  137. }else
  138. {
  139. ui->label_tipTitle->setText("普通提示!");
  140. }
  141. break;
  142. case OPERATOR_WARN:
  143. _backgroundColor = QColor(255, 251, 230);
  144. _borderColor = QColor(255, 229, 143);
  145. img.load(":/Tip/Tips2x.png");
  146. if(m_customText)
  147. {
  148. ui->label_tipTitle->setText(m_tipText);
  149. }else
  150. {
  151. ui->label_tipTitle->setText("需注意!");
  152. }
  153. break;
  154. default:
  155. break;
  156. }
  157. img = img.scaled(ui->label_tipIcon->width(), ui->label_tipIcon->height(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
  158. ui->label_tipIcon->setPixmap(img);
  159. }
  160. void TipWidget::setEndPos(int x, int y)
  161. {
  162. _endRect.moveTo(x, y);
  163. }
  164. void TipWidget::run()
  165. {
  166. if (nullptr == _animation)
  167. _animation = new QPropertyAnimation(this, "geometry");
  168. _animation->setDuration(1 * 1000);
  169. _animation->setStartValue(QRect(_endRect.x(), _endRect.y() + 150, _endRect.width(), _endRect.height()));
  170. _animation->setEndValue(_endRect);
  171. connect(_animation, &QPropertyAnimation::finished, this, [this]{
  172. if (nullptr == _pKillTimer)
  173. _pKillTimer = new QTimer(this);
  174. _pKillTimer->start(1 * 1000);
  175. connect(_pKillTimer, &QTimer::timeout, this, &TipWidget::onFadeOut);
  176. });
  177. _animation->start();
  178. }
  179. void TipWidget::onFadeOut()
  180. {
  181. if (nullptr != _pKillTimer && _pKillTimer->isActive())
  182. {
  183. _pKillTimer->stop();
  184. }
  185. QGraphicsOpacityEffect* effect = new QGraphicsOpacityEffect(this);
  186. this->setGraphicsEffect(effect);
  187. if (nullptr == _animFadeOut)
  188. _animFadeOut = new QPropertyAnimation(effect, "opacity");
  189. _animFadeOut->setDuration(1 * 1000);
  190. _animFadeOut->setStartValue(1.0);
  191. _animFadeOut->setEndValue(0.0);
  192. connect(_animFadeOut, &QPropertyAnimation::finished, this, [this]{
  193. onBtnClose();
  194. });
  195. _animFadeOut->start();
  196. }
  197. void TipWidget::onBtnClose()
  198. {
  199. close();
  200. for (int i = 0; i < sl_tipWdgs.count(); ++i)
  201. {
  202. if (sl_tipWdgs.at(i) == this)
  203. {
  204. sl_tipWdgs[i] = Q_NULLPTR;
  205. }
  206. }
  207. deleteLater();
  208. }
  209. void TipWidget::paintEvent(QPaintEvent *event)
  210. {
  211. QRect rc(rect());
  212. rc.adjust(1, 1, -2, -2);
  213. QPainter painter(this);
  214. painter.setRenderHint(QPainter::Antialiasing, true);
  215. painter.save();
  216. QBrush brush(_backgroundColor);
  217. painter.setBrush(brush);
  218. painter.drawRoundedRect(rc, 2.0, 2.0);
  219. painter.restore();
  220. painter.save();
  221. QPen pen;
  222. pen.setWidth(2);
  223. pen.setColor(_borderColor);
  224. painter.setPen(pen);
  225. painter.setBrush(Qt::transparent);
  226. painter.drawRoundedRect(rc, 2.0, 2.0);
  227. painter.restore();
  228. QWidget::paintEvent(event);
  229. }