KShare/hotkeying.cpp

47 lines
1.3 KiB
C++
Raw Normal View History

2017-04-26 22:00:13 +02:00
#include "hotkeying.hpp"
2017-04-28 16:49:56 +02:00
#include <QDebug>
2017-04-26 22:00:13 +02:00
#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;
2017-04-29 11:29:50 +02:00
QString name = seqName;
name.prepend("hotkey_");
if (settings::settings().contains(name))
h = new QHotkey(QKeySequence(settings::settings().value(name).toString()), true);
2017-04-26 22:00:13 +02:00
else
h = new QHotkey;
QObject::connect(h, &QHotkey::activated, func);
hotkeys.insert(seqName, h);
}
bool hotkeying::valid(QString seq)
{
2017-04-29 23:00:32 +02:00
return seq.isEmpty() || !QKeySequence(seq).toString().isEmpty();
2017-04-26 22:00:13 +02:00
}
QString hotkeying::sequence(QString seqName)
{
2017-04-29 11:29:50 +02:00
return hotkeys.contains(seqName) ? hotkeys.value(seqName)->shortcut().toString() : "";
2017-04-26 22:00:13 +02:00
}