Skip to content

Commit

Permalink
Disable TTS If No Such QT Module
Browse files Browse the repository at this point in the history
Some systems or QT versions might not have or the module is not available.
  • Loading branch information
ShaopengLin committed Nov 14, 2024
1 parent 6ee240f commit 7b629ff
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 10 deletions.
11 changes: 7 additions & 4 deletions kiwix-desktop.pro
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
QT += core gui network
QT += webenginewidgets webchannel
QT += printsupport
QT += texttospeech
qtHaveModule(texttospeech): QT += texttospeech

# Avoid stripping incompatible files, due to false identification as executables, on WSL
DETECT_WSL = $$system(test -f /proc/sys/fs/binfmt_misc/WSLInterop && echo true || echo false)
Expand Down Expand Up @@ -94,7 +94,8 @@ SOURCES += \
src/fullscreennotification.cpp \
src/zimview.cpp \
src/multizimbutton.cpp \
src/texttospeechbar.cpp \

qtHaveModule(texttospeech): SOURCES += src/texttospeechbar.cpp \

HEADERS += \
src/choiceitem.h \
Expand Down Expand Up @@ -150,7 +151,8 @@ HEADERS += \
src/css_constants.h \
src/multizimbutton.h \
src/kiwixwebchannelobject.h \
src/texttospeechbar.h \

qtHaveModule(texttospeech): HEADERS += src/texttospeechbar.h \

FORMS += \
src/choiceitem.ui \
Expand All @@ -165,7 +167,8 @@ FORMS += \
ui/localkiwixserver.ui \
ui/settings.ui \
src/tableofcontentbar.ui \
src/texttospeechbar.ui \

qtHaveModule(texttospeech): FORMS += src/texttospeechbar.ui \

include(subprojects/QtSingleApplication/src/qtsingleapplication.pri)
CODECFORSRC = UTF-8
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
2 changes: 2 additions & 0 deletions src/webview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,12 @@ void WebView::contextMenuEvent(QContextMenuEvent *event)
menu = createLinkContextMenu();
}

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

menu->exec(event->globalPos());
}
Expand Down
21 changes: 15 additions & 6 deletions src/zimview.cpp
Original file line number Diff line number Diff line change
@@ -1,29 +1,37 @@
#include "zimview.h"
#include "kiwixapp.h"
#include "texttospeechbar.h"
#include <QAction>
#include <QVBoxLayout>
#include <QToolTip>

#if defined(QT_TEXTTOSPEECH_LIB)
#include "texttospeechbar.h"
#endif

ZimView::ZimView(TabBar *tabBar, QWidget *parent)
: QWidget(parent),
mp_tabBar(tabBar),
mp_findInPageBar(new FindInPageBar(this)),
mp_ttsBar(new TextToSpeechBar(this))
mp_findInPageBar(new FindInPageBar(this))
{
mp_webView = new WebView();
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(mp_webView);
layout->addWidget(mp_findInPageBar);
layout->addWidget(mp_ttsBar);
layout->setContentsMargins(0,0,0,0);
layout->setSpacing(0);
setLayout(layout); // now 'mp_webView' has 'this' as the parent QObject
mp_findInPageBar->hide();
mp_ttsBar->hide();
auto app = KiwixApp::instance();

#if defined(QT_TEXTTOSPEECH_LIB)
mp_ttsBar = new TextToSpeechBar(this);
layout->addWidget(mp_ttsBar);
mp_ttsBar->hide();
connect(mp_webView, &WebView::zimIdChanged, this, &ZimView::setSpeechLocaleByZimId);
connect(app->getAction(KiwixApp::ReadArticleAction), &QAction::triggered, this, &ZimView::readArticle);
connect(app->getAction(KiwixApp::ReadTextAction), &QAction::triggered, this, &ZimView::readSelectedText);
#endif

connect(app->getAction(KiwixApp::ZoomInAction), &QAction::triggered,
this, [=]() {
if (mp_tabBar->currentZimView() != this)
Expand Down Expand Up @@ -101,7 +109,6 @@ ZimView::ZimView(TabBar *tabBar, QWidget *parent)
QToolTip::showText(pos, link);
}
});
connect(mp_webView, &WebView::zimIdChanged, this, &ZimView::setSpeechLocaleByZimId);
}

void ZimView::openFindInPageBar()
Expand All @@ -110,6 +117,7 @@ void ZimView::openFindInPageBar()
mp_findInPageBar->getFindLineEdit()->setFocus();
}

#if defined(QT_TEXTTOSPEECH_LIB)
void ZimView::readArticle()
{
if (mp_tabBar->currentZimView() != this)
Expand Down Expand Up @@ -144,3 +152,4 @@ void ZimView::setSpeechLocaleByZimId(const QString& zimId)
mp_ttsBar->setLocale(isValidISO2 ? iso2Locale : QLocale(iso3));
} catch (...) { /* Blank */ }
}
#endif
8 changes: 8 additions & 0 deletions src/zimview.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
class FindInPageBar;
class TabBar;
class WebView;

#if defined(QT_TEXTTOSPEECH_LIB)
class TextToSpeechBar;
#endif

class ZimView : public QWidget
{
Expand All @@ -20,9 +23,11 @@ class ZimView : public QWidget
void openFindInPageBar();

public slots:
#if defined(QT_TEXTTOSPEECH_LIB)
void readArticle();
void readSelectedText();
void setSpeechLocaleByZimId(const QString& zimId);
#endif

signals:
void webActionEnabledChanged(QWebEnginePage::WebAction action, bool enabled);
Expand All @@ -31,7 +36,10 @@ public slots:
WebView *mp_webView;
TabBar *mp_tabBar;
FindInPageBar *mp_findInPageBar;

#if defined(QT_TEXTTOSPEECH_LIB)
TextToSpeechBar *mp_ttsBar;
#endif
};

#endif // ZIMVIEW_H

0 comments on commit 7b629ff

Please sign in to comment.