-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotify.cpp
156 lines (126 loc) · 4.23 KB
/
notify.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
146
147
148
149
150
151
152
153
154
155
156
#include <QPropertyAnimation>
#include <QTimer>
#include <QLabel>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QDebug>
#include <QPixmap>
#include <QFontMetrics>
#include <QDesktopServices>
#include "notify.h"
Notify::Notify (int displayTime, QWidget *parent) : QWidget(parent),
displayTime(displayTime)
{
this->setWindowFlags(Qt::FramelessWindowHint|Qt::WindowSystemMenuHint| Qt::Tool | Qt::WindowStaysOnTopHint);
this->setAttribute(Qt::WA_NoSystemBackground, true);
this->setAttribute(Qt::WA_TranslucentBackground,true);
backgroundLabel = new QLabel(this);
backgroundLabel->move(0, 0);
backgroundLabel->setObjectName("notify-background");
QHBoxLayout *mainLayout = new QHBoxLayout(backgroundLabel);
QVBoxLayout *contentLayout = new QVBoxLayout();
iconLabel = new QLabel(backgroundLabel);
iconLabel->setFixedWidth(40);
iconLabel->setAlignment(Qt::AlignCenter);
titleLabel = new QLabel(backgroundLabel);
titleLabel->setObjectName("notify-title");
bodyLabel = new QLabel(backgroundLabel);
bodyLabel->setObjectName("notify-body");
QFont font = bodyLabel->font();
font.setPixelSize(12);
bodyLabel->setFont(font);
contentLayout->addWidget(titleLabel);
contentLayout->addWidget(bodyLabel);
mainLayout->addWidget(iconLabel);
mainLayout->addSpacing(5);
mainLayout->addLayout(contentLayout);
closeBtn = new QPushButton("×", backgroundLabel);
closeBtn->setObjectName("notify-close-btn");
closeBtn->setFixedSize(24, 24);
connect(closeBtn, &QPushButton::clicked, this, [this]{
Q_EMIT disappeared();
});
this->setStyleSheet("#notify-background {"
"border: 1px solid #ccc;"
"background:white;"
"border-radius: 4px;"
"} "
"#notify-title{"
"font-weight: bold;"
"color: #333;"
"font-size: 14px;"
"}"
"#notify-body{"
"color: #444;"
"}"
"#notify-close-btn{ "
"border: 0;"
"color: #999;"
"}"
"#notify-close-btn:hover{ "
"background: #ccc;"
"}");
}
void Notify::showGriant()
{
this->show();
titleLabel->setText(title);
QPixmap tempPix = QPixmap(this->icon);
tempPix = tempPix.scaled(QSize(30, 30), Qt::KeepAspectRatio);
iconLabel->setPixmap(tempPix);
backgroundLabel->setFixedSize(this->size());
closeBtn->move(backgroundLabel->width() - closeBtn->width(), 0);
// 超过长度省略号
QFontMetrics elidfont(bodyLabel->font());
QString text = elidfont.elidedText(this->body, Qt::ElideRight, bodyLabel->width() - 5);
bodyLabel->setText(text);
QPropertyAnimation *animation = new QPropertyAnimation(this, "windowOpacity", this);
animation->setStartValue(0);
animation->setEndValue(1);
animation->setDuration(200);
animation->start();
connect(animation, &QPropertyAnimation::finished, this, [animation, this](){
animation->deleteLater();
QTimer::singleShot(displayTime, this, [this](){
this->hideGriant();
});
});
}
void Notify::hideGriant()
{
QPropertyAnimation *animation = new QPropertyAnimation(this, "windowOpacity", this);
animation->setStartValue(this->windowOpacity());
animation->setEndValue(0);
animation->setDuration(200);
animation->start();
connect(animation, &QPropertyAnimation::finished, this, [animation, this](){
this->hide();
animation->deleteLater();
Q_EMIT disappeared();
});
}
void Notify::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton) {
if(!url.isEmpty()){
QDesktopServices::openUrl(url);
}
hideGriant();
}
}
void Notify::setUrl(const QString &value)
{
url = value;
}
void Notify::setBody(const QString &value)
{
body = value;
}
void Notify::setTitle(const QString &value)
{
title = value;
}
void Notify::setIcon(const QString &value)
{
icon = value;
}