File indexing completed on 2025-04-20 12:50:01
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 "texttospeechconfiginterface.h" 0008 #include "textedittexttospeech_debug.h" 0009 #include <KLocalizedString> 0010 #include <QTextToSpeech> 0011 using namespace TextEditTextToSpeech; 0012 0013 TextToSpeechConfigInterface::TextToSpeechConfigInterface(QObject *parent) 0014 : QObject(parent) 0015 { 0016 } 0017 0018 TextToSpeechConfigInterface::~TextToSpeechConfigInterface() = default; 0019 0020 QVector<QVoice> TextToSpeechConfigInterface::availableVoices() const 0021 { 0022 QVector<QVoice> voices; 0023 if (mTextToSpeech) { 0024 voices = mTextToSpeech->availableVoices(); 0025 } else { 0026 qCWarning(TEXTEDITTEXTTOSPEECH_LOG) << "Text To Speech is not created. "; 0027 } 0028 return voices; 0029 } 0030 0031 QStringList TextToSpeechConfigInterface::availableEngines() const 0032 { 0033 if (mTextToSpeech) { 0034 return mTextToSpeech->availableEngines(); 0035 } 0036 qCWarning(TEXTEDITTEXTTOSPEECH_LOG) << "Text To Speech is not created. "; 0037 return {}; 0038 } 0039 0040 QVector<QLocale> TextToSpeechConfigInterface::availableLocales() const 0041 { 0042 if (mTextToSpeech) { 0043 return mTextToSpeech->availableLocales(); 0044 } 0045 qCWarning(TEXTEDITTEXTTOSPEECH_LOG) << "Text To Speech is not created. "; 0046 return {}; 0047 } 0048 0049 QLocale TextToSpeechConfigInterface::locale() const 0050 { 0051 if (mTextToSpeech) { 0052 return mTextToSpeech->locale(); 0053 } 0054 qCWarning(TEXTEDITTEXTTOSPEECH_LOG) << "Text To Speech is not created. "; 0055 return {}; 0056 } 0057 0058 void TextToSpeechConfigInterface::setEngine(const QString &engineName) 0059 { 0060 #if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0) 0061 if (!mTextToSpeech || (mTextToSpeech && (mTextToSpeech->engine() != engineName))) 0062 #endif 0063 { 0064 delete mTextToSpeech; 0065 mTextToSpeech = new QTextToSpeech(engineName, this); 0066 } 0067 } 0068 0069 void TextToSpeechConfigInterface::testEngine(const EngineSettings &engineSettings) 0070 { 0071 if (!mTextToSpeech) { 0072 qCWarning(TEXTEDITTEXTTOSPEECH_LOG) << "Text To Speech is not created. "; 0073 return; 0074 } 0075 const int rate = engineSettings.rate; 0076 const double rateDouble = rate / 100.0; 0077 mTextToSpeech->setRate(rateDouble); 0078 const int pitch = engineSettings.pitch; 0079 const double pitchDouble = pitch / 100.0; 0080 mTextToSpeech->setPitch(pitchDouble); 0081 const int volume = engineSettings.volume; 0082 const double volumeDouble = volume / 100.0; 0083 mTextToSpeech->setVolume(volumeDouble); 0084 mTextToSpeech->setLocale(QLocale(engineSettings.localeName)); 0085 mTextToSpeech->setVoice(engineSettings.voice); 0086 0087 // TODO change text ? 0088 mTextToSpeech->say(i18n("Morning, this is the test for testing settings.")); 0089 } 0090 0091 QDebug operator<<(QDebug d, const TextEditTextToSpeech::TextToSpeechConfigInterface::EngineSettings &t) 0092 { 0093 d << " Rate " << t.rate; 0094 d << " pitch " << t.pitch; 0095 d << " volume " << t.volume; 0096 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) 0097 d << " voice " << t.voice; 0098 #endif 0099 d << " localeName " << t.localeName; 0100 return d; 0101 } 0102 0103 #include "moc_texttospeechconfiginterface.cpp"