1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2017 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the examples of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:BSD$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** BSD License Usage |
18 | ** Alternatively, you may use this file under the terms of the BSD license |
19 | ** as follows: |
20 | ** |
21 | ** "Redistribution and use in source and binary forms, with or without |
22 | ** modification, are permitted provided that the following conditions are |
23 | ** met: |
24 | ** * Redistributions of source code must retain the above copyright |
25 | ** notice, this list of conditions and the following disclaimer. |
26 | ** * Redistributions in binary form must reproduce the above copyright |
27 | ** notice, this list of conditions and the following disclaimer in |
28 | ** the documentation and/or other materials provided with the |
29 | ** distribution. |
30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
31 | ** contributors may be used to endorse or promote products derived |
32 | ** from this software without specific prior written permission. |
33 | ** |
34 | ** |
35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
46 | ** |
47 | ** $QT_END_LICENSE$ |
48 | ** |
49 | ****************************************************************************/ |
50 | |
51 | |
52 | |
53 | #include "mainwindow.h" |
54 | #include <QLoggingCategory> |
55 | |
56 | MainWindow::MainWindow(QWidget *parent) |
57 | : QMainWindow(parent), |
58 | m_speech(nullptr) |
59 | { |
60 | ui.setupUi(this); |
61 | QLoggingCategory::setFilterRules(QStringLiteral("qt.speech.tts=true \n qt.speech.tts.*=true" )); |
62 | |
63 | // Populate engine selection list |
64 | ui.engine->addItem(atext: "Default" , auserData: QString("default" )); |
65 | const auto engines = QTextToSpeech::availableEngines(); |
66 | for (const QString &engine : engines) |
67 | ui.engine->addItem(atext: engine, auserData: engine); |
68 | ui.engine->setCurrentIndex(0); |
69 | engineSelected(index: 0); |
70 | |
71 | connect(sender: ui.speakButton, signal: &QPushButton::clicked, receiver: this, slot: &MainWindow::speak); |
72 | connect(sender: ui.pitch, signal: &QSlider::valueChanged, receiver: this, slot: &MainWindow::setPitch); |
73 | connect(sender: ui.rate, signal: &QSlider::valueChanged, receiver: this, slot: &MainWindow::setRate); |
74 | connect(sender: ui.volume, signal: &QSlider::valueChanged, receiver: this, slot: &MainWindow::setVolume); |
75 | connect(sender: ui.engine, signal: static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), receiver: this, slot: &MainWindow::engineSelected); |
76 | } |
77 | |
78 | void MainWindow::speak() |
79 | { |
80 | m_speech->say(text: ui.plainTextEdit->toPlainText()); |
81 | } |
82 | void MainWindow::stop() |
83 | { |
84 | m_speech->stop(); |
85 | } |
86 | |
87 | void MainWindow::setRate(int rate) |
88 | { |
89 | m_speech->setRate(rate / 10.0); |
90 | } |
91 | |
92 | void MainWindow::setPitch(int pitch) |
93 | { |
94 | m_speech->setPitch(pitch / 10.0); |
95 | } |
96 | |
97 | void MainWindow::setVolume(int volume) |
98 | { |
99 | m_speech->setVolume(volume / 100.0); |
100 | } |
101 | |
102 | void MainWindow::stateChanged(QTextToSpeech::State state) |
103 | { |
104 | if (state == QTextToSpeech::Speaking) { |
105 | ui.statusbar->showMessage(text: "Speech started..." ); |
106 | } else if (state == QTextToSpeech::Ready) { |
107 | ui.statusbar->showMessage(text: "Speech stopped..." , timeout: 2000); |
108 | if (!m_localesQueried) |
109 | queryLocales(); |
110 | } |
111 | else if (state == QTextToSpeech::Paused) |
112 | ui.statusbar->showMessage(text: "Speech paused..." ); |
113 | else |
114 | ui.statusbar->showMessage(text: "Speech error!" ); |
115 | |
116 | ui.pauseButton->setEnabled(state == QTextToSpeech::Speaking); |
117 | ui.resumeButton->setEnabled(state == QTextToSpeech::Paused); |
118 | ui.stopButton->setEnabled(state == QTextToSpeech::Speaking || state == QTextToSpeech::Paused); |
119 | } |
120 | |
121 | void MainWindow::engineSelected(int index) |
122 | { |
123 | m_localesQueried = false; |
124 | QString engineName = ui.engine->itemData(index).toString(); |
125 | delete m_speech; |
126 | if (engineName == "default" ) |
127 | m_speech = new QTextToSpeech(this); |
128 | else |
129 | m_speech = new QTextToSpeech(engineName, this); |
130 | |
131 | connect(sender: m_speech, signal: &QTextToSpeech::stateChanged, receiver: this, slot: &MainWindow::stateChanged); |
132 | } |
133 | |
134 | void MainWindow::queryLocales() |
135 | { |
136 | disconnect(sender: ui.language, signal: static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), receiver: this, slot: &MainWindow::languageSelected); |
137 | ui.language->clear(); |
138 | // Populate the languages combobox before connecting its signal. |
139 | const QVector<QLocale> locales = m_speech->availableLocales(); |
140 | QLocale current = m_speech->locale(); |
141 | for (const QLocale &locale : locales) { |
142 | QString name(QString("%1 (%2)" ) |
143 | .arg(a: QLocale::languageToString(language: locale.language())) |
144 | .arg(a: QLocale::countryToString(country: locale.country()))); |
145 | QVariant localeVariant(locale); |
146 | ui.language->addItem(atext: name, auserData: localeVariant); |
147 | if (locale.name() == current.name()) |
148 | current = locale; |
149 | } |
150 | setRate(ui.rate->value()); |
151 | setPitch(ui.pitch->value()); |
152 | setVolume(ui.volume->value()); |
153 | connect(sender: ui.stopButton, signal: &QPushButton::clicked, receiver: m_speech, slot: &QTextToSpeech::stop); |
154 | connect(sender: ui.pauseButton, signal: &QPushButton::clicked, receiver: m_speech, slot: &QTextToSpeech::pause); |
155 | connect(sender: ui.resumeButton, signal: &QPushButton::clicked, receiver: m_speech, slot: &QTextToSpeech::resume); |
156 | |
157 | connect(sender: m_speech, signal: &QTextToSpeech::localeChanged, receiver: this, slot: &MainWindow::localeChanged); |
158 | |
159 | connect(sender: ui.language, signal: static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), receiver: this, slot: &MainWindow::languageSelected); |
160 | localeChanged(locale: current); |
161 | m_localesQueried = true; |
162 | } |
163 | |
164 | void MainWindow::languageSelected(int language) |
165 | { |
166 | QLocale locale = ui.language->itemData(index: language).toLocale(); |
167 | m_speech->setLocale(locale); |
168 | } |
169 | |
170 | void MainWindow::voiceSelected(int index) |
171 | { |
172 | m_speech->setVoice(m_voices.at(i: index)); |
173 | } |
174 | |
175 | void MainWindow::localeChanged(const QLocale &locale) |
176 | { |
177 | QVariant localeVariant(locale); |
178 | ui.language->setCurrentIndex(ui.language->findData(data: localeVariant)); |
179 | |
180 | disconnect(sender: ui.voice, signal: static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), receiver: this, slot: &MainWindow::voiceSelected); |
181 | ui.voice->clear(); |
182 | |
183 | m_voices = m_speech->availableVoices(); |
184 | QVoice currentVoice = m_speech->voice(); |
185 | for (const QVoice &voice : qAsConst(t&: m_voices)) { |
186 | ui.voice->addItem(atext: QString("%1 - %2 - %3" ).arg(a: voice.name()) |
187 | .arg(a: QVoice::genderName(gender: voice.gender())) |
188 | .arg(a: QVoice::ageName(age: voice.age()))); |
189 | if (voice.name() == currentVoice.name()) |
190 | ui.voice->setCurrentIndex(ui.voice->count() - 1); |
191 | } |
192 | connect(sender: ui.voice, signal: static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), receiver: this, slot: &MainWindow::voiceSelected); |
193 | } |
194 | |