-
Notifications
You must be signed in to change notification settings - Fork 0
/
NotifyWidget.cpp
224 lines (184 loc) · 4.94 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include "NotifyWidget.h"
#include <QtCore/QPropertyAnimation>
#include <QtCore/QPointer>
#include <QtCore/QTimer>
#include <QtCore/QDebug>
#include <QtGui/QDesktopWidget>
#include <QtGui/QApplication>
#include <QtGui/QHBoxLayout>
#include <QtGui/QGridLayout>
#include <QtGui/QMouseEvent>
#include <QtGui/QVBoxLayout>
#include <QtGui/QLabel>
namespace Notify
{
Label::Label(const QPixmap &icon, const QString &text, int timeout)
{
m_icon = new QLabel(this);
m_icon->setPixmap(icon);
m_icon->setMaximumWidth(16);
m_icon->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
if(icon.width() > 16) {
m_icon->setScaledContents(true);
}
m_text = new QLabel(text, this);
m_text->installEventFilter(this);
connect(m_text, SIGNAL(linkActivated(QString)), SIGNAL(linkClicked(QString)));
m_timeout = timeout;
QHBoxLayout *lay = new QHBoxLayout(this);
lay->setMargin(0);
lay->addWidget(m_icon);
lay->addWidget(m_text);
if(timeout > 0) {
QTimer::singleShot(timeout, this, SIGNAL(timeout()));
}
}
bool Label::eventFilter(QObject *obj, QEvent *event)
{
if(event->type() == QEvent::MouseButtonRelease) {
emit clicked();
}
return QFrame::eventFilter(obj, event);
}
Widget::Widget(QWidget *parent):
QWidget(parent, Qt::Tool | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint),
m_mouseInside(false),
m_animation(false),
m_position(BottomRight),
m_limit(20)
{
m_centralWidget = new QFrame(this);
m_centralWidget->setObjectName("centralWidget");
QGridLayout *lay = new QGridLayout(this);
lay->setMargin(0);
lay->setSpacing(0);
lay->addWidget(m_centralWidget);
m_layout = new QVBoxLayout(m_centralWidget);
m_layout->setMargin(0);
m_layout->setSpacing(0);
setAttribute(Qt::WA_TranslucentBackground);
}
void Widget::addNotify(const QPixmap &icon, const QString &text, int timeout)
{
QPointer<Label> label = new Label(icon, "<style type=\"text/css\">" + styleSheet() + "</style>" + text, timeout);
connect(label, SIGNAL(timeout()), SLOT(maybeRemove()));
connect(label, SIGNAL(clicked()), SLOT(removeAll()));
connect(label, SIGNAL(linkClicked(QString)), SLOT(removeClicked(QString)));
if((m_limit > 0) && (m_layout->count() >= m_limit)) {
delete m_layout->takeAt(0)->widget();
}
m_layout->addWidget(label);
if(!isVisible()) {
move(-1000, -1000);
show();
}
adjustSize();
}
void Widget::resizeEvent(QResizeEvent *event)
{
if(m_layout->count()) {
QRect desktop = qApp->desktop()->availableGeometry();
bool animate = (x() < 0) && m_animation;
if(!animate) {
switch(m_position) {
case TopLeft:
move(desktop.left(), desktop.top());
break;
case TopRight:
move(desktop.right() - width(), desktop.top());
break;
case BottomLeft:
move(desktop.left(), desktop.bottom() - height());
break;
case BottomRight:
move(desktop.right() - width(), desktop.bottom() - height());
break;
}
} else {
QPoint target;
QPropertyAnimation *animation = new QPropertyAnimation(this, "pos");
switch(m_position) {
case TopLeft:
move(desktop.left(), desktop.top() - height());
target = QPoint(desktop.left(), desktop.top());
break;
case TopRight:
move(desktop.right() - width(), desktop.top() - height());
target = QPoint(desktop.right() - width(), desktop.top());
break;
case BottomLeft:
{
move(desktop.left(), desktop.bottom());
target = QPoint(desktop.left(), desktop.bottom() - height());
}
break;
case BottomRight:
{
move(desktop.right() - width(), desktop.bottom());
target = QPoint(desktop.right() - width(), desktop.bottom() - height());
}
break;
}
animation->setStartValue(pos());
animation->setEndValue(target);
animation->setDuration(150);
animation->start(QPropertyAnimation::DeleteWhenStopped);
}
}
}
void Widget::enterEvent(QEvent *event)
{
QWidget::enterEvent(event);
m_mouseInside = true;
}
void Widget::leaveEvent(QEvent *event)
{
QWidget::leaveEvent(event);
m_mouseInside = false;
foreach(Label *label, m_timedOut) {
if(label) {
removeLabel(label);
}
}
m_timedOut.clear();
}
void Widget::maybeRemove()
{
if(Label *label = qobject_cast<Label*>(sender())) {
if(m_mouseInside) {
m_timedOut << label;
} else {
removeLabel(label);
}
}
}
void Widget::removeClicked(const QString &url)
{
if(Label *label = qobject_cast<Label*>(sender())) {
if(!m_timedOut.contains(label)) {
removeLabel(label);
}
}
emit linkClicked(url);
}
void Widget::removeLabel(Label *label)
{
if(label) {
m_timedOut.removeAll(label);
label->setParent(0);
label->deleteLater();
}
m_centralWidget->adjustSize();
adjustSize();
if(!m_layout->count()) {
hide();
}
}
void Widget::removeAll()
{
m_timedOut.clear();
foreach(Label *label, findChildren<Label*>()) {
removeLabel(label);
}
}
}