7#include "texttospeech.h"
8#include "texttospeechutil.h"
11#include <KConfigGroup>
13#include <QTextToSpeech>
16class TextEditTextToSpeech::TextToSpeechPrivate
20 QTextToSpeech *mTextToSpeech =
nullptr;
23using namespace TextEditTextToSpeech;
24TextToSpeech::TextToSpeech(
QObject *parent)
26 , d(new TextEditTextToSpeech::TextToSpeechPrivate)
31TextToSpeech::~TextToSpeech() =
default;
33void TextToSpeech::reloadSettings()
35 const TextEditTextToSpeech::TextToSpeechUtil::TextToSpeechSettings settings = TextEditTextToSpeech::TextToSpeechUtil::loadSettings();
37 const QString engineName = settings.engineName;
38 if (d->mDefaultEngine != engineName) {
39 if (d->mTextToSpeech) {
40 if (d->mTextToSpeech && (d->mTextToSpeech->engine() != engineName)) {
41 disconnect(d->mTextToSpeech, &QTextToSpeech::stateChanged,
this, &TextToSpeech::slotStateChanged);
42 delete d->mTextToSpeech;
43 d->mTextToSpeech =
nullptr;
47 if (!d->mTextToSpeech) {
48 d->mTextToSpeech =
new QTextToSpeech(engineName,
this);
49 connect(d->mTextToSpeech, &QTextToSpeech::stateChanged,
this, &TextToSpeech::slotStateChanged);
51 d->mDefaultEngine = engineName;
52 const int rate = settings.rate;
53 const double rateDouble = rate / 100.0;
54 d->mTextToSpeech->setRate(rateDouble);
55 const int pitch = settings.pitch;
56 const double pitchDouble = pitch / 100.0;
57 d->mTextToSpeech->setPitch(pitchDouble);
58 const int volumeValue = settings.volumeValue;
59 const double volumeDouble = volumeValue / 100.0;
60 d->mTextToSpeech->setVolume(volumeDouble);
61 d->mTextToSpeech->setLocale(
QLocale(settings.localeName));
71void TextToSpeech::slotStateChanged()
73 TextToSpeech::State state;
74 switch (d->mTextToSpeech->state()) {
75 case QTextToSpeech::Ready:
76 state = TextToSpeech::Ready;
78 case QTextToSpeech::Speaking:
79 state = TextToSpeech::Speaking;
81 case QTextToSpeech::Paused:
82 state = TextToSpeech::Paused;
84 case QTextToSpeech::Error:
85 state = TextToSpeech::BackendError;
87 case QTextToSpeech::Synthesizing:
88 state = TextToSpeech::Synthesizing;
91 Q_EMIT stateChanged(state);
94bool TextToSpeech::isReady()
const
96 return d->mTextToSpeech->state() != QTextToSpeech::Error;
99void TextToSpeech::say(
const QString &text)
101 d->mTextToSpeech->say(text);
104void TextToSpeech::stop()
106 d->mTextToSpeech->stop();
109void TextToSpeech::pause()
111 d->mTextToSpeech->pause();
114void TextToSpeech::resume()
116 d->mTextToSpeech->resume();
119void TextToSpeech::setRate(
double rate)
121 d->mTextToSpeech->setRate(rate);
124void TextToSpeech::setPitch(
double pitch)
126 d->mTextToSpeech->setPitch(pitch);
129void TextToSpeech::setVolume(
double volume)
131 d->mTextToSpeech->setVolume(volume);
134double TextToSpeech::volume()
const
136 return d->mTextToSpeech->volume();
141 return d->mTextToSpeech->availableLocales();
149 for (
const QVoice &voice : voices) {
157 return QTextToSpeech::availableEngines();
160void TextToSpeech::setLocale(
const QLocale &locale)
const
162 d->mTextToSpeech->setLocale(locale);
165QLocale TextToSpeech::locale()
const
167 return d->mTextToSpeech->locale();
170#include "moc_texttospeech.cpp"
qsizetype count() const const
void reserve(qsizetype size)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool disconnect(const QMetaObject::Connection &connection)