-
Notifications
You must be signed in to change notification settings - Fork 7
/
toast.cpp
90 lines (71 loc) · 2.74 KB
/
toast.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
* Toast Widget
* @brief 自动消失提示框
* @anchor Ho229
* @date 2020/12/12
*/
#include "toast.h"
#include <QScreen>
#include <QHBoxLayout>
#include <QGuiApplication>
#include <QPropertyAnimation>
#include <QSequentialAnimationGroup>
Toast::Toast(QWidget *parent, int horizontalMargin, int verticalMargin,
int maximumWidth, bool wordWrap, int waitMsecs, const QString &style) :
QWidget(parent),
m_messageLabel(new QLabel(this)),
m_layout(new QHBoxLayout(this)),
m_animation(new QSequentialAnimationGroup(this)),
m_posAnimation(new QPropertyAnimation(this, "pos")),
m_opacityAnimation(new QPropertyAnimation(this, "windowOpacity"))
{
this->setAttribute(Qt::WA_TransparentForMouseEvents);
this->setAttribute(Qt::WA_TranslucentBackground);
this->setWindowFlags(Qt::FramelessWindowHint | Qt::ToolTip | Qt::CustomizeWindowHint);
m_layout->addWidget(m_messageLabel);
m_layout->setContentsMargins(0, 0, 0, 0);
m_messageLabel->setStyleSheet(style);
m_messageLabel->setContentsMargins(horizontalMargin, verticalMargin,
horizontalMargin, verticalMargin);
m_messageLabel->setAlignment(Qt::AlignCenter);
m_messageLabel->setMaximumWidth(maximumWidth);
m_messageLabel->setWordWrap(wordWrap);
/* 显示动画 */
m_posAnimation->setDuration(300);
m_posAnimation->setEasingCurve(QEasingCurve::OutCubic);
/* 消失动画 */
m_opacityAnimation->setDuration(250);
m_opacityAnimation->setStartValue(1);
m_opacityAnimation->setEndValue(0);
m_animation->addAnimation(m_posAnimation);
m_animation->addPause(waitMsecs);
m_animation->addAnimation(m_opacityAnimation);
connect(m_animation, &QSequentialAnimationGroup::finished, this, &Toast::close);
}
Toast::~Toast()
{
}
void Toast::setText(const QString &text)
{
// 自适应大小
m_messageLabel->setText(text);
m_messageLabel->adjustSize();
this->adjustSize();
// 计算动画起止坐标
QRect rect = this->rect();
QRect parentGeometry = this->parentWidget() == nullptr ?
QGuiApplication::primaryScreen()->geometry() : this->parentWidget()->frameGeometry();
rect.moveCenter(parentGeometry.center());
rect.translate(0, parentGeometry.height() / 4);
m_posAnimation->setEndValue(rect.topLeft());
rect.translate(0, parentGeometry.height() / 4 - this->height() / 2);
m_posAnimation->setStartValue(rect.topLeft());
}
void Toast::toast()
{
if(m_animation->state() == QSequentialAnimationGroup::Running)
m_animation->stop();
this->setWindowOpacity(1); // 透明度复位
this->show();
m_animation->start();
}