tipwidget.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. this->setFixedWidth(width);
  94. }
  95. }
  96. TipWidget::~TipWidget()
  97. {
  98. delete ui;
  99. }
  100. void TipWidget::setFormType(FormType type)
  101. {
  102. QPixmap img;
  103. switch(type)
  104. {
  105. case OPERATOR_OK:
  106. _backgroundColor = QColor(246, 255, 237);
  107. _borderColor = QColor(183, 235, 143);
  108. img.load(R"(:/Tip/Complete2x.png)");
  109. if(m_customText)
  110. {
  111. ui->label_tipTitle->setText(m_tipText);
  112. }else
  113. {
  114. ui->label_tipTitle->setText("操作成功!");
  115. }
  116. break;
  117. case OPERATOR_FAIL:
  118. _backgroundColor = QColor(255, 241, 240);
  119. _borderColor = QColor(255, 163, 158);
  120. img.load(":/Tip/Failed2x.png");
  121. if(m_customText)
  122. {
  123. ui->label_tipTitle->setText(m_tipText);
  124. }else
  125. {
  126. ui->label_tipTitle->setText("操作失败!");
  127. }
  128. break;
  129. case OPERATOR_TIP:
  130. _backgroundColor = QColor(236, 238, 254);
  131. _borderColor = QColor(68, 88, 254);
  132. img.load(":/Tip/Wait2x.png");
  133. if(m_customText)
  134. {
  135. ui->label_tipTitle->setText(m_tipText);
  136. }else
  137. {
  138. ui->label_tipTitle->setText("普通提示!");
  139. }
  140. break;
  141. case OPERATOR_WARN:
  142. _backgroundColor = QColor(255, 251, 230);
  143. _borderColor = QColor(255, 229, 143);
  144. img.load(":/Tip/Tips2x.png");
  145. if(m_customText)
  146. {
  147. ui->label_tipTitle->setText(m_tipText);
  148. }else
  149. {
  150. ui->label_tipTitle->setText("需注意!");
  151. }
  152. break;
  153. default:
  154. break;
  155. }
  156. img = img.scaled(ui->label_tipIcon->width(), ui->label_tipIcon->height(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
  157. ui->label_tipIcon->setPixmap(img);
  158. }
  159. void TipWidget::setEndPos(int x, int y)
  160. {
  161. _endRect.moveTo(x, y);
  162. }
  163. void TipWidget::run()
  164. {
  165. if (nullptr == _animation)
  166. _animation = new QPropertyAnimation(this, "geometry");
  167. _animation->setDuration(1 * 1000);
  168. _animation->setStartValue(QRect(_endRect.x(), _endRect.y() + 150, _endRect.width(), _endRect.height()));
  169. _animation->setEndValue(_endRect);
  170. connect(_animation, &QPropertyAnimation::finished, this, [this]{
  171. if (nullptr == _pKillTimer)
  172. _pKillTimer = new QTimer(this);
  173. _pKillTimer->start(1 * 1000);
  174. connect(_pKillTimer, &QTimer::timeout, this, &TipWidget::onFadeOut);
  175. });
  176. _animation->start();
  177. }
  178. void TipWidget::onFadeOut()
  179. {
  180. if (nullptr != _pKillTimer && _pKillTimer->isActive())
  181. {
  182. _pKillTimer->stop();
  183. }
  184. QGraphicsOpacityEffect* effect = new QGraphicsOpacityEffect(this);
  185. this->setGraphicsEffect(effect);
  186. if (nullptr == _animFadeOut)
  187. _animFadeOut = new QPropertyAnimation(effect, "opacity");
  188. _animFadeOut->setDuration(1 * 1000);
  189. _animFadeOut->setStartValue(1.0);
  190. _animFadeOut->setEndValue(0.0);
  191. connect(_animFadeOut, &QPropertyAnimation::finished, this, [this]{
  192. onBtnClose();
  193. });
  194. _animFadeOut->start();
  195. }
  196. void TipWidget::onBtnClose()
  197. {
  198. close();
  199. for (int i = 0; i < sl_tipWdgs.count(); ++i)
  200. {
  201. if (sl_tipWdgs.at(i) == this)
  202. {
  203. sl_tipWdgs[i] = Q_NULLPTR;
  204. }
  205. }
  206. deleteLater();
  207. }
  208. void TipWidget::paintEvent(QPaintEvent *event)
  209. {
  210. QRect rc(rect());
  211. rc.adjust(1, 1, -2, -2);
  212. QPainter painter(this);
  213. painter.setRenderHint(QPainter::Antialiasing, true);
  214. painter.save();
  215. QBrush brush(_backgroundColor);
  216. painter.setBrush(brush);
  217. painter.drawRoundedRect(rc, 2.0, 2.0);
  218. painter.restore();
  219. painter.save();
  220. QPen pen;
  221. pen.setWidth(2);
  222. pen.setColor(_borderColor);
  223. painter.setPen(pen);
  224. painter.setBrush(Qt::transparent);
  225. painter.drawRoundedRect(rc, 2.0, 2.0);
  226. painter.restore();
  227. QWidget::paintEvent(event);
  228. }