Skip to content

Commit

Permalink
Added option to change voice
Browse files Browse the repository at this point in the history
Needed as some voices does not work
  • Loading branch information
ShaopengLin committed Aug 5, 2024
1 parent df3d5ff commit ba921f6
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
3 changes: 2 additions & 1 deletion resources/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,6 @@
"zim-path": "Zim File Path",
"read-article": "Read article",
"read-text": "Read selected text",
"stop": "Stop"
"stop": "Stop",
"voice": "Voice"
}
3 changes: 2 additions & 1 deletion resources/i18n/qqq.json
Original file line number Diff line number Diff line change
Expand Up @@ -183,5 +183,6 @@
"zim-path": "The term for the path of a Zim file",
"read-article": "Represents the action of letting the computer read out all the text in the current article.",
"read-text": "Represents the action of letting the computer read out the selected text on the article.",
"stop": "{{Identical|Stop}}"
"stop": "{{Identical|Stop}}",
"voice": "{{Identical|Voice}}"
}
21 changes: 20 additions & 1 deletion src/texttospeechbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ TextToSpeechBar::TextToSpeechBar(QWidget *parent)
close();
mp_ui->setupUi(this);
mp_ui->stopButton->setText(gt("stop"));
mp_ui->voiceLabel->setText(gt("voice"));
connect(mp_speech, &QTextToSpeech::stateChanged, this,
&TextToSpeechBar::onStateChanged);
connect(mp_ui->stopButton, &QPushButton::released, this,
Expand Down Expand Up @@ -59,7 +60,25 @@ void TextToSpeechBar::onStateChanged(QTextToSpeech::State state)
void TextToSpeechBar::languageSelected(int index)
{
QLocale locale = mp_ui->langComboBox->itemData(index).toLocale();
mp_speech->setLocale(locale);
disconnect(mp_ui->voiceComboBox, &QComboBox::currentIndexChanged, this, &TextToSpeechBar::voiceSelected);
mp_ui->voiceComboBox->clear();

m_voices = mp_speech->availableVoices();
QVoice currentVoice = mp_speech->voice();
for (auto voice : m_voices)
{
mp_ui->voiceComboBox->addItem(QString("%1 - %2 - %3").arg(voice.name())
.arg(QVoice::genderName(voice.gender()))
.arg(QVoice::ageName(voice.age())));
if (voice.name() == currentVoice.name())
mp_ui->voiceComboBox->setCurrentIndex(mp_ui->voiceComboBox->count() - 1);
}
connect(mp_ui->voiceComboBox, &QComboBox::currentIndexChanged, this, &TextToSpeechBar::voiceSelected);
}

void TextToSpeechBar::voiceSelected(int index)
{
mp_speech->setVoice(m_voices.at(index));
}

void TextToSpeechBar::speechBarClose()
Expand Down
2 changes: 2 additions & 0 deletions src/texttospeechbar.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ class TextToSpeechBar : public QFrame
public slots:
void speechBarClose();
void languageSelected(int index);
void voiceSelected(int index);
void onStateChanged(QTextToSpeech::State state);

private:
QTextToSpeech *mp_speech;
Ui::TextToSpeechBar *mp_ui;
QVector<QVoice> m_voices;
};

#endif // TEXTTOSPEECHMANAGER_H
14 changes: 14 additions & 0 deletions src/texttospeechbar.ui
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="voiceLabel">
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="voiceComboBox">
<property name="editable">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
Expand Down

0 comments on commit ba921f6

Please sign in to comment.