2017-04-26 22:00:13 +02:00
|
|
|
#include "hotkeying.hpp"
|
|
|
|
|
|
|
|
#include <QHotkey>
|
|
|
|
#include <QMap>
|
|
|
|
#include <settings.hpp>
|
|
|
|
|
|
|
|
QMap<QString, QHotkey *> hotkeys;
|
|
|
|
|
|
|
|
// func gets bound only on first set, or load
|
2017-05-09 17:26:00 +02:00
|
|
|
void hotkeying::hotkey(QString seqName, QKeySequence seq, std::function<void()> func) {
|
2017-06-29 16:48:40 +02:00
|
|
|
QHotkey *hotkey;
|
2017-05-09 17:26:00 +02:00
|
|
|
if (hotkeys.contains(seqName))
|
2017-06-29 16:48:40 +02:00
|
|
|
(hotkey = hotkeys.value(seqName))->setShortcut(seq, true);
|
2017-05-09 17:26:00 +02:00
|
|
|
else {
|
2017-06-29 16:48:40 +02:00
|
|
|
hotkey = new QHotkey(seq, true);
|
2017-05-09 17:26:00 +02:00
|
|
|
QObject::connect(hotkey, &QHotkey::activated, func);
|
|
|
|
hotkeys.insert(seqName, hotkey);
|
|
|
|
}
|
|
|
|
settings::settings().setValue(seqName.prepend("hotkey_"), seq.toString());
|
2017-06-29 16:48:40 +02:00
|
|
|
if (!hotkey->isRegistered() && !seq.toString().isEmpty())
|
|
|
|
qWarning().noquote().nospace()
|
|
|
|
<< "Could not bind the hotkey " << seqName << "! Is the keybind already registered?";
|
2017-04-26 22:00:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// forces the hotkey from settings
|
2017-06-04 01:04:42 +02:00
|
|
|
void hotkeying::load(QString seqName, std::function<void()> func, QString def) {
|
2017-05-09 17:26:00 +02:00
|
|
|
QHotkey *h;
|
|
|
|
QString name = seqName;
|
|
|
|
name.prepend("hotkey_");
|
2017-06-23 14:39:51 +02:00
|
|
|
if (hotkeys.contains(seqName)) return;
|
2017-05-09 17:26:00 +02:00
|
|
|
if (settings::settings().contains(name))
|
|
|
|
h = new QHotkey(QKeySequence(settings::settings().value(name).toString()), true);
|
|
|
|
else
|
2017-06-04 01:04:42 +02:00
|
|
|
h = new QHotkey(def.isNull() ? "" : def, true);
|
2017-05-09 17:26:00 +02:00
|
|
|
QObject::connect(h, &QHotkey::activated, func);
|
|
|
|
hotkeys.insert(seqName, h);
|
2017-06-29 16:48:40 +02:00
|
|
|
if (!h->isRegistered() && (settings::settings().contains(name) || !def.isEmpty()))
|
|
|
|
qWarning().noquote().nospace()
|
|
|
|
<< "Could not bind the hotkey " << seqName << "! Is the keybind already registered?";
|
2017-04-26 22:00:13 +02:00
|
|
|
}
|
|
|
|
|
2017-05-06 13:21:12 +02:00
|
|
|
bool hotkeying::valid(QString seq) {
|
2017-05-09 17:26:00 +02:00
|
|
|
return seq.isEmpty() || !QKeySequence(seq).toString().isEmpty();
|
2017-04-26 22:00:13 +02:00
|
|
|
}
|
|
|
|
|
2017-05-06 13:21:12 +02:00
|
|
|
QString hotkeying::sequence(QString seqName) {
|
2017-06-04 22:58:29 +02:00
|
|
|
return hotkeys.contains(seqName) ?
|
|
|
|
hotkeys.value(seqName)->isRegistered() ? hotkeys.value(seqName)->shortcut().toString() : "" :
|
|
|
|
"";
|
2017-04-26 22:00:13 +02:00
|
|
|
}
|