2021-05-24 07:25:56 +00:00
|
|
|
/*
|
|
|
|
SPDX-FileCopyrightText: 2008 Pino Toscano <pino@kde.org>
|
|
|
|
|
|
|
|
SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
2008-03-05 02:13:09 +00:00
|
|
|
|
|
|
|
#include "tts.h"
|
|
|
|
|
2020-07-08 11:54:37 +00:00
|
|
|
#include <QSet>
|
2008-03-05 12:02:14 +00:00
|
|
|
|
2014-11-08 04:33:23 +00:00
|
|
|
#include <KLocalizedString>
|
2008-03-05 02:13:09 +00:00
|
|
|
|
2019-09-24 19:46:23 +00:00
|
|
|
#include "settings.h"
|
|
|
|
|
2008-03-05 02:13:09 +00:00
|
|
|
/* Private storage. */
|
|
|
|
class OkularTTS::Private
|
|
|
|
{
|
|
|
|
public:
|
2021-08-21 23:14:34 +00:00
|
|
|
explicit Private(OkularTTS *qq)
|
2019-09-24 19:46:23 +00:00
|
|
|
: q(qq)
|
|
|
|
, speech(new QTextToSpeech(Okular::Settings::ttsEngine()))
|
2008-03-05 02:13:09 +00:00
|
|
|
{
|
2022-02-07 21:24:39 +00:00
|
|
|
const QVector<QVoice> voices = speech->availableVoices();
|
|
|
|
QString voiceName = Okular::Settings::ttsVoice();
|
|
|
|
for (const QVoice &voice : voices) {
|
|
|
|
if (voice.name() == voiceName) {
|
|
|
|
speech->setVoice(voice);
|
|
|
|
}
|
|
|
|
}
|
2008-03-05 02:13:09 +00:00
|
|
|
}
|
|
|
|
|
2014-11-08 04:33:23 +00:00
|
|
|
~Private()
|
2008-03-05 02:13:09 +00:00
|
|
|
{
|
2014-11-08 04:33:23 +00:00
|
|
|
delete speech;
|
2017-09-05 21:27:18 +00:00
|
|
|
speech = nullptr;
|
2008-03-05 02:13:09 +00:00
|
|
|
}
|
2008-03-05 10:18:55 +00:00
|
|
|
|
2014-11-08 04:33:23 +00:00
|
|
|
OkularTTS *q;
|
|
|
|
QTextToSpeech *speech;
|
2019-09-26 06:08:50 +00:00
|
|
|
// Which speech engine was used when above object was created.
|
|
|
|
// When the setting changes, we need to stop speaking and recreate.
|
|
|
|
QString speechEngine;
|
2014-11-08 04:33:23 +00:00
|
|
|
};
|
2008-03-05 10:18:55 +00:00
|
|
|
|
2008-08-23 11:09:04 +00:00
|
|
|
OkularTTS::OkularTTS(QObject *parent)
|
2008-03-05 10:18:55 +00:00
|
|
|
: QObject(parent)
|
|
|
|
, d(new Private(this))
|
|
|
|
{
|
2019-09-26 06:08:50 +00:00
|
|
|
// Initialize speechEngine so we can reinitialize if it changes.
|
|
|
|
d->speechEngine = Okular::Settings::ttsEngine();
|
2014-11-08 04:33:23 +00:00
|
|
|
connect(d->speech, &QTextToSpeech::stateChanged, this, &OkularTTS::slotSpeechStateChanged);
|
2022-02-07 21:24:39 +00:00
|
|
|
connect(Okular::Settings::self(), &KCoreConfigSkeleton::configChanged, this, &OkularTTS::slotConfigChanged);
|
2008-03-05 10:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
OkularTTS::~OkularTTS()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OkularTTS::say(const QString &text)
|
|
|
|
{
|
|
|
|
if (text.isEmpty()) {
|
|
|
|
return;
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2008-03-05 10:18:55 +00:00
|
|
|
|
2014-11-08 04:33:23 +00:00
|
|
|
d->speech->say(text);
|
2008-03-05 10:18:55 +00:00
|
|
|
}
|
|
|
|
|
2008-03-05 12:02:14 +00:00
|
|
|
void OkularTTS::stopAllSpeechs()
|
|
|
|
{
|
2014-11-08 04:33:23 +00:00
|
|
|
if (!d->speech) {
|
2008-03-05 12:02:14 +00:00
|
|
|
return;
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2008-03-05 12:02:14 +00:00
|
|
|
|
2014-11-08 04:33:23 +00:00
|
|
|
d->speech->stop();
|
2008-03-05 10:18:55 +00:00
|
|
|
}
|
|
|
|
|
2019-09-19 19:50:58 +00:00
|
|
|
void OkularTTS::pauseResumeSpeech()
|
|
|
|
{
|
|
|
|
if (!d->speech) {
|
|
|
|
return;
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2019-09-19 19:50:58 +00:00
|
|
|
|
|
|
|
if (d->speech->state() == QTextToSpeech::Speaking) {
|
|
|
|
d->speech->pause();
|
|
|
|
} else {
|
|
|
|
d->speech->resume();
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2019-09-19 19:50:58 +00:00
|
|
|
}
|
|
|
|
|
2014-11-08 04:33:23 +00:00
|
|
|
void OkularTTS::slotSpeechStateChanged(QTextToSpeech::State state)
|
2008-03-05 12:02:14 +00:00
|
|
|
{
|
2014-11-08 04:33:23 +00:00
|
|
|
if (state == QTextToSpeech::Speaking) {
|
2022-03-18 21:35:45 +00:00
|
|
|
Q_EMIT isSpeaking(true);
|
|
|
|
Q_EMIT canPauseOrResume(true);
|
2014-11-08 04:33:23 +00:00
|
|
|
} else {
|
2022-03-18 21:35:45 +00:00
|
|
|
Q_EMIT isSpeaking(false);
|
2019-09-26 05:46:33 +00:00
|
|
|
if (state == QTextToSpeech::Paused) {
|
2022-03-18 21:35:45 +00:00
|
|
|
Q_EMIT canPauseOrResume(true);
|
2019-09-26 05:46:33 +00:00
|
|
|
} else {
|
2022-03-18 21:35:45 +00:00
|
|
|
Q_EMIT canPauseOrResume(false);
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2019-09-26 05:46:33 +00:00
|
|
|
}
|
2008-03-05 12:02:14 +00:00
|
|
|
}
|
|
|
|
|
2019-09-26 06:08:50 +00:00
|
|
|
void OkularTTS::slotConfigChanged()
|
|
|
|
{
|
|
|
|
const QString engine = Okular::Settings::ttsEngine();
|
2022-02-07 21:24:39 +00:00
|
|
|
const QString voiceName = Okular::Settings::ttsVoice();
|
2019-09-26 06:08:50 +00:00
|
|
|
if (engine != d->speechEngine) {
|
|
|
|
d->speech->stop();
|
|
|
|
delete d->speech;
|
|
|
|
d->speech = new QTextToSpeech(engine);
|
|
|
|
connect(d->speech, &QTextToSpeech::stateChanged, this, &OkularTTS::slotSpeechStateChanged);
|
|
|
|
d->speechEngine = engine;
|
|
|
|
}
|
2022-02-07 21:24:39 +00:00
|
|
|
|
|
|
|
const QVector<QVoice> voices = d->speech->availableVoices();
|
|
|
|
for (const QVoice &voice : voices) {
|
|
|
|
if (voice.name() == voiceName) {
|
|
|
|
d->speech->setVoice(voice);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-09-26 06:08:50 +00:00
|
|
|
}
|