7#include "texttospeech.h"
8#include "texttospeechutil.h"
12#include <QTextToSpeech>
15class TextEditTextToSpeech::TextToSpeechPrivate
19 QTextToSpeech *mTextToSpeech =
nullptr;
22using namespace TextEditTextToSpeech;
23TextToSpeech::TextToSpeech(
QObject *parent)
25 , d(new TextEditTextToSpeech::TextToSpeechPrivate)
30TextToSpeech::~TextToSpeech() =
default;
32void TextToSpeech::reloadSettings()
34 const TextEditTextToSpeech::TextToSpeechUtil::TextToSpeechSettings settings = TextEditTextToSpeech::TextToSpeechUtil::loadSettings();
36 const QString engineName = settings.engineName;
37 if (d->mDefaultEngine != engineName) {
38 if (d->mTextToSpeech) {
39 if (d->mTextToSpeech && (d->mTextToSpeech->engine() != engineName)) {
40 disconnect(d->mTextToSpeech, &QTextToSpeech::stateChanged,
this, &TextToSpeech::slotStateChanged);
41 delete d->mTextToSpeech;
42 d->mTextToSpeech =
nullptr;
46 if (!d->mTextToSpeech) {
47 d->mTextToSpeech =
new QTextToSpeech(engineName,
this);
48 connect(d->mTextToSpeech, &QTextToSpeech::stateChanged,
this, &TextToSpeech::slotStateChanged);
50 d->mDefaultEngine = engineName;
51 const int rate = settings.rate;
52 const double rateDouble = rate / 100.0;
53 d->mTextToSpeech->setRate(rateDouble);
54 const int pitch = settings.pitch;
55 const double pitchDouble = pitch / 100.0;
56 d->mTextToSpeech->setPitch(pitchDouble);
57 const int volumeValue = settings.volumeValue;
58 const double volumeDouble = volumeValue / 100.0;
59 d->mTextToSpeech->setVolume(volumeDouble);
60 d->mTextToSpeech->setLocale(
QLocale(settings.localeName));
70void TextToSpeech::slotStateChanged()
72 TextToSpeech::State state;
73 switch (d->mTextToSpeech->state()) {
74 case QTextToSpeech::Ready:
75 state = TextToSpeech::Ready;
77 case QTextToSpeech::Speaking:
78 state = TextToSpeech::Speaking;
80 case QTextToSpeech::Paused:
81 state = TextToSpeech::Paused;
83 case QTextToSpeech::Error:
84 state = TextToSpeech::BackendError;
86 case QTextToSpeech::Synthesizing:
87 state = TextToSpeech::Synthesizing;
90 Q_EMIT stateChanged(state);
93bool TextToSpeech::isReady()
const
95 return d->mTextToSpeech->state() != QTextToSpeech::Error;
98void TextToSpeech::say(
const QString &text)
100 d->mTextToSpeech->say(text);
103void TextToSpeech::stop()
105 d->mTextToSpeech->stop();
108void TextToSpeech::pause()
110 d->mTextToSpeech->pause();
113void TextToSpeech::resume()
115 d->mTextToSpeech->resume();
118void TextToSpeech::setRate(
double rate)
120 d->mTextToSpeech->setRate(rate);
123void TextToSpeech::setPitch(
double pitch)
125 d->mTextToSpeech->setPitch(pitch);
128void TextToSpeech::setVolume(
double volume)
130 d->mTextToSpeech->setVolume(volume);
133double TextToSpeech::volume()
const
135 return d->mTextToSpeech->volume();
140 return d->mTextToSpeech->availableLocales();
148 for (
const QVoice &voice : voices) {
156 return QTextToSpeech::availableEngines();
159void TextToSpeech::setLocale(
const QLocale &locale)
const
161 d->mTextToSpeech->setLocale(locale);
164QLocale TextToSpeech::locale()
const
166 return d->mTextToSpeech->locale();
169#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)