Warning, file /libraries/ktextaddons/textedittexttospeech/texttospeechconfigwidget.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002    SPDX-FileCopyrightText: 2014-2023 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "texttospeechconfigwidget.h"
0008 #include "textedittexttospeech_debug.h"
0009 #include "texttospeechconfiginterface.h"
0010 #include "texttospeechlanguagecombobox.h"
0011 #include "texttospeechsliderwidget.h"
0012 #include "texttospeechvoicecombobox.h"
0013 
0014 #include "texttospeechutil.h"
0015 #include <KLocalizedString>
0016 
0017 #include <KConfig>
0018 #include <KConfigGroup>
0019 #include <QComboBox>
0020 #include <QFormLayout>
0021 #include <QPushButton>
0022 #include <QTimer>
0023 
0024 using namespace TextEditTextToSpeech;
0025 TextToSpeechConfigWidget::TextToSpeechConfigWidget(QWidget *parent)
0026     : QWidget(parent)
0027     , mVolume(new TextToSpeechSliderWidget(QStringLiteral("%1 %"), this))
0028     , mRate(new TextToSpeechSliderWidget(QStringLiteral("%1"), this))
0029     , mPitch(new TextToSpeechSliderWidget(QStringLiteral("%1"), this))
0030     , mLanguage(new TextToSpeechLanguageComboBox(this))
0031     , mTextToSpeechConfigInterface(new TextToSpeechConfigInterface(this))
0032     , mAvailableEngine(new QComboBox(this))
0033     , mVoice(new TextToSpeechVoiceComboBox(this))
0034     , mTestButton(new QPushButton(QIcon::fromTheme(QStringLiteral("player-volume")), i18n("Test"), this))
0035 {
0036     auto layout = new QFormLayout(this);
0037     mVolume->setObjectName(QStringLiteral("volume"));
0038     mVolume->setRange(0, 100);
0039     connect(mVolume, &TextToSpeechSliderWidget::valueChanged, this, &TextToSpeechConfigWidget::valueChanged);
0040 
0041     layout->addRow(i18n("Volume:"), mVolume);
0042 
0043     mRate->setObjectName(QStringLiteral("rate"));
0044     mRate->setRange(-100, 100);
0045     layout->addRow(i18n("Rate:"), mRate);
0046     connect(mRate, &TextToSpeechSliderWidget::valueChanged, this, &TextToSpeechConfigWidget::valueChanged);
0047 
0048     mPitch->setRange(-100, 100);
0049     connect(mPitch, &TextToSpeechSliderWidget::valueChanged, this, &TextToSpeechConfigWidget::valueChanged);
0050     mPitch->setObjectName(QStringLiteral("pitch"));
0051     layout->addRow(i18n("Pitch:"), mPitch);
0052 
0053     mAvailableEngine->setObjectName(QStringLiteral("engine"));
0054     layout->addRow(i18n("Engine:"), mAvailableEngine);
0055     connect(mAvailableEngine, &QComboBox::currentIndexChanged, this, &TextToSpeechConfigWidget::slotAvailableEngineChanged);
0056 
0057     mLanguage->setObjectName(QStringLiteral("language"));
0058     layout->addRow(i18n("Language:"), mLanguage);
0059     connect(mLanguage, &QComboBox::currentIndexChanged, this, &TextToSpeechConfigWidget::valueChanged);
0060 
0061     mVoice->setObjectName(QStringLiteral("voice"));
0062     layout->addRow(i18n("Voice:"), mVoice);
0063     connect(mVoice, &QComboBox::currentIndexChanged, this, &TextToSpeechConfigWidget::valueChanged);
0064 
0065     mTestButton->setObjectName(QStringLiteral("mTestButton"));
0066     layout->addWidget(mTestButton);
0067     connect(mTestButton, &QPushButton::clicked, this, &TextToSpeechConfigWidget::slotTestTextToSpeech);
0068     QTimer::singleShot(0, this, &TextToSpeechConfigWidget::slotUpdateSettings);
0069 }
0070 
0071 TextToSpeechConfigWidget::~TextToSpeechConfigWidget() = default;
0072 
0073 void TextToSpeechConfigWidget::initializeSettings()
0074 {
0075     slotAvailableEngineChanged();
0076 }
0077 
0078 void TextToSpeechConfigWidget::slotAvailableEngineChanged()
0079 {
0080     slotEngineChanged();
0081     slotLanguageChanged();
0082     valueChanged();
0083 }
0084 
0085 void TextToSpeechConfigWidget::valueChanged()
0086 {
0087     Q_EMIT configChanged(true);
0088 }
0089 
0090 void TextToSpeechConfigWidget::updateLocale()
0091 {
0092     KConfig config(TextEditTextToSpeech::TextToSpeechUtil::textToSpeechConfigFileName());
0093     const KConfigGroup grp = config.group(TextEditTextToSpeech::TextToSpeechUtil::textToSpeechConfigGroupName());
0094     const QString localeName = grp.readEntry("localeName");
0095     if (localeName.isEmpty()) {
0096         return;
0097     }
0098     mLanguage->selectLocaleName(localeName);
0099 }
0100 
0101 void TextToSpeechConfigWidget::readConfig()
0102 {
0103     const TextEditTextToSpeech::TextToSpeechUtil::TextToSpeechSettings settings = TextEditTextToSpeech::TextToSpeechUtil::loadSettings();
0104     mRate->setValue(settings.rate);
0105     mPitch->setValue(settings.pitch);
0106     mVolume->setValue(settings.volumeValue);
0107     mLanguage->selectLocaleName(settings.localeName);
0108     const QString engineName = settings.engineName;
0109     // qDebug() << " engineName " << engineName;
0110     const int engineIndex = mAvailableEngine->findData(engineName);
0111     // qDebug() << " engineIndex " << engineIndex;
0112     if (engineIndex != -1) {
0113         mAvailableEngine->setCurrentIndex(engineIndex);
0114     }
0115     // FIXME: list of voice is not loading here... need to fix it
0116     mVoice->setCurrentVoice(settings.voice);
0117     // qDebug() << " load settings " << settings;
0118 }
0119 
0120 void TextToSpeechConfigWidget::writeConfig()
0121 {
0122     TextEditTextToSpeech::TextToSpeechUtil::TextToSpeechSettings settings;
0123     settings.volumeValue = mVolume->value();
0124     settings.rate = mRate->value();
0125     settings.pitch = mPitch->value();
0126     settings.localeName = mLanguage->currentData().toLocale().name();
0127     settings.engineName = mAvailableEngine->currentData().toString();
0128     settings.voice = mVoice->currentVoice();
0129     // qDebug() << " save settings " << settings;
0130     TextEditTextToSpeech::TextToSpeechUtil::writeConfig(std::move(settings));
0131 }
0132 
0133 void TextToSpeechConfigWidget::slotLocalesAndVoices()
0134 {
0135     updateAvailableLocales();
0136     updateAvailableVoices();
0137 }
0138 
0139 void TextToSpeechConfigWidget::slotUpdateSettings()
0140 {
0141     updateAvailableEngine();
0142     slotLocalesAndVoices();
0143     readConfig();
0144 }
0145 
0146 void TextToSpeechConfigWidget::setTextToSpeechConfigInterface(TextToSpeechConfigInterface *interface)
0147 {
0148     delete mTextToSpeechConfigInterface;
0149     mTextToSpeechConfigInterface = interface;
0150     slotLocalesAndVoices();
0151 }
0152 
0153 void TextToSpeechConfigWidget::restoreDefaults()
0154 {
0155     mRate->setValue(0);
0156     mPitch->setValue(0);
0157     mVolume->setValue(50);
0158 
0159     // TODO load default value
0160 }
0161 
0162 void TextToSpeechConfigWidget::slotTestTextToSpeech()
0163 {
0164     TextToSpeechConfigInterface::EngineSettings settings;
0165     settings.rate = mRate->value();
0166     settings.pitch = mPitch->value();
0167     settings.volume = mVolume->value();
0168     settings.localeName = mLanguage->currentData().toLocale().name();
0169     settings.voice = mVoice->currentVoice();
0170     qCDebug(TEXTEDITTEXTTOSPEECH_LOG) << " settings " << settings;
0171     mTextToSpeechConfigInterface->testEngine(settings);
0172 }
0173 
0174 void TextToSpeechConfigWidget::updateAvailableEngine()
0175 {
0176     mAvailableEngine->clear();
0177     const QStringList lst = mTextToSpeechConfigInterface->availableEngines();
0178     for (const QString &engine : lst) {
0179         if (engine != QLatin1String("mock")) {
0180             mAvailableEngine->addItem(engine, engine);
0181         }
0182     }
0183     mAvailableEngine->setSizeAdjustPolicy(QComboBox::AdjustToContents);
0184     updateEngine();
0185 }
0186 
0187 void TextToSpeechConfigWidget::updateAvailableVoices()
0188 {
0189     const QVector<QVoice> voices = mTextToSpeechConfigInterface->availableVoices();
0190     mVoice->updateVoices(voices);
0191     updateVoice();
0192 }
0193 
0194 void TextToSpeechConfigWidget::updateVoice()
0195 {
0196     KConfig config(TextEditTextToSpeech::TextToSpeechUtil::textToSpeechConfigFileName());
0197     const KConfigGroup grp = config.group(TextEditTextToSpeech::TextToSpeechUtil::textToSpeechConfigGroupName());
0198     const QString voice = grp.readEntry("voice");
0199     int index = mVoice->findData(voice);
0200     if (index == -1) {
0201         index = 0;
0202     }
0203     mVoice->setCurrentIndex(index);
0204 }
0205 
0206 void TextToSpeechConfigWidget::updateEngine()
0207 {
0208     KConfig config(TextEditTextToSpeech::TextToSpeechUtil::textToSpeechConfigFileName());
0209     const KConfigGroup grp = config.group(TextEditTextToSpeech::TextToSpeechUtil::textToSpeechConfigGroupName());
0210     const QString engineName = grp.readEntry("engine");
0211     int index = mAvailableEngine->findData(engineName);
0212     if (index == -1) {
0213         index = 0;
0214     }
0215     mAvailableEngine->setCurrentIndex(index);
0216 }
0217 
0218 void TextToSpeechConfigWidget::updateAvailableLocales()
0219 {
0220     mLanguage->clear();
0221     const QVector<QLocale> locales = mTextToSpeechConfigInterface->availableLocales();
0222     const QLocale current = mTextToSpeechConfigInterface->locale();
0223     mLanguage->updateAvailableLocales(locales, current);
0224     updateLocale();
0225 }
0226 
0227 void TextToSpeechConfigWidget::slotEngineChanged()
0228 {
0229     const QString newEngineName = mAvailableEngine->currentData().toString();
0230     qCDebug(TEXTEDITTEXTTOSPEECH_LOG) << "newEngineName " << newEngineName;
0231     mTextToSpeechConfigInterface->setEngine(newEngineName);
0232     updateAvailableLocales();
0233     slotLocalesAndVoices();
0234 }
0235 
0236 void TextToSpeechConfigWidget::slotLanguageChanged()
0237 {
0238     // QLocale locale = mLanguage->currentData().value<QLocale>();
0239     // TODO
0240     qCWarning(TEXTEDITTEXTTOSPEECH_LOG) << "slotLanguageChanged: not implemented yet";
0241 }
0242 
0243 #include "moc_texttospeechconfigwidget.cpp"