7#include "texttospeechconfigwidget.h"
10#include "textedittexttospeech_debug.h"
11#include "texttospeechconfiginterface.h"
12#include "texttospeechlanguagecombobox.h"
13#include "texttospeechsliderwidget.h"
14#include "texttospeechvoicecombobox.h"
16#include "texttospeechutil.h"
17#include <KLocalizedString>
20#include <KConfigGroup>
26using namespace TextEditTextToSpeech;
27TextToSpeechConfigWidget::TextToSpeechConfigWidget(
QWidget *parent)
29 , mVolume(new TextToSpeechSliderWidget(QStringLiteral(
"%1 %"), this))
30 , mRate(new TextToSpeechSliderWidget(QStringLiteral(
"%1"), this))
31 , mPitch(new TextToSpeechSliderWidget(QStringLiteral(
"%1"), this))
33 , mLanguage(new TextToSpeechLanguageComboBox(this))
35 , mVoice(new TextToSpeechVoiceComboBox(this))
36 , mTestButton(new
QPushButton(
QIcon::fromTheme(QStringLiteral(
"player-volume")),
i18n(
"Test"), this))
39 mVolume->setObjectName(QStringLiteral(
"volume"));
40 mVolume->setRange(0, 100);
41 connect(mVolume, &TextToSpeechSliderWidget::valueChanged,
this, &TextToSpeechConfigWidget::valueChanged);
43 layout->addRow(
i18n(
"Volume:"), mVolume);
45 mRate->setObjectName(QStringLiteral(
"rate"));
46 mRate->setRange(-100, 100);
47 layout->addRow(
i18n(
"Rate:"), mRate);
48 connect(mRate, &TextToSpeechSliderWidget::valueChanged,
this, &TextToSpeechConfigWidget::valueChanged);
50 mPitch->setRange(-100, 100);
51 connect(mPitch, &TextToSpeechSliderWidget::valueChanged,
this, &TextToSpeechConfigWidget::valueChanged);
52 mPitch->setObjectName(QStringLiteral(
"pitch"));
53 layout->addRow(
i18n(
"Pitch:"), mPitch);
55 mAvailableEngine->setObjectName(QStringLiteral(
"engine"));
56 layout->addRow(
i18n(
"Engine:"), mAvailableEngine);
59 mLanguage->setObjectName(QStringLiteral(
"language"));
60 layout->addRow(
i18n(
"Language:"), mLanguage);
63 mVoice->setObjectName(QStringLiteral(
"voice"));
64 layout->addRow(
i18n(
"Voice:"), mVoice);
67 mTestButton->setObjectName(QStringLiteral(
"mTestButton"));
68 layout->addWidget(mTestButton);
73TextToSpeechConfigWidget::~TextToSpeechConfigWidget() =
default;
75void TextToSpeechConfigWidget::initializeSettings()
77 slotAvailableEngineChanged();
80void TextToSpeechConfigWidget::slotAvailableEngineChanged()
83 slotLanguageChanged();
87void TextToSpeechConfigWidget::valueChanged()
89 Q_EMIT configChanged(
true);
92void TextToSpeechConfigWidget::updateLocale()
94 KConfig config(TextEditTextToSpeech::TextToSpeechUtil::textToSpeechConfigFileName());
95 const KConfigGroup grp = config.
group(TextEditTextToSpeech::TextToSpeechUtil::textToSpeechConfigGroupName());
100 mLanguage->selectLocaleName(localeName);
103void TextToSpeechConfigWidget::readConfig()
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;
112 const int engineIndex = mAvailableEngine->
findData(engineName);
114 if (engineIndex != -1) {
118 mVoice->setCurrentVoice(settings.voice);
122void TextToSpeechConfigWidget::writeConfig()
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();
132 TextEditTextToSpeech::TextToSpeechUtil::writeConfig(std::move(settings));
135void TextToSpeechConfigWidget::slotLocalesAndVoices()
137 updateAvailableLocales();
138 updateAvailableVoices();
141void TextToSpeechConfigWidget::slotUpdateSettings()
143 updateAvailableEngine();
144 slotLocalesAndVoices();
150 delete mTextToSpeechConfigInterface;
151 mTextToSpeechConfigInterface = interface;
152 slotLocalesAndVoices();
155void TextToSpeechConfigWidget::restoreDefaults()
159 mVolume->setValue(50);
164void TextToSpeechConfigWidget::slotTestTextToSpeech()
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);
176void TextToSpeechConfigWidget::updateAvailableEngine()
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);
189void TextToSpeechConfigWidget::updateAvailableVoices()
191 const QVector<QVoice> voices = mTextToSpeechConfigInterface->availableVoices();
192 mVoice->updateVoices(voices);
196void TextToSpeechConfigWidget::updateVoice()
198 KConfig config(TextEditTextToSpeech::TextToSpeechUtil::textToSpeechConfigFileName());
199 const KConfigGroup grp = config.
group(TextEditTextToSpeech::TextToSpeechUtil::textToSpeechConfigGroupName());
201 int index = mVoice->
findData(voice);
208void TextToSpeechConfigWidget::updateEngine()
210 KConfig config(TextEditTextToSpeech::TextToSpeechUtil::textToSpeechConfigFileName());
211 const KConfigGroup grp = config.
group(TextEditTextToSpeech::TextToSpeechUtil::textToSpeechConfigGroupName());
213 int index = mAvailableEngine->
findData(engineName);
220void TextToSpeechConfigWidget::updateAvailableLocales()
223 const QVector<QLocale> locales = mTextToSpeechConfigInterface->availableLocales();
224 const QLocale current = mTextToSpeechConfigInterface->locale();
225 mLanguage->updateAvailableLocales(locales, current);
229void TextToSpeechConfigWidget::slotEngineChanged()
232 qCDebug(TEXTEDITTEXTTOSPEECH_LOG) <<
"newEngineName " << newEngineName;
233 mTextToSpeechConfigInterface->setEngine(newEngineName);
234 updateAvailableLocales();
235 slotLocalesAndVoices();
238void TextToSpeechConfigWidget::slotLanguageChanged()
242 qCWarning(TEXTEDITTEXTTOSPEECH_LOG) <<
"slotLanguageChanged: not implemented yet";
245#include "moc_texttospeechconfigwidget.cpp"
KConfigGroup group(const QString &group)
QString readEntry(const char *key, const char *aDefault=nullptr) const
The TextToSpeechConfigInterface class.
QString i18n(const char *text, const TYPE &arg...)
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
void setSizeAdjustPolicy(SizeAdjustPolicy policy)
bool isEmpty() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)