-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtray.cpp
327 lines (283 loc) · 10.8 KB
/
tray.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#include <QSystemTrayIcon>
#include <QMenu>
#include <QMessageBox>
#include <QApplication>
#include <QCursor>
#include <QStandardPaths>
#include <QDir>
#include "tray.h"
#include "logging.h"
#include "volumechanger.h"
#include <math.h>
#include <QLabel>
#include <QFont>
#include <Qtimer>
#include <QDesktopWidget.h>
#include <QApplication>
#define VK_0 0x30
#define CFG_FILENAME QDir(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation)).filePath("vol_ctrl.cfg")
inline double round(double x) { return (floor(x + 0.5)); }
void Tray::muteCb()
{
bool newVal = !VolumeChanger::Instance().isMute();
VolumeChanger::Instance().setMute(newVal);
emit Instance().muteChanged(newVal);
DBG( (VolumeChanger::Instance().isMute()? "Mute" : "Unmute") );
}
void Tray::minusCb()
{
double vol = round(VolumeChanger::Instance().getVolume() * 100) / 100;
if (vol >0)
{
VolumeChanger::Instance().setVolume(vol-0.01);
emit Instance().volumeChanged(vol);
}
DBG("Volume decreased. New volume is " << VolumeChanger::Instance().getVolume());
}
void Tray::plusCb()
{
double vol = round(VolumeChanger::Instance().getVolume() * 100) / 100;
if (vol <1)
{
VolumeChanger::Instance().setVolume(vol+0.01);
emit Instance().volumeChanged(vol);
}
DBG("Volume increased. New volume is " << VolumeChanger::Instance().getVolume());
}
Tray::Tray(QObject *parent)
: QObject(parent), _iconMenu(NULL), _settingsDialog(NULL), _aboutDialog(NULL),
_popupWidget(NULL), _config(Settings::DEFAULT_CFG)
{
// Create Icon object
QIcon icon (":/img/tray_pic.png");
_icon = new QSystemTrayIcon(icon, this);
_icon->setToolTip("Volume control");
_icon->setVisible(true);
connect(_icon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this, SLOT(iconClicked(QSystemTrayIcon::ActivationReason)));
DBG("Config file: " << CFG_FILENAME);
if (!_config.loadFromFile(CFG_FILENAME))
_config = Settings::DEFAULT_CFG;
// setup of key listener
_keyLstnr.addKey( _config.mute.nativeVirtualKey(), _config.mute.nativeModifiers(), Tray::muteCb );
_keyLstnr.addKey( _config.volDown.nativeVirtualKey(), _config.volDown.nativeModifiers(), Tray::minusCb);
_keyLstnr.addKey( _config.volUp.nativeVirtualKey(), _config.volUp.nativeModifiers(), Tray::plusCb );
//setup popup windows slots
connect(this, SIGNAL(volumeChanged(double)), this, SLOT(showVolumePopup()));
connect(this, SIGNAL(muteChanged(bool)), this, SLOT(showMutePopup()));
// start key listener in separated thread
_keyLstnr.start();
// configure timer for popup messages
_popupTimer = new QTimer(this);
_popupTimer->setSingleShot(true);
}
Tray::~Tray()
{
_icon->setVisible(false);
}
Tray& Tray::Instance()
{
static Tray singleton;
return singleton;
}
void Tray::iconClicked(QSystemTrayIcon::ActivationReason iReason)
{
switch(iReason)
{
case QSystemTrayIcon::Trigger: // single left click
//_icon->showMessage("Volume control", "Current volume: " + QString::number(VolumeChanger::Instance().getVolume()));
DBG("Icon clicked with left button" );
break;
case QSystemTrayIcon::Context: // right click
showMenu();
break;
case QSystemTrayIcon::DoubleClick: // double click
showSettingsWindow();
break;
case QSystemTrayIcon::MiddleClick:
DBG("Icon clicked with middle button");
VolumeChanger::Instance().setMute(!VolumeChanger::Instance().isMute());
break;
case QSystemTrayIcon::Unknown:
default:
DBG("Nothing to do for tray Icon activation");
break;
}
}
void Tray::showMenu()
{
// Create Icon Menu for right-click
if (!_iconMenu)
{
_iconMenu = new QMenu(0);
_iconMenu->setAttribute( Qt::WA_DeleteOnClose );
connect(_iconMenu, SIGNAL(destroyed(QObject*)), this, SLOT(finishMenu(QObject*)));
_iconMenu->setTitle("Volume Cntrol");
_statusAction = _iconMenu->addAction(QString("Volume: %1%").arg(22));
_statusAction->setEnabled(false);
_iconMenu->addSeparator();
_iconMenu->addAction("Settings", this, SLOT(showSettingsWindow()));
_iconMenu->addSeparator();
_iconMenu->addAction("&About", this, SLOT(showAboutWindow()));
_iconMenu->addAction("&Quit", QApplication::instance(), SLOT(quit()), QKeySequence::Quit );
connect(_iconMenu, SIGNAL(aboutToShow()), this, SLOT(updateStatus()));
}
_iconMenu->exec(QCursor::pos());
}
void Tray::finishMenu(QObject * )
{
// don't "delete" because this slot is called on "destroyed" signal
_iconMenu = NULL;
DBG("FinishMenu");
}
void Tray::msgClicked()
{
DBG("Message clicked");
}
void Tray::showAboutWindow()
{
//QMessageBox::about(0, "About", "Volume Control is a very nice tool.\n"
// "Thanks for using it!\n");
if(!_aboutDialog)
{
_aboutDialog = new About(NULL);
_aboutDialog->setAttribute( Qt::WA_DeleteOnClose );
_aboutDialog->setWindowFlags(Qt::Window | Qt::MSWindowsFixedSizeDialogHint);
connect(_aboutDialog, SIGNAL(destroyed(QObject*)), this, SLOT(finishAboutWindow(QObject*)));
}
_aboutDialog->show();
}
void Tray::finishAboutWindow(QObject*)
{
// All signals are disconnected on delete
// no need to "delete" because this slot is called on "destroyed" signal
_aboutDialog = NULL;
DBG("Finish About");
}
void Tray::showSettingsWindow()
{
if(!_settingsDialog)
{
_settingsDialog = new Settings(NULL, _config);
_settingsDialog->setAttribute( Qt::WA_DeleteOnClose );
_settingsDialog->setWindowFlags(Qt::Window | Qt::MSWindowsFixedSizeDialogHint);
connect(_settingsDialog, SIGNAL(volumeChanged(double)), this, SLOT(changeVolume(double)));
connect(_settingsDialog, SIGNAL(destroyed(QObject*)), this, SLOT(finishSettingsWindow(QObject*)));
connect(_settingsDialog, SIGNAL(configChanged(SettingsConfig_t)), this, SLOT(changeConfig(SettingsConfig_t)));
}
_settingsDialog->show();
}
void Tray::finishSettingsWindow(QObject*)
{
// All signals are disconnected on delete
// no need to "delete" because this slot is called on "destroyed" signal
_settingsDialog = NULL;
DBG("Finish Settings");
}
void Tray::updateStatus()
{
QString mute = (VolumeChanger::Instance().isMute())?" (MUTE)":"";
_statusAction->setText(QString("Volume: %1%%2").arg(VolumeChanger::Instance().getVolume()*100).arg(mute));
}
void Tray::changeVolume(double val)
{
VolumeChanger::Instance().setVolume(val);
// unmute if user changed the volume using slider in settings window
if (VolumeChanger::Instance().isMute()){VolumeChanger::Instance().setMute(false);};
}
void Tray::changeConfig(SettingsConfig_t val)
{
_config = val;
_keyLstnr.stop();
_keyLstnr.clear();
_keyLstnr.addKey( _config.mute.nativeVirtualKey(), _config.mute.nativeModifiers(), muteCb );
_keyLstnr.addKey( _config.volDown.nativeVirtualKey(), _config.volDown.nativeModifiers(), minusCb);
_keyLstnr.addKey( _config.volUp.nativeVirtualKey(), _config.volUp.nativeModifiers(), plusCb );
_keyLstnr.start();
if (!_keyLstnr.isRunning())
{
QMessageBox msgbox(QMessageBox::Warning, "Error", "It seems your hotkeys combination cannot be taken\n"
"Please choose another hotkey combination",
QMessageBox::Ok);
msgbox.exec();
}
if (!QDir(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation)).exists())
{
if (!QDir().mkdir(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation)))
{
QMessageBox msgbox(QMessageBox::Warning, "Error", "Cannot create config dir\n"
"Make sure you have enough space and your filesystem is not read-only",
QMessageBox::Ok);
msgbox.exec();
}
}
if (!_config.saveToFile(CFG_FILENAME))
{
QMessageBox msgbox(QMessageBox::Warning, "Error", "Cannot save your config to file\n"
"Make sure you have enough space and your filesystem is not read-only",
QMessageBox::Ok);
msgbox.exec();
}
}
void Tray::showVolumePopup()
{
QString str = "Volume: "+QString::number(VolumeChanger::Instance().getVolume()*100) + "%" ;
showMsgPopup(str);
}
void Tray::showMutePopup()
{
QString str = (VolumeChanger::Instance().isMute())?"MUTE":"UNMUTE";
showMsgPopup(str);
}
void Tray::showMsgPopup(const QString& iMessage)
{
if (!this->_config.showPopup){return;}
QLabel *popupLabel;
if (_popupWidget != NULL )
{
DBG("Previous widget is not deleted! Stopping the timer and using it again");
_popupTimer->stop();
popupLabel = _popupWidget->findChild<QLabel*>("__POPUP_LABEL__");
if (!popupLabel){popupLabel = new QLabel(_popupWidget);}
}
else
{
_popupWidget = new QWidget();
_popupWidget->setWindowFlags(Qt::Tool |
//Qt::CustomizeWindowHint|
Qt::WindowDoesNotAcceptFocus |
Qt::NoDropShadowWindowHint |
Qt::FramelessWindowHint |
Qt::WindowStaysOnTopHint
);
_popupWidget->setAttribute(Qt::WA_DeleteOnClose);
_popupWidget->setAttribute(Qt::WA_ShowWithoutActivating);
_popupWidget->setAttribute(Qt::WA_TranslucentBackground);
_popupWidget->setStyleSheet("background:transparent");
_popupWidget->setWindowOpacity(0.8);
connect(_popupWidget, SIGNAL(destroyed(QObject*)), this, SLOT(finishPopupWidget(QObject*)));
popupLabel = new QLabel(_popupWidget);
popupLabel->setObjectName("__POPUP_LABEL__");
QFont font( "Arial", 24, QFont::Bold);
popupLabel->setFont(font);
popupLabel->setStyleSheet("QLabel { color : #FEC617; }"); // 32CC99
}
popupLabel->setText(iMessage);
popupLabel->adjustSize();
//Choose position: right bottom corner
QRect scr = QApplication::desktop()->availableGeometry();
_popupWidget->setGeometry(scr.width() - popupLabel->geometry().width(),
scr.height() - popupLabel->geometry().height(),
popupLabel->geometry().width(),
popupLabel->geometry().height());
connect(_popupTimer, SIGNAL(timeout()), _popupWidget, SLOT(close()));
_popupWidget->show();
_popupTimer->start(650);
}
void Tray::finishPopupWidget(QObject*)
{
// All signals are disconnected on delete
// no need to "delete" because this slot is called on "destroyed" signal
_popupWidget = NULL;
DBG("Finish PopupWidget");
}