Introduce KiwixWebChannelObject.{h, cpp}

Pass TOC title translation. More to come later
This commit is contained in:
ShaopengLin 2024-09-25 15:21:18 -04:00
parent 9d42ddfdc3
commit 4e933f1f03
6 changed files with 47 additions and 5 deletions

View File

@ -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 \

View File

@ -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;
});

View File

@ -0,0 +1,7 @@
#include "kiwixwebchannelobject.h"
#include "kiwixapp.h"
QString KiwixWebChannelObject::getTocTitle() const
{
return gt("table-of-content");
}

View File

@ -0,0 +1,16 @@
#ifndef KIWIXWEBCHANNELOBJECT_H
#define KIWIXWEBCHANNELOBJECT_H
#include <QObject>
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

View File

@ -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)

View File

@ -5,6 +5,10 @@
#include <QMessageBox>
#include "kiwixapp.h"
#include <QWebEngineProfile>
#include <QWebChannel>
#include <QWebEngineScript>
#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*/)