summaryrefslogtreecommitdiffstats
path: root/examples/speech/hello_speak/mainwindow.cpp
diff options
context:
space:
mode:
authorVenugopal Shivashankar <venugopal.shivashankar@digia.com>2015-08-21 12:37:36 +0200
committerFrederik Gladhorn <frederik.gladhorn@theqtcompany.com>2015-08-22 08:47:13 +0000
commitbc8a097b1ccfa7de31193b5dccb5a64f8bfd7551 (patch)
treec5682edc6c8d113703ad486f8cfb5dbe67b229f6 /examples/speech/hello_speak/mainwindow.cpp
parent2e95885ca368b2f3cdc1a258fd6567b2e0a7fe3b (diff)
Doc: Moved the examples to examples/speech
With the earlier directory structure of examples/widgets, the speech examples would end up in the widgets directory along with other Qt Widgets examples in the Qt binary pkg. Change-Id: Ib9ad0826e23c493a4fc5b1d1e7acf9addad3be65 Reviewed-by: Frederik Gladhorn <frederik.gladhorn@theqtcompany.com>
Diffstat (limited to 'examples/speech/hello_speak/mainwindow.cpp')
-rw-r--r--examples/speech/hello_speak/mainwindow.cpp140
1 files changed, 140 insertions, 0 deletions
diff --git a/examples/speech/hello_speak/mainwindow.cpp b/examples/speech/hello_speak/mainwindow.cpp
new file mode 100644
index 0000000..9daba96
--- /dev/null
+++ b/examples/speech/hello_speak/mainwindow.cpp
@@ -0,0 +1,140 @@
+/****************************************************************************
+**
+** Copyright (C) 2015 The Qt Company Ltd.
+** Contact: http://www.qt.io/licensing/
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of The Qt Company Ltd nor the names of its
+** contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+
+
+#include "mainwindow.h"
+
+MainWindow::MainWindow(QWidget *parent)
+ : QMainWindow(parent)
+{
+ 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);
+
+ 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);
+}
+
+void MainWindow::speak()
+{
+ m_speech.say(ui.plainTextEdit->toPlainText());
+}
+void MainWindow::stop()
+{
+ m_speech.stop();
+}
+
+void MainWindow::setRate(int rate)
+{
+ m_speech.setRate(rate / 10.0);
+}
+
+void MainWindow::setPitch(int pitch)
+{
+ m_speech.setPitch(pitch / 10.0);
+}
+
+void MainWindow::stateChanged(QTextToSpeech::State state)
+{
+ if (state == QTextToSpeech::Speaking) {
+ ui.statusbar->showMessage("Speech started...");
+ } else if (state == QTextToSpeech::Ready)
+ ui.statusbar->showMessage("Speech stopped...", 2000);
+ else if (state == QTextToSpeech::Paused)
+ ui.statusbar->showMessage("Speech paused...");
+ else
+ ui.statusbar->showMessage("Speech error!");
+
+ ui.pauseButton->setEnabled(state == QTextToSpeech::Speaking);
+ ui.resumeButton->setEnabled(state == QTextToSpeech::Paused);
+ ui.stopButton->setEnabled(state == QTextToSpeech::Speaking || QTextToSpeech::Paused);
+}
+
+void MainWindow::languageSelected(int language)
+{
+ QLocale locale = ui.language->itemData(language).toLocale();
+ m_speech.setLocale(locale);
+}
+
+void MainWindow::voiceSelected(int index)
+{
+ m_speech.setVoice(m_voices.at(index));
+}
+
+void MainWindow::localeChanged(const QLocale &locale)
+{
+ QVariant localeVariant(locale);
+ ui.language->setCurrentIndex(ui.language->findData(localeVariant));
+
+ 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();
+ foreach (const QVoice &voice, m_voices) {
+ ui.voice->addItem(QString("%1 - %2 - %3").arg(voice.name())
+ .arg(QVoice::genderName(voice.gender()))
+ .arg(QVoice::ageName(voice.age())));
+ if (voice.name() == currentVoice.name())
+ ui.voice->setCurrentIndex(ui.voice->count() - 1);
+ }
+ connect(ui.voice, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &MainWindow::voiceSelected);
+}