-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.cpp
96 lines (72 loc) · 2.46 KB
/
popup.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
#include "popup.h"
PopUp::PopUp(QWidget *parent) : QWidget(parent) {
setWindowFlags(Qt::FramelessWindowHint |
Qt::Tool |
Qt::WindowStaysOnTopHint);
setAttribute(Qt::WA_TranslucentBackground);
setAttribute(Qt::WA_ShowWithoutActivating);
animation.setTargetObject(this);
animation.setPropertyName("popupOpacity");
connect(&animation, &QAbstractAnimation::finished, this, &PopUp::hide);
label.setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
label.setStyleSheet("QLabel { color : white; "
"font-size: 14px;"
"margin-top: 6px;"
"margin-bottom: 6px;"
"margin-left: 10px;"
"margin-right: 10px; }");
layout.addWidget(&label, 0, 0);
setLayout(&layout);
timer = new QTimer();
connect(timer, &QTimer::timeout, this, &PopUp::hideAnimation);
}
void PopUp::paintEvent(QPaintEvent *event) {
Q_UNUSED(event)
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
QRect roundedRect;
roundedRect.setX(rect().x() + 5);
roundedRect.setY(rect().y() + 5);
roundedRect.setWidth(rect().width() - 10);
roundedRect.setHeight(rect().height() - 10);
painter.setBrush(QBrush(QColor(0,0,0,180)));
painter.setPen(Qt::NoPen);
painter.drawRoundedRect(roundedRect, 10, 10);
}
void PopUp::setPopupText(const QString &text) {
label.setText(text);
adjustSize();
}
void PopUp::show() {
setWindowOpacity(0.0);
animation.setDuration(150);
animation.setStartValue(0.0);
animation.setEndValue(1.0);
int padding = 25;
setGeometry(QApplication::activeWindow()->geometry().width() - padding - width() + QApplication::activeWindow()->geometry().x(),
QApplication::activeWindow()->geometry().height() - padding - height() + QApplication::activeWindow()->geometry().y(),
width(),
height());
QWidget::show();
animation.start();
timer->start(1000);
}
void PopUp::hideAnimation() {
timer->stop();
animation.setDuration(1000);
animation.setStartValue(1.0);
animation.setEndValue(0.0);
animation.start();
}
void PopUp::hide() {
if(getPopupOpacity() == 0.0){
QWidget::hide();
}
}
void PopUp::setPopupOpacity(float opacity) {
popupOpacity = opacity;
setWindowOpacity(opacity);
}
float PopUp::getPopupOpacity() const {
return popupOpacity;
}