-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrayicon.cpp
More file actions
108 lines (94 loc) · 2.29 KB
/
trayicon.cpp
File metadata and controls
108 lines (94 loc) · 2.29 KB
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"
#include "mainwindow.h"
#include "qmutex.h"
#include "qmenu.h"
#include "qapplication.h"
#include <QApplication>
#pragma execution_character_set("utf-8")
QScopedPointer<TrayIcon> TrayIcon::self;
TrayIcon *TrayIcon::Instance()
{
if (self.isNull())
{
static QMutex mutex;
QMutexLocker locker(&mutex);
if (self.isNull())
{
self.reset(new TrayIcon);
}
}
return self.data();
}
TrayIcon::TrayIcon(QObject *parent) : QObject(parent)
{
mainWidget = 0;
trayIcon = new QSystemTrayIcon(this);
connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this, SLOT(iconIsActived(QSystemTrayIcon::ActivationReason)));
menu = new QMenu();
exitDirect = true;
}
void TrayIcon::iconIsActived(QSystemTrayIcon::ActivationReason reason)
{
switch (reason)
{
case QSystemTrayIcon::Trigger:
{
break;
}
case QSystemTrayIcon::DoubleClick:
{
qobject_cast<MainWindow*>(mainWidget)->slotShowWindow();
break;
}
default:
break;
}
}
bool TrayIcon::getVisible() const
{
return trayIcon->isVisible();
}
void TrayIcon::setExitDirect(bool exitDirect)
{
if (this->exitDirect != exitDirect)
{
this->exitDirect = exitDirect;
}
}
void TrayIcon::setMainWidget(QWidget *mainWidget)
{
this->mainWidget = mainWidget;
menu->addAction(tr("MainWindow"), mainWidget, SLOT(slotShowWindow()));
if (exitDirect)
{
menu->addAction(tr("Exit"), this, SLOT(closeAll()));
}
else
{
menu->addAction(tr("Exit"), this, SIGNAL(trayIconExit()));
}
trayIcon->setContextMenu(menu);
}
void TrayIcon::showMessage(const QString &title, const QString &msg, QSystemTrayIcon::MessageIcon icon, int msecs)
{
trayIcon->showMessage(title, msg, icon, msecs);
}
void TrayIcon::setIcon(const QString &strIcon)
{
trayIcon->setIcon(QIcon(strIcon));
}
void TrayIcon::setToolTip(const QString &tip)
{
trayIcon->setToolTip(tip);
}
void TrayIcon::setVisible(bool visible)
{
trayIcon->setVisible(visible);
}
void TrayIcon::closeAll()
{
trayIcon->hide();
trayIcon->deleteLater();
qApp->exit();
}