Debian Codegraph @

src/librssguard/gui/notifications/basetoastnotification.cpp

raw symbols SCIP
1 // For license of this file, see <project-root-folder>/LICENSE.md.
2
3 #include "gui/notifications/basetoastnotification.h"
4
5 #include "miscellaneous/iconfactory.h"
6 #include "miscellaneous/settings.h"
7
8 #include <chrono>
9
10 #include <QAbstractButton>
11 #include <QCloseEvent>
12 #include <QLabel>
13 #include <QTimerEvent>
14
15 using namespace std::chrono_literals;
16
17 BaseToastNotification::BaseToastNotification(QWidget* parent) : QDialog(parent), m_timerId(-1) {
18 setAttribute(Qt::WidgetAttribute::WA_ShowWithoutActivating);
19 setFocusPolicy(Qt::FocusPolicy::NoFocus);
20 setAttribute(Qt::WidgetAttribute::WA_DeleteOnClose, false);
21
22 setWindowFlags(
23 #if defined(Q_OS_MAC)
24 Qt::WindowType::SubWindow |
25 #else
26 Qt::WindowType::Tool |
27 #endif
28 Qt::WindowType::FramelessWindowHint | Qt::WindowType::WindowStaysOnTopHint | Qt::WindowType::WindowSystemMenuHint);
29
30 setStyleSheet(QSL("BaseToastNotification { border: 1px solid %1; }").arg(palette().windowText().color().name()));
31 installEventFilter(this);
32
33 m_timerClosingClick.setInterval(200);
34 m_timerClosingClick.setSingleShot(true);
35
36 connect(&m_timerClosingClick, &QTimer::timeout, this, &BaseToastNotification::close);
37 }
38
39 BaseToastNotification::~BaseToastNotification() {}
40
41 void BaseToastNotification::reject() {
42 close();
43 }
44
45 void BaseToastNotification::setupCloseButton(QAbstractButton* btn) {
46 btn->setToolTip(tr("Close this notification"));
47 btn->setIcon(qApp->icons()->fromTheme(QSL("dialog-close"), QSL("gtk-close")));
48
49 connect(btn, &QAbstractButton::clicked, this, &BaseToastNotification::close);
50 }
51
52 void BaseToastNotification::setupHeading(QLabel* lbl) {
53 auto fon = lbl->font();
54
55 fon.setBold(true);
56 fon.setPointSize(fon.pointSize() * 1.2);
57
58 lbl->setFont(fon);
59 }
60
61 void BaseToastNotification::stopTimedClosing() {
62 if (m_timerId >= 0) {
63 killTimer(m_timerId);
64 m_timerId = -1;
65
66 qDebugNN << LOGSEC_NOTIFICATIONS << "Stopping timed closing for notification.";
67 }
68 }
69
70 void BaseToastNotification::setupTimedClosing(bool want_shorter_timeout) {
71 if (m_timerId < 0) {
72 m_timerId = startTimer(want_shorter_timeout ? NOTIFICATION_SHORT_TIMEOUT : NOTIFICATIONS_TIMEOUT);
73
74 qDebugNN << LOGSEC_NOTIFICATIONS << "Starting timed closing for notification.";
75 }
76 }
77
78 bool BaseToastNotification::eventFilter(QObject* watched, QEvent* event) {
79 if (watched == this && event->type() == QEvent::Type::Enter) {
80 qDebugNN << LOGSEC_NOTIFICATIONS << "Notification got ENTER event.";
81 stopTimedClosing();
82 }
83
84 if (watched == this && event->type() == QEvent::Type::Leave) {
85 qDebugNN << LOGSEC_NOTIFICATIONS << "Notification got LEAVE event.";
86 setupTimedClosing(true);
87 }
88
89 if (event->type() == QEvent::Type::MouseButtonPress || event->type() == QEvent::Type::MouseButtonRelease) {
90 if (dynamic_cast<QMouseEvent*>(event)->button() == Qt::MouseButton::RightButton) {
91 event->accept();
92 QCoreApplication::processEvents();
93 m_timerClosingClick.start();
94 return true;
95 }
96 }
97
98 return QDialog::eventFilter(watched, event);
99 }
100
101 void BaseToastNotification::closeEvent(QCloseEvent* event) {
102 Q_UNUSED(event)
103
104 qDebugNN << LOGSEC_NOTIFICATIONS << "Notification got CLOSE event.";
105 stopTimedClosing();
106 emit closeRequested(this);
107 }
108
109 void BaseToastNotification::timerEvent(QTimerEvent* event) {
110 if (event->timerId() == m_timerId) {
111 qDebugNN << LOGSEC_NOTIFICATIONS << "Notification got TIMER event.";
112 stopTimedClosing();
113 emit closeRequested(this);
114 }
115 }