243 lines
11 KiB
C++
243 lines
11 KiB
C++
#include <QCoreApplication>
|
|
#include "radiosettingspage.h"
|
|
#include "ptt_hotkey_manager.h"
|
|
#include "radio_audio_engine.h"
|
|
#include "../db/db_manager.h"
|
|
#include "../../lib/debug_config.h"
|
|
|
|
#include <QApplication>
|
|
#include <QVBoxLayout>
|
|
#include <QHBoxLayout>
|
|
#include <QLabel>
|
|
#include <QPushButton>
|
|
#include <QHideEvent>
|
|
#include <QShowEvent>
|
|
#include <QFormLayout>
|
|
#include <QSpinBox>
|
|
#include <QCheckBox>
|
|
|
|
RadioSettingsPage::RadioSettingsPage(DbManager* db, QWidget* parent) : QWidget(parent), m_db(db) {
|
|
auto* layout = new QVBoxLayout(this);
|
|
auto* title = new QLabel(QCoreApplication::translate("ChatGui", "Radio (PTT)"), this);
|
|
title->setStyleSheet("font-weight: bold; font-size: 13px;");
|
|
layout->addWidget(title);
|
|
m_audioSettings = new QWidget(this);
|
|
auto* form = new QFormLayout(m_audioSettings);
|
|
form->setContentsMargins(0, 0, 0, 12);
|
|
auto add = [&](const QString& label, int min, int max, const QString& suffix) {
|
|
auto* spin = new QSpinBox(m_audioSettings);
|
|
spin->setRange(min, max);
|
|
spin->setSuffix(suffix);
|
|
form->addRow(label, spin);
|
|
return spin;
|
|
};
|
|
m_threshold = add(QCoreApplication::translate("ChatGui", "VAD speech threshold:"), 10, 95, QStringLiteral(" %"));
|
|
m_threshold->setToolTip(QCoreApplication::translate("ChatGui",
|
|
"Speech probability threshold: lower is more sensitive. This is not a volume threshold."));
|
|
m_hangover = add(QCoreApplication::translate("ChatGui",
|
|
"Silence before ending transmission:"), 50, 2000, QCoreApplication::translate("ChatGui",
|
|
" ms"));
|
|
m_compressor = new QCheckBox(QCoreApplication::translate("ChatGui", "Transmission and VAD compressor"), m_audioSettings);
|
|
form->addRow(m_compressor);
|
|
m_txGain = add(QCoreApplication::translate("ChatGui", "Transmission max gain:"), 1, 60, QCoreApplication::translate("ChatGui", " dB"));
|
|
m_vadGain = add(QCoreApplication::translate("ChatGui", "VAD max gain:"), 1, 60, QCoreApplication::translate("ChatGui", " dB"));
|
|
auto* explanation = new QLabel(QCoreApplication::translate("ChatGui",
|
|
"Enable VAD using the button on the radio panel. Speech prebuffer is 0.6 s. The VAD "
|
|
"compressor has a separate maximum gain; other parameters are shared with the transmission "
|
|
"compressor. Set the gain rise rate in Audio."), m_audioSettings);
|
|
explanation->setWordWrap(true);
|
|
form->addRow(explanation);
|
|
layout->addWidget(m_audioSettings);
|
|
connect(RadioAudioEngine::instance(), &RadioAudioEngine::stateChanged, this, [this]() {
|
|
auto* engine = RadioAudioEngine::instance();
|
|
m_audioSettings->setEnabled(engine->settingsReady() && !engine->settingsBusy());
|
|
if (!m_audioSettingsLoaded && engine->settingsReady()) loadAudioSettings();
|
|
});
|
|
loadAudioSettings();
|
|
if (PttHotkeyManager::usesPortal()) {
|
|
auto* hint = new QLabel(QCoreApplication::translate("ChatGui",
|
|
"Global shortcuts work from any application. Assign a regular key with Ctrl, Shift, Alt or "
|
|
"Win in the system dialog. Changes are saved by the system immediately. Keyboard "
|
|
"transmission is disabled during setup."), this);
|
|
hint->setWordWrap(true);
|
|
layout->addWidget(hint);
|
|
auto* button = new QPushButton(QCoreApplication::translate("ChatGui", "Set PTT shortcuts…"), this);
|
|
layout->addWidget(button);
|
|
m_buttons << button;
|
|
for (int i = 0; i < 3; i++) {
|
|
auto* label = new QLabel(this);
|
|
label->setWordWrap(true); label->setTextFormat(Qt::PlainText);
|
|
layout->addWidget(label); m_labels << label;
|
|
}
|
|
m_portalStatus = new QLabel(this);
|
|
m_portalStatus->setWordWrap(true);
|
|
m_portalStatus->setTextFormat(Qt::PlainText);
|
|
m_portalStatus->setStyleSheet("color: gray; font-size: 11px;");
|
|
layout->addWidget(m_portalStatus);
|
|
connect(button, &QPushButton::clicked, this, [this]() { PttHotkeyManager::instance()->configurePortal(this); });
|
|
connect(PttHotkeyManager::instance(), &PttHotkeyManager::portalChanged, this, &RadioSettingsPage::updatePortalLabels);
|
|
updatePortalLabels();
|
|
layout->addStretch(1);
|
|
return;
|
|
}
|
|
auto* hint = new QLabel(
|
|
QCoreApplication::translate("ChatGui",
|
|
"Hold a shortcut to transmit from any application, even when ChatGUI is not focused. "
|
|
"Channel radio must be enabled. To record a shortcut, hold the desired keys together and "
|
|
"release them. Left and right Ctrl, Shift, Alt and Win are distinct. Fn is available only "
|
|
"if the keyboard reports it to the OS."), this);
|
|
hint->setWordWrap(true);
|
|
hint->setStyleSheet("color: gray; font-size: 11px;");
|
|
layout->addWidget(hint);
|
|
if (!PttHotkeyManager::isSupported()) {
|
|
auto* warn = new QLabel(QCoreApplication::translate("ChatGui",
|
|
"Global shortcuts are available on Windows and Linux/X11. Shortcut recording is "
|
|
"unavailable on this platform."), this);
|
|
warn->setWordWrap(true);
|
|
warn->setStyleSheet("color: #e74c3c; font-size: 12px;");
|
|
layout->addWidget(warn);
|
|
}
|
|
for (int i = 0; i < 3; i++) {
|
|
auto* row = new QHBoxLayout();
|
|
auto* button = new QPushButton(QCoreApplication::translate("ChatGui", "Set PTT shortcut %1").arg(i + 1), this);
|
|
button->setEnabled(PttHotkeyManager::isSupported());
|
|
auto* clear = new QPushButton(QCoreApplication::translate("ChatGui", "Clear"), this);
|
|
row->addWidget(button); row->addWidget(clear);
|
|
layout->addLayout(row);
|
|
auto* label = new QLabel(this);
|
|
label->setWordWrap(true);
|
|
label->setTextFormat(Qt::PlainText);
|
|
label->setStyleSheet("font-size: 13px; padding-bottom: 12px;");
|
|
layout->addWidget(label);
|
|
m_buttons << button; m_labels << label; m_chords << PttKeyChord();
|
|
connect(button, &QPushButton::clicked, this, [this, i]() {
|
|
if (m_captureIndex == i) finishCapture(false);
|
|
else startCapture(i);
|
|
});
|
|
connect(clear, &QPushButton::clicked, this, [this, i]() {
|
|
finishCapture(false);
|
|
m_chords[i].clear(); updateLabel(i);
|
|
});
|
|
}
|
|
connect(PttHotkeyManager::instance(), &PttHotkeyManager::keysChanged, this, [this](const PttKeyChord& held) {
|
|
if (m_captureIndex < 0) return;
|
|
if (m_capture.observe(held)) finishCapture(true);
|
|
else if (!held.isEmpty())
|
|
m_labels[m_captureIndex]->setText(PttHotkeyManager::instance()->describe(m_capture.candidate));
|
|
});
|
|
layout->addStretch(1);
|
|
loadFromDb();
|
|
}
|
|
|
|
RadioSettingsPage::~RadioSettingsPage() {
|
|
finishCapture(false);
|
|
if (m_portalStatus && isVisible()) PttHotkeyManager::instance()->inhibitPortal(false);
|
|
}
|
|
|
|
void RadioSettingsPage::startCapture(int index) {
|
|
finishCapture(false);
|
|
if (!PttHotkeyManager::instance()->beginCapture()) {
|
|
m_labels[index]->setText(QCoreApplication::translate("ChatGui", "Failed to start key recording. See the radio log for details."));
|
|
return;
|
|
}
|
|
m_captureIndex = index;
|
|
m_capture.candidate.clear();
|
|
m_buttons[index]->setText(QCoreApplication::translate("ChatGui", "Cancel recording"));
|
|
m_labels[index]->setText(QCoreApplication::translate("ChatGui", "Press one or more keys together…"));
|
|
qApp->installEventFilter(this);
|
|
}
|
|
|
|
void RadioSettingsPage::finishCapture(bool save) {
|
|
if (m_captureIndex < 0) return;
|
|
int index = m_captureIndex;
|
|
if (save) {
|
|
m_chords[index] = m_capture.candidate;
|
|
DEBUG_INFO(DEBUG_CATEGORY_RADIO, "PTT: captured chord=%s", qPrintable(PttHotkeyManager::instance()->describe(m_capture.candidate)));
|
|
}
|
|
m_captureIndex = -1;
|
|
qApp->removeEventFilter(this);
|
|
PttHotkeyManager::instance()->endCapture();
|
|
m_buttons[index]->setText(QCoreApplication::translate("ChatGui", "Set PTT shortcut %1").arg(index + 1));
|
|
updateLabel(index);
|
|
}
|
|
|
|
bool RadioSettingsPage::eventFilter(QObject* object, QEvent* event) {
|
|
if (m_captureIndex >= 0) {
|
|
if (event->type() == QEvent::ShortcutOverride) { event->accept(); return true; }
|
|
if (event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease) return true;
|
|
}
|
|
return QWidget::eventFilter(object, event);
|
|
}
|
|
|
|
void RadioSettingsPage::showEvent(QShowEvent* event) {
|
|
if (m_portalStatus) PttHotkeyManager::instance()->inhibitPortal(true);
|
|
QWidget::showEvent(event);
|
|
}
|
|
|
|
void RadioSettingsPage::hideEvent(QHideEvent* event) {
|
|
if (m_portalStatus) PttHotkeyManager::instance()->inhibitPortal(false);
|
|
finishCapture(false);
|
|
QWidget::hideEvent(event);
|
|
}
|
|
|
|
void RadioSettingsPage::updateLabel(int index) {
|
|
m_labels[index]->setText(PttHotkeyManager::instance()->describe(m_chords[index]));
|
|
}
|
|
|
|
void RadioSettingsPage::loadFromDb() {
|
|
loadAudioSettings();
|
|
if (m_portalStatus) { updatePortalLabels(); return; }
|
|
finishCapture(false);
|
|
QStringList values;
|
|
if (m_db && m_db->isOpen()) values = m_db->getUiState("radio_ptt_hotkeys").split('|', Qt::KeepEmptyParts);
|
|
for (int i = 0; i < m_chords.size(); i++) {
|
|
m_chords[i] = i < values.size() ? pttDecodeChord(values[i]) : PttKeyChord();
|
|
updateLabel(i);
|
|
}
|
|
}
|
|
|
|
QStringList RadioSettingsPage::hotkeys() const {
|
|
QStringList values;
|
|
// Пустые позиции тоже сохраняем, чтобы хоткей №3 не переезжал в №1 после открытия.
|
|
for (const auto& chord : m_chords) values << pttEncodeChord(chord);
|
|
return values;
|
|
}
|
|
|
|
bool RadioSettingsPage::apply() {
|
|
if (!RadioAudioEngine::instance()->applySettings({
|
|
{QStringLiteral("radio_vad_threshold"), m_threshold->value()},
|
|
{QStringLiteral("radio_vad_hangover_ms"), m_hangover->value()},
|
|
{QStringLiteral("radio_compressor_enabled"), m_compressor->isChecked() ? 1 : 0},
|
|
{QStringLiteral("radio_compressor_max_gain_db"), m_txGain->value()},
|
|
{QStringLiteral("radio_vad_compressor_max_gain_db"), m_vadGain->value()}})) return false;
|
|
if (m_portalStatus) return true;
|
|
finishCapture(false);
|
|
PttHotkeyManager::instance()->setHotkeys(hotkeys());
|
|
return true;
|
|
}
|
|
|
|
void RadioSettingsPage::loadAudioSettings() {
|
|
auto* engine = RadioAudioEngine::instance();
|
|
m_audioSettings->setEnabled(engine->settingsReady() && !engine->settingsBusy());
|
|
if (!engine->settingsReady()) return;
|
|
m_threshold->setValue(engine->setting(QStringLiteral("radio_vad_threshold")));
|
|
m_hangover->setValue(engine->setting(QStringLiteral("radio_vad_hangover_ms")));
|
|
m_compressor->setChecked(engine->setting(QStringLiteral("radio_compressor_enabled")) != 0);
|
|
m_txGain->setValue(engine->setting(QStringLiteral("radio_compressor_max_gain_db")));
|
|
m_vadGain->setValue(engine->setting(QStringLiteral("radio_vad_compressor_max_gain_db")));
|
|
m_audioSettingsLoaded = true;
|
|
}
|
|
|
|
void RadioSettingsPage::updatePortalLabels() {
|
|
auto* manager = PttHotkeyManager::instance();
|
|
const auto descriptions = manager->portalDescriptions();
|
|
for (int i = 0; i < m_labels.size(); i++) {
|
|
QString text = descriptions.value(i);
|
|
m_labels[i]->setText(QCoreApplication::translate("ChatGui",
|
|
"PTT shortcut %1: %2").arg(i + 1).arg(text.isEmpty() ? QCoreApplication::translate("ChatGui",
|
|
"Not set") : text));
|
|
}
|
|
m_portalStatus->setText(manager->portalStatus());
|
|
m_buttons[0]->setEnabled(!manager->portalBusy());
|
|
}
|