Skip to content

Commit

Permalink
Added option to switch language
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaopengLin committed Sep 20, 2024
1 parent d74ee09 commit b793432
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
30 changes: 29 additions & 1 deletion src/texttospeechbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,36 @@ TextToSpeechBar::TextToSpeechBar(QWidget *parent)
{
close();
mp_ui->setupUi(this);
mp_speech->setLocale(QLocale::system().language());
mp_ui->stopButton->setText(gt("stop"));
connect(mp_speech, &QTextToSpeech::stateChanged, this,
&TextToSpeechBar::onStateChanged);
connect(mp_ui->stopButton, &QPushButton::released, this,
&TextToSpeechBar::stop);
connect(mp_ui->closeButton, &QPushButton::released,
this, &TextToSpeechBar::speechBarClose);

mp_ui->langLabel->setText(gt("language"));
mp_ui->langComboBox->setMaxVisibleItems(10);
QLocale current = QLocale::system().language();
for (auto locale : mp_speech->availableLocales())
{
QString name(QString("%1 (%2)")
.arg(QLocale::languageToString(locale.language()))
.arg(locale.nativeLanguageName()));
QVariant localeVariant(locale);
mp_ui->langComboBox->addItem(name, localeVariant);
if (locale.name() == current.name())
current = locale;
}

/* Work around to both have max visible item and a read-only combobox.*/
mp_ui->langComboBox->lineEdit()->setReadOnly(true);
mp_ui->langComboBox->lineEdit()->setFrame(false);

connect(mp_ui->langComboBox, &QComboBox::currentIndexChanged,
this, &TextToSpeechBar::languageSelected);
mp_ui->langComboBox->setCurrentIndex(mp_ui->langComboBox->findData(current));
languageSelected(mp_ui->langComboBox->currentIndex());
}

void TextToSpeechBar::speak(const QString &text)
Expand All @@ -34,6 +56,12 @@ void TextToSpeechBar::onStateChanged(QTextToSpeech::State state)
mp_ui->stopButton->setEnabled(state != QTextToSpeech::Ready);
}

void TextToSpeechBar::languageSelected(int index)
{
QLocale locale = mp_ui->langComboBox->itemData(index).toLocale();
mp_speech->setLocale(locale);
}

void TextToSpeechBar::speechBarClose()
{
/* Prevent webview from scrolling to up to the top after losing focus. */
Expand Down
1 change: 1 addition & 0 deletions src/texttospeechbar.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class TextToSpeechBar : public QFrame

public slots:
void speechBarClose();
void languageSelected(int index);
void onStateChanged(QTextToSpeech::State state);

private:
Expand Down
24 changes: 22 additions & 2 deletions src/texttospeechbar.ui
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,30 @@
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<spacer name="horizontalSpacer">
<widget class="QLabel" name="langLabel">
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="langComboBox">
<property name="layoutDirection">
<enum>Qt::LeftToRight</enum>
</property>
<property name="editable">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Expanding</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
Expand Down Expand Up @@ -57,7 +77,7 @@
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
Expand Down

0 comments on commit b793432

Please sign in to comment.