QtPass 2.0.0
Developer documentation
Loading...
Searching...
No Matches
trayicon.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2015 Anne Jan Brouwer
2// SPDX-License-Identifier: GPL-3.0-or-later
8#include "trayicon.h"
9#include <QAction>
10#include <QApplication>
11#include <QMainWindow>
12#include <QMenu>
13
14#include "qtpasslogging.h"
15
16TrayIcon::TrayIcon(QMainWindow *parent) : QObject(parent) {
17 parentwin = parent;
18
19 if (QSystemTrayIcon::isSystemTrayAvailable()) {
22
23 sysTrayIcon->setIcon(
24 QIcon::fromTheme("qtpass-tray", QIcon(":/artwork/icon.png")));
25
26 sysTrayIcon->show();
27
28 QObject::connect(sysTrayIcon, &QSystemTrayIcon::activated, this,
30
31 isAllocated = true;
32 } else {
33 qCDebug(lcQtPass)
34 << "No tray icon for this OS possibly also not show options?";
35 }
36}
37
38auto TrayIcon::getIsAllocated() -> bool { return isAllocated; }
39
41 showAction = new QAction(tr("&Show"), this);
42 connect(showAction, &QAction::triggered, parentwin, &QWidget::show);
43 hideAction = new QAction(tr("&Hide"), this);
44 connect(hideAction, &QAction::triggered, parentwin, &QWidget::hide);
45
46 minimizeAction = new QAction(tr("Mi&nimize"), this);
47 connect(minimizeAction, &QAction::triggered, parentwin,
48 &QWidget::showMinimized);
49 maximizeAction = new QAction(tr("Ma&ximize"), this);
50 connect(maximizeAction, &QAction::triggered, parentwin,
51 &QWidget::showMaximized);
52 restoreAction = new QAction(tr("&Restore"), this);
53 connect(restoreAction, &QAction::triggered, parentwin, &QWidget::showNormal);
54
55 quitAction = new QAction(tr("&Quit"), this);
56 connect(quitAction, &QAction::triggered, QApplication::instance(),
57 &QApplication::quit);
58}
59
61 // QMenu needs a widget parent; the main window owns the menu, this object
62 // owns the tray icon.
63 trayIconMenu = new QMenu(parentwin);
64 trayIconMenu->addAction(showAction);
65 trayIconMenu->addAction(hideAction);
66 trayIconMenu->addAction(minimizeAction);
67 trayIconMenu->addAction(maximizeAction);
68 trayIconMenu->addAction(restoreAction);
69 quitSeparator = trayIconMenu->addSeparator();
70 trayIconMenu->addAction(quitAction);
71
72 sysTrayIcon = new QSystemTrayIcon(this);
73 sysTrayIcon->setContextMenu(trayIconMenu);
74}
75
76void TrayIcon::addToggle(QAction *action) {
77 if (trayIconMenu == nullptr) {
78 return;
79 }
80 trayIconMenu->insertSeparator(quitSeparator);
81 trayIconMenu->insertAction(quitSeparator, action);
82}
83
85 if (parentwin->isVisible()) {
86 parentwin->hide();
87 } else {
88 parentwin->show();
89 }
90}
91
92void TrayIcon::iconActivated(QSystemTrayIcon::ActivationReason reason) {
93 switch (reason) {
94 case QSystemTrayIcon::Trigger:
95 case QSystemTrayIcon::DoubleClick:
97 break;
98 case QSystemTrayIcon::MiddleClick:
99 break;
100 default: {
101 }
102 }
103}
QAction * hideAction
Hides the main window (QWidget::hide()).
Definition trayicon.h:83
void showHideParent()
Toggle the visibility of the associated main window.
Definition trayicon.cpp:84
void createActions()
Create the Show/Hide/Minimize/Maximize/Restore/Quit actions and wire each one to the matching QWidget...
Definition trayicon.cpp:40
void iconActivated(QSystemTrayIcon::ActivationReason reason)
Handle activation events from the system tray icon.
Definition trayicon.cpp:92
QAction * restoreAction
Restores the main window to its normal size.
Definition trayicon.h:89
void addToggle(QAction *action)
Put one of the window's toggles in the tray menu, above Quit.
Definition trayicon.cpp:76
TrayIcon(QMainWindow *parent)
Construct a TrayIcon associated with the given main window.
Definition trayicon.cpp:16
QSystemTrayIcon * sysTrayIcon
The icon in the notification area; owned by this object so it is destroyed, and the icon removed,...
Definition trayicon.h:103
QAction * maximizeAction
Maximises the main window.
Definition trayicon.h:87
QAction * quitSeparator
Separator directly above Quit; addToggle() inserts the window's toggles (each with its own separator)...
Definition trayicon.h:96
QMenu * trayIconMenu
Context menu of the tray icon; a child of the main window because QMenu needs a widget parent....
Definition trayicon.h:109
bool isAllocated
true once the actions, menu and icon exist; stays false when QSystemTrayIcon::isSystemTrayAvailable()...
Definition trayicon.h:117
void createTrayIcon()
Build the context menu from the actions and create the QSystemTrayIcon that pops it up....
Definition trayicon.cpp:60
QAction * minimizeAction
Minimises the main window.
Definition trayicon.h:85
QMainWindow * parentwin
The main window the actions and showHideParent() operate on.
Definition trayicon.h:111
QAction * showAction
Shows the main window (QWidget::show()).
Definition trayicon.h:81
auto getIsAllocated() -> bool
Check whether tray resources have been allocated and initialized.
Definition trayicon.cpp:38
QAction * quitAction
Quits the application.
Definition trayicon.h:91
The "qtpass" logging category.
TrayIcon: the system tray icon and its context menu that show, hide, minimise, maximise,...