summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTuomas Tuononen <tuomas.tuononen@code-q.fi>2015-08-31 17:02:20 +0300
committerTuomas Tuononen <tuomas.tuononen@code-q.fi>2015-10-16 13:16:16 +0000
commit6b712af8f66c69445bc71f6030b3b3aca4d628b2 (patch)
tree9b325203282b01f047f643eb9066bad18a85006b
parente207c6542f50977b5eb420e576c9b198db273dfe (diff)
Add engine selection in the TTS example application
- Support for plug-in engines Change-Id: I9a4a1a1ceb45849b6a39dbf1473e6f9badf5d1a6 Reviewed-by: Jeremy Whiting <jpwhiting@kde.org> Reviewed-by: Frederik Gladhorn <frederik.gladhorn@theqtcompany.com>
-rw-r--r--examples/speech/hello_speak/mainwindow.cpp77
-rw-r--r--examples/speech/hello_speak/mainwindow.h3
-rw-r--r--examples/speech/hello_speak/mainwindow.ui62
3 files changed, 94 insertions, 48 deletions
diff --git a/examples/speech/hello_speak/mainwindow.cpp b/examples/speech/hello_speak/mainwindow.cpp
index 9daba96..b496cda 100644
--- a/examples/speech/hello_speak/mainwindow.cpp
+++ b/examples/speech/hello_speak/mainwindow.cpp
@@ -43,53 +43,41 @@
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
- : QMainWindow(parent)
+ : QMainWindow(parent),
+ m_speech(new QTextToSpeech(this))
{
ui.setupUi(this);
- // Populate the languages combobox before connecting its signal.
- QVector<QLocale> locales = m_speech.availableLocales();
- QLocale current = m_speech.locale();
- foreach (const QLocale &locale, locales) {
- QVariant localeVariant(locale);
- ui.language->addItem(QLocale::languageToString(locale.language()), localeVariant);
- if (locale.name() == current.name())
- ui.language->setCurrentIndex(ui.language->count() - 1);
- }
- localeChanged(current);
+ // Populate engine selection list
+ ui.engine->addItem("Default", QString("default"));
+ foreach (QString engine, QTextToSpeech::availableEngines())
+ ui.engine->addItem(engine, engine);
+ ui.engine->setCurrentIndex(0);
+ engineSelected(0);
connect(ui.speakButton, &QPushButton::clicked, this, &MainWindow::speak);
- connect(ui.stopButton, &QPushButton::clicked, &m_speech, &QTextToSpeech::stop);
- connect(ui.pauseButton, &QPushButton::clicked, &m_speech, &QTextToSpeech::pause);
- connect(ui.resumeButton, &QPushButton::clicked, &m_speech, &QTextToSpeech::resume);
-
connect(ui.pitch, &QSlider::valueChanged, this, &MainWindow::setPitch);
connect(ui.rate, &QSlider::valueChanged, this, &MainWindow::setRate);
- connect(ui.volume, &QSlider::valueChanged, &m_speech, &QTextToSpeech::setVolume);
-
- connect(&m_speech, &QTextToSpeech::stateChanged, this, &MainWindow::stateChanged);
- connect(&m_speech, &QTextToSpeech::localeChanged, this, &MainWindow::localeChanged);
- connect(ui.language, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &MainWindow::languageSelected);
- connect(ui.voice, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &MainWindow::voiceSelected);
+ connect(ui.engine, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &MainWindow::engineSelected);
}
void MainWindow::speak()
{
- m_speech.say(ui.plainTextEdit->toPlainText());
+ m_speech->say(ui.plainTextEdit->toPlainText());
}
void MainWindow::stop()
{
- m_speech.stop();
+ m_speech->stop();
}
void MainWindow::setRate(int rate)
{
- m_speech.setRate(rate / 10.0);
+ m_speech->setRate(rate / 10.0);
}
void MainWindow::setPitch(int pitch)
{
- m_speech.setPitch(pitch / 10.0);
+ m_speech->setPitch(pitch / 10.0);
}
void MainWindow::stateChanged(QTextToSpeech::State state)
@@ -108,15 +96,46 @@ void MainWindow::stateChanged(QTextToSpeech::State state)
ui.stopButton->setEnabled(state == QTextToSpeech::Speaking || QTextToSpeech::Paused);
}
+void MainWindow::engineSelected(int index)
+{
+ QString engineName = ui.engine->itemData(index).toString();
+ delete m_speech;
+ if (engineName == "default")
+ m_speech = new QTextToSpeech(this);
+ else
+ m_speech = new QTextToSpeech(this, engineName);
+ disconnect(ui.language, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &MainWindow::languageSelected);
+ ui.language->clear();
+ // Populate the languages combobox before connecting its signal.
+ QVector<QLocale> locales = m_speech->availableLocales();
+ QLocale current = m_speech->locale();
+ foreach (const QLocale &locale, locales) {
+ QVariant localeVariant(locale);
+ ui.language->addItem(QLocale::languageToString(locale.language()), localeVariant);
+ if (locale.name() == current.name())
+ current = locale;
+ }
+ connect(ui.stopButton, &QPushButton::clicked, m_speech, &QTextToSpeech::stop);
+ connect(ui.pauseButton, &QPushButton::clicked, m_speech, &QTextToSpeech::pause);
+ connect(ui.resumeButton, &QPushButton::clicked, m_speech, &QTextToSpeech::resume);
+ connect(ui.volume, &QSlider::valueChanged, m_speech, &QTextToSpeech::setVolume);
+
+ connect(m_speech, &QTextToSpeech::stateChanged, this, &MainWindow::stateChanged);
+ connect(m_speech, &QTextToSpeech::localeChanged, this, &MainWindow::localeChanged);
+
+ connect(ui.language, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &MainWindow::languageSelected);
+ localeChanged(current);
+}
+
void MainWindow::languageSelected(int language)
{
QLocale locale = ui.language->itemData(language).toLocale();
- m_speech.setLocale(locale);
+ m_speech->setLocale(locale);
}
void MainWindow::voiceSelected(int index)
{
- m_speech.setVoice(m_voices.at(index));
+ m_speech->setVoice(m_voices.at(index));
}
void MainWindow::localeChanged(const QLocale &locale)
@@ -127,8 +146,8 @@ void MainWindow::localeChanged(const QLocale &locale)
disconnect(ui.voice, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &MainWindow::voiceSelected);
ui.voice->clear();
- m_voices = m_speech.availableVoices();
- QVoice currentVoice = m_speech.voice();
+ m_voices = m_speech->availableVoices();
+ QVoice currentVoice = m_speech->voice();
foreach (const QVoice &voice, m_voices) {
ui.voice->addItem(QString("%1 - %2 - %3").arg(voice.name())
.arg(QVoice::genderName(voice.gender()))
diff --git a/examples/speech/hello_speak/mainwindow.h b/examples/speech/hello_speak/mainwindow.h
index 7b89efc..154ea97 100644
--- a/examples/speech/hello_speak/mainwindow.h
+++ b/examples/speech/hello_speak/mainwindow.h
@@ -63,6 +63,7 @@ public slots:
void setPitch(int);
void stateChanged(QTextToSpeech::State state);
+ void engineSelected(int index);
void languageSelected(int language);
void voiceSelected(int index);
@@ -70,7 +71,7 @@ public slots:
private:
Ui::MainWindow ui;
- QTextToSpeech m_speech;
+ QTextToSpeech *m_speech;
QVector<QVoice> m_voices;
};
diff --git a/examples/speech/hello_speak/mainwindow.ui b/examples/speech/hello_speak/mainwindow.ui
index 966dd28..6f3accf 100644
--- a/examples/speech/hello_speak/mainwindow.ui
+++ b/examples/speech/hello_speak/mainwindow.ui
@@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
- <width>552</width>
- <height>449</height>
+ <width>551</width>
+ <height>448</height>
</rect>
</property>
<property name="windowTitle">
@@ -34,6 +34,22 @@ Done, over and out.</string>
</item>
<item>
<layout class="QGridLayout" name="gridLayout">
+ <item row="4" column="0">
+ <widget class="QLabel" name="label_5">
+ <property name="sizePolicy">
+ <sizepolicy hsizetype="Minimum" vsizetype="Preferred">
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ <property name="text">
+ <string>Engine</string>
+ </property>
+ <property name="alignment">
+ <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
+ </property>
+ </widget>
+ </item>
<item row="3" column="0">
<widget class="QLabel" name="label_3">
<property name="sizePolicy">
@@ -50,7 +66,7 @@ Done, over and out.</string>
</property>
</widget>
</item>
- <item row="4" column="0">
+ <item row="5" column="0">
<widget class="QLabel" name="label_4">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Preferred">
@@ -69,7 +85,7 @@ Done, over and out.</string>
</property>
</widget>
</item>
- <item row="3" column="1">
+ <item row="3" column="2">
<widget class="QSlider" name="pitch">
<property name="minimum">
<number>-10</number>
@@ -85,7 +101,7 @@ Done, over and out.</string>
</property>
</widget>
</item>
- <item row="5" column="0">
+ <item row="6" column="0">
<widget class="QLabel" name="label_6">
<property name="text">
<string>Voice name:</string>
@@ -95,7 +111,7 @@ Done, over and out.</string>
</property>
</widget>
</item>
- <item row="1" column="1">
+ <item row="1" column="2">
<widget class="QSlider" name="volume">
<property name="maximum">
<number>100</number>
@@ -114,7 +130,17 @@ Done, over and out.</string>
</property>
</widget>
</item>
- <item row="5" column="1">
+ <item row="5" column="2">
+ <widget class="QComboBox" name="language">
+ <property name="sizePolicy">
+ <sizepolicy hsizetype="Expanding" vsizetype="Fixed">
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ </widget>
+ </item>
+ <item row="6" column="2">
<widget class="QComboBox" name="voice"/>
</item>
<item row="2" column="0">
@@ -133,16 +159,6 @@ Done, over and out.</string>
</property>
</widget>
</item>
- <item row="4" column="1">
- <widget class="QComboBox" name="language">
- <property name="sizePolicy">
- <sizepolicy hsizetype="Expanding" vsizetype="Fixed">
- <horstretch>0</horstretch>
- <verstretch>0</verstretch>
- </sizepolicy>
- </property>
- </widget>
- </item>
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<property name="sizePolicy">
@@ -159,7 +175,7 @@ Done, over and out.</string>
</property>
</widget>
</item>
- <item row="2" column="1">
+ <item row="2" column="2">
<widget class="QSlider" name="rate">
<property name="minimum">
<number>-10</number>
@@ -172,6 +188,16 @@ Done, over and out.</string>
</property>
</widget>
</item>
+ <item row="4" column="2">
+ <widget class="QComboBox" name="engine">
+ <property name="sizePolicy">
+ <sizepolicy hsizetype="Expanding" vsizetype="Fixed">
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ </widget>
+ </item>
</layout>
</item>
<item>