-
Notifications
You must be signed in to change notification settings - Fork 78
/
TrayIcon.cpp
192 lines (151 loc) · 3.44 KB
/
TrayIcon.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
/**
* Copyright (c) 2018-2024 Governikus GmbH & Co. KG, Germany
*/
#include "TrayIcon.h"
#include "AppSettings.h"
#include "ApplicationModel.h"
#include "Env.h"
#include <QCoreApplication>
#if !defined(Q_OS_ANDROID) && !defined(Q_OS_IOS)
#include <QMenu>
#endif
using namespace governikus;
#if !defined(Q_OS_ANDROID) && !defined(Q_OS_IOS)
void TrayIcon::onActivated(QSystemTrayIcon::ActivationReason pReason)
{
#ifdef Q_OS_MACOS
Q_UNUSED(pReason)
#else
if (pReason == QSystemTrayIcon::Trigger)
{
Q_EMIT fireShow();
}
#endif
}
#endif
TrayIcon::TrayIcon()
: QObject()
#ifdef Q_OS_MACOS
, mIcon(QStringLiteral(":/images/macos/appIcon.svg"))
#else
, mIcon(QStringLiteral(":/images/npa.svg"))
#endif
#if !defined(Q_OS_ANDROID) && !defined(Q_OS_IOS)
, mTrayIcon(nullptr)
#endif
{
}
TrayIcon::~TrayIcon()
{
shutdown();
}
const QIcon& TrayIcon::getIcon() const
{
return mIcon;
}
void TrayIcon::create()
{
#if !defined(Q_OS_ANDROID) && !defined(Q_OS_IOS)
if (!QSystemTrayIcon::isSystemTrayAvailable())
{
return;
}
mTrayIcon = new QSystemTrayIcon(mIcon);
connect(mTrayIcon, &QSystemTrayIcon::activated, this, &TrayIcon::onActivated);
connect(mTrayIcon, &QSystemTrayIcon::messageClicked, this, &TrayIcon::fireShow);
updateMenu();
mTrayIcon->setToolTip(QCoreApplication::applicationName());
const auto& generalSettings = Env::getSingleton<AppSettings>()->getGeneralSettings();
if (generalSettings.showTrayIcon())
{
mTrayIcon->show();
}
if (!generalSettings.isAutoStart())
{
//: LABEL DESKTOP
Env::getSingleton<ApplicationModel>()->showFeedback(tr("Application was started."));
}
#endif
}
#if !defined(Q_OS_ANDROID) && !defined(Q_OS_IOS)
void TrayIcon::updateMenu()
{
if (!mTrayIcon)
{
return;
}
QMenu* trayIconMenu = mTrayIcon->contextMenu();
if (!trayIconMenu)
{
trayIconMenu = new QMenu(nullptr);
mTrayIcon->setContextMenu(trayIconMenu);
}
trayIconMenu->clear();
#if defined(Q_OS_MACOS) || defined(Q_OS_LINUX) || defined(Q_OS_BSD4)
//: LABEL DESKTOP
QAction* showApplicationAction = new QAction(tr("Open"), trayIconMenu);
connect(showApplicationAction, &QAction::triggered, this, &TrayIcon::fireShow);
trayIconMenu->addAction(showApplicationAction);
trayIconMenu->addSeparator();
#endif
//: LABEL DESKTOP
const auto quitAction = new QAction(tr("Quit %1").arg(QCoreApplication::applicationName()), trayIconMenu);
connect(quitAction, &QAction::triggered, this, &TrayIcon::fireQuit);
trayIconMenu->addAction(quitAction);
}
#endif
void TrayIcon::onTranslationChanged()
{
#if !defined(Q_OS_ANDROID) && !defined(Q_OS_IOS)
updateMenu();
#endif
}
void TrayIcon::shutdown()
{
#if !defined(Q_OS_ANDROID) && !defined(Q_OS_IOS)
if (mTrayIcon != nullptr)
{
const QMenu* menu = mTrayIcon->contextMenu();
if (menu != nullptr)
{
qDeleteAll(menu->actions());
delete menu;
}
delete mTrayIcon;
mTrayIcon = nullptr;
}
#endif
}
void TrayIcon::setVisible(bool pVisible)
{
#if !defined(Q_OS_ANDROID) && !defined(Q_OS_IOS)
if (mTrayIcon)
{
mTrayIcon->setVisible(pVisible);
}
#else
Q_UNUSED(pVisible)
#endif
}
bool TrayIcon::isVisible() const
{
#if !defined(Q_OS_ANDROID) && !defined(Q_OS_IOS)
if (mTrayIcon)
{
mTrayIcon->isVisible();
}
#endif
return false;
}
void TrayIcon::showMessage(const QString& pTitle, const QString& pMessage)
{
#if !defined(Q_OS_ANDROID) && !defined(Q_OS_IOS)
if (mTrayIcon)
{
mTrayIcon->showMessage(pTitle, pMessage, mIcon, 3000);
}
#else
Q_UNUSED(pTitle)
Q_UNUSED(pMessage)
#endif
}