Changeset View
Changeset View
Standalone View
Standalone View
ui/tts.cpp
1 | /*************************************************************************** | 1 | /*************************************************************************** | ||
---|---|---|---|---|---|
2 | * Copyright (C) 2008 by Pino Toscano <pino@kde.org> * | 2 | * Copyright (C) 2008 by Pino Toscano <pino@kde.org> * | ||
3 | * * | 3 | * * | ||
4 | * This program is free software; you can redistribute it and/or modify * | 4 | * This program is free software; you can redistribute it and/or modify * | ||
5 | * it under the terms of the GNU General Public License as published by * | 5 | * it under the terms of the GNU General Public License as published by * | ||
6 | * the Free Software Foundation; either version 2 of the License, or * | 6 | * the Free Software Foundation; either version 2 of the License, or * | ||
7 | * (at your option) any later version. * | 7 | * (at your option) any later version. * | ||
8 | ***************************************************************************/ | 8 | ***************************************************************************/ | ||
9 | 9 | | |||
10 | #include "tts.h" | 10 | #include "tts.h" | ||
11 | 11 | | |||
12 | #include <qdbusservicewatcher.h> | 12 | #include <qdbusservicewatcher.h> | ||
13 | #include <qset.h> | 13 | #include <qset.h> | ||
14 | 14 | | |||
15 | #include <KLocalizedString> | 15 | #include <KLocalizedString> | ||
16 | 16 | | |||
17 | #include "settings.h" | ||||
18 | | ||||
17 | /* Private storage. */ | 19 | /* Private storage. */ | ||
18 | class OkularTTS::Private | 20 | class OkularTTS::Private | ||
19 | { | 21 | { | ||
20 | public: | 22 | public: | ||
21 | Private( OkularTTS *qq ) | 23 | Private( OkularTTS *qq ) | ||
22 | : q( qq ), speech( new QTextToSpeech ) | 24 | : q( qq ), speech( new QTextToSpeech( Okular::Settings::ttsEngine() ) ) | ||
23 | { | 25 | { | ||
24 | } | 26 | } | ||
25 | 27 | | |||
26 | ~Private() | 28 | ~Private() | ||
27 | { | 29 | { | ||
28 | delete speech; | 30 | delete speech; | ||
29 | speech = nullptr; | 31 | speech = nullptr; | ||
30 | } | 32 | } | ||
31 | 33 | | |||
32 | OkularTTS *q; | 34 | OkularTTS *q; | ||
33 | QTextToSpeech *speech; | 35 | QTextToSpeech *speech; | ||
36 | // Which speech engine was used when above object was created. | ||||
37 | // When the setting changes, we need to stop speaking and recreate. | ||||
38 | QString speechEngine; | ||||
34 | }; | 39 | }; | ||
35 | 40 | | |||
36 | OkularTTS::OkularTTS( QObject *parent ) | 41 | OkularTTS::OkularTTS( QObject *parent ) | ||
37 | : QObject( parent ), d( new Private( this ) ) | 42 | : QObject( parent ), d( new Private( this ) ) | ||
38 | { | 43 | { | ||
44 | // Initialize speechEngine so we can reinitialize if it changes. | ||||
45 | d->speechEngine = Okular::Settings::ttsEngine(); | ||||
39 | connect( d->speech, &QTextToSpeech::stateChanged, this, &OkularTTS::slotSpeechStateChanged); | 46 | connect( d->speech, &QTextToSpeech::stateChanged, this, &OkularTTS::slotSpeechStateChanged); | ||
47 | connect( Okular::Settings::self(), &KConfigSkeleton::configChanged, | ||||
48 | this, &OkularTTS::slotConfigChanged); | ||||
40 | } | 49 | } | ||
41 | 50 | | |||
42 | OkularTTS::~OkularTTS() | 51 | OkularTTS::~OkularTTS() | ||
43 | { | 52 | { | ||
44 | delete d; | 53 | delete d; | ||
45 | } | 54 | } | ||
46 | 55 | | |||
47 | void OkularTTS::say( const QString &text ) | 56 | void OkularTTS::say( const QString &text ) | ||
48 | { | 57 | { | ||
49 | if ( text.isEmpty() ) | 58 | if ( text.isEmpty() ) | ||
50 | return; | 59 | return; | ||
51 | 60 | | |||
52 | d->speech->say( text ); | 61 | d->speech->say( text ); | ||
53 | } | 62 | } | ||
54 | 63 | | |||
55 | void OkularTTS::stopAllSpeechs() | 64 | void OkularTTS::stopAllSpeechs() | ||
56 | { | 65 | { | ||
57 | if ( !d->speech ) | 66 | if ( !d->speech ) | ||
58 | return; | 67 | return; | ||
59 | 68 | | |||
60 | d->speech->stop(); | 69 | d->speech->stop(); | ||
61 | } | 70 | } | ||
62 | 71 | | |||
72 | void OkularTTS::pauseResumeSpeech() | ||||
73 | { | ||||
74 | if ( !d->speech ) | ||||
75 | return; | ||||
76 | | ||||
77 | if ( d->speech->state() == QTextToSpeech::Speaking ) | ||||
78 | d->speech->pause(); | ||||
79 | else | ||||
80 | d->speech->resume(); | ||||
81 | } | ||||
82 | | ||||
63 | void OkularTTS::slotSpeechStateChanged(QTextToSpeech::State state) | 83 | void OkularTTS::slotSpeechStateChanged(QTextToSpeech::State state) | ||
64 | { | 84 | { | ||
65 | if (state == QTextToSpeech::Speaking) | 85 | if (state == QTextToSpeech::Speaking) | ||
86 | { | ||||
66 | emit isSpeaking(true); | 87 | emit isSpeaking(true); | ||
88 | emit canPauseOrResume(true); | ||||
89 | } | ||||
67 | else | 90 | else | ||
91 | { | ||||
68 | emit isSpeaking(false); | 92 | emit isSpeaking(false); | ||
93 | if (state == QTextToSpeech::Paused) | ||||
94 | emit canPauseOrResume(true); | ||||
95 | else | ||||
96 | emit canPauseOrResume(false); | ||||
97 | } | ||||
98 | } | ||||
99 | | ||||
100 | void OkularTTS::slotConfigChanged() | ||||
101 | { | ||||
102 | const QString engine = Okular::Settings::ttsEngine(); | ||||
103 | if (engine != d->speechEngine) | ||||
104 | { | ||||
105 | d->speech->stop(); | ||||
106 | delete d->speech; | ||||
107 | d->speech = new QTextToSpeech(engine); | ||||
108 | connect( d->speech, &QTextToSpeech::stateChanged, this, &OkularTTS::slotSpeechStateChanged); | ||||
109 | d->speechEngine = engine; | ||||
110 | } | ||||
69 | } | 111 | } | ||
70 | 112 | | |||
71 | #include "moc_tts.cpp" | 113 | #include "moc_tts.cpp" | ||
72 | 114 | |