File indexing completed on 2024-12-22 04:28:24
0001 /* 0002 SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "speechtotextselectdevicewidget.h" 0008 #include <KConfigGroup> 0009 #include <KLocalizedString> 0010 #include <KSharedConfig> 0011 #include <QAudioDevice> 0012 #include <QMediaDevices> 0013 0014 #include <QComboBox> 0015 #include <QHBoxLayout> 0016 #include <QLabel> 0017 0018 using namespace TextSpeechToText; 0019 namespace 0020 { 0021 const char mySoundGroupName[] = "Speech To Text"; 0022 } 0023 SpeechToTextSelectDeviceWidget::SpeechToTextSelectDeviceWidget(QWidget *parent) 0024 : QWidget{parent} 0025 , mDeviceComboBox(new QComboBox(this)) 0026 { 0027 auto mainLayout = new QHBoxLayout(this); 0028 mainLayout->setObjectName(QStringLiteral("mainLayout")); 0029 mainLayout->setContentsMargins({}); 0030 0031 auto label = new QLabel(i18n("Input:"), this); 0032 label->setObjectName(QStringLiteral("label")); 0033 mainLayout->addWidget(label); 0034 0035 mDeviceComboBox->setObjectName(QStringLiteral("mDeviceComboBox")); 0036 mainLayout->addWidget(mDeviceComboBox); 0037 initializeInput(); 0038 } 0039 0040 SpeechToTextSelectDeviceWidget::~SpeechToTextSelectDeviceWidget() = default; 0041 0042 void SpeechToTextSelectDeviceWidget::loadSettings() 0043 { 0044 KConfigGroup group(KSharedConfig::openConfig(), QLatin1String(mySoundGroupName)); 0045 const QByteArray deviceIdentifier = group.readEntry("SoundDevice", QByteArray()); 0046 if (!deviceIdentifier.isEmpty()) { 0047 const int nbDevice{mDeviceComboBox->count()}; 0048 for (int i = 0; i < nbDevice; ++i) { 0049 const QAudioDevice audioDevice = mDeviceComboBox->itemData(i).value<QAudioDevice>(); 0050 if (audioDevice.id() == deviceIdentifier) { 0051 mDeviceComboBox->setCurrentIndex(i); 0052 break; 0053 } 0054 } 0055 } 0056 } 0057 0058 void SpeechToTextSelectDeviceWidget::saveSettings() 0059 { 0060 KConfigGroup group(KSharedConfig::openConfig(), QLatin1String(mySoundGroupName)); 0061 const auto device = mDeviceComboBox->itemData(mDeviceComboBox->currentIndex()).value<QAudioDevice>(); 0062 if (!device.isNull()) { 0063 const QByteArray deviceIdentifier = device.id(); 0064 group.writeEntry("SoundDevice", deviceIdentifier); 0065 } 0066 } 0067 0068 void SpeechToTextSelectDeviceWidget::initializeInput() 0069 { 0070 mDeviceComboBox->addItem(i18n("Default"), QVariant(QString())); 0071 for (const auto &device : QMediaDevices::audioInputs()) { 0072 const auto name = device.description(); 0073 mDeviceComboBox->addItem(name, QVariant::fromValue(device)); 0074 } 0075 } 0076 0077 #include "moc_speechtotextselectdevicewidget.cpp"