KShare/src/hotkeying.cpp

53 lines
1.8 KiB
C++
Raw Normal View History

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
void hotkeying::hotkey(QString seqName, QKeySequence seq, std::function<void()> func) {
QHotkey *hotkey;
if (hotkeys.contains(seqName))
(hotkey = hotkeys.value(seqName))->setShortcut(seq, true);
else {
hotkey = new QHotkey(seq, true);
QObject::connect(hotkey, &QHotkey::activated, func);
hotkeys.insert(seqName, hotkey);
}
settings::settings().setValue(seqName.prepend("hotkey_"), seq.toString());
if (!hotkey->isRegistered() && !seq.toString().isEmpty())
qWarning().noquote().nospace()
<< 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
void hotkeying::load(QString seqName, std::function<void()> func, QString def) {
QHotkey *h;
QString name = seqName;
name.prepend("hotkey_");
2017-06-23 14:39:51 +02:00
if (hotkeys.contains(seqName)) return;
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
h = new QHotkey(def.isEmpty() ? "" : def, true);
QObject::connect(h, &QHotkey::activated, func);
hotkeys.insert(seqName, h);
if (!h->isRegistered() && !h->shortcut().toString().isEmpty())
qWarning().noquote().nospace()
<< 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) {
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) {
return hotkeys.contains(seqName) ?
hotkeys.value(seqName)->isRegistered() ? hotkeys.value(seqName)->shortcut().toString() : "" :
"";
2017-04-26 22:00:13 +02:00
}