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()
|
2017-07-29 17:22:17 +02:00
|
|
|
<< QObject::tr("Could not bind the hotkey %1! Is the keybind already registered?").arg(seqName);
|
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-07-05 16:41:53 +02:00
|
|
|
QString k = settings::settings().value(name).toString();
|
|
|
|
if (!k.isEmpty()) {
|
2017-07-07 16:26:00 +02:00
|
|
|
h = new QHotkey(QKeySequence(k), true);
|
2017-07-05 15:30:50 +02:00
|
|
|
} else
|
2017-07-05 16:41:53 +02:00
|
|
|
h = new QHotkey(def.isEmpty() ? "" : def, true);
|
2017-05-09 17:26:00 +02:00
|
|
|
QObject::connect(h, &QHotkey::activated, func);
|
|
|
|
hotkeys.insert(seqName, h);
|
2017-07-05 16:41:53 +02:00
|
|
|
if (!h->isRegistered() && !h->shortcut().toString().isEmpty())
|
2017-06-29 16:48:40 +02:00
|
|
|
qWarning().noquote().nospace()
|
2017-07-29 17:22:17 +02:00
|
|
|
<< QObject::tr("Could not bind the hotkey %1! Is the keybind already registered?").arg(seqName);
|
|
|
|
;
|
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
|
|
|
}
|