-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrayicon.cpp
More file actions
176 lines (145 loc) · 5.05 KB
/
trayicon.cpp
File metadata and controls
176 lines (145 loc) · 5.05 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "trayicon.h"
TrayIcon::TrayIcon():
settings (new Settings("settings.json"))
{
// Load settings, and prepare for settings change.
loadSettings();
connect(settings, &Settings::sSettingsChanged, this, &TrayIcon::loadAndApplySettings);
// Build context menu.
showSettingsAction = new QAction("Settings...", this);
connect(showSettingsAction, &QAction::triggered, this, &TrayIcon::showSettingsWindow);
quitProgramAction = new QAction("Quit", this);
connect(quitProgramAction, &QAction::triggered, this, &TrayIcon::quitProgram);
trayIconMenu.addAction( showSettingsAction);
trayIconMenu.addAction( quitProgramAction);
trayIcon.setContextMenu(&trayIconMenu);
// Allow tray icon to receive mouse inputs.
connect(&trayIcon, &QSystemTrayIcon::activated, this, &TrayIcon::iconActivated);
// Connect the refresh timer.
connect(&refreshTimer, &QTimer::timeout, this, &TrayIcon::refresh);
// Apply settings and start refreshing.
applySettings();
refresh();
refreshTimer.start();
// Display the tray icon!
trayIcon.show();
}
TrayIcon::~TrayIcon()
{
m_shuttingDown = true;
delete showSettingsAction;
delete quitProgramAction;
delete settings; // Meaning: save settings via its destructor. Then free memory.
}
void TrayIcon::loadSettings()
{
m_refreshInterval = settings->value("refreshInterval").toInt();
m_showValue = settings->value("showValue").toBool();
m_showBackground = settings->value("showBackground").toBool();
m_showBackgroundProgress = settings->value("showBackgroundProgress").toBool();
m_colorText1.setNamedColor( settings->value("colorText1").toString());
m_colorText2.setNamedColor( settings->value("colorText2").toString());
m_colorBack1.setNamedColor( settings->value("colorBack1").toString());
m_colorBack2.setNamedColor( settings->value("colorBack2").toString());
m_availableMemory = settings->value("availableMemory").toBool();
}
void TrayIcon::generateIconsWithCurrentSettings()
{
allIcons.clear();
for (int i = 0; i <= 100; ++i)
allIcons.insert(i, generateIcon(i));
}
void TrayIcon::applySettings()
{
refreshTimer.setInterval(m_refreshInterval);
// Set option on memUsage object.
#if defined(Q_OS_LINUX)
memUsage.setAvailableMemory( m_availableMemory);
#endif
generateIconsWithCurrentSettings();
}
void TrayIcon::loadAndApplySettings()
{
refreshTimer.stop();
loadSettings();
applySettings();
refresh();
refreshTimer.start();
}
void TrayIcon::refresh()
{
if (m_shuttingDown)
return;
int memoryUsed = memUsage.getMemoryUsed_inPercent();
trayIcon.setIcon( allIcons.value( memoryUsed, generateIcon(0)));
if (window != nullptr)
window->setWindowIcon( allIcons.value( memoryUsed, generateIcon(0)));
trayIcon.setToolTip("Memory Usage: " + QString::number(memoryUsed) + "%");
}
void TrayIcon::showSettingsWindow()
{
if (window != nullptr)
return;
window = new MainWindow( settings);
connect( window, &MainWindow::sCloseWindow, this, &TrayIcon::deleteMainWindow);
window->setAttribute( Qt::WA_DeleteOnClose);
window->show();
}
void TrayIcon::deleteMainWindow()
{
if (window != nullptr)
window->deleteLater();
window = nullptr;
}
void TrayIcon::quitProgram()
{
qApp->quit(); // quit the event loop. then run trayicon destructor in main.
}
void TrayIcon::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
switch (reason) {
case QSystemTrayIcon::Trigger:
case QSystemTrayIcon::Context:
{ // Both left and right click triggers this.
QPoint pos = QCursor::pos();
pos.setY( pos.y() - trayIcon.contextMenu()->height());
trayIcon.contextMenu()->exec( pos);
break;
}
// The rest do nothing.
case QSystemTrayIcon::DoubleClick:
case QSystemTrayIcon::MiddleClick:
case QSystemTrayIcon::Unknown:
break;
}
}
QIcon TrayIcon::generateIcon(int value)
{
QIcon icon;
QSize size (48, 48);
QImage image = QImage(size, QImage::Format_ARGB32);
double usedAmount = value / 100.0;
double freeAmount = 1 - usedAmount;
QPainter painter( &image);
QRect area1 (0, 0, image.width(), image.height() * freeAmount);
QRect area2 (0, image.height() * freeAmount, image.width(), image.height());
if (m_showBackgroundProgress)
{
painter.fillRect( area1, m_colorBack1);
painter.fillRect( area2, m_colorBack2);
} else if (m_showBackground) {
painter.fillRect( image.rect(), m_colorBack1);
}
if (m_showValue)
{
painter.setPen( m_colorText1 );
painter.setFont( QFont("Arial", 28, QFont::Bold));
painter.setClipRect(area1);
painter.drawText( image.rect(), Qt::AlignCenter, QString::number(value));
if (m_showBackgroundProgress) painter.setPen( m_colorText2 );
painter.setClipRect(area2);
painter.drawText( image.rect(), Qt::AlignCenter, QString::number(value));
}
icon.addPixmap( QPixmap::fromImage( image));
return icon;
}