KTextAddons

speechtotextcomboboxwidget.cpp
1/*
2 SPDX-FileCopyrightText: 2023-2025 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "speechtotextcomboboxwidget.h"
8#include "speechtotext/textspeechtotextutil.h"
9#include <KConfigGroup>
10#include <KLocalizedString>
11#include <KSharedConfig>
12#include <QComboBox>
13#include <QHBoxLayout>
14#include <QLabel>
15#include <QToolButton>
16#include <TextSpeechToText/SpeechToTextEngineLoader>
17
18using namespace TextSpeechToText;
19SpeechToTextComboBoxWidget::SpeechToTextComboBoxWidget(QWidget *parent)
20 : QWidget(parent)
21 , mEngine(new QComboBox(this))
22 , mConfigureEngine(new QToolButton(this))
23{
24 auto mainLayout = new QHBoxLayout(this);
25 mainLayout->setObjectName(QStringLiteral("mainLayout"));
26 mainLayout->setContentsMargins({});
27
28 auto label = new QLabel(i18nc("@label:textbox", "Engine:"), this);
29 label->setObjectName(QStringLiteral("label"));
30 label->setTextFormat(Qt::PlainText);
31 mainLayout->addWidget(label);
32
33 mEngine->setObjectName(QStringLiteral("mEngine"));
34 mainLayout->addWidget(mEngine);
35
36 mConfigureEngine->setObjectName(QStringLiteral("mConfigureEngine"));
37 mainLayout->addWidget(mConfigureEngine);
38 mConfigureEngine->setEnabled(false); // Disable by default
39 mConfigureEngine->setIcon(QIcon::fromTheme(QStringLiteral("settings-configure")));
40
41 connect(mConfigureEngine, &QToolButton::clicked, this, &SpeechToTextComboBoxWidget::slotConfigureEngine);
42 connect(mEngine, &QComboBox::currentIndexChanged, this, &SpeechToTextComboBoxWidget::slotEngineChanged);
43}
44
45SpeechToTextComboBoxWidget::~SpeechToTextComboBoxWidget() = default;
46
47void SpeechToTextComboBoxWidget::fillEngine()
48{
49 const QMap<QString, QString> map = TextSpeechToText::SpeechToTextEngineLoader::self()->speechToTextEngineInfos();
50 QMapIterator<QString, QString> iMap(map);
51 while (iMap.hasNext()) {
52 iMap.next();
53 mEngine->addItem(iMap.value(), iMap.key());
54 }
55}
56
57QString SpeechToTextComboBoxWidget::engineName() const
58{
59 return mEngine->currentData().toString();
60}
61
62void SpeechToTextComboBoxWidget::setEngineName(const QString &engineName)
63{
64 const int countItem(mEngine->count());
65 for (int i = 0; i < countItem; ++i) {
66 if (mEngine->itemData(i).toString() == engineName) {
67 mEngine->setCurrentIndex(i);
68 break;
69 }
70 }
71}
72
73void SpeechToTextComboBoxWidget::slotConfigureEngine()
74{
75 const QString engine = mEngine->currentData().toString();
76 if (TextSpeechToText::SpeechToTextEngineLoader::self()->hasConfigurationDialog(engine)) {
77 if (TextSpeechToText::SpeechToTextEngineLoader::self()->showConfigureDialog(engine, this)) {
78 Q_EMIT configureChanged(engine);
79 }
80 }
81}
82
83void SpeechToTextComboBoxWidget::slotEngineChanged(int index)
84{
85 const QString engine = mEngine->itemData(index).toString();
86 mConfigureEngine->setEnabled(TextSpeechToText::SpeechToTextEngineLoader::self()->hasConfigurationDialog(engine));
87 Q_EMIT engineChanged(engine);
88}
89
90void SpeechToTextComboBoxWidget::load()
91{
92 KConfigGroup groupTranslate(KSharedConfig::openConfig(), TextSpeechToTextUtil::groupTranslateName());
93 const QString engine = groupTranslate.readEntry(TextSpeechToTextUtil::engineTextToSpeechName(), TextSpeechToTextUtil::defaultEngineName());
94 const int index = mEngine->findData(engine);
95 if (index != -1) {
96 mEngine->setCurrentIndex(index);
97 }
98}
99
100void SpeechToTextComboBoxWidget::save()
101{
102 const QString engine = mEngine->currentData().toString();
103 KConfigGroup groupTranslate(KSharedConfig::openConfig(), TextSpeechToTextUtil::groupTranslateName());
104 groupTranslate.writeEntry(TextSpeechToTextUtil::engineTextToSpeechName(), engine);
105}
106
107#include "moc_speechtotextcomboboxwidget.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 label(StandardShortcut id)
void clicked(bool checked)
void currentIndexChanged(int index)
QIcon fromTheme(const QString &name)
Q_EMITQ_EMIT
PlainText
QFuture< void > map(Iterator begin, Iterator end, MapFunctor &&function)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Mar 28 2025 11:48:40 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.