QtPass  1.2.3
Multi-platform GUI for pass, the standard unix password manager.
trayicon.cpp
Go to the documentation of this file.
1 #include "trayicon.h"
2 #include "debughelper.h"
3 #include <QAction>
4 #include <QApplication>
5 #include <QMainWindow>
6 #include <QMenu>
7 
13 TrayIcon::TrayIcon(QMainWindow *parent) {
14  parentwin = parent;
15 
16  if (QSystemTrayIcon::isSystemTrayAvailable() == true) {
17  createActions();
18  createTrayIcon();
19 
20  sysTrayIcon->setIcon(
21  QIcon::fromTheme("qtpass-tray", QIcon(":/artwork/icon.png")));
22 
23  sysTrayIcon->show();
24 
25  QObject::connect(sysTrayIcon,
26  SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this,
27  SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
28 
29  isAllocated = true;
30  } else {
31  dbg() << "No tray icon for this OS possibly also not show options?";
32 
33  isAllocated = false;
34 
35  showAction = nullptr;
36  hideAction = nullptr;
37  minimizeAction = nullptr;
38  maximizeAction = nullptr;
39  restoreAction = nullptr;
40  quitAction = nullptr;
41  sysTrayIcon = nullptr;
42  trayIconMenu = nullptr;
43  }
44 }
45 
50 void TrayIcon::setVisible(bool visible) {
51  if (visible == true)
52  parentwin->show();
53  else
54  parentwin->hide();
55 }
56 
60 bool TrayIcon::getIsAllocated() { return isAllocated; }
61 
65 void TrayIcon::createActions() {
66  showAction = new QAction(tr("&Show"), this);
67  connect(showAction, SIGNAL(triggered()), parentwin, SLOT(show()));
68  hideAction = new QAction(tr("&Hide"), this);
69  connect(hideAction, SIGNAL(triggered()), parentwin, SLOT(hide()));
70 
71  minimizeAction = new QAction(tr("Mi&nimize"), this);
72  connect(minimizeAction, SIGNAL(triggered()), parentwin,
73  SLOT(showMinimized()));
74  maximizeAction = new QAction(tr("Ma&ximize"), this);
75  connect(maximizeAction, SIGNAL(triggered()), parentwin,
76  SLOT(showMaximized()));
77  restoreAction = new QAction(tr("&Restore"), this);
78  connect(restoreAction, SIGNAL(triggered()), parentwin, SLOT(showNormal()));
79 
80  quitAction = new QAction(tr("&Quit"), this);
81  connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
82 }
83 
87 void TrayIcon::createTrayIcon() {
88  trayIconMenu = new QMenu(this);
89  trayIconMenu->addAction(showAction);
90  trayIconMenu->addAction(hideAction);
91  trayIconMenu->addAction(minimizeAction);
92  trayIconMenu->addAction(maximizeAction);
93  trayIconMenu->addAction(restoreAction);
94  trayIconMenu->addSeparator();
95  trayIconMenu->addAction(quitAction);
96 
97  sysTrayIcon = new QSystemTrayIcon(this);
98  sysTrayIcon->setContextMenu(trayIconMenu);
99 }
100 
105  if (parentwin->isVisible() == true)
106  parentwin->hide();
107  else
108  parentwin->show();
109 }
110 
115 void TrayIcon::iconActivated(QSystemTrayIcon::ActivationReason reason) {
116  switch (reason) {
117  case QSystemTrayIcon::Trigger:
118  case QSystemTrayIcon::DoubleClick:
119  showHideParent();
120  break;
121  case QSystemTrayIcon::MiddleClick:
122  showMessage("test", "test msg", 1000);
123  break;
124  default: {};
125  }
126 }
127 
134 void TrayIcon::showMessage(QString title, QString msg, int time) {
135  sysTrayIcon->showMessage(title, msg, QSystemTrayIcon::Information, time);
136 }
#define dbg()
Definition: debughelper.h:7
void setVisible(bool visible)
TrayIcon::setVisible show or hide the icon.
Definition: trayicon.cpp:50
TrayIcon(QMainWindow *parent)
TrayIcon::TrayIcon use a (system) tray icon with a nice QtPass logo on it (currently) only Quits...
Definition: trayicon.cpp:13
void showMessage(QString title, QString msg, int time)
TrayIcon::showMessage show a systray message for notification.
Definition: trayicon.cpp:134
void iconActivated(QSystemTrayIcon::ActivationReason reason)
TrayIcon::iconActivated you clicked on the trayicon.
Definition: trayicon.cpp:115
void showHideParent()
TrayIcon::showHideParent toggle app visibility.
Definition: trayicon.cpp:104
bool getIsAllocated()
TrayIcon::getIsAllocated return if TrayIcon is allocated.
Definition: trayicon.cpp:60