13 changed files with 189 additions and 21 deletions
@ -0,0 +1,3 @@
|
||||
[submodule "QHotkey"] |
||||
path = QHotkey |
||||
url = https://github.com/Skycoder42/QHotkey.git |
@ -0,0 +1,43 @@
|
||||
#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) |
||||
{ |
||||
if (hotkeys.contains(seqName)) |
||||
hotkeys.value(seqName)->setShortcut(seq, true); |
||||
else |
||||
{ |
||||
QHotkey *hotkey = new QHotkey(seq, true); |
||||
QObject::connect(hotkey, &QHotkey::activated, func); |
||||
hotkeys.insert(seqName, hotkey); |
||||
} |
||||
settings::settings().setValue(seqName.prepend("hotkey_"), seq.toString()); |
||||
} |
||||
|
||||
// forces the hotkey from settings
|
||||
void hotkeying::load(QString seqName, std::function<void()> func) |
||||
{ |
||||
QHotkey *h; |
||||
if (settings::settings().contains(seqName.prepend("hotkey_"))) |
||||
h = new QHotkey(QKeySequence(settings::settings().value(seqName.prepend("hotkey_")).toString()), true); |
||||
else |
||||
h = new QHotkey; |
||||
QObject::connect(h, &QHotkey::activated, func); |
||||
hotkeys.insert(seqName, h); |
||||
} |
||||
|
||||
bool hotkeying::valid(QString seq) |
||||
{ |
||||
return !QKeySequence(seq).toString().isEmpty(); |
||||
} |
||||
|
||||
QString hotkeying::sequence(QString seqName) |
||||
{ |
||||
return hotkeys.contains(seqName) ? hotkeys.value(seqName)->shortcut().toString() : ""; |
||||
} |
@ -0,0 +1,16 @@
|
||||
#ifndef HOTKEYING_HPP |
||||
#define HOTKEYING_HPP |
||||
|
||||
#include <QKeySequence> |
||||
#include <QString> |
||||
#include <functional> |
||||
|
||||
namespace hotkeying |
||||
{ |
||||
void hotkey(QString seqName, QKeySequence seq, std::function<void()> func); |
||||
bool valid(QString seq); |
||||
void load(QString seqName, std::function<void()> func); |
||||
QString sequence(QString seqName); |
||||
} |
||||
|
||||
#endif // HOTKEYING_HPP
|
Loading…
Reference in new issue