-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsettings.cpp
More file actions
251 lines (213 loc) · 8.48 KB
/
settings.cpp
File metadata and controls
251 lines (213 loc) · 8.48 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
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
#include "settings.h"
#include "ui_settings.h"
#include "volumechanger.h"
#include "logging.h"
#include "settings.h"
#include <QKeyEvent>
#include <windows.h>
#include <QFile>
#include <QMessageBox>
#include <QSettings>
#include <QDir>
#define FILE_HEADER 0x5D6A1F
#define FILE_VERSION 0x02
SettingsConfig_t Settings::DEFAULT_CFG = {
QKeyEvent(QEvent::KeyPress, Qt::Key_0, Qt::AltModifier|Qt::ShiftModifier, 0, 48, 517),
QKeyEvent(QEvent::KeyPress, Qt::Key_Minus, Qt::AltModifier|Qt::ShiftModifier, 0, 189, 517),
QKeyEvent(QEvent::KeyPress, Qt::Key_Equal, Qt::AltModifier|Qt::ShiftModifier, 0, 187, 517),
true // show popup messages
};
Settings::Settings(QWidget *parent, SettingsConfig_t& config) :
QDialog(parent),
ui(new Ui::Settings),
_config(config)
{
this->setFixedSize(this->sizeHint());
ui->setupUi(this);
double aVol = VolumeChanger::Instance().getVolume();
ui->_volumeSlider->setValue( aVol*100 );
ui->_volumeLevelLabel->setText(VolumeChanger::Instance().isMute()?"MUTE":QString::number(aVol));
ui->_keyMute-> setKeySequence(QKeySequence(_config.mute.modifiers()|_config.mute.key()));
ui->_keyVolDown->setKeySequence(QKeySequence(_config.volDown.modifiers()|_config.volDown.key()));
ui->_keyVolUp-> setKeySequence(QKeySequence(_config.volUp.modifiers()|_config.volUp.key()));
ui->_autorunCheckBox->setChecked(isAutoRun());
ui->_showPopupCheckBox->setChecked(this->_config.showPopup);
// internal connections
connect(ui->_volumeSlider, SIGNAL(valueChanged(int)), this, SLOT(slt_sliderValueChanged(int)));
connect(ui->_keyMute, SIGNAL(hotKeysReady(QKeyEvent)), this, SLOT(slt_muteChanged(QKeyEvent)));
connect(ui->_keyVolDown, SIGNAL(hotKeysReady(QKeyEvent)), this, SLOT(slt_volDownChanged(QKeyEvent)));
connect(ui->_keyVolUp, SIGNAL(hotKeysReady(QKeyEvent)), this, SLOT(slt_volUpChanged(QKeyEvent)));
}
Settings::~Settings()
{
delete ui;
}
// Private slots
void Settings::slt_sliderValueChanged(int value)
{
double vald = (double)value/100;
ui->_volumeLevelLabel->setText(QString::number(vald));
emit volumeChanged(vald);
}
void Settings::slt_muteChanged(QKeyEvent value)
{
_config.mute = value;
}
void Settings::slt_volDownChanged(QKeyEvent value)
{
_config.volDown = value;
}
void Settings::slt_volUpChanged(QKeyEvent value)
{
_config.volUp = value;
}
void Settings::setAutoRun(bool iAutoRun)
{
QSettings autorun("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",QSettings::NativeFormat);
if(iAutoRun)
{
autorun.setValue(QCoreApplication::applicationName(), QDir::toNativeSeparators(QCoreApplication::applicationFilePath()));
autorun.sync();
}
else
{
autorun.remove(QCoreApplication::applicationName());
}
}
bool Settings::isAutoRun()
{
QSettings autorun("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",QSettings::NativeFormat);
return autorun.contains(QCoreApplication::applicationName());
}
void Settings::accept()
{
if (!_config.isCorrect())
{
QMessageBox msgbox(QMessageBox::Warning, "Error", "All hotkeys combination must be different :)");
msgbox.exec();
return;
}
DBG("New config accepted:\n"
"\tmute \t" << QKeySequence(_config.mute.modifiers()|_config.mute.key()).toString() << "\n"
"\tvolume down\t"<< QKeySequence(_config.volDown.modifiers()|_config.volDown.key()).toString() <<"\n"
"\tvolume up \t"<< QKeySequence(_config.volUp.modifiers()|_config.volUp.key()).toString() << "\n"
"\tAutorun \t"<< ui->_autorunCheckBox->isChecked());
setAutoRun(ui->_autorunCheckBox->isChecked());
_config.showPopup = ui->_showPopupCheckBox->isChecked();
emit configChanged(_config);
QDialog::accept();
}
void SettingsKeySequence::keyPressEvent(QKeyEvent *event)
{
setKeySequence(QKeySequence(event->modifiers()|event->key()).toString().trimmed());
if (!this->keySequence().toString().isEmpty())
{
// Problem: NativeModifiers()&0xFF returns SHIFT instead of ALT and ALT intead of SHIFT
// that's why we have to exchange bitsnumber 0 and 2 manually
int num = event->nativeModifiers();
if ((num ^ num >> 2) & 1)
num^=1 | 1 << 2;
//DBG("key: " << event->nativeVirtualKey() <<"\tModifiers: "<< (num & 0xFF) << "\t Scancode:" << event->nativeScanCode());
emit hotKeysReady(QKeyEvent(event->type(),
event->key(),
event->modifiers(),
event->nativeScanCode(),
event->nativeVirtualKey(),
num,
event->text()));
}
}
QDataStream &operator<<(QDataStream &out, const SettingsConfig_t &settingsConfig)
{
out << settingsConfig.mute.type() << settingsConfig.mute.key() << settingsConfig.mute.modifiers() <<
settingsConfig.mute.nativeScanCode() << settingsConfig.mute.nativeVirtualKey() << settingsConfig.mute.nativeModifiers();
out << settingsConfig.volDown.type() << settingsConfig.volDown.key() << settingsConfig.volDown.modifiers() <<
settingsConfig.volDown.nativeScanCode() << settingsConfig.volDown.nativeVirtualKey() << settingsConfig.volDown.nativeModifiers();
out << settingsConfig.volUp.type() << settingsConfig.volUp.key() << settingsConfig.volUp.modifiers() <<
settingsConfig.volUp.nativeScanCode() << settingsConfig.volUp.nativeVirtualKey() << settingsConfig.volUp.nativeModifiers();
out << settingsConfig.showPopup;
return out;
}
QDataStream &operator>>(QDataStream &in, SettingsConfig_t &settingsConfig)
{
int type;
int key;
int modifiers;
bool showPopup;
quint32 nativeScanCode;
quint32 nativeVirtualKey;
quint32 nativeModifiers;
in >> type >> key >> modifiers >> nativeScanCode >> nativeVirtualKey >> nativeModifiers;
QKeyEvent mute(QEvent::Type(type), key, Qt::KeyboardModifiers(modifiers), nativeScanCode, nativeVirtualKey, nativeModifiers);
in >> type >> key >> modifiers >> nativeScanCode >> nativeVirtualKey >> nativeModifiers;
QKeyEvent volDown(QEvent::Type(type), key, Qt::KeyboardModifiers(modifiers), nativeScanCode, nativeVirtualKey, nativeModifiers);
in >> type >> key >> modifiers >> nativeScanCode >> nativeVirtualKey >> nativeModifiers;
QKeyEvent volUp (QEvent::Type(type), key, Qt::KeyboardModifiers(modifiers), nativeScanCode, nativeVirtualKey, nativeModifiers);
in >> showPopup;
settingsConfig = {mute, volDown, volUp, showPopup};
return in;
}
bool SettingsConfig::saveToFile(const QString &filename)
{
QFile file(filename);
if(!file.open(QIODevice::WriteOnly))
{
ERR("Cannot save config to file " << filename);
return false;
}
QDataStream out(&file);
out << (quint32)FILE_HEADER;
out << (quint32)FILE_VERSION;
out.setVersion(QDataStream::Qt_5_5);
out << *this;
file.flush();
file.close();
return true;
}
bool SettingsConfig::loadFromFile(const QString &filename)
{
QFile file(filename);
if (!file.open(QIODevice::ReadOnly))
{
ERR("Cannot open file " << filename << " to load config");
return false;
}
QDataStream in(&file);
// Read and check the header
quint32 magic;
in >> magic;
if (magic != FILE_HEADER)
{
ERR("Cannot load config from file " << filename << ": bad file format");
return false;
}
// Read the version
qint32 version;
in >> version;
if (version > FILE_VERSION)
{
ERR("Cannot load config from file " << filename << ": supported file versions till " << FILE_VERSION
<< ", version in file: " << version);
return false;
}
in.setVersion(QDataStream::Qt_5_5);
// Read the data
in >> *this;
file.close();
// Backward compatibility
if (version == 1)
{
// From file version 1 to 2
this->showPopup = Settings::DEFAULT_CFG.showPopup;
// Upgrade file version
this->saveToFile(filename);
DBG("Migrating to newer version of config file. File "<<filename<<" has been overwritten");
}
return true;
}
bool SettingsConfig::isCorrect()
{
return !((mute.key() == volUp.key() && mute.modifiers() == volUp.modifiers()) ||
(mute.key() == volDown.key() && volDown.modifiers() == volDown.modifiers()) ||
(volDown.key() == volUp.key() && volDown.modifiers() == volUp.modifiers()));
}