File indexing completed on 2023-05-30 10:39:36

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 <QTextCodec>
0024 
0025 #include <KLocalizedString>
0026 
0027 #include "speech.h"
0028 #include "texttospeechsystem.h"
0029 
0030 #include <QtTextToSpeech>
0031 
0032 TextToSpeechConfigurationWidget::TextToSpeechConfigurationWidget(QWidget *parent, const QString &name)
0033     : QWizardPage(parent)
0034 {
0035     setObjectName(name);
0036     setupUi(this);
0037     ttsSystem = new TextToSpeechSystem(this);
0038 
0039     connect(qtspeechGroupBox, &QGroupBox::toggled,
0040             this, &TextToSpeechConfigurationWidget::useQtspeechChanged);
0041     buildCodecList();
0042 
0043     // BEGIN Text-to-speech section
0044     // Populate tts engines and use their names directly as key and item text:
0045     const QStringList engines = QTextToSpeech::availableEngines();
0046     for (const QString &engine : engines) {
0047         engineComboBox->addItem(engine);
0048     }
0049 
0050     connect(engineComboBox, qOverload<int>(&QComboBox::currentIndexChanged),
0051             this, &TextToSpeechConfigurationWidget::slotTTSEngineChanged);
0052     connect(voiceComboBox, &QComboBox::currentTextChanged,
0053             this, &TextToSpeechConfigurationWidget::slotTTSVoiceChanged);
0054 
0055     // Preload voice list
0056     slotTTSEngineChanged();
0057 }
0058 
0059 TextToSpeechConfigurationWidget::~TextToSpeechConfigurationWidget()
0060 {
0061 }
0062 
0063 void TextToSpeechConfigurationWidget::buildCodecList()
0064 {
0065     QString local = i18nc("Local characterset", "Local") + QStringLiteral(" (");
0066     local += QLatin1String(QTextCodec::codecForLocale()->name()) + QLatin1Char(')');
0067     characterCodingBox->addItem(local, Speech::Local);
0068     characterCodingBox->addItem(i18nc("Latin1 characterset", "Latin1"), Speech::Latin1);
0069     characterCodingBox->addItem(i18n("Unicode"), Speech::Unicode);
0070     for (int i = 0; i < ttsSystem->codecList->count(); i++)
0071         characterCodingBox->addItem(QLatin1String(ttsSystem->codecList->at(i)->name()), Speech::UseCodec + i);
0072 }
0073 
0074 void TextToSpeechConfigurationWidget::cancel()
0075 {
0076     urlReq->setUrl(QUrl::fromLocalFile(ttsSystem->ttsCommand));
0077     stdInButton->setChecked(ttsSystem->stdIn);
0078     characterCodingBox->setCurrentIndex(ttsSystem->codec);
0079     qtspeechGroupBox->setChecked(ttsSystem->useQtSpeech);
0080 }
0081 
0082 void TextToSpeechConfigurationWidget::useQtspeechChanged(bool enabled)
0083 {
0084     alternativeGroupBox->setEnabled(!enabled);
0085 }
0086 
0087 void TextToSpeechConfigurationWidget::slotTTSEngineChanged()
0088 {
0089     QString engine = engineComboBox->currentText();
0090     ttsSystem->ttsEngine = engine;
0091 
0092     // Get list of voices and repopulate voice tts combobox
0093     QTextToSpeech *ttsEngine = new QTextToSpeech(engine);
0094     const QVector<QVoice> voices = ttsEngine->availableVoices();
0095     voiceComboBox->blockSignals(true);
0096     voiceComboBox->clear();
0097     for (const QVoice &voice : voices) {
0098         voiceComboBox->addItem(voice.name());
0099     }
0100     delete ttsEngine;
0101 
0102     // If there's a voice set, try to load it
0103     if (!ttsSystem->ttsVoice.isEmpty()) {
0104         voiceComboBox->setCurrentText(ttsSystem->ttsVoice);
0105     }
0106     voiceComboBox->blockSignals(false);
0107 }
0108 
0109 void TextToSpeechConfigurationWidget::slotTTSVoiceChanged(const QString &voice)
0110 {
0111     ttsSystem->ttsVoice = voice;
0112 }
0113 
0114 void TextToSpeechConfigurationWidget::ok()
0115 {
0116     ttsSystem->ttsCommand = urlReq->url().toLocalFile();
0117     ttsSystem->stdIn = stdInButton->isChecked();
0118     ttsSystem->codec = characterCodingBox->currentIndex();
0119     ttsSystem->useQtSpeech = qtspeechGroupBox->isChecked();
0120 }
0121 
0122 TextToSpeechSystem *TextToSpeechConfigurationWidget::getTTSSystem() const
0123 {
0124     return ttsSystem;
0125 }
0126 
0127 void TextToSpeechConfigurationWidget::readOptions(const QString &langGroup)
0128 {
0129     ttsSystem->readOptions(langGroup);
0130     urlReq->setUrl(QUrl::fromLocalFile(ttsSystem->ttsCommand));
0131     stdInButton->setChecked(ttsSystem->stdIn);
0132     characterCodingBox->setCurrentIndex(ttsSystem->codec);
0133     qtspeechGroupBox->setChecked(ttsSystem->useQtSpeech);
0134     engineComboBox->setCurrentText(ttsSystem->ttsEngine);
0135 
0136     if (!ttsSystem->ttsVoice.isEmpty()) {
0137         voiceComboBox->setCurrentText(ttsSystem->ttsVoice);
0138     }
0139 }
0140 
0141 void TextToSpeechConfigurationWidget::saveOptions(const QString &langGroup)
0142 {
0143     ttsSystem->saveOptions(langGroup);
0144 }
0145