some refactor

This commit is contained in:
Martin Rotter 2022-03-29 10:41:17 +02:00
parent e80331e88c
commit 8daa37badd
7 changed files with 36 additions and 26 deletions

View File

@ -26,7 +26,7 @@
<url type="donation">https://github.com/sponsors/martinrotter</url>
<content_rating type="oars-1.1" />
<releases>
<release version="4.2.1" date="2022-03-28"/>
<release version="4.2.1" date="2022-03-29"/>
</releases>
<content_rating type="oars-1.0">
<content_attribute id="violence-cartoon">none</content_attribute>

View File

@ -10,7 +10,6 @@
#include "miscellaneous/iconfactory.h"
#include "miscellaneous/skinfactory.h"
#include "network-web/adblock/adblockicon.h"
#include "network-web/adblock/adblockmanager.h"
#include "network-web/adblock/adblockrequestinfo.h"
#include "network-web/downloader.h"
#include "network-web/networkfactory.h"
@ -50,6 +49,7 @@ void LiteHtmlViewer::bindToBrowser(WebBrowser* browser) {
browser->m_actionStop->setEnabled(false);
connect(this, &LiteHtmlViewer::zoomFactorChanged, browser, &WebBrowser::onZoomFactorChanged);
connect(this, &LiteHtmlViewer::linkHighlighted, browser, [browser](const QUrl& url) {
browser->onLinkHovered(url.toString());
});
@ -69,11 +69,14 @@ void LiteHtmlViewer::findText(const QString& text, bool backwards) {
void LiteHtmlViewer::setUrl(const QUrl& url) {
emit loadStarted();
QString html_str;
QUrl nonconst_url = url;
bool is_error = false;
auto block_result = blockedWithAdblock(url);
if (blockedWithAdblock(url)) {
if (block_result.m_blocked) {
is_error = true;
html_str = tr("Site \"%1\" was blocked with AdBlock.").arg(url.toString());
nonconst_url = QUrl::fromUserInput(QSL(INTERNAL_URL_ADBLOCKED));
html_str = qApp->skins()->adBlockedPage(url.toString(), block_result.m_blockedByFilter);
}
else {
QEventLoop loop;
@ -97,7 +100,7 @@ void LiteHtmlViewer::setUrl(const QUrl& url) {
}
}
setHtml(html_str, url);
setHtml(html_str, nonconst_url);
emit loadFinished(is_error);
}
@ -205,7 +208,7 @@ void LiteHtmlViewer::setVerticalScrollBarPosition(double pos) {
verticalScrollBar()->setValue(pos);
}
void LiteHtmlViewer::reloadFontSettings(const QFont& fon) {
void LiteHtmlViewer::applyFont(const QFont& fon) {
QLiteHtmlWidget::setDefaultFont(fon);
}
@ -331,7 +334,7 @@ void LiteHtmlViewer::wheelEvent(QWheelEvent* event) {
QLiteHtmlWidget::wheelEvent(event);
}
bool LiteHtmlViewer::blockedWithAdblock(const QUrl& url) {
BlockingResult LiteHtmlViewer::blockedWithAdblock(const QUrl& url) {
AdblockRequestInfo block_request(url);
if (url.path().endsWith(QSL("css"))) {
@ -341,17 +344,19 @@ bool LiteHtmlViewer::blockedWithAdblock(const QUrl& url) {
block_request.setResourceType(QSL("image"));
}
if (qApp->web()->adBlock()->block(block_request).m_blocked) {
auto block_result = qApp->web()->adBlock()->block(block_request);
if (block_result.m_blocked) {
qWarningNN << LOGSEC_ADBLOCK << "Blocked request:" << QUOTE_W_SPACE_DOT(block_request.requestUrl().toString());
return true;
return block_result;
}
else {
return false;
return block_result;
}
}
QByteArray LiteHtmlViewer::handleResource(const QUrl& url) {
if (blockedWithAdblock(url)) {
if (blockedWithAdblock(url).m_blocked) {
return {};
}
else {

View File

@ -6,6 +6,8 @@
#include "3rd-party/qlitehtml/qlitehtmlwidget.h"
#include "gui/webviewer.h"
#include "network-web/adblock/adblockmanager.h"
class Downloader;
class QWheelEvent;
class QMenu;
@ -27,7 +29,7 @@ class LiteHtmlViewer : public QLiteHtmlWidget, public WebViewer {
virtual void loadMessages(const QList<Message>& messages, RootItem* root);
virtual double verticalScrollBarPosition() const;
virtual void setVerticalScrollBarPosition(double pos);
virtual void reloadFontSettings(const QFont& fon);
virtual void applyFont(const QFont& fon);
virtual bool canZoomIn() const;
virtual bool canZoomOut() const;
virtual qreal zoomFactor() const;
@ -52,7 +54,7 @@ class LiteHtmlViewer : public QLiteHtmlWidget, public WebViewer {
virtual void wheelEvent(QWheelEvent* event);
private:
bool blockedWithAdblock(const QUrl& url);
BlockingResult blockedWithAdblock(const QUrl& url);
QByteArray handleResource(const QUrl& url);
private:

View File

@ -115,7 +115,7 @@ void WebBrowser::reloadFontSettings() {
fon.fromString(qApp->settings()->value(GROUP(Messages),
SETTING(Messages::PreviewerFontStandard)).toString());
m_webView->reloadFontSettings(fon);
m_webView->applyFont(fon);
}
void WebBrowser::onZoomFactorChanged() {

View File

@ -227,10 +227,6 @@ QWebEngineView* WebEngineViewer::createWindow(QWebEnginePage::WebWindowType type
}
}
void WebEngineViewer::wheelEvent(QWheelEvent* event) {
QWebEngineView::wheelEvent(event);
}
bool WebEngineViewer::eventFilter(QObject* object, QEvent* event) {
Q_UNUSED(object)
@ -241,11 +237,13 @@ bool WebEngineViewer::eventFilter(QObject* object, QEvent* event) {
if (wh_event->angleDelta().y() > 0 && canZoomIn()) {
zoomIn();
emit zoomFactorChanged();
return true;
}
else if (wh_event->angleDelta().y() < 0 && canZoomOut()) {
zoomOut();
emit zoomFactorChanged();
return true;
}
}
@ -287,12 +285,13 @@ void WebEngineViewer::bindToBrowser(WebBrowser* browser) {
browser->m_actionStop = pageAction(QWebEnginePage::WebAction::Stop);
connect(this, &WebEngineViewer::zoomFactorChanged, browser, &WebBrowser::onZoomFactorChanged);
connect(this, &WebEngineViewer::urlChanged, browser, &WebBrowser::updateUrl);
connect(this, &WebEngineViewer::loadStarted, browser, &WebBrowser::onLoadingStarted);
connect(this, &WebEngineViewer::loadProgress, browser, &WebBrowser::onLoadingProgress);
connect(this, &WebEngineViewer::loadFinished, browser, &WebBrowser::onLoadingFinished);
connect(this, &WebEngineViewer::titleChanged, browser, &WebBrowser::onTitleChanged);
connect(this, &WebEngineViewer::iconChanged, browser, &WebBrowser::onIconChanged);
connect(this, &WebEngineViewer::urlChanged, browser, &WebBrowser::updateUrl);
connect(page(), &WebEnginePage::windowCloseRequested, browser, &WebBrowser::closeRequested);
connect(page(), &WebEnginePage::linkHovered, browser, &WebBrowser::onLinkHovered);
@ -332,7 +331,7 @@ void WebEngineViewer::setVerticalScrollBarPosition(double pos) {
page()->runJavaScript(QSL("window.scrollTo(0, %1);").arg(pos));
}
void WebEngineViewer::reloadFontSettings(const QFont& fon) {
void WebEngineViewer::applyFont(const QFont& fon) {
auto pixel_size = QFontMetrics(fon).ascent();
QWebEngineProfile::defaultProfile()->settings()->setFontFamily(QWebEngineSettings::FontFamily::StandardFont, fon.family());

View File

@ -30,7 +30,7 @@ class WebEngineViewer : public QWebEngineView, public WebViewer {
virtual void clear();
virtual double verticalScrollBarPosition() const;
virtual void setVerticalScrollBarPosition(double pos);
virtual void reloadFontSettings(const QFont& fon);
virtual void applyFont(const QFont& fon);
virtual bool canZoomIn() const;
virtual bool canZoomOut() const;
virtual qreal zoomFactor() const;
@ -46,7 +46,6 @@ class WebEngineViewer : public QWebEngineView, public WebViewer {
protected:
virtual QWebEngineView* createWindow(QWebEnginePage::WebWindowType type);
virtual void contextMenuEvent(QContextMenuEvent* event);
virtual void wheelEvent(QWheelEvent* event);
virtual bool event(QEvent* event);
virtual bool eventFilter(QObject* object, QEvent* event);

View File

@ -6,11 +6,16 @@
class WebBrowser;
class RootItem;
// Abstract class to define interface for web viewers.
// Interface for web/article viewers.
class WebViewer {
public:
// Performs necessary steps to make viewer work with browser.
// NOTE: Each implementor must do this in this method:
// 1. Initialize all WebBrowser QActions and maintain their "enabled" state all the time.
// 2. Connect to all slots of WebBrowser to ensure updating of title/icon, notifications
// of loading start/progress/finish, link highlight etc.
// 3. Viewer must set WebBrowser to be event filter at some point.
virtual void bindToBrowser(WebBrowser* browser) = 0;
// Perform inline search.
@ -32,7 +37,7 @@ class WebViewer {
// Clears displayed URL.
virtual void clear() = 0;
// Displays all messages;
// Displays all messages and ensures that vertical scrollbar is set to 0 (scrolled to top).
virtual void loadMessages(const QList<Message>& messages, RootItem* root) = 0;
// Vertical scrollbar changer.
@ -40,14 +45,14 @@ class WebViewer {
virtual void setVerticalScrollBarPosition(double pos) = 0;
// Apply font.
virtual void reloadFontSettings(const QFont& fon) = 0;
virtual void applyFont(const QFont& fon) = 0;
// Zooming.
virtual bool canZoomIn() const = 0;
virtual bool canZoomOut() const = 0;
virtual qreal zoomFactor() const = 0;
virtual void zoomIn() = 0;
virtual void zoomOut() = 0;
virtual qreal zoomFactor() const = 0;
virtual void setZoomFactor(qreal zoom_factor) = 0;
};