Skip to content

Commit 8a8589a

Browse files
thothonegankrf
authored andcommitted
Add system tray icon with a simple tooltip
1 parent e868de1 commit 8a8589a

2 files changed

Lines changed: 100 additions & 1 deletion

File tree

src/mainwindow.cc

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#include "utils.h"
3434

35+
#include <QCloseEvent>
3536
#include <QDebug>
3637
#include <QLabel>
3738
#include <QMenuBar>
@@ -61,6 +62,7 @@ struct PlatformStat
6162
MainWindow::MainWindow(QWidget *parent)
6263
: QMainWindow(parent)
6364
, m_view(nullptr)
65+
, m_systemTrayIcon(nullptr)
6466
{
6567
QIcon appIcon = QIcon();
6668
appIcon.addFile(QStringLiteral(":/images/128-apps-icemon.png"), QSize(128, 128));
@@ -81,8 +83,32 @@ MainWindow::MainWindow(QWidget *parent)
8183
m_jobStatsWidget = new QLabel;
8284
m_jobStatsWidget->setVisible(false);
8385
statusBar()->addPermanentWidget(m_jobStatsWidget);
86+
87+
QAction *action = nullptr;
8488

85-
QAction *action = fileMenu->addAction(tr("&Quit"), this, SLOT(close()), tr("Ctrl+Q"));
89+
if (QSystemTrayIcon::isSystemTrayAvailable())
90+
{
91+
action = fileMenu->addAction(tr("Show in System Tray"));
92+
action->setIcon(QIcon::fromTheme(QStringLiteral("systemtray")));
93+
action->setCheckable(true);
94+
connect(action, &QAction::triggered, this, &MainWindow::updateSystemTrayVisible);
95+
m_showInSystemTrayAction = action;
96+
97+
QMenu *systrayMenu = new QMenu(this);
98+
QAction *quitAction = systrayMenu->addAction(tr("&Quit"), this, SLOT(quit()), tr("Ctrl+Q"));
99+
quitAction->setIcon(QIcon::fromTheme(QStringLiteral("application-exit")));
100+
quitAction->setMenuRole(QAction::QuitRole);
101+
102+
m_systemTrayIcon = new QSystemTrayIcon(this);
103+
m_systemTrayIcon->setIcon(appIcon);
104+
m_systemTrayIcon->setToolTip(windowTitle());
105+
m_systemTrayIcon->setContextMenu(systrayMenu);
106+
connect(m_systemTrayIcon, &QSystemTrayIcon::activated, this, &MainWindow::systemTrayIconActivated);
107+
108+
fileMenu->addSeparator();
109+
}
110+
111+
action = fileMenu->addAction(tr("&Quit"), this, SLOT(quit()), tr("Ctrl+Q"));
86112
action->setIcon(QIcon::fromTheme(QStringLiteral("application-exit")));
87113
action->setMenuRole(QAction::QuitRole);
88114

@@ -146,6 +172,25 @@ MainWindow::~MainWindow()
146172

147173
void MainWindow::closeEvent(QCloseEvent *e)
148174
{
175+
if (m_systemTrayIcon->isVisible())
176+
{
177+
QSettings settings;
178+
const bool shownBefore = settings.value(QStringLiteral("programWillKeepRunningWarningShown")).toBool();
179+
if (!shownBefore) {
180+
QMessageBox::information(this, tr("Systray"),
181+
tr("The program will keep running in the "
182+
"system tray. To terminate the program, "
183+
"choose <b>Quit</b> in the context menu "
184+
"of the system tray entry."));
185+
settings.setValue(QStringLiteral("programWillKeepRunningWarningShown"), true);
186+
settings.sync();
187+
}
188+
189+
hide();
190+
e->ignore();
191+
return;
192+
}
193+
149194
writeSettings();
150195

151196
QMainWindow::closeEvent(e);
@@ -156,17 +201,22 @@ void MainWindow::readSettings()
156201
QSettings settings;
157202
restoreGeometry(settings.value(QStringLiteral("geometry")).toByteArray());
158203
restoreState(settings.value(QStringLiteral("windowState")).toByteArray());
204+
bool showSystemTray = settings.value(QStringLiteral("showSystemTray")).toBool();
159205
QString viewId = settings.value(QStringLiteral("currentView")).toString();
160206

161207
auto view = StatusViewFactory::create(viewId, this);
162208
setView(view);
209+
210+
m_showInSystemTrayAction->setChecked(showSystemTray);
211+
updateSystemTrayVisible();
163212
}
164213

165214
void MainWindow::writeSettings()
166215
{
167216
QSettings settings;
168217
settings.setValue(QStringLiteral("geometry"), saveGeometry());
169218
settings.setValue(QStringLiteral("windowState"), saveState());
219+
settings.setValue(QStringLiteral("showSystemTray"), m_systemTrayIcon->isVisible());
170220
settings.setValue(QStringLiteral("currentView"), (m_view ? m_view->id() : QString()));
171221
settings.sync();
172222
}
@@ -247,6 +297,44 @@ void MainWindow::configureView()
247297
m_view->configureView();
248298
}
249299

300+
void MainWindow::updateSystemTrayVisible()
301+
{
302+
if (m_showInSystemTrayAction->isChecked()) {
303+
m_systemTrayIcon->show();
304+
} else {
305+
m_systemTrayIcon->hide();
306+
}
307+
}
308+
309+
void MainWindow::quit()
310+
{
311+
writeSettings();
312+
qApp->quit();
313+
}
314+
315+
void MainWindow::systemTrayIconActivated(QSystemTrayIcon::ActivationReason reason)
316+
{
317+
switch (reason) {
318+
case QSystemTrayIcon::Trigger:
319+
if (isVisible()) {
320+
if (isMinimized()) {
321+
showMaximized();
322+
} else {
323+
hide();
324+
}
325+
} else {
326+
show();
327+
}
328+
break;
329+
330+
case QSystemTrayIcon::DoubleClick:
331+
case QSystemTrayIcon::MiddleClick:
332+
case QSystemTrayIcon::Unknown:
333+
default:
334+
;
335+
}
336+
}
337+
250338
void MainWindow::about()
251339
{
252340
QString about = tr("<p><strong>%1</strong><br/>"
@@ -306,6 +394,7 @@ void MainWindow::updateJobStats()
306394
if (!m_monitor->schedulerState()) {
307395
m_jobStatsWidget->clear();
308396
m_jobStatsWidget->setVisible(false);
397+
m_systemTrayIcon->setToolTip(tr("Scheduler is offline."));
309398
return;
310399
}
311400

@@ -337,6 +426,7 @@ void MainWindow::updateJobStats()
337426

338427
// Compose the text
339428
QString text;
429+
QString textNoHTML;
340430
foreach (auto pair, statistics) {
341431
const QString& platform = pair.first;
342432
const PlatformStat& stat = pair.second;
@@ -346,13 +436,16 @@ void MainWindow::updateJobStats()
346436

347437
if (!text.isEmpty()) {
348438
text.append(QStringLiteral(" - "));
439+
textNoHTML.append(QStringLiteral(" - "));
349440
}
350441

351442
text.append(QStringLiteral("<strong>%2/%3</strong> on %1").arg(platform).arg(stat.jobs).arg(stat.maxJobs));
443+
textNoHTML.append(QStringLiteral("%2/%3 on %1").arg(platform).arg(stat.jobs).arg(stat.maxJobs));
352444
}
353445

354446
m_jobStatsWidget->setText(tr("| Active jobs: %1").arg(text));
355447
m_jobStatsWidget->setVisible(true);
448+
m_systemTrayIcon->setToolTip(tr("Active jobs: %1").arg(textNoHTML));
356449
}
357450

358451
void MainWindow::setCurrentNet(const QByteArray &netname)

src/mainwindow.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include <QMainWindow>
2828
#include <QPointer>
29+
#include <QSystemTrayIcon>
2930

3031
#include "monitor.h"
3132
#include "job.h"
@@ -60,6 +61,9 @@ class MainWindow
6061
private slots:
6162
void pauseView();
6263
void configureView();
64+
void updateSystemTrayVisible();
65+
void systemTrayIconActivated(QSystemTrayIcon::ActivationReason reason);
66+
void quit();
6367

6468
void about();
6569

@@ -81,13 +85,15 @@ private slots:
8185
HostInfoManager *m_hostInfoManager;
8286
QPointer<Monitor> m_monitor;
8387
StatusView *m_view;
88+
QSystemTrayIcon* m_systemTrayIcon;
8489

8590
QLabel *m_schedStatusWidget;
8691
QLabel *m_jobStatsWidget;
8792

8893
QActionGroup *m_viewMode;
8994
QAction *m_configureViewAction;
9095
QAction *m_pauseViewAction;
96+
QAction *m_showInSystemTrayAction;
9197

9298
JobList m_activeJobs;
9399
};

0 commit comments

Comments
 (0)