KTextAddons

speechtotextcomboboxwidget.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 "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 mainLayout->addWidget(label);
31
32 mEngine->setObjectName(QStringLiteral("mEngine"));
33 mainLayout->addWidget(mEngine);
34
35 mConfigureEngine->setObjectName(QStringLiteral("mConfigureEngine"));
36 mainLayout->addWidget(mConfigureEngine);
37 mConfigureEngine->setEnabled(false); // Disable by default
38 mConfigureEngine->setIcon(QIcon::fromTheme(QStringLiteral("settings-configure")));
39
40 connect(mConfigureEngine, &QToolButton::clicked, this, &SpeechToTextComboBoxWidget::slotConfigureEngine);
41 connect(mEngine, &QComboBox::currentIndexChanged, this, &SpeechToTextComboBoxWidget::slotEngineChanged);
42}
43
44SpeechToTextComboBoxWidget::~SpeechToTextComboBoxWidget() = default;
45
46void SpeechToTextComboBoxWidget::fillEngine()
47{
48 const QMap<QString, QString> map = TextSpeechToText::SpeechToTextEngineLoader::self()->speechToTextEngineInfos();
50 while (iMap.hasNext()) {
51 iMap.next();
52 mEngine->addItem(iMap.value(), iMap.key());
53 }
54}
55
56QString SpeechToTextComboBoxWidget::engineName() const
57{
58 return mEngine->currentData().toString();
59}
60
61void SpeechToTextComboBoxWidget::setEngineName(const QString &engineName)
62{
63 const int countItem(mEngine->count());
64 for (int i = 0; i < countItem; ++i) {
65 if (mEngine->itemData(i).toString() == engineName) {
66 mEngine->setCurrentIndex(i);
67 break;
68 }
69 }
70}
71
72void SpeechToTextComboBoxWidget::slotConfigureEngine()
73{
74 const QString engine = mEngine->currentData().toString();
75 if (TextSpeechToText::SpeechToTextEngineLoader::self()->hasConfigurationDialog(engine)) {
76 if (TextSpeechToText::SpeechToTextEngineLoader::self()->showConfigureDialog(engine, this)) {
77 Q_EMIT configureChanged(engine);
78 }
79 }
80}
81
82void SpeechToTextComboBoxWidget::slotEngineChanged(int index)
83{
84 const QString engine = mEngine->itemData(index).toString();
85 mConfigureEngine->setEnabled(TextSpeechToText::SpeechToTextEngineLoader::self()->hasConfigurationDialog(engine));
86 Q_EMIT engineChanged(engine);
87}
88
89void SpeechToTextComboBoxWidget::load()
90{
91 KConfigGroup groupTranslate(KSharedConfig::openConfig(), TextSpeechToTextUtil::groupTranslateName());
92 const QString engine = groupTranslate.readEntry(TextSpeechToTextUtil::engineTextToSpeechName(), TextSpeechToTextUtil::defaultEngineName());
93 const int index = mEngine->findData(engine);
94 if (index != -1) {
95 mEngine->setCurrentIndex(index);
96 }
97}
98
99void SpeechToTextComboBoxWidget::save()
100{
101 const QString engine = mEngine->currentData().toString();
102 KConfigGroup groupTranslate(KSharedConfig::openConfig(), TextSpeechToTextUtil::groupTranslateName());
103 groupTranslate.writeEntry(TextSpeechToTextUtil::engineTextToSpeechName(), engine);
104}
105
106#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 addItem(const QIcon &icon, const QString &text, const QVariant &userData)
void setCurrentIndex(int index)
void currentIndexChanged(int index)
int findData(const QVariant &data, int role, Qt::MatchFlags flags) const const
QVariant itemData(int index, int role) const const
QIcon fromTheme(const QString &name)
Q_EMITQ_EMIT
QFuture< void > map(Iterator begin, Iterator end, MapFunctor &&function)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QString toString() const const
void setEnabled(bool)
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.