You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
1.9 KiB
75 lines
1.9 KiB
#include "ptt_hotkey_manager.h" |
|
#include "radio_audio_engine.h" |
|
#include "../db/db_manager.h" |
|
#include "qhotkey.h" |
|
|
|
#include <QKeySequence> |
|
#include <QDebug> |
|
|
|
static PttHotkeyManager* g_pttHotkeys = nullptr; |
|
|
|
PttHotkeyManager* PttHotkeyManager::instance() { |
|
if (!g_pttHotkeys) g_pttHotkeys = new PttHotkeyManager(); |
|
return g_pttHotkeys; |
|
} |
|
|
|
bool PttHotkeyManager::isSupported() { |
|
return QHotkey::isPlatformSupported(); |
|
} |
|
|
|
void PttHotkeyManager::init(DbManager* db) { |
|
m_db = db; |
|
reload(); |
|
} |
|
|
|
QStringList PttHotkeyManager::hotkeys() const { |
|
return m_seqs; |
|
} |
|
|
|
void PttHotkeyManager::setHotkeys(const QStringList& seqs) { |
|
applyHotkeys(seqs); |
|
} |
|
|
|
void PttHotkeyManager::reload() { |
|
QStringList seqs; |
|
if (m_db && m_db->isOpen()) { |
|
const QString raw = m_db->getUiState("radio_ptt_hotkeys"); |
|
if (!raw.isEmpty()) |
|
seqs = raw.split('|', Qt::SkipEmptyParts); |
|
} |
|
applyHotkeys(seqs); |
|
} |
|
|
|
void PttHotkeyManager::clearHotkeys() { |
|
for (QHotkey* h : m_hotkeys) { |
|
h->resetShortcut(); |
|
h->deleteLater(); |
|
} |
|
m_hotkeys.clear(); |
|
} |
|
|
|
void PttHotkeyManager::applyHotkeys(const QStringList& seqs) { |
|
clearHotkeys(); |
|
m_seqs.clear(); |
|
|
|
for (const QString& s : seqs) { |
|
QKeySequence seq(s); |
|
if (seq.isEmpty()) continue; |
|
auto* hk = new QHotkey(seq, true, this); |
|
if (!hk->isRegistered()) { |
|
qWarning("PttHotkey: failed to register '%s'", qPrintable(s)); |
|
hk->deleteLater(); |
|
continue; |
|
} |
|
connect(hk, &QHotkey::activated, this, []() { |
|
RadioAudioEngine::instance()->talkBegin(); |
|
}); |
|
connect(hk, &QHotkey::released, this, []() { |
|
RadioAudioEngine::instance()->talkEnd(); |
|
}); |
|
m_hotkeys.append(hk); |
|
m_seqs.append(seq.toString()); |
|
} |
|
|
|
qDebug("PttHotkey: applied %d hotkey(s)", (int)m_hotkeys.size()); |
|
}
|
|
|