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

View file

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

View file

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

View file

@ -115,7 +115,7 @@ void WebBrowser::reloadFontSettings() {
fon.fromString(qApp->settings()->value(GROUP(Messages), fon.fromString(qApp->settings()->value(GROUP(Messages),
SETTING(Messages::PreviewerFontStandard)).toString()); SETTING(Messages::PreviewerFontStandard)).toString());
m_webView->reloadFontSettings(fon); m_webView->applyFont(fon);
} }
void WebBrowser::onZoomFactorChanged() { 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) { bool WebEngineViewer::eventFilter(QObject* object, QEvent* event) {
Q_UNUSED(object) Q_UNUSED(object)
@ -241,11 +237,13 @@ bool WebEngineViewer::eventFilter(QObject* object, QEvent* event) {
if (wh_event->angleDelta().y() > 0 && canZoomIn()) { if (wh_event->angleDelta().y() > 0 && canZoomIn()) {
zoomIn(); zoomIn();
emit zoomFactorChanged(); emit zoomFactorChanged();
return true; return true;
} }
else if (wh_event->angleDelta().y() < 0 && canZoomOut()) { else if (wh_event->angleDelta().y() < 0 && canZoomOut()) {
zoomOut(); zoomOut();
emit zoomFactorChanged(); emit zoomFactorChanged();
return true; return true;
} }
} }
@ -287,12 +285,13 @@ void WebEngineViewer::bindToBrowser(WebBrowser* browser) {
browser->m_actionStop = pageAction(QWebEnginePage::WebAction::Stop); browser->m_actionStop = pageAction(QWebEnginePage::WebAction::Stop);
connect(this, &WebEngineViewer::zoomFactorChanged, browser, &WebBrowser::onZoomFactorChanged); connect(this, &WebEngineViewer::zoomFactorChanged, browser, &WebBrowser::onZoomFactorChanged);
connect(this, &WebEngineViewer::urlChanged, browser, &WebBrowser::updateUrl);
connect(this, &WebEngineViewer::loadStarted, browser, &WebBrowser::onLoadingStarted); connect(this, &WebEngineViewer::loadStarted, browser, &WebBrowser::onLoadingStarted);
connect(this, &WebEngineViewer::loadProgress, browser, &WebBrowser::onLoadingProgress); connect(this, &WebEngineViewer::loadProgress, browser, &WebBrowser::onLoadingProgress);
connect(this, &WebEngineViewer::loadFinished, browser, &WebBrowser::onLoadingFinished); connect(this, &WebEngineViewer::loadFinished, browser, &WebBrowser::onLoadingFinished);
connect(this, &WebEngineViewer::titleChanged, browser, &WebBrowser::onTitleChanged); connect(this, &WebEngineViewer::titleChanged, browser, &WebBrowser::onTitleChanged);
connect(this, &WebEngineViewer::iconChanged, browser, &WebBrowser::onIconChanged); connect(this, &WebEngineViewer::iconChanged, browser, &WebBrowser::onIconChanged);
connect(this, &WebEngineViewer::urlChanged, browser, &WebBrowser::updateUrl);
connect(page(), &WebEnginePage::windowCloseRequested, browser, &WebBrowser::closeRequested); connect(page(), &WebEnginePage::windowCloseRequested, browser, &WebBrowser::closeRequested);
connect(page(), &WebEnginePage::linkHovered, browser, &WebBrowser::onLinkHovered); 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)); 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(); auto pixel_size = QFontMetrics(fon).ascent();
QWebEngineProfile::defaultProfile()->settings()->setFontFamily(QWebEngineSettings::FontFamily::StandardFont, fon.family()); 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 void clear();
virtual double verticalScrollBarPosition() const; virtual double verticalScrollBarPosition() const;
virtual void setVerticalScrollBarPosition(double pos); virtual void setVerticalScrollBarPosition(double pos);
virtual void reloadFontSettings(const QFont& fon); virtual void applyFont(const QFont& fon);
virtual bool canZoomIn() const; virtual bool canZoomIn() const;
virtual bool canZoomOut() const; virtual bool canZoomOut() const;
virtual qreal zoomFactor() const; virtual qreal zoomFactor() const;
@ -46,7 +46,6 @@ class WebEngineViewer : public QWebEngineView, public WebViewer {
protected: protected:
virtual QWebEngineView* createWindow(QWebEnginePage::WebWindowType type); virtual QWebEngineView* createWindow(QWebEnginePage::WebWindowType type);
virtual void contextMenuEvent(QContextMenuEvent* event); virtual void contextMenuEvent(QContextMenuEvent* event);
virtual void wheelEvent(QWheelEvent* event);
virtual bool event(QEvent* event); virtual bool event(QEvent* event);
virtual bool eventFilter(QObject* object, QEvent* event); virtual bool eventFilter(QObject* object, QEvent* event);

View file

@ -6,11 +6,16 @@
class WebBrowser; class WebBrowser;
class RootItem; class RootItem;
// Abstract class to define interface for web viewers. // Interface for web/article viewers.
class WebViewer { class WebViewer {
public: public:
// Performs necessary steps to make viewer work with browser. // 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; virtual void bindToBrowser(WebBrowser* browser) = 0;
// Perform inline search. // Perform inline search.
@ -32,7 +37,7 @@ class WebViewer {
// Clears displayed URL. // Clears displayed URL.
virtual void clear() = 0; 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; virtual void loadMessages(const QList<Message>& messages, RootItem* root) = 0;
// Vertical scrollbar changer. // Vertical scrollbar changer.
@ -40,14 +45,14 @@ class WebViewer {
virtual void setVerticalScrollBarPosition(double pos) = 0; virtual void setVerticalScrollBarPosition(double pos) = 0;
// Apply font. // Apply font.
virtual void reloadFontSettings(const QFont& fon) = 0; virtual void applyFont(const QFont& fon) = 0;
// Zooming. // Zooming.
virtual bool canZoomIn() const = 0; virtual bool canZoomIn() const = 0;
virtual bool canZoomOut() const = 0; virtual bool canZoomOut() const = 0;
virtual qreal zoomFactor() const = 0;
virtual void zoomIn() = 0; virtual void zoomIn() = 0;
virtual void zoomOut() = 0; virtual void zoomOut() = 0;
virtual qreal zoomFactor() const = 0;
virtual void setZoomFactor(qreal zoom_factor) = 0; virtual void setZoomFactor(qreal zoom_factor) = 0;
}; };