diff options
author | Venugopal Shivashankar <venugopal.shivashankar@digia.com> | 2015-08-21 12:37:36 +0200 |
---|---|---|
committer | Frederik Gladhorn <frederik.gladhorn@theqtcompany.com> | 2015-08-22 08:47:13 +0000 |
commit | bc8a097b1ccfa7de31193b5dccb5a64f8bfd7551 (patch) | |
tree | c5682edc6c8d113703ad486f8cfb5dbe67b229f6 /examples/speech/hello_speak | |
parent | 2e95885ca368b2f3cdc1a258fd6567b2e0a7fe3b (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')
-rw-r--r-- | examples/speech/hello_speak/hello_speak.pro | 9 | ||||
-rw-r--r-- | examples/speech/hello_speak/main.cpp | 52 | ||||
-rw-r--r-- | examples/speech/hello_speak/mainwindow.cpp | 140 | ||||
-rw-r--r-- | examples/speech/hello_speak/mainwindow.h | 77 | ||||
-rw-r--r-- | examples/speech/hello_speak/mainwindow.ui | 241 |
5 files changed, 519 insertions, 0 deletions
diff --git a/examples/speech/hello_speak/hello_speak.pro b/examples/speech/hello_speak/hello_speak.pro new file mode 100644 index 0000000..40df883 --- /dev/null +++ b/examples/speech/hello_speak/hello_speak.pro @@ -0,0 +1,9 @@ +TEMPLATE = app + +QT += widgets texttospeech + +SOURCES = main.cpp \ + mainwindow.cpp +HEADERS = mainwindow.h + +FORMS = mainwindow.ui diff --git a/examples/speech/hello_speak/main.cpp b/examples/speech/hello_speak/main.cpp new file mode 100644 index 0000000..4071fc0 --- /dev/null +++ b/examples/speech/hello_speak/main.cpp @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** 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 <QApplication> + +#include "mainwindow.h" + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + MainWindow win; + win.show(); + return app.exec(); +} 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); +} diff --git a/examples/speech/hello_speak/mainwindow.h b/examples/speech/hello_speak/mainwindow.h new file mode 100644 index 0000000..7b89efc --- /dev/null +++ b/examples/speech/hello_speak/mainwindow.h @@ -0,0 +1,77 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + + + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include <QtWidgets/qmainwindow.h> + +#include "ui_mainwindow.h" + +#include <QTextToSpeech> + +class MainWindow : public QMainWindow +{ + Q_OBJECT +public: + MainWindow(QWidget *parent = 0); + +public slots: + void speak(); + void stop(); + + void setRate(int); + void setPitch(int); + + void stateChanged(QTextToSpeech::State state); + void languageSelected(int language); + void voiceSelected(int index); + + void localeChanged(const QLocale &locale); + +private: + Ui::MainWindow ui; + QTextToSpeech m_speech; + QVector<QVoice> m_voices; +}; + +#endif diff --git a/examples/speech/hello_speak/mainwindow.ui b/examples/speech/hello_speak/mainwindow.ui new file mode 100644 index 0000000..966dd28 --- /dev/null +++ b/examples/speech/hello_speak/mainwindow.ui @@ -0,0 +1,241 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>MainWindow</class> + <widget class="QMainWindow" name="MainWindow"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>552</width> + <height>449</height> + </rect> + </property> + <property name="windowTitle"> + <string>MainWindow</string> + </property> + <widget class="QWidget" name="centralwidget"> + <layout class="QVBoxLayout" name="verticalLayout"> + <item> + <widget class="QPlainTextEdit" name="plainTextEdit"> + <property name="sizePolicy"> + <sizepolicy hsizetype="Expanding" vsizetype="Expanding"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="plainText"> + <string>Hello QtTextToSpeech, +this is an example text in English. + +QtSpeech is a library that makes text to speech easy with Qt. +Done, over and out.</string> + </property> + </widget> + </item> + <item> + <layout class="QGridLayout" name="gridLayout"> + <item row="3" column="0"> + <widget class="QLabel" name="label_3"> + <property name="sizePolicy"> + <sizepolicy hsizetype="Minimum" vsizetype="Preferred"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="text"> + <string>Pitch:</string> + </property> + <property name="alignment"> + <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set> + </property> + </widget> + </item> + <item row="4" column="0"> + <widget class="QLabel" name="label_4"> + <property name="sizePolicy"> + <sizepolicy hsizetype="Minimum" vsizetype="Preferred"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="text"> + <string>&Language:</string> + </property> + <property name="alignment"> + <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set> + </property> + <property name="buddy"> + <cstring>language</cstring> + </property> + </widget> + </item> + <item row="3" column="1"> + <widget class="QSlider" name="pitch"> + <property name="minimum"> + <number>-10</number> + </property> + <property name="maximum"> + <number>10</number> + </property> + <property name="singleStep"> + <number>1</number> + </property> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + </widget> + </item> + <item row="5" column="0"> + <widget class="QLabel" name="label_6"> + <property name="text"> + <string>Voice name:</string> + </property> + <property name="alignment"> + <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set> + </property> + </widget> + </item> + <item row="1" column="1"> + <widget class="QSlider" name="volume"> + <property name="maximum"> + <number>100</number> + </property> + <property name="singleStep"> + <number>5</number> + </property> + <property name="pageStep"> + <number>20</number> + </property> + <property name="value"> + <number>70</number> + </property> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + </widget> + </item> + <item row="5" column="1"> + <widget class="QComboBox" name="voice"/> + </item> + <item row="2" column="0"> + <widget class="QLabel" name="label"> + <property name="sizePolicy"> + <sizepolicy hsizetype="Minimum" vsizetype="Preferred"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="text"> + <string>Rate:</string> + </property> + <property name="alignment"> + <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set> + </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"> + <sizepolicy hsizetype="Minimum" vsizetype="Preferred"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="text"> + <string>Volume:</string> + </property> + <property name="alignment"> + <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set> + </property> + </widget> + </item> + <item row="2" column="1"> + <widget class="QSlider" name="rate"> + <property name="minimum"> + <number>-10</number> + </property> + <property name="maximum"> + <number>10</number> + </property> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + </widget> + </item> + </layout> + </item> + <item> + <layout class="QHBoxLayout" name="horizontalLayout"> + <item> + <widget class="QPushButton" name="speakButton"> + <property name="text"> + <string>Speak</string> + </property> + </widget> + </item> + <item> + <widget class="QPushButton" name="pauseButton"> + <property name="enabled"> + <bool>false</bool> + </property> + <property name="text"> + <string>Pause</string> + </property> + </widget> + </item> + <item> + <widget class="QPushButton" name="resumeButton"> + <property name="enabled"> + <bool>false</bool> + </property> + <property name="text"> + <string>Resume</string> + </property> + </widget> + </item> + <item> + <widget class="QPushButton" name="stopButton"> + <property name="text"> + <string>Stop</string> + </property> + </widget> + </item> + </layout> + </item> + <item> + <spacer name="verticalSpacer"> + <property name="orientation"> + <enum>Qt::Vertical</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>20</width> + <height>40</height> + </size> + </property> + </spacer> + </item> + </layout> + </widget> + <widget class="QStatusBar" name="statusbar"/> + </widget> + <tabstops> + <tabstop>plainTextEdit</tabstop> + <tabstop>speakButton</tabstop> + <tabstop>pauseButton</tabstop> + <tabstop>resumeButton</tabstop> + <tabstop>stopButton</tabstop> + </tabstops> + <resources/> + <connections/> +</ui> |