-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.cpp
103 lines (83 loc) · 3.09 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
97
98
99
100
101
102
103
#include "popup.h"
#include <QPainter>
#include <QApplication>
#include <QDesktopWidget>
#include <QDebug>
PopUp::PopUp(QWidget *parent) : QWidget(parent)
{
setWindowFlags(Qt::FramelessWindowHint | // Disable window decoration
Qt::Tool | // Discard display in a separate window
Qt::WindowStaysOnTopHint); // Set on top of all windows
setAttribute(Qt::WA_TranslucentBackground); // Indicates that the background will be transparent
setAttribute(Qt::WA_ShowWithoutActivating); // At the show, the widget does not get the focus automatically
animation.setTargetObject(this); // Set the target animation
animation.setPropertyName("popupOpacity"); //
connect(&animation, &QAbstractAnimation::finished, this, &PopUp::hide);
label.setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
label.setStyleSheet("QLabel { color : white; "
"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); // Set the text in the Label
adjustSize(); // With the recalculation notice sizes
}
void PopUp::show()
{
setWindowOpacity(0.0); // Set the transparency to zero
animation.setDuration(200); // Configuring the duration of the animation
animation.setStartValue(0.0); // The start value is 0 (fully transparent widget)
animation.setEndValue(1.0); // End - completely opaque widget
setGeometry(QApplication::desktop()->availableGeometry().width() - 36 - width() + QApplication::desktop() -> availableGeometry().x(),
QApplication::desktop()->availableGeometry().height() - 36 - height() + QApplication::desktop() -> availableGeometry().y(),
width(),
height());
QWidget::show();
animation.start();
timer->start(3000);
}
void PopUp::hideAnimation()
{
timer->stop();
animation.setDuration(1000);
animation.setStartValue(1.0);
animation.setEndValue(0.0);
animation.start();
}
void PopUp::hide()
{
// If the widget is transparent, then hide it
if(getPopupOpacity() == 0.0){
QWidget::hide();
}
}
void PopUp::setPopupOpacity(float opacity)
{
popupOpacity = opacity;
setWindowOpacity(opacity);
}
float PopUp::getPopupOpacity() const
{
return popupOpacity;
}