File indexing completed on 2024-04-21 03:40:22

0001 /***************************************************************************
0002  *   Copyright (C) 2002 by Gunnar Schmi Dt <kmouth@schmi-dt.de             *
0003  *             (C) 2015 by Jeremy Whiting <jpwhiting@kde.org>              *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
0019  ***************************************************************************/
0020 
0021 #include "texttospeechconfigurationwidget.h"
0022 
0023 #include <KLocalizedString>
0024 
0025 #include "speech.h"
0026 #include "texttospeechsystem.h"
0027 
0028 #include <QtTextToSpeech>
0029 
0030 TextToSpeechConfigurationWidget::TextToSpeechConfigurationWidget(QWidget *parent, const QString &name)
0031     : QWizardPage(parent)
0032 {
0033     setObjectName(name);
0034     setupUi(this);
0035     ttsSystem = new TextToSpeechSystem(this);
0036 
0037     connect(qtspeechGroupBox, &QGroupBox::toggled, this, &TextToSpeechConfigurationWidget::useQtspeechChanged);
0038     buildCodecList();
0039 
0040     // BEGIN Text-to-speech section
0041     // Populate tts engines and use their names directly as key and item text:
0042     const QStringList engines = QTextToSpeech::availableEngines();
0043     for (const QString &engine : engines) {
0044         if (engine != QLatin1String("mock")) {
0045             engineComboBox->addItem(engine);
0046         }
0047     }
0048 
0049     connect(engineComboBox, qOverload<int>(&QComboBox::currentIndexChanged), this, &TextToSpeechConfigurationWidget::slotTTSEngineChanged);
0050     connect(voiceComboBox, &QComboBox::currentTextChanged, this, &TextToSpeechConfigurationWidget::slotTTSVoiceChanged);
0051 
0052     // Preload voice list
0053     slotTTSEngineChanged();
0054 }
0055 
0056 TextToSpeechConfigurationWidget::~TextToSpeechConfigurationWidget()
0057 {
0058 }
0059 
0060 void TextToSpeechConfigurationWidget::buildCodecList()
0061 {
0062     for (int encoding = QStringConverter::System; encoding <= QStringConverter::System; ++encoding) {
0063         QString name = QLatin1String(QStringConverter::nameForEncoding(QStringConverter::Encoding(encoding)));
0064         characterCodingBox->addItem(name, encoding);
0065     }
0066 }
0067 
0068 void TextToSpeechConfigurationWidget::cancel()
0069 {
0070     urlReq->setUrl(QUrl::fromLocalFile(ttsSystem->ttsCommand));
0071     stdInButton->setChecked(ttsSystem->stdIn);
0072     characterCodingBox->setCurrentIndex(ttsSystem->encoding);
0073     qtspeechGroupBox->setChecked(ttsSystem->useQtSpeech);
0074 }
0075 
0076 void TextToSpeechConfigurationWidget::useQtspeechChanged(bool enabled)
0077 {
0078     alternativeGroupBox->setEnabled(!enabled);
0079 }
0080 
0081 void TextToSpeechConfigurationWidget::slotTTSEngineChanged()
0082 {
0083     QString engine = engineComboBox->currentText();
0084     ttsSystem->ttsEngine = engine;
0085 
0086     // Get list of voices and repopulate voice tts combobox
0087     QTextToSpeech *ttsEngine = new QTextToSpeech(engine);
0088     const QList<QVoice> voices = ttsEngine->availableVoices();
0089     voiceComboBox->blockSignals(true);
0090     voiceComboBox->clear();
0091     for (const QVoice &voice : voices) {
0092         voiceComboBox->addItem(voice.name());
0093     }
0094     delete ttsEngine;
0095 
0096     // If there's a voice set, try to load it
0097     if (!ttsSystem->ttsVoice.isEmpty()) {
0098         voiceComboBox->setCurrentText(ttsSystem->ttsVoice);
0099     }
0100     voiceComboBox->blockSignals(false);
0101 }
0102 
0103 void TextToSpeechConfigurationWidget::slotTTSVoiceChanged(const QString &voice)
0104 {
0105     ttsSystem->ttsVoice = voice;
0106 }
0107 
0108 void TextToSpeechConfigurationWidget::ok()
0109 {
0110     ttsSystem->ttsCommand = urlReq->url().toLocalFile();
0111     ttsSystem->stdIn = stdInButton->isChecked();
0112     ttsSystem->encoding = QStringConverter::Encoding(characterCodingBox->currentIndex());
0113     ttsSystem->useQtSpeech = qtspeechGroupBox->isChecked();
0114 }
0115 
0116 TextToSpeechSystem *TextToSpeechConfigurationWidget::getTTSSystem() const
0117 {
0118     return ttsSystem;
0119 }
0120 
0121 void TextToSpeechConfigurationWidget::readOptions(const QString &langGroup)
0122 {
0123     ttsSystem->readOptions(langGroup);
0124     urlReq->setUrl(QUrl::fromLocalFile(ttsSystem->ttsCommand));
0125     stdInButton->setChecked(ttsSystem->stdIn);
0126     characterCodingBox->setCurrentIndex(ttsSystem->encoding);
0127     qtspeechGroupBox->setChecked(ttsSystem->useQtSpeech);
0128     engineComboBox->setCurrentText(ttsSystem->ttsEngine);
0129 
0130     if (!ttsSystem->ttsVoice.isEmpty()) {
0131         voiceComboBox->setCurrentText(ttsSystem->ttsVoice);
0132     }
0133 }
0134 
0135 void TextToSpeechConfigurationWidget::saveOptions(const QString &langGroup)
0136 {
0137     ttsSystem->saveOptions(langGroup);
0138 }