Debian Codegraph @

src/librssguard/gui/systemtrayicon.cpp

raw symbols SCIP
1 // For license of this file, see <project-root-folder>/LICENSE.md.
2
3 #include "gui/systemtrayicon.h"
4
5 #include "definitions/definitions.h"
6 #include "gui/dialogs/formmain.h"
7 #include "miscellaneous/application.h"
8 #include "miscellaneous/settings.h"
9
10 #include <QPainter>
11 #include <QTimer>
12
13 #if defined(Q_OS_WIN)
14 TrayIconMenu::TrayIconMenu(const QString& title, QWidget* parent) : QMenu(title, parent) {}
15
16 bool TrayIconMenu::event(QEvent* event) {
17 if (event->type() == QEvent::Type::Show && QApplication::activeModalWidget() != nullptr) {
18 QTimer::singleShot(0, this, &TrayIconMenu::hide);
19 qApp->showGuiMessage(Notification::Event::GeneralEvent,
20 {tr("Close dialogs"),
21 tr("Close opened modal dialogs first."),
22 QSystemTrayIcon::MessageIcon::Warning});
23 }
24
25 return QMenu::event(event);
26 }
27
28 #endif
29
30 SystemTrayIcon::SystemTrayIcon(const QString& normal_icon, const QString& plain_icon, FormMain* parent)
31 : QSystemTrayIcon(parent), m_normalIcon(normal_icon), m_plainPixmap(plain_icon) {
32 qDebugNN << LOGSEC_GUI << "Creating SystemTrayIcon instance.";
33 m_font.setBold(true);
34
35 m_tmrDoubleFire.setSingleShot(true);
36 m_tmrDoubleFire.setInterval(100);
37
38 // Initialize icon.
39 setNumber();
40 setContextMenu(parent->trayMenu());
41
42 // Create necessary connections.
43 connect(this, &SystemTrayIcon::activated, this, &SystemTrayIcon::onActivated);
44 }
45
46 SystemTrayIcon::~SystemTrayIcon() {
47 qDebugNN << LOGSEC_GUI << "Destroying SystemTrayIcon instance.";
48 hide();
49 }
50
51 void SystemTrayIcon::onActivated(QSystemTrayIcon::ActivationReason reason) {
52 if (m_tmrDoubleFire.isActive()) {
53 return;
54 }
55
56 m_tmrDoubleFire.start();
57
58 switch (reason) {
59 case SystemTrayIcon::ActivationReason::Trigger:
60 case SystemTrayIcon::ActivationReason::DoubleClick:
61 case SystemTrayIcon::ActivationReason::MiddleClick:
62 static_cast<FormMain*>(parent())->switchVisibility();
63 break;
64
65 default:
66 break;
67 }
68 }
69
70 bool SystemTrayIcon::isSystemTrayAreaAvailable() {
71 return QSystemTrayIcon::isSystemTrayAvailable() && QSystemTrayIcon::supportsMessages();
72 }
73
74 bool SystemTrayIcon::isSystemTrayDesired() {
75 return qApp->settings()->value(GROUP(GUI), SETTING(GUI::UseTrayIcon)).toBool();
76 }
77
78 void SystemTrayIcon::showPrivate() {
79 // Make sure that application does not exit some window (for example
80 // the settings window) gets closed. Behavior for main window
81 // is handled explicitly by FormMain::closeEvent() method.
82 qApp->setQuitOnLastWindowClosed(false);
83
84 // Display the tray icon.
85 QSystemTrayIcon::show();
86 emit shown();
87
88 qDebugNN << LOGSEC_GUI << "Tray icon displayed.";
89 }
90
91 void SystemTrayIcon::show() {
92 // Show immediately.
93 qDebugNN << LOGSEC_GUI << "Showing tray icon immediately.";
94 showPrivate();
95 }
96
97 void SystemTrayIcon::setNumber(int number, bool any_feed_has_new_unread_messages) {
98 Q_UNUSED(any_feed_has_new_unread_messages)
99
100 if (number <= 0 || !qApp->settings()->value(GROUP(GUI), SETTING(GUI::UnreadNumbersInTrayIcon)).toBool()) {
101 // Either no unread messages or numbers in tray icon are disabled.
102 setToolTip(QSL(APP_LONG_NAME));
103 QSystemTrayIcon::setIcon(QIcon(m_normalIcon));
104 }
105 else {
106 setToolTip(tr("%1\nUnread news: %2").arg(QSL(APP_LONG_NAME), QString::number(number)));
107 QPixmap background(m_plainPixmap);
108 QPainter tray_painter;
109
110 tray_painter.begin(&background);
111
112 if (qApp->settings()->value(GROUP(GUI), SETTING(GUI::MonochromeTrayIcon)).toBool() &&
113 !qApp->settings()->value(GROUP(GUI), SETTING(GUI::ColoredBusyTrayIcon)).toBool()) {
114 tray_painter.setPen(Qt::GlobalColor::white);
115 }
116 else {
117 tray_painter.setPen(Qt::GlobalColor::black);
118 }
119
120 tray_painter.setRenderHint(QPainter::RenderHint::SmoothPixmapTransform, true);
121 tray_painter.setRenderHint(QPainter::RenderHint::TextAntialiasing, true);
122
123 // Numbers with more than 5 digits won't be readable, display
124 // infinity symbol in that case.
125 QString num_txt;
126
127 if (number > 99999) {
128 num_txt = QChar(8734);
129 m_font.setPixelSize(background.width() * 0.78);
130 }
131 else if (number > 999) {
132 num_txt = QSL("%1k").arg(int(number / 1000));
133 m_font.setPixelSize(background.width() * 0.43);
134 }
135 else if (number > 99) {
136 num_txt = QString::number(number);
137 m_font.setPixelSize(background.width() * 0.43);
138 }
139 else if (number > 9) {
140 num_txt = QString::number(number);
141 m_font.setPixelSize(background.width() * 0.56);
142 }
143 else {
144 num_txt = QString::number(number);
145 m_font.setPixelSize(background.width() * 0.78);
146 }
147
148 tray_painter.setFont(m_font);
149 tray_painter.drawText(background.rect(), Qt::AlignmentFlag::AlignCenter, num_txt);
150 tray_painter.end();
151
152 QSystemTrayIcon::setIcon(QIcon(background));
153 }
154 }
155
156 void SystemTrayIcon::showMessage(const QString& title,
157 const QString& message,
158 QSystemTrayIcon::MessageIcon icon,
159 int milliseconds_timeout_hint,
160 const std::function<void()>& functor) {
161 if (m_connection != nullptr) {
162 // Disconnect previous bubble click signalling.
163 disconnect(m_connection);
164 }
165
166 if (functor) {
167 // Establish new connection for bubble click.
168 m_connection = connect(this, &SystemTrayIcon::messageClicked, functor);
169 }
170
171 // NOTE: If connections do not work, then use QMetaObject::invokeMethod(...).
172 QSystemTrayIcon::showMessage(title, message, icon, milliseconds_timeout_hint);
173 }