#include "voiceform.h"
#include "ui_voiceform.h"
#include <QLoggingCategory>
VoiceForm::VoiceForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::VoiceForm)
{
    ui->setupUi(this);
    setWindowFlags(Qt::WindowCloseButtonHint);
    setWindowTitle("语音播报");
    m_speech = new QTextToSpeech(this);
    QLoggingCategory::setFilterRules(QStringLiteral("qt.speech.tts=true \n qt.speech.tts.*=true"));
    // 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,SIGNAL(clicked(bool)), this, SLOT(speak()));
    connect(ui->pitch, SIGNAL(valueChanged(int)), this, SLOT(setPitch(int)));
    connect(ui->rate, SIGNAL(valueChanged(int)), this, SLOT(setRate(int)));
    connect(ui->volume, SIGNAL(valueChanged(int)), this, SLOT(setVolume(int)));
    connect(ui->engine, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &VoiceForm::engineSelected);
}

VoiceForm::~VoiceForm()
{
    delete ui;
}
void VoiceForm::sayVoice(QString str)
{
    m_speech->say(str);
}
void VoiceForm::speak()
{
    m_speech->say(ui->plainTextEdit->toPlainText());
}
void VoiceForm::stop()
{
    m_speech->stop();
}
/*设置语速-1.0到1.0*/
void VoiceForm::setRate(int rate)
{
    m_speech->setRate(rate / 10.0);
}
/*设置音高-1.0到1.0*/
void VoiceForm::setPitch(int pitch)
{
    m_speech->setPitch(pitch / 10.0);
}
/*设置音量0.0-1.0*/
void VoiceForm::setVolume(int volume)
{
    m_speech->setVolume(volume / 100.0);
}
/*状态改变*/
void VoiceForm::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 || state == QTextToSpeech::Paused);
}
/*选择引擎*/
void VoiceForm::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(engineName, this);
    disconnect(ui->language, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &VoiceForm::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)
    {
        QString name(QString("%1 (%2)")
                     .arg(QLocale::languageToString(locale.language()))
                     .arg(QLocale::countryToString(locale.country())));
        QVariant localeVariant(locale);
        ui->language->addItem(name, localeVariant);
        if (locale.name() == current.name())
            current = locale;
    }
    setRate(ui->rate->value());
    setPitch(ui->pitch->value());
    setVolume(ui->volume->value());
//    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(m_speech, &QTextToSpeech::stateChanged, this, &VoiceForm::stateChanged);
    connect(m_speech, &QTextToSpeech::localeChanged, this, &VoiceForm::localeChanged);

    connect(ui->language, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &VoiceForm::languageSelected);
    localeChanged(current);
}
/*选择语言*/
void VoiceForm::languageSelected(int language)
{
    QLocale locale = ui->language->itemData(language).toLocale();
    m_speech->setLocale(locale);
}
/*选择声音*/
void VoiceForm::voiceSelected(int index)
{
    m_speech->setVoice(m_voices.at(index));
}
/*语言环境改变*/
void VoiceForm::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, &VoiceForm::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, &VoiceForm::voiceSelected);
}
