From 4e933f1f036cc507e24c97c21d25cd5ddfb16f2f Mon Sep 17 00:00:00 2001 From: ShaopengLin Date: Wed, 25 Sep 2024 15:21:18 -0400 Subject: [PATCH] Introduce KiwixWebChannelObject.{h, cpp} Pass TOC title translation. More to come later --- kiwix-desktop.pro | 4 +++- resources/js/toc.js | 14 ++++++++++---- src/kiwixwebchannelobject.cpp | 7 +++++++ src/kiwixwebchannelobject.h | 16 ++++++++++++++++ src/kprofile.cpp | 2 ++ src/webpage.cpp | 9 +++++++++ 6 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 src/kiwixwebchannelobject.cpp create mode 100644 src/kiwixwebchannelobject.h diff --git a/kiwix-desktop.pro b/kiwix-desktop.pro index a8a16cb..416cf85 100644 --- a/kiwix-desktop.pro +++ b/kiwix-desktop.pro @@ -5,7 +5,7 @@ #------------------------------------------------- QT += core gui network -QT += webenginewidgets +QT += webenginewidgets webchannel QT += printsupport # Avoid stripping incompatible files, due to false identification as executables, on WSL @@ -85,6 +85,7 @@ SOURCES += \ src/fullscreenwindow.cpp \ src/fullscreennotification.cpp \ src/zimview.cpp \ + src/kiwixwebchannelobject.cpp \ HEADERS += \ src/choiceitem.h \ @@ -134,6 +135,7 @@ HEADERS += \ src/menuproxystyle.h \ src/zimview.h \ src/portutils.h \ + src/kiwixwebchannelobject.h \ FORMS += \ src/choiceitem.ui \ diff --git a/resources/js/toc.js b/resources/js/toc.js index ff3a85c..a9870fa 100644 --- a/resources/js/toc.js +++ b/resources/js/toc.js @@ -63,7 +63,6 @@ function setupTOC() var tocTitle = document.createElement('p'); tocTitle.id = "kiwix-toc-title"; - tocTitle.textContent = "Table of content"; var tocSideDiv = document.createElement('div'); tocSideDiv.id = "kiwix-toc-side"; @@ -73,6 +72,13 @@ function setupTOC() document.body.prepend(tocSideDiv); } -document.body.style.marginLeft = "310px"; -document.body.style.maxWidth = "calc(100vw - 310px)"; -setupTOC(); +new QWebChannel(qt.webChannelTransport, function(channel) { + + var kiwixObj = channel.objects.kiwixChannelObj + document.body.style.marginLeft = "310px"; + document.body.style.maxWidth = "calc(100vw - 310px)"; + setupTOC(); + + document.getElementById("kiwix-toc-title").textContent = kiwixObj.tocTitle; +}); + diff --git a/src/kiwixwebchannelobject.cpp b/src/kiwixwebchannelobject.cpp new file mode 100644 index 0000000..bb48935 --- /dev/null +++ b/src/kiwixwebchannelobject.cpp @@ -0,0 +1,7 @@ +#include "kiwixwebchannelobject.h" +#include "kiwixapp.h" + +QString KiwixWebChannelObject::getTocTitle() const +{ + return gt("table-of-content"); +} diff --git a/src/kiwixwebchannelobject.h b/src/kiwixwebchannelobject.h new file mode 100644 index 0000000..6be3113 --- /dev/null +++ b/src/kiwixwebchannelobject.h @@ -0,0 +1,16 @@ +#ifndef KIWIXWEBCHANNELOBJECT_H +#define KIWIXWEBCHANNELOBJECT_H + +#include + +class KiwixWebChannelObject : public QObject +{ + Q_OBJECT +public: + explicit KiwixWebChannelObject(QObject *parent = nullptr) : QObject(parent) {}; + + Q_INVOKABLE QString getTocTitle() const; + Q_PROPERTY(QString tocTitle READ getTocTitle CONSTANT); +}; + +#endif // KIWIXWEBCHANNELOBJECT_H diff --git a/src/kprofile.cpp b/src/kprofile.cpp index bc1dbc0..8da8fe7 100644 --- a/src/kprofile.cpp +++ b/src/kprofile.cpp @@ -31,6 +31,8 @@ KProfile::KProfile(QObject *parent) : scripts()->insert(getScript("qrc:/js/toc.js")); scripts()->insert(getScript("qrc:/js/tocCSS.js")); + scripts()->insert(getScript("qrc:/qtwebchannel/qwebchannel.js", + QWebEngineScript::DocumentCreation)); } #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) diff --git a/src/webpage.cpp b/src/webpage.cpp index 8824710..553961d 100644 --- a/src/webpage.cpp +++ b/src/webpage.cpp @@ -5,6 +5,10 @@ #include #include "kiwixapp.h" #include +#include +#include + +#include "kiwixwebchannelobject.h" WebPage::WebPage(QObject *parent) : QWebEnginePage(KiwixApp::instance()->getProfile(), parent) @@ -12,6 +16,11 @@ WebPage::WebPage(QObject *parent) : action(QWebEnginePage::SavePage)->setVisible(false); action(QWebEnginePage::ViewSource)->setVisible(false); action(QWebEnginePage::Reload)->setVisible(false); + + QWebChannel *channel = new QWebChannel(this); + KiwixWebChannelObject *kiwixChannelObj = new KiwixWebChannelObject(this); + setWebChannel(channel, QWebEngineScript::UserWorld); + channel->registerObject("kiwixChannelObj", kiwixChannelObj); } bool WebPage::acceptNavigationRequest(const QUrl &url, QWebEnginePage::NavigationType /*type*/, bool /*isMainFrame*/)