-
Notifications
You must be signed in to change notification settings - Fork 7
/
notifywidget.cpp
145 lines (119 loc) · 3.91 KB
/
notifywidget.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* Notify Widget
* @brief 右下角提示窗
* @anchor Ho229<2189684957@qq.com>
* @date 2021/2/1
*/
#include "notifywidget.h"
#include <QLabel>
#include <QScreen>
#include <QEventLoop>
#include <QSpacerItem>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGuiApplication>
#include <QPropertyAnimation>
#include "countdownbutton.h"
NotifyWidget::NotifyWidget(QWidget *parent, const QString &title,
const QString &messsage, const QIcon& icon,
const QString &style) :
QWidget(parent),
m_iconLabel(new QLabel(this)),
m_titleLabel(new QLabel(this)),
m_messageLabel(new QLabel(this)),
m_hLayout(new QHBoxLayout()),
m_mLayout(new QHBoxLayout()),
m_vLayout(new QVBoxLayout(this)),
m_hSpacer(new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum)),
m_icon(icon),
m_closeButton(new CountdownButton(this)),
m_animation(new QPropertyAnimation(this, "pos", this))
{
// Title and close button
m_hLayout->addWidget(m_titleLabel);
m_hLayout->addSpacerItem(m_hSpacer);
m_hLayout->addWidget(m_closeButton);
// Icon and message
m_mLayout->addWidget(m_iconLabel);
m_mLayout->addWidget(m_messageLabel);
m_mLayout->setStretch(1, 1);
m_mLayout->setSpacing(9);
m_vLayout->addLayout(m_hLayout);
m_vLayout->addLayout(m_mLayout);
QFont font = m_titleLabel->font();
font.setPointSize(15);
font.setBold(true);
m_titleLabel->setFont(font);
m_titleLabel->setText(title);
m_titleLabel->setMaximumWidth(300);
if(m_icon.isNull())
m_iconLabel->hide();
else
{
m_iconLabel->setPixmap(m_icon.pixmap(m_iconLabel->size()));
m_iconLabel->adjustSize();
}
m_messageLabel->setText(messsage);
m_messageLabel->setWordWrap(true);
m_messageLabel->setMaximumSize(QSize(300, 70));
m_closeButton->setFixedSize(QSize(30, 30));
m_closeButton->setIcon(QIcon(":/image/resource/image/close.png"));
QObject::connect(m_closeButton, &CountdownButton::clicked, this,
&NotifyWidget::closeAnimation);
m_animation->setDuration(350);
m_animation->setEasingCurve(QEasingCurve::OutQuart);
this->setAttribute(Qt::WA_DeleteOnClose);
this->setWindowFlags(Qt::FramelessWindowHint | Qt::Tool | Qt::WindowStaysOnTopHint
| Qt::WindowSystemMenuHint);
this->setStyleSheet(style);
this->setLayout(m_vLayout);
this->adjustSize();
}
NotifyWidget::~NotifyWidget()
{
}
void NotifyWidget::setCloseCountdown(int ms)
{
m_closeButton->conutdownCilk(ms);
}
int NotifyWidget::closeCountdown() const
{
return m_closeButton->countdown();
}
void NotifyWidget::animatMove(int x, int y)
{
if(m_animation->state() == QPropertyAnimation::Running)
m_animation->stop();
m_animation->setEndValue(QPoint(x, y));
m_animation->setStartValue(this->pos());
m_animation->start();
}
int NotifyWidget::leftTime() const
{
return m_animation->duration() - m_animation->currentLoopTime();
}
void NotifyWidget::showEvent(QShowEvent *event)
{
this->animatMove(this->x() - this->width(), this->y());
return QWidget::showEvent(event);
}
void NotifyWidget::closeAnimation()
{
m_isClosing = true;
this->animatMove(this->x() + this->width(), this->y());
QObject::connect(m_animation, &QPropertyAnimation::finished, this,
[this]{
if(QGuiApplication::primaryScreen()->size().width() == this->x())
this->close();
});
}
void NotifyWidget::closeEvent(QCloseEvent *event)
{
emit closed();
return QWidget::closeEvent(event);
}
void NotifyWidget::mouseReleaseEvent(QMouseEvent *event)
{
emit clicked();
return QWidget::mouseReleaseEvent(event);
}