forked from IJHack/QtPass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trayicon.cpp
108 lines (94 loc) · 2.89 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
#include "trayicon.h"
/**
* @brief TrayIcon::TrayIcon use a (system) tray icon with a nice QtPass logo on
* it (currently) only Quits.
* @param parent
*/
TrayIcon::TrayIcon(QMainWindow *parent) {
parentwin = parent;
createActions();
createTrayIcon();
sysTrayIcon->setIcon(QIcon(":/artwork/icon.png"));
sysTrayIcon->show();
QObject::connect(sysTrayIcon,
SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this,
SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
}
/**
* @brief TrayIcon::setVisible show or hide the icon.
* @param visible
*/
void TrayIcon::setVisible(bool visible) {
if (visible == true)
parentwin->show();
else
parentwin->hide();
}
/**
* @brief TrayIcon::createActions setup the signals.
*/
void TrayIcon::createActions() {
showAction = new QAction(tr("&Show"), this);
connect(showAction, SIGNAL(triggered()), parentwin, SLOT(show()));
hideAction = new QAction(tr("&Hide"), this);
connect(hideAction, SIGNAL(triggered()), parentwin, SLOT(hide()));
minimizeAction = new QAction(tr("Mi&nimize"), this);
connect(minimizeAction, SIGNAL(triggered()), parentwin,
SLOT(showMinimized()));
maximizeAction = new QAction(tr("Ma&ximize"), this);
connect(maximizeAction, SIGNAL(triggered()), parentwin,
SLOT(showMaximized()));
restoreAction = new QAction(tr("&Restore"), this);
connect(restoreAction, SIGNAL(triggered()), parentwin, SLOT(showNormal()));
quitAction = new QAction(tr("&Quit"), this);
connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
}
/**
* @brief TrayIcon::createTrayIcon set up menu.
*/
void TrayIcon::createTrayIcon() {
trayIconMenu = new QMenu(this);
trayIconMenu->addAction(showAction);
trayIconMenu->addAction(hideAction);
trayIconMenu->addAction(minimizeAction);
trayIconMenu->addAction(maximizeAction);
trayIconMenu->addAction(restoreAction);
trayIconMenu->addSeparator();
trayIconMenu->addAction(quitAction);
sysTrayIcon = new QSystemTrayIcon(this);
sysTrayIcon->setContextMenu(trayIconMenu);
}
/**
* @brief TrayIcon::showHideParent toggle app visibility.
*/
void TrayIcon::showHideParent() {
if (parentwin->isVisible() == true)
parentwin->hide();
else
parentwin->show();
}
/**
* @brief TrayIcon::iconActivated you clicked on the trayicon.
* @param reason
*/
void TrayIcon::iconActivated(QSystemTrayIcon::ActivationReason reason) {
switch (reason) {
case QSystemTrayIcon::Trigger:
case QSystemTrayIcon::DoubleClick:
showHideParent();
break;
case QSystemTrayIcon::MiddleClick:
showMessage("test", "test msg", 1000);
break;
default: {};
}
}
/**
* @brief TrayIcon::showMessage show a systray message for notification.
* @param title
* @param msg
* @param time
*/
void TrayIcon::showMessage(QString title, QString msg, int time) {
sysTrayIcon->showMessage(title, msg, QSystemTrayIcon::Information, time);
}