-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotificator.cpp
195 lines (156 loc) · 4.95 KB
/
notificator.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
#include <QResizeEvent>
#include <QPainter>
#include <QDebug>
#include <QLabel>
#include <QWindow>
#include "ui_notificator.h"
#include "notificator_p.h"
#include "notificator.h"
#define TAG "[Notificator]"
#define QUEUE_TIMEOUT 3000 // ms
#define ANIMATION_DURATION 150 // ms
#define NOTIFICATOR_HEIGHT 60 // px
NotificatorPrivate::NotificatorPrivate(Notificator* qq) :
q_ptr(qq),
align(Qt::AlignBottom),
notifyKillTimer(-1),
isAutoHide(true)
{
}
NotificatorPrivate::~NotificatorPrivate()
{
}
void NotificatorPrivate::init()
{
Q_Q(Notificator);
animator.setTargetObject(this);
animator.setDirection(QPropertyAnimation::Backward);
animator.setStartValue(positionOffset);
animator.setPropertyName("translation");
animator.setDuration(ANIMATION_DURATION);
animator.setEasingCurve(QEasingCurve::OutCubic);
connect(&animator, &QPropertyAnimation::stateChanged, [this, q]
(QPropertyAnimation::State newState, QPropertyAnimation::State) {
bool directionForward = animator.direction() == QPropertyAnimation::Forward;
bool directionBackward = animator.direction() == QPropertyAnimation::Backward;
bool animationStopped = newState == QPropertyAnimation::Stopped;
if (animationStopped && directionBackward)
// Animation has stopped and it set notificator to hidden state
handleNotifyRequest();
if (isAutoHide && animationStopped && directionForward)
// Animation has set notificator to completely visible state
notifyKillTimer = startTimer(QUEUE_TIMEOUT);
});
connect(q->ui->button, &QPushButton::clicked, q, &Notificator::hideNotify);
}
void NotificatorPrivate::handleNotifyRequest()
{
Q_Q(Notificator);
bool directionForward = animator.direction() == QPropertyAnimation::Forward;
bool runningForward = animator.state() == QPropertyAnimation::Running && directionForward;
bool stoppedForward = animator.state() == QPropertyAnimation::Stopped && directionForward;
if (runningForward || stoppedForward) {
// Schedule the hide event when animation finished
bool killTimerActive = notifyKillTimer == -1;
if (runningForward && !killTimerActive) {
// Hide notify when animation stopped and will be shown about 2 sec
int timeLeft = ANIMATION_DURATION - animator.currentTime();
notifyKillTimer = startTimer(QUEUE_TIMEOUT + timeLeft);
} else if (stoppedForward) {
// When notify visible and animation stopped then just hide notify
notifyKillTimer = startTimer(QUEUE_TIMEOUT);
}
return;
}
if (messageQueue.isEmpty()) {
// Prevent blinking animation
animator.stop();
return;
}
q->ui->messageLabel->setText(messageQueue.takeFirst());
animator.setStartValue(positionOffset);
animator.setEndValue(QPoint(0, NOTIFICATOR_HEIGHT));
animator.setDirection(QPropertyAnimation::Forward);
animator.start();
}
QPoint NotificatorPrivate::translation() const
{
return positionOffset;
}
void NotificatorPrivate::setTranslation(const QPoint& translation)
{
Q_Q(Notificator);
positionOffset = translation;
if (align == Qt::AlignBottom)
q->move(0, q->window()->height() - translation.y());
else if (align == Qt::AlignTop)
q->move(0, translation.y());
}
void NotificatorPrivate::timerEvent(QTimerEvent* event)
{
Q_Q(Notificator);
if (notifyKillTimer == event->timerId())
q->hideNotify();
}
Notificator::Notificator(QWidget *parent) :
QWidget(parent),
ui(new Ui::Notificator),
d_ptr(new NotificatorPrivate(this))
{
Q_D(Notificator);
ui->setupUi(this);
d->init();
}
Notificator::~Notificator()
{
delete ui;
}
void Notificator::setAlignment(Qt::Alignment alignment)
{
Q_D(Notificator);
d->align = alignment;
}
void Notificator::paintEvent(QPaintEvent*)
{
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
void Notificator::resizeEvent(QResizeEvent* event)
{
Q_D(Notificator);
resize(event->size().width(), NOTIFICATOR_HEIGHT);
move(0, window()->height() - d->positionOffset.y());
}
void Notificator::hideNotify()
{
Q_D(Notificator);
if (d->notifyKillTimer != -1) {
d->killTimer(d->notifyKillTimer);
d->notifyKillTimer = -1;
}
d->animator.stop();
d->animator.setDirection(QPropertyAnimation::Backward);
d->animator.start();
}
void Notificator::notify(const QString& message)
{
Q_D(Notificator);
if (parent() == Q_NULLPTR) {
qDebug() << TAG << "Warning: parent isn't set";
return;
}
d->messageQueue.append(message);
d->handleNotifyRequest();
}
bool Notificator::isAutoHide() const
{
Q_D(const Notificator);
return d->isAutoHide;
}
void Notificator::setAutoHide(bool isAutoHide)
{
Q_D(Notificator);
d->isAutoHide = isAutoHide;
}