tts.cpp 2.81 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/***************************************************************************
 *   Copyright (C) 2008 by Pino Toscano <pino@kde.org>                     *
 *                                                                         *
 *   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"

12
#include <qdbusservicewatcher.h>
13 14
#include <qset.h>

15
#include <KLocalizedString>
16

17 18
#include "settings.h"

19 20 21 22
/* Private storage. */
class OkularTTS::Private
{
public:
23
    Private( OkularTTS *qq )
24
        : q( qq ), speech( new QTextToSpeech( Okular::Settings::ttsEngine() ) )
25 26 27
    {
    }

28
    ~Private()
29
    {
30
        delete speech;
31
        speech = nullptr;
32
    }
33

34 35
    OkularTTS *q;
    QTextToSpeech *speech;
36 37 38
    // Which speech engine was used when above object was created.
    // When the setting changes, we need to stop speaking and recreate.
    QString speechEngine;
39
};
40

41
OkularTTS::OkularTTS( QObject *parent )
42 43
    : QObject( parent ), d( new Private( this ) )
{
44 45
    // Initialize speechEngine so we can reinitialize if it changes.
    d->speechEngine = Okular::Settings::ttsEngine();
46
    connect( d->speech, &QTextToSpeech::stateChanged, this, &OkularTTS::slotSpeechStateChanged);
47 48
    connect( Okular::Settings::self(), &KConfigSkeleton::configChanged,
             this, &OkularTTS::slotConfigChanged);
49 50 51 52 53 54 55 56 57 58 59 60
}

OkularTTS::~OkularTTS()
{
    delete d;
}

void OkularTTS::say( const QString &text )
{
    if ( text.isEmpty() )
        return;

61
    d->speech->say( text );
62 63
}

64 65
void OkularTTS::stopAllSpeechs()
{
66
    if ( !d->speech )
67 68
        return;

69
    d->speech->stop();
70 71
}

72 73 74 75 76 77 78 79 80 81 82
void OkularTTS::pauseResumeSpeech()
{
    if ( !d->speech )
        return;

    if ( d->speech->state() == QTextToSpeech::Speaking )
        d->speech->pause();
    else
        d->speech->resume();
}

83
void OkularTTS::slotSpeechStateChanged(QTextToSpeech::State state)
84
{
85
    if (state == QTextToSpeech::Speaking)
86
    {
87
        emit isSpeaking(true);
88 89
        emit canPauseOrResume(true);
    }
90
    else
91
    {
92
        emit isSpeaking(false);
93 94 95 96 97
        if (state == QTextToSpeech::Paused)
            emit canPauseOrResume(true);
        else
            emit canPauseOrResume(false);
    }
98 99
}

100 101 102 103 104 105 106 107 108 109 110 111
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;
    }
}