/LQFormWidget/src/msgNotify/notify.cpp

https://github.com/kevinlq/LQFramKit · C++ · 159 lines · 127 code · 30 blank · 2 comment · 3 complexity · 79cff81aa8efac302f0eba6deb71ace3 MD5 · raw file

  1. #include <QPropertyAnimation>
  2. #include <QTimer>
  3. #include <QLabel>
  4. #include <QHBoxLayout>
  5. #include <QVBoxLayout>
  6. #include <QDebug>
  7. #include <QPixmap>
  8. #include <QFontMetrics>
  9. #include <QDesktopServices>
  10. //#include "notify.h"
  11. #include "msgNotify/notify.h"
  12. Notify::Notify (int displayTime, QWidget *parent) : QWidget(parent),
  13. displayTime(displayTime)
  14. {
  15. this->setWindowFlags(Qt::FramelessWindowHint|Qt::WindowSystemMenuHint|
  16. Qt::Tool | Qt::WindowStaysOnTopHint);
  17. this->setAttribute(Qt::WA_NoSystemBackground, true);
  18. this->setAttribute(Qt::WA_TranslucentBackground,true);
  19. backgroundLabel = new QLabel(this);
  20. backgroundLabel->move(0, 0);
  21. backgroundLabel->setObjectName("notify-background");
  22. QHBoxLayout *mainLayout = new QHBoxLayout(backgroundLabel);
  23. QVBoxLayout *contentLayout = new QVBoxLayout();
  24. iconLabel = new QLabel(backgroundLabel);
  25. iconLabel->setFixedWidth(40);
  26. iconLabel->setAlignment(Qt::AlignCenter);
  27. titleLabel = new QLabel(backgroundLabel);
  28. titleLabel->setObjectName("notify-title");
  29. bodyLabel = new QLabel(backgroundLabel);
  30. bodyLabel->setObjectName("notify-body");
  31. QFont font = bodyLabel->font();
  32. font.setPixelSize(12);
  33. bodyLabel->setFont(font);
  34. contentLayout->addWidget(titleLabel);
  35. contentLayout->addWidget(bodyLabel);
  36. mainLayout->addWidget(iconLabel);
  37. mainLayout->addSpacing(5);
  38. mainLayout->addLayout(contentLayout);
  39. closeBtn = new QPushButton("×", backgroundLabel);
  40. closeBtn->setObjectName("notify-close-btn");
  41. closeBtn->setFixedSize(24, 24);
  42. connect(closeBtn, &QPushButton::clicked, this, [this]{
  43. Q_EMIT disappeared();
  44. });
  45. this->setStyleSheet("#notify-background {"
  46. "border: 1px solid #ccc;"
  47. "background:white;"
  48. "border-radius: 4px;"
  49. "} "
  50. "#notify-title{"
  51. "font-weight: bold;"
  52. "color: #333;"
  53. "font-size: 14px;"
  54. "}"
  55. "#notify-body{"
  56. "color: #444;"
  57. "}"
  58. "#notify-close-btn{ "
  59. "border: 0;"
  60. "color: #999;"
  61. "}"
  62. "#notify-close-btn:hover{ "
  63. "background: #ccc;"
  64. "}");
  65. }
  66. void Notify::showGriant()
  67. {
  68. this->show();
  69. titleLabel->setText(title);
  70. QPixmap tempPix = QPixmap(this->icon);
  71. tempPix = tempPix.scaled(QSize(30, 30), Qt::KeepAspectRatio);
  72. iconLabel->setPixmap(tempPix);
  73. backgroundLabel->setFixedSize(this->size());
  74. closeBtn->move(backgroundLabel->width() - closeBtn->width(), 0);
  75. // 超过长度省略号
  76. QFontMetrics elidfont(bodyLabel->font());
  77. QString text = elidfont.elidedText(this->body, Qt::ElideRight,
  78. bodyLabel->width() - 5);
  79. bodyLabel->setText(text);
  80. QPropertyAnimation *animation = new QPropertyAnimation(this, "windowOpacity", this);
  81. animation->setStartValue(0);
  82. animation->setEndValue(1);
  83. animation->setDuration(200);
  84. animation->start();
  85. connect(animation, &QPropertyAnimation::finished, this, [animation, this](){
  86. animation->deleteLater();
  87. QTimer::singleShot(displayTime, this, [this](){
  88. this->hideGriant();
  89. });
  90. });
  91. }
  92. void Notify::hideGriant()
  93. {
  94. QPropertyAnimation *animation = new QPropertyAnimation(this, "windowOpacity", this);
  95. animation->setStartValue(this->windowOpacity());
  96. animation->setEndValue(0);
  97. animation->setDuration(200);
  98. animation->start();
  99. connect(animation, &QPropertyAnimation::finished, this, [animation, this](){
  100. this->hide();
  101. animation->deleteLater();
  102. Q_EMIT disappeared();
  103. });
  104. }
  105. void Notify::mousePressEvent(QMouseEvent *event)
  106. {
  107. if(event->button() == Qt::LeftButton) {
  108. if(!url.isEmpty()){
  109. QDesktopServices::openUrl(url);
  110. }
  111. hideGriant();
  112. }
  113. }
  114. void Notify::setUrl(const QString &value)
  115. {
  116. url = value;
  117. }
  118. void Notify::setBody(const QString &value)
  119. {
  120. body = value;
  121. }
  122. void Notify::setTitle(const QString &value)
  123. {
  124. title = value;
  125. }
  126. void Notify::setIcon(const QString &value)
  127. {
  128. icon = value;
  129. }