diff --git a/ui/tts.cpp b/ui/tts.cpp index d309576da..37675d0bb 100644 --- a/ui/tts.cpp +++ b/ui/tts.cpp @@ -1,94 +1,114 @@ /*************************************************************************** * Copyright (C) 2008 by Pino Toscano * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * ***************************************************************************/ #include "tts.h" #include #include #include #include "settings.h" /* Private storage. */ class OkularTTS::Private { public: Private( OkularTTS *qq ) : q( qq ), speech( new QTextToSpeech( Okular::Settings::ttsEngine() ) ) { } ~Private() { delete speech; speech = nullptr; } OkularTTS *q; QTextToSpeech *speech; + // Which speech engine was used when above object was created. + // When the setting changes, we need to stop speaking and recreate. + QString speechEngine; }; OkularTTS::OkularTTS( QObject *parent ) : QObject( parent ), d( new Private( this ) ) { + // Initialize speechEngine so we can reinitialize if it changes. + d->speechEngine = Okular::Settings::ttsEngine(); connect( d->speech, &QTextToSpeech::stateChanged, this, &OkularTTS::slotSpeechStateChanged); + connect( Okular::Settings::self(), &KConfigSkeleton::configChanged, + this, &OkularTTS::slotConfigChanged); } OkularTTS::~OkularTTS() { delete d; } void OkularTTS::say( const QString &text ) { if ( text.isEmpty() ) return; d->speech->say( text ); } void OkularTTS::stopAllSpeechs() { if ( !d->speech ) return; d->speech->stop(); } void OkularTTS::pauseResumeSpeech() { if ( !d->speech ) return; if ( d->speech->state() == QTextToSpeech::Speaking ) d->speech->pause(); else d->speech->resume(); } void OkularTTS::slotSpeechStateChanged(QTextToSpeech::State state) { if (state == QTextToSpeech::Speaking) { emit isSpeaking(true); emit canPauseOrResume(true); } else { emit isSpeaking(false); if (state == QTextToSpeech::Paused) emit canPauseOrResume(true); else emit canPauseOrResume(false); } } +void OkularTTS::slotConfigChanged() +{ + const QString engine = Okular::Settings::ttsEngine(); + 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; + } +} + #include "moc_tts.cpp" diff --git a/ui/tts.h b/ui/tts.h index 9030a88a1..eaa8557cb 100644 --- a/ui/tts.h +++ b/ui/tts.h @@ -1,40 +1,41 @@ /*************************************************************************** * Copyright (C) 2008 by Pino Toscano * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * ***************************************************************************/ #ifndef _TTS_H_ #define _TTS_H_ #include #include class OkularTTS : public QObject { Q_OBJECT public: explicit OkularTTS( QObject *parent = nullptr ); ~OkularTTS(); void say( const QString &text ); void stopAllSpeechs(); void pauseResumeSpeech(); public slots: void slotSpeechStateChanged(QTextToSpeech::State state); + void slotConfigChanged(); signals: void isSpeaking( bool speaking ); void canPauseOrResume( bool speakingOrPaused ); private: // private storage class Private; Private *d; }; #endif