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