KTextAddons

speechtotextselectdevicewidget.cpp
1/*
2 SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "speechtotextselectdevicewidget.h"
8#include <KConfigGroup>
9#include <KLocalizedString>
10#include <KSharedConfig>
11#include <QAudioDevice>
12#include <QMediaDevices>
13
14#include <QComboBox>
15#include <QHBoxLayout>
16#include <QLabel>
17
18using namespace TextSpeechToText;
19namespace
20{
21const char mySoundGroupName[] = "Speech To Text";
22}
23SpeechToTextSelectDeviceWidget::SpeechToTextSelectDeviceWidget(QWidget *parent)
24 : QWidget{parent}
25 , mDeviceComboBox(new QComboBox(this))
26{
27 auto mainLayout = new QHBoxLayout(this);
28 mainLayout->setObjectName(QStringLiteral("mainLayout"));
29 mainLayout->setContentsMargins({});
30
31 auto label = new QLabel(i18nc("@label:textbox", "Input:"), this);
32 label->setObjectName(QStringLiteral("label"));
33 mainLayout->addWidget(label);
34
35 mDeviceComboBox->setObjectName(QStringLiteral("mDeviceComboBox"));
36 mainLayout->addWidget(mDeviceComboBox);
37 initializeInput();
38}
39
40SpeechToTextSelectDeviceWidget::~SpeechToTextSelectDeviceWidget() = default;
41
42void SpeechToTextSelectDeviceWidget::loadSettings()
43{
45 const QByteArray deviceIdentifier = group.readEntry("SoundDevice", QByteArray());
46 if (!deviceIdentifier.isEmpty()) {
47 const int nbDevice{mDeviceComboBox->count()};
48 for (int i = 0; i < nbDevice; ++i) {
49 const QAudioDevice audioDevice = mDeviceComboBox->itemData(i).value<QAudioDevice>();
50 if (audioDevice.id() == deviceIdentifier) {
51 mDeviceComboBox->setCurrentIndex(i);
52 break;
53 }
54 }
55 }
56}
57
58void SpeechToTextSelectDeviceWidget::saveSettings()
59{
61 const auto device = mDeviceComboBox->itemData(mDeviceComboBox->currentIndex()).value<QAudioDevice>();
62 if (!device.isNull()) {
63 const QByteArray deviceIdentifier = device.id();
64 group.writeEntry("SoundDevice", deviceIdentifier);
65 }
66}
67
68void SpeechToTextSelectDeviceWidget::initializeInput()
69{
70 mDeviceComboBox->addItem(i18n("Default"), QVariant(QString()));
71 for (const auto &device : QMediaDevices::audioInputs()) {
72 const auto name = device.description();
73 mDeviceComboBox->addItem(name, QVariant::fromValue(device));
74 }
75}
76
77#include "moc_speechtotextselectdevicewidget.cpp"
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
QString name(GameStandardAction id)
QString label(StandardShortcut id)
bool isEmpty() const const
void addItem(const QIcon &icon, const QString &text, const QVariant &userData)
void setCurrentIndex(int index)
QVariant itemData(int index, int role) const const
QVariant fromValue(T &&value)
T value() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:29:59 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.