File: texttospeechconfiginterface.cpp

package info (click to toggle)
ktextaddons 1.9.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,856 kB
  • sloc: cpp: 62,045; ansic: 6,520; xml: 2,630; sh: 11; makefile: 7
file content (106 lines) | stat: -rw-r--r-- 3,155 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
   SPDX-FileCopyrightText: 2014-2026 Laurent Montel <montel@kde.org>

   SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include "texttospeechconfiginterface.h"
#include "textedittexttospeech_debug.h"
#include <KLocalizedString>
#include <QTextToSpeech>
using namespace TextEditTextToSpeech;

TextToSpeechConfigInterface::TextToSpeechConfigInterface(QObject *parent)
    : QObject(parent)
{
}

TextToSpeechConfigInterface::~TextToSpeechConfigInterface() = default;

QVector<QVoice> TextToSpeechConfigInterface::availableVoices() const
{
    QVector<QVoice> voices;
    if (mTextToSpeech) {
        voices = mTextToSpeech->availableVoices();
    } else {
        qCWarning(TEXTEDITTEXTTOSPEECH_LOG) << "Text To Speech is not created. ";
    }
    return voices;
}

QStringList TextToSpeechConfigInterface::availableEngines() const
{
    if (mTextToSpeech) {
        return mTextToSpeech->availableEngines();
    }
    qCWarning(TEXTEDITTEXTTOSPEECH_LOG) << "Text To Speech is not created. ";
    return {};
}

QVector<QLocale> TextToSpeechConfigInterface::availableLocales() const
{
    if (mTextToSpeech) {
        return mTextToSpeech->availableLocales();
    }
    qCWarning(TEXTEDITTEXTTOSPEECH_LOG) << "Text To Speech is not created. ";
    return {};
}

QLocale TextToSpeechConfigInterface::locale() const
{
    if (mTextToSpeech) {
        return mTextToSpeech->locale();
    }
    qCWarning(TEXTEDITTEXTTOSPEECH_LOG) << "Text To Speech is not created. ";
    return {};
}

void TextToSpeechConfigInterface::setEngine(const QString &engineName)
{
    if (!mTextToSpeech || (mTextToSpeech->engine() != engineName)) {
        delete mTextToSpeech;
        mTextToSpeech = new QTextToSpeech(engineName, this);
        connect(mTextToSpeech, &QTextToSpeech::stateChanged, this, &TextToSpeechConfigInterface::stateChanged);
    }
}

void TextToSpeechConfigInterface::testEngine(const EngineSettings &engineSettings)
{
    if (!mTextToSpeech) {
        qCWarning(TEXTEDITTEXTTOSPEECH_LOG) << "Text To Speech is not created. ";
        return;
    }
    const int rate = engineSettings.rate;
    const double rateDouble = rate / 100.0;
    mTextToSpeech->setRate(rateDouble);
    const int pitch = engineSettings.pitch;
    const double pitchDouble = pitch / 100.0;
    mTextToSpeech->setPitch(pitchDouble);
    const int volume = engineSettings.volume;
    const double volumeDouble = volume / 100.0;
    mTextToSpeech->setVolume(volumeDouble);
    mTextToSpeech->setLocale(QLocale(engineSettings.localeName));
    mTextToSpeech->setVoice(engineSettings.voice);

    // TODO change text ?
    mTextToSpeech->say(i18n("Morning, this is the test for testing settings."));
}

void TextToSpeechConfigInterface::stop()
{
    if (mTextToSpeech->state() == QTextToSpeech::Speaking) {
        mTextToSpeech->stop();
    }
}

QDebug operator<<(QDebug d, const TextEditTextToSpeech::TextToSpeechConfigInterface::EngineSettings &t)
{
    d << " Rate " << t.rate;
    d << " pitch " << t.pitch;
    d << " volume " << t.volume;
    d << " voice " << t.voice;
    d << " localeName " << t.localeName;
    return d;
}

#include "moc_texttospeechconfiginterface.cpp"