KTextAddons

texttospeechconfigwidget.cpp
1/*
2 SPDX-FileCopyrightText: 2014-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "texttospeechconfigwidget.h"
8using namespace Qt::Literals::StringLiterals;
9
10#include "textedittexttospeech_debug.h"
11#include "texttospeechconfiginterface.h"
12#include "texttospeechlanguagecombobox.h"
13#include "texttospeechsliderwidget.h"
14#include "texttospeechvoicecombobox.h"
15
16#include "texttospeechutil.h"
17#include <KLocalizedString>
18
19#include <KConfig>
20#include <KConfigGroup>
21#include <QComboBox>
22#include <QFormLayout>
23#include <QPushButton>
24#include <QTimer>
25
26using namespace TextEditTextToSpeech;
27TextToSpeechConfigWidget::TextToSpeechConfigWidget(QWidget *parent)
28 : QWidget(parent)
29 , mVolume(new TextToSpeechSliderWidget(QStringLiteral("%1 %"), this))
30 , mRate(new TextToSpeechSliderWidget(QStringLiteral("%1"), this))
31 , mPitch(new TextToSpeechSliderWidget(QStringLiteral("%1"), this))
32 , mAvailableEngine(new QComboBox(this))
33 , mLanguage(new TextToSpeechLanguageComboBox(this))
34 , mTextToSpeechConfigInterface(new TextToSpeechConfigInterface(this))
35 , mVoice(new TextToSpeechVoiceComboBox(this))
36 , mTestButton(new QPushButton(QIcon::fromTheme(QStringLiteral("player-volume")), i18n("Test"), this))
37{
38 auto layout = new QFormLayout(this);
39 mVolume->setObjectName(QStringLiteral("volume"));
40 mVolume->setRange(0, 100);
41 connect(mVolume, &TextToSpeechSliderWidget::valueChanged, this, &TextToSpeechConfigWidget::valueChanged);
42
43 layout->addRow(i18n("Volume:"), mVolume);
44
45 mRate->setObjectName(QStringLiteral("rate"));
46 mRate->setRange(-100, 100);
47 layout->addRow(i18n("Rate:"), mRate);
48 connect(mRate, &TextToSpeechSliderWidget::valueChanged, this, &TextToSpeechConfigWidget::valueChanged);
49
50 mPitch->setRange(-100, 100);
51 connect(mPitch, &TextToSpeechSliderWidget::valueChanged, this, &TextToSpeechConfigWidget::valueChanged);
52 mPitch->setObjectName(QStringLiteral("pitch"));
53 layout->addRow(i18n("Pitch:"), mPitch);
54
55 mAvailableEngine->setObjectName(QStringLiteral("engine"));
56 layout->addRow(i18n("Engine:"), mAvailableEngine);
57 connect(mAvailableEngine, &QComboBox::currentIndexChanged, this, &TextToSpeechConfigWidget::slotAvailableEngineChanged);
58
59 mLanguage->setObjectName(QStringLiteral("language"));
60 layout->addRow(i18n("Language:"), mLanguage);
61 connect(mLanguage, &QComboBox::currentIndexChanged, this, &TextToSpeechConfigWidget::valueChanged);
62
63 mVoice->setObjectName(QStringLiteral("voice"));
64 layout->addRow(i18n("Voice:"), mVoice);
65 connect(mVoice, &QComboBox::currentIndexChanged, this, &TextToSpeechConfigWidget::valueChanged);
66
67 mTestButton->setObjectName(QStringLiteral("mTestButton"));
68 layout->addWidget(mTestButton);
69 connect(mTestButton, &QPushButton::clicked, this, &TextToSpeechConfigWidget::slotTestTextToSpeech);
70 QTimer::singleShot(0, this, &TextToSpeechConfigWidget::slotUpdateSettings);
71}
72
73TextToSpeechConfigWidget::~TextToSpeechConfigWidget() = default;
74
75void TextToSpeechConfigWidget::initializeSettings()
76{
77 slotAvailableEngineChanged();
78}
79
80void TextToSpeechConfigWidget::slotAvailableEngineChanged()
81{
82 slotEngineChanged();
83 slotLanguageChanged();
84 valueChanged();
85}
86
87void TextToSpeechConfigWidget::valueChanged()
88{
89 Q_EMIT configChanged(true);
90}
91
92void TextToSpeechConfigWidget::updateLocale()
93{
94 KConfig config(TextEditTextToSpeech::TextToSpeechUtil::textToSpeechConfigFileName());
95 const KConfigGroup grp = config.group(TextEditTextToSpeech::TextToSpeechUtil::textToSpeechConfigGroupName());
96 const QString localeName = grp.readEntry("localeName");
97 if (localeName.isEmpty()) {
98 return;
99 }
100 mLanguage->selectLocaleName(localeName);
101}
102
103void TextToSpeechConfigWidget::readConfig()
104{
105 const TextEditTextToSpeech::TextToSpeechUtil::TextToSpeechSettings settings = TextEditTextToSpeech::TextToSpeechUtil::loadSettings();
106 mRate->setValue(settings.rate);
107 mPitch->setValue(settings.pitch);
108 mVolume->setValue(settings.volumeValue);
109 mLanguage->selectLocaleName(settings.localeName);
110 const QString engineName = settings.engineName;
111 // qDebug() << " engineName " << engineName;
112 const int engineIndex = mAvailableEngine->findData(engineName);
113 // qDebug() << " engineIndex " << engineIndex;
114 if (engineIndex != -1) {
115 mAvailableEngine->setCurrentIndex(engineIndex);
116 }
117 // FIXME: list of voice is not loading here... need to fix it
118 mVoice->setCurrentVoice(settings.voice);
119 // qDebug() << " load settings " << settings;
120}
121
122void TextToSpeechConfigWidget::writeConfig()
123{
124 TextEditTextToSpeech::TextToSpeechUtil::TextToSpeechSettings settings;
125 settings.volumeValue = mVolume->value();
126 settings.rate = mRate->value();
127 settings.pitch = mPitch->value();
128 settings.localeName = mLanguage->currentData().toLocale().name();
129 settings.engineName = mAvailableEngine->currentData().toString();
130 settings.voice = mVoice->currentVoice();
131 // qDebug() << " save settings " << settings;
132 TextEditTextToSpeech::TextToSpeechUtil::writeConfig(std::move(settings));
133}
134
135void TextToSpeechConfigWidget::slotLocalesAndVoices()
136{
137 updateAvailableLocales();
138 updateAvailableVoices();
139}
140
141void TextToSpeechConfigWidget::slotUpdateSettings()
142{
143 updateAvailableEngine();
144 slotLocalesAndVoices();
145 readConfig();
146}
147
148void TextToSpeechConfigWidget::setTextToSpeechConfigInterface(TextToSpeechConfigInterface *interface)
149{
150 delete mTextToSpeechConfigInterface;
151 mTextToSpeechConfigInterface = interface;
152 slotLocalesAndVoices();
153}
154
155void TextToSpeechConfigWidget::restoreDefaults()
156{
157 mRate->setValue(0);
158 mPitch->setValue(0);
159 mVolume->setValue(50);
160
161 // TODO load default value
162}
163
164void TextToSpeechConfigWidget::slotTestTextToSpeech()
165{
166 TextToSpeechConfigInterface::EngineSettings settings;
167 settings.rate = mRate->value();
168 settings.pitch = mPitch->value();
169 settings.volume = mVolume->value();
170 settings.localeName = mLanguage->currentData().toLocale().name();
171 settings.voice = mVoice->currentVoice();
172 qCDebug(TEXTEDITTEXTTOSPEECH_LOG) << " settings " << settings;
173 mTextToSpeechConfigInterface->testEngine(settings);
174}
175
176void TextToSpeechConfigWidget::updateAvailableEngine()
177{
178 mAvailableEngine->clear();
179 const QStringList lst = mTextToSpeechConfigInterface->availableEngines();
180 for (const QString &engine : lst) {
181 if (engine != "mock"_L1) {
182 mAvailableEngine->addItem(engine, engine);
183 }
184 }
186 updateEngine();
187}
188
189void TextToSpeechConfigWidget::updateAvailableVoices()
190{
191 const QVector<QVoice> voices = mTextToSpeechConfigInterface->availableVoices();
192 mVoice->updateVoices(voices);
193 updateVoice();
194}
195
196void TextToSpeechConfigWidget::updateVoice()
197{
198 KConfig config(TextEditTextToSpeech::TextToSpeechUtil::textToSpeechConfigFileName());
199 const KConfigGroup grp = config.group(TextEditTextToSpeech::TextToSpeechUtil::textToSpeechConfigGroupName());
200 const QString voice = grp.readEntry("voice");
201 int index = mVoice->findData(voice);
202 if (index == -1) {
203 index = 0;
204 }
205 mVoice->setCurrentIndex(index);
206}
207
208void TextToSpeechConfigWidget::updateEngine()
209{
210 KConfig config(TextEditTextToSpeech::TextToSpeechUtil::textToSpeechConfigFileName());
211 const KConfigGroup grp = config.group(TextEditTextToSpeech::TextToSpeechUtil::textToSpeechConfigGroupName());
212 const QString engineName = grp.readEntry("engine");
213 int index = mAvailableEngine->findData(engineName);
214 if (index == -1) {
215 index = 0;
216 }
217 mAvailableEngine->setCurrentIndex(index);
218}
219
220void TextToSpeechConfigWidget::updateAvailableLocales()
221{
222 mLanguage->clear();
223 const QVector<QLocale> locales = mTextToSpeechConfigInterface->availableLocales();
224 const QLocale current = mTextToSpeechConfigInterface->locale();
225 mLanguage->updateAvailableLocales(locales, current);
226 updateLocale();
227}
228
229void TextToSpeechConfigWidget::slotEngineChanged()
230{
231 const QString newEngineName = mAvailableEngine->currentData().toString();
232 qCDebug(TEXTEDITTEXTTOSPEECH_LOG) << "newEngineName " << newEngineName;
233 mTextToSpeechConfigInterface->setEngine(newEngineName);
234 updateAvailableLocales();
235 slotLocalesAndVoices();
236}
237
238void TextToSpeechConfigWidget::slotLanguageChanged()
239{
240 // QLocale locale = mLanguage->currentData().value<QLocale>();
241 // TODO
242 qCWarning(TEXTEDITTEXTTOSPEECH_LOG) << "slotLanguageChanged: not implemented yet";
243}
244
245#include "moc_texttospeechconfigwidget.cpp"
KConfigGroup group(const QString &group)
QString readEntry(const char *key, const char *aDefault=nullptr) const
QString i18n(const char *text, const TYPE &arg...)
void clicked(bool checked)
void addItem(const QIcon &icon, const QString &text, const QVariant &userData)
void clear()
void setCurrentIndex(int index)
void currentIndexChanged(int index)
int findData(const QVariant &data, int role, Qt::MatchFlags flags) const const
void setSizeAdjustPolicy(SizeAdjustPolicy policy)
Q_EMITQ_EMIT
bool isEmpty() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:19:49 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.