forked from rigexpert/AntScope2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotification.cpp
More file actions
161 lines (136 loc) · 4.05 KB
/
Copy pathNotification.cpp
File metadata and controls
161 lines (136 loc) · 4.05 KB
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
#include "Notification.h"
#include <QTimer>
#include <QLabel>
#include <QFont>
#include <QPainter>
#include <QPaintEvent>
#include <QCoreApplication>
#include <QDesktopServices>
#include <QDebug>
namespace
{
const QColor FILL_COLOR(66, 68, 70);
const QColor TEXT_COLOR(Qt::red);
const QFont FONT("MS Sans Serif", 14);
const int DISPLAY_TIME = 1500;
}
QQueue<Notification*> Notification::queue;
bool Notification::underProcessing = false;
Notification::Notification(const QString& text, QWidget* parent)
: Notification(text, FONT, DISPLAY_TIME, parent)
{}
Notification::Notification(const QString& text, const QFont& font, int milliseconds, QWidget* parent)
: QWidget(parent)
, m_label(text)
, m_opacity(.5)
, m_milliseconds(milliseconds)
{
setFont(font);
m_textColor = TEXT_COLOR;
m_label.prepare(QTransform(), font);
setWindowFlags(windowFlags() | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
if (parent)
{
QPoint offset(m_label.size().width() / 2 + 20, m_label.size().height() / 2 + 20);
setGeometry(QRect(parent->rect().center() - offset, parent->rect().center() + offset));
}
else
resize(270, 80);
}
Notification::~Notification()
{}
void Notification::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
if (!m_url.isEmpty())
{
QDesktopServices::openUrl(QUrl::fromUserInput(m_url));
event->accept();
}
} else {
QWidget::mouseReleaseEvent(event);
}
fadeOut();
}
void Notification::showImmediatly()
{
show();
QCoreApplication::processEvents();
}
void Notification::run()
{
underProcessing = true;
show();
update();
QTimer::singleShot(m_milliseconds, this, &Notification::fadeOut);
}
void Notification::fadeOut()
{
m_animation = new QPropertyAnimation(this, "opacity", this);
connect(m_animation, &QPropertyAnimation::finished, this, [=] () {
underProcessing = false;
if (!queue.isEmpty())
queue.dequeue()->run();
this->deleteLater();
});
m_animation->setDuration(500);
m_animation->setStartValue(opacity());
m_animation->setEndValue(0.);
m_animation->start(QAbstractAnimation::DeleteWhenStopped);
}
void Notification::setOpacity(qreal opacity)
{
m_opacity = opacity;
update();
}
qreal Notification::opacity() const
{
return m_opacity;
}
void Notification::showMessage(const QString& message, QColor textColor, QRect rect, int milliseconds, QWidget* parent)
{
Notification* notification = new Notification(message, FONT, milliseconds, parent);
notification->setTextColor(textColor);
notification->setGeometry(rect);
//notification->run();
append(notification);
}
void Notification::showMessage(const QString& message, const QFont& font, int milliseconds, QWidget* parent)
{
append(new Notification(message, font, milliseconds, parent));
}
void Notification::showMessage(const QString& message, QWidget* parent)
{
showMessage(message, FONT, parent);
}
void Notification::showMessage(const QString& message, const QFont& font, QWidget* parent)
{
showMessage(message, font, DISPLAY_TIME, parent);
}
void Notification::showMessage(const QString& message, QString& url, QRect rect, int milliseconds, QWidget* parent)
{
Notification* notification = new Notification(message, FONT, milliseconds, parent);
notification->m_url = url;
notification->setGeometry(rect);
//notification->run();
append(notification);
}
void Notification::paintEvent(QPaintEvent* event)
{
QPainter p(this);
p.setOpacity(m_opacity);
p.fillRect(event->rect(), FILL_COLOR);
p.setPen(m_textColor);
p.drawRect(event->rect().adjusted(0, 0, -1, -1));
p.setFont(font());
QSize halfSize = m_label.size().toSize() / 2;
p.drawStaticText(rect().center() - QPoint(halfSize.width(), halfSize.height()), m_label);
}
void Notification::append(Notification *notification)
{
if (!underProcessing) {
notification->run();
} else {
queue.enqueue(notification);
}
}