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 Sep 20, 2024
1 parent b793432 commit 837fc3f
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 @@ -171,5 +171,6 @@
"scroll-previous-tab": "Scroll to previous tab",
"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 @@ -179,5 +179,6 @@
"scroll-previous-tab": "Represents the action of scrolling to the previous tab of the current tab which toward the start of the tab bar.",
"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 837fc3f

Please sign in to comment.