Skip to content

Commit

Permalink
fixup! Crash and UI issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaopengLin committed Nov 12, 2024
1 parent 455ef3f commit 6fb2227
Show file tree
Hide file tree
Showing 15 changed files with 170 additions and 46 deletions.
1 change: 1 addition & 0 deletions debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Build-Depends: debhelper-compat (= 13),
pkg-config,
qtbase5-dev,
qtwebengine5-dev,
libqt5texttospeech5-dev,
libkiwix-dev (>= 14.0.0), libkiwix-dev (<< 15.0.0),
libzim-dev (>= 9.0.0), libzim-dev (<< 10.0.0),
Standards-Version: 4.5.0
Expand Down
3 changes: 2 additions & 1 deletion kiwix-desktop.pro
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ QT += webenginewidgets
QT += printsupport

HAS_TTS = FALSE
versionAtLeast(QT_VERSION, 6.4.0) : qtHaveModule(texttospeech) {
qtHaveModule(texttospeech) {
HAS_TTS = TRUE
}
equals(HAS_TTS, TRUE) : QT += texttospeech
Expand All @@ -18,6 +18,7 @@ equals(HAS_TTS, TRUE) : QT += texttospeech
DETECT_WSL = $$system(test -f /proc/sys/fs/binfmt_misc/WSLInterop && echo true || echo false)
equals(DETECT_WSL , "true"): CONFIG += nostrip
CONFIG += link_pkgconfig
CONFIG += warn_off

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

Expand Down
47 changes: 42 additions & 5 deletions resources/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -399,28 +399,65 @@ ContentTypeFilter {
#readinglistbar QPushButton::menu-indicator {
width: 0;
height: 0;

}
/* ----------------------------------------
Text to Speech Page
*/

#TextToSpeechBar {
TextToSpeechBar {
border-top: 1px solid #ccc;
}

TextToSpeechBar > #closeButton {
TextToSpeechBar #closeButton {
outline: none;
max-height: 36px;
max-width: 36px;
border: 1px solid #ccc;
border-radius: 0;
}

TextToSpeechBar > #closeButton:pressed {
TextToSpeechBar #stopButton {
outline: none;
max-height: 36px;
padding-left: 10px;
padding-right: 10px;
border: 1px solid #ccc;
background-color: white;
border-radius: 0;
}

TextToSpeechBar #stopButton:hover {
background-color: #D9E9FF;
border: 1px solid #3366CC;
}

TextToSpeechBar > #stopButton:disabled {
TextToSpeechBar #stopButton:disabled {
color: grey;
background-color: darkgrey;
}

TextToSpeechBar QComboBox::drop-down {
height: 22px;
width: 15px;
padding-left: 2px;
padding-right: 2px;
/* width: 22px; */
margin: 0px;
image: url(":/icons/drop-down.svg");
background-color: white;
}

TextToSpeechBar QComboBox {
background-color: white;
border: 1px solid #ccc;
}

TextToSpeechBar QAbstractItemView QScrollBar {
width: 5px;
border: none;
outline: none;
}

TextToSpeechBar QAbstractItemView QScrollBar::handle {
background-color: grey;
}
1 change: 0 additions & 1 deletion resources/i18n/qqq.json
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@
"move-files-to-trash-text": "Description text for the action of moving ZIM files to trash, similar to deleting them.",
"perma-delete-files-text": "Description text for the action of permanently deleting ZIM files.",
"clear-filter": "Represents the action of clearing the filters selected for a filter type.",
<<<<<<< HEAD
"language-searcher-placeholder": "Placeholder text displayed to indicate the ZIM file searcher will filter by language",
"category-searcher-placeholder": "Placeholder text displayed to indicate the ZIM file searcher will filter by category",
"content-type-searcher-placeholder": "Placeholder text displayed to indicate the ZIM file searcher will filter by content type",
Expand Down
3 changes: 3 additions & 0 deletions resources/icons/drop-down.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions resources/kiwix.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,6 @@
<file>icons/more-vertical.svg</file>
<file>icons/caret-left-solid.svg</file>
<file>icons/stop-circle.svg</file>
<file>icons/drop-down.svg</file>
</qresource>
</RCC>
10 changes: 10 additions & 0 deletions src/kiwixapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,11 @@ void KiwixApp::saveWindowState()
mp_session->setValue("windowState", getMainWindow()->saveState());
}

void KiwixApp::saveVoiceName(const QString& langName, const QString& voiceName)
{
mp_session->setValue(langName + "/voice", voiceName);
}

void KiwixApp::restoreWindowState()
{
getMainWindow()->restoreGeometry(mp_session->value("geometry").toByteArray());
Expand All @@ -566,3 +571,8 @@ QString KiwixApp::getPrevSaveDir() const
QDir dir(prevSaveDir);
return dir.exists() ? prevSaveDir : DEFAULT_SAVE_DIR;
}

QString KiwixApp::getSavedVoiceName(const QString& langName) const
{
return mp_session->value(langName + "/voice", "").toString();
}
2 changes: 2 additions & 0 deletions src/kiwixapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,12 @@ class KiwixApp : public QtSingleApplication
QString parseStyleFromFile(QString filePath);
void saveListOfOpenTabs();
void saveWindowState();
void saveVoiceName(const QString& langName, const QString& voiceName);
void restoreWindowState();
void saveCurrentTabIndex();
void savePrevSaveDir(const QString& prevSaveDir);
QString getPrevSaveDir() const;
QString getSavedVoiceName(const QString& langName) const;

public slots:
void newTab();
Expand Down
2 changes: 2 additions & 0 deletions src/mainmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ MainMenu::MainMenu(QWidget *parent) :
m_editMenu.ADD_ACTION(FindInPageAction);
m_editMenu.ADD_ACTION(ToggleAddBookmarkAction);
m_editMenu.ADD_ACTION(OpenMultiZimAction);
#if defined(QT_TEXTTOSPEECH_LIB)
m_editMenu.ADD_ACTION(ReadArticleAction);
m_editMenu.ADD_ACTION(ReadTextAction);
#endif
addMenu(&m_editMenu);

m_viewMenu.setTitle(gt("view"));
Expand Down
87 changes: 72 additions & 15 deletions src/texttospeechbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
#include "ui_texttospeechbar.h"
#include <QDebug>

// QTextToSpeech TextToSpeechBar::m_speech;
TextToSpeechBar::TextToSpeechBar(QWidget *parent)
: QFrame(parent), mp_speech(new QTextToSpeech(this)),
: QFrame(parent),
mp_ui(new Ui::TextToSpeechBar)
{
close();
mp_ui->setupUi(this);
mp_ui->stopButton->setText(gt("stop"));
mp_ui->voiceLabel->setText(gt("voice"));
connect(mp_speech, &QTextToSpeech::stateChanged, this,
connect(&m_speech, &QTextToSpeech::stateChanged, this,
&TextToSpeechBar::onStateChanged);
connect(mp_ui->stopButton, &QPushButton::released, this,
&TextToSpeechBar::stop);
Expand All @@ -20,8 +21,19 @@ TextToSpeechBar::TextToSpeechBar(QWidget *parent)

mp_ui->langLabel->setText(gt("language"));
mp_ui->langComboBox->setMaxVisibleItems(10);
mp_ui->langComboBox->setLineEdit(new ComboBoxLineEdit(mp_ui->langComboBox));
connect( mp_ui->langComboBox->lineEdit(), &QLineEdit::selectionChanged, mp_ui->langComboBox->lineEdit(), [=]()
{
mp_ui->langComboBox->lineEdit()->setSelection(0,0);
});

mp_ui->voiceComboBox->setLineEdit(new ComboBoxLineEdit(mp_ui->langComboBox));
connect( mp_ui->voiceComboBox->lineEdit(), &QLineEdit::selectionChanged, mp_ui->voiceComboBox->lineEdit(), [=]()
{
mp_ui->voiceComboBox->lineEdit()->setSelection(0,0);
});
QLocale current = QLocale::system().language();
for (auto locale : mp_speech->availableLocales())
for (auto locale : m_speech.availableLocales())
{
QString name(QString("%1 (%2)")
.arg(QLocale::languageToString(locale.language()))
Expand All @@ -36,20 +48,34 @@ TextToSpeechBar::TextToSpeechBar(QWidget *parent)
mp_ui->langComboBox->lineEdit()->setReadOnly(true);
mp_ui->langComboBox->lineEdit()->setFrame(false);

connect(mp_ui->langComboBox, &QComboBox::currentIndexChanged,
connect(mp_ui->langComboBox, QOverload<int>::of(&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)
{
mp_speech->say(text);
m_text = text;
m_speech.say(text);
}

void TextToSpeechBar::stop()
{
mp_speech->stop();
m_speech.stop();
}

void TextToSpeechBar::setLocale(QLocale locale)
{
for (int i = 0; i < mp_ui->langComboBox->count(); i++)
{
if (mp_ui->langComboBox->itemData(i).toLocale().language() == locale.language())
{
mp_ui->langComboBox->setCurrentIndex(i);
languageSelected(i);
return;
}
}
}

void TextToSpeechBar::onStateChanged(QTextToSpeech::State state)
Expand All @@ -60,25 +86,49 @@ void TextToSpeechBar::onStateChanged(QTextToSpeech::State state)
void TextToSpeechBar::languageSelected(int index)
{
QLocale locale = mp_ui->langComboBox->itemData(index).toLocale();
disconnect(mp_ui->voiceComboBox, &QComboBox::currentIndexChanged, this, &TextToSpeechBar::voiceSelected);
disconnect(mp_ui->voiceComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &TextToSpeechBar::voiceSelected);
mp_ui->voiceComboBox->clear();

m_voices = mp_speech->availableVoices();
QVoice currentVoice = mp_speech->voice();
m_speech.setLocale(locale);
m_voices = m_speech.availableVoices();
mp_ui->voiceComboBox->setMaxVisibleItems(10);
QVoice currentVoice = m_speech.voice();
QString savedVoiceName = KiwixApp::instance()->getSavedVoiceName(locale.name());
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);

int voiceIndex = 0;
for (int i = 0; i < mp_ui->voiceComboBox->count(); i++)
{
if (m_voices[i].name() == currentVoice.name())
voiceIndex = i;
if (m_voices[i].name() == savedVoiceName)
{
voiceIndex = i;
break;
}
}
mp_ui->voiceComboBox->setCurrentIndex(voiceIndex);
voiceSelected(voiceIndex);

mp_ui->voiceComboBox->lineEdit()->setReadOnly(true);
mp_ui->voiceComboBox->lineEdit()->setFrame(false);
connect(mp_ui->voiceComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &TextToSpeechBar::voiceSelected);
}

void TextToSpeechBar::voiceSelected(int index)
{
mp_speech->setVoice(m_voices.at(index));
{
auto voice = m_voices.at(index);
auto currentLang = mp_ui->langComboBox->currentData().toLocale().name();
KiwixApp::instance()->saveVoiceName(currentLang, voice.name());

m_speech.setVoice(voice);
if (m_speech.state() == QTextToSpeech::Speaking)
speak(m_text);
}

void TextToSpeechBar::speechBarClose()
Expand All @@ -89,6 +139,13 @@ void TextToSpeechBar::speechBarClose()
return;
current->setFocus();

mp_speech->stop();
m_speech.stop();
close();
}

void ComboBoxLineEdit::mouseReleaseEvent(QMouseEvent *)
{
QComboBox* combo = dynamic_cast<QComboBox*>(parent());
if(combo)
combo->showPopup();
}
13 changes: 12 additions & 1 deletion src/texttospeechbar.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@

#include <QTextToSpeech>
#include <QFrame>
#include <QLineEdit>

namespace Ui {
class TextToSpeechBar;
}

class ComboBoxLineEdit : public QLineEdit
{
public:
ComboBoxLineEdit(QWidget *parent = 0) : QLineEdit(parent){}
protected:
void mouseReleaseEvent(QMouseEvent *);
};

class TextToSpeechBar : public QFrame
{
Q_OBJECT
Expand All @@ -16,6 +25,7 @@ class TextToSpeechBar : public QFrame

void speak(const QString& text);
void stop();
void setLocale(QLocale locale);

public slots:
void speechBarClose();
Expand All @@ -24,9 +34,10 @@ public slots:
void onStateChanged(QTextToSpeech::State state);

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

#endif // TEXTTOSPEECHMANAGER_H
18 changes: 1 addition & 17 deletions src/texttospeechbar.ui
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,6 @@
</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>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="stopButton">
<property name="enabled">
Expand All @@ -79,7 +63,7 @@
</sizepolicy>
</property>
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;&lt;br/&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
<string/>
</property>
<property name="text">
<string/>
Expand Down
2 changes: 2 additions & 0 deletions src/webview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,12 @@ void WebView::contextMenuEvent(QContextMenuEvent *event)
menu = createLinkContextMenu();
}

#if defined(QT_TEXTTOSPEECH_LIB)
auto app = KiwixApp::instance();
if (page()->hasSelection())
menu->addAction(app->getAction(KiwixApp::ReadTextAction));
menu->addAction(app->getAction(KiwixApp::ReadArticleAction));
#endif

menu->exec(event->globalPos());
}
Expand Down
Loading

0 comments on commit 6fb2227

Please sign in to comment.