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 Oct 2, 2024
1 parent 9349ef5 commit 619be31
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 43 deletions.
2 changes: 2 additions & 0 deletions debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Build-Depends: debhelper-compat (= 13),
pkg-config,
qtbase5-dev,
qtwebengine5-dev,
libqt5texttospeech5-dev,
qtspeech5-speechd-plugin,
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 @@ -359,28 +359,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 @@ -152,7 +152,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>
2 changes: 2 additions & 0 deletions src/mainmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ MainMenu::MainMenu(QWidget *parent) :
m_editMenu.ADD_ACTION(SearchLibraryAction);
m_editMenu.ADD_ACTION(FindInPageAction);
m_editMenu.ADD_ACTION(ToggleAddBookmarkAction);
#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
51 changes: 39 additions & 12 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 @@ -21,7 +22,7 @@ TextToSpeechBar::TextToSpeechBar(QWidget *parent)
mp_ui->langLabel->setText(gt("language"));
mp_ui->langComboBox->setMaxVisibleItems(10);
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,49 +37,75 @@ 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)
{
mp_ui->stopButton->setEnabled(state != QTextToSpeech::Ready);
if (state == QTextToSpeech::Ready)
m_text.clear();
}

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();
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);
voiceSelected(mp_ui->voiceComboBox->count() - 1);
}
}
connect(mp_ui->voiceComboBox, &QComboBox::currentIndexChanged, this, &TextToSpeechBar::voiceSelected);
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));
qInfo() << index;
m_speech.setVoice(m_voices.at(index));
if (m_speech.state() == QTextToSpeech::Speaking)
m_speech.say(m_text);
}

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

mp_speech->stop();
m_speech.stop();
close();
}
4 changes: 3 additions & 1 deletion src/texttospeechbar.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class TextToSpeechBar : public QFrame

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

public slots:
void speechBarClose();
Expand All @@ -24,9 +25,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 @@ -244,10 +244,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
22 changes: 18 additions & 4 deletions src/zimview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
#include <QAction>
#include <QVBoxLayout>
#include <QToolTip>
#if defined(QT_TEXTTOSPEECH_LIB) && QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
#include <kiwix/tools.h>
#if defined(QT_TEXTTOSPEECH_LIB)
#include "texttospeechbar.h"
#endif

Expand All @@ -16,7 +17,7 @@ ZimView::ZimView(TabBar *tabBar, QWidget *parent)
mp_webView = new WebView();
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(mp_webView);
#if defined(QT_TEXTTOSPEECH_LIB) && QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
#if defined(QT_TEXTTOSPEECH_LIB)
mp_ttsBar = new TextToSpeechBar(this);
layout->addWidget(mp_ttsBar);
connect(app->getAction(KiwixApp::ReadArticleAction), &QAction::triggered,
Expand All @@ -33,17 +34,30 @@ ZimView::ZimView(TabBar *tabBar, QWidget *parent)
return;
mp_ttsBar->speak(mp_webView->page()->selectedText());
});
connect(app->getAction(KiwixApp::ReadTextAction), &QAction::triggered, [=](){
connect(app->getAction(KiwixApp::ReadTextAction), &QAction::triggered, this, [=](){
if (mp_tabBar->currentZimView() != this)
return;
if (this->getWebView()->page()->hasSelection())
mp_ttsBar->show();
});
connect(app->getAction(KiwixApp::ReadArticleAction), &QAction::triggered, [=](){
connect(app->getAction(KiwixApp::ReadArticleAction), &QAction::triggered, this, [=](){
if (mp_tabBar->currentZimView() == this)
mp_ttsBar->show();
});
connect(mp_webView, &WebView::zimIdChanged, mp_ttsBar, [=]{
try {
auto book = app->getLibrary()->getBookById(mp_tabBar->currentZimId());
auto iso2 = QString::fromStdString(book.getLanguages().at(0)).chopped(1);
auto iso3 = QString::fromStdString(book.getLanguages().at(0));

/* Try both 3 letter and two letter name. */
auto cLocale = QLocale(QLocale::C);
auto locale = QLocale(iso2).language() == cLocale.language() ? QLocale(iso3) : QLocale(iso2);
mp_ttsBar->setLocale(locale);
} catch (...) { /* Blank */ }
});
#endif

layout->addWidget(mp_findInPageBar);
layout->setContentsMargins(0,0,0,0);
layout->setSpacing(0);
Expand Down
4 changes: 2 additions & 2 deletions src/zimview.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <QWebEnginePage>

class FindInPageBar;
#if defined(QT_TEXTTOSPEECH_LIB) && QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
#if defined(QT_TEXTTOSPEECH_LIB)
class TextToSpeechBar;
#endif
class TabBar;
Expand All @@ -28,7 +28,7 @@ class ZimView : public QWidget
WebView *mp_webView;
TabBar *mp_tabBar;
FindInPageBar *mp_findInPageBar;
#if defined(QT_TEXTTOSPEECH_LIB) && QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
#if defined(QT_TEXTTOSPEECH_LIB)
TextToSpeechBar *mp_ttsBar;
#endif
};
Expand Down

0 comments on commit 619be31

Please sign in to comment.