unify event handling for zooming, search

This commit is contained in:
Martin Rotter 2022-03-29 11:16:13 +02:00
parent 8daa37badd
commit 1787368ee1
7 changed files with 54 additions and 95 deletions

View File

@ -35,6 +35,8 @@ LiteHtmlViewer::LiteHtmlViewer(QWidget* parent) : QLiteHtmlWidget(parent), m_dow
} }
void LiteHtmlViewer::bindToBrowser(WebBrowser* browser) { void LiteHtmlViewer::bindToBrowser(WebBrowser* browser) {
installEventFilter(browser);
browser->m_actionBack = new QAction(this); browser->m_actionBack = new QAction(this);
browser->m_actionForward = new QAction(this); browser->m_actionForward = new QAction(this);
browser->m_actionReload = new QAction(this); browser->m_actionReload = new QAction(this);
@ -319,21 +321,6 @@ void LiteHtmlViewer::showContextMenu(const QPoint& pos, const QUrl& url) {
m_contextMenu->popup(mapToGlobal(pos)); m_contextMenu->popup(mapToGlobal(pos));
} }
void LiteHtmlViewer::wheelEvent(QWheelEvent* event) {
if ((event->modifiers() & Qt::KeyboardModifier::ControlModifier) > 0) {
if (event->angleDelta().y() > 0 && canZoomIn()) {
zoomIn();
emit zoomFactorChanged();
}
else if (event->angleDelta().y() < 0 && canZoomOut()) {
zoomOut();
emit zoomFactorChanged();
}
}
QLiteHtmlWidget::wheelEvent(event);
}
BlockingResult LiteHtmlViewer::blockedWithAdblock(const QUrl& url) { BlockingResult LiteHtmlViewer::blockedWithAdblock(const QUrl& url) {
AdblockRequestInfo block_request(url); AdblockRequestInfo block_request(url);

View File

@ -51,7 +51,6 @@ class LiteHtmlViewer : public QLiteHtmlWidget, public WebViewer {
protected: protected:
virtual void keyPressEvent(QKeyEvent* event); virtual void keyPressEvent(QKeyEvent* event);
virtual void wheelEvent(QWheelEvent* event);
private: private:
BlockingResult blockedWithAdblock(const QUrl& url); BlockingResult blockedWithAdblock(const QUrl& url);

View File

@ -115,7 +115,7 @@ void MessagePreviewer::loadMessage(const Message& message, RootItem* root) {
m_txtMessage->loadUrl(m_message.m_url); m_txtMessage->loadUrl(m_message.m_url);
} }
else { else {
m_txtMessage->loadMessage(message, m_root); m_txtMessage->loadMessages({ message }, m_root);
} }
} }
} }

View File

@ -122,28 +122,6 @@ void WebBrowser::onZoomFactorChanged() {
qApp->settings()->setValue(GROUP(Messages), Messages::Zoom, m_webView->zoomFactor()); qApp->settings()->setValue(GROUP(Messages), Messages::Zoom, m_webView->zoomFactor());
} }
void WebBrowser::increaseZoom() {
if (m_webView->canZoomIn()) {
m_webView->zoomIn();
qApp->settings()->setValue(GROUP(Messages), Messages::Zoom, m_webView->zoomFactor());
}
}
void WebBrowser::decreaseZoom() {
if (m_webView->canZoomOut()) {
m_webView->zoomOut();
qApp->settings()->setValue(GROUP(Messages), Messages::Zoom, m_webView->zoomFactor());
}
}
void WebBrowser::resetZoom() {
m_webView->setZoomFactor(1.0f);
qApp->settings()->setValue(GROUP(Messages), Messages::Zoom, m_webView->zoomFactor());
}
void WebBrowser::clear(bool also_hide) { void WebBrowser::clear(bool also_hide) {
m_webView->clear(); m_webView->clear();
m_messages.clear(); m_messages.clear();
@ -170,10 +148,6 @@ void WebBrowser::loadMessages(const QList<Message>& messages, RootItem* root) {
} }
} }
void WebBrowser::loadMessage(const Message& message, RootItem* root) {
loadMessages({ message }, root);
}
void WebBrowser::readabilePage() { void WebBrowser::readabilePage() {
m_actionReadabilePage->setEnabled(false); m_actionReadabilePage->setEnabled(false);
qApp->web()->readability()->makeHtmlReadable(m_webView->html(), m_webView->url().toString()); qApp->web()->readability()->makeHtmlReadable(m_webView->html(), m_webView->url().toString());
@ -182,15 +156,58 @@ void WebBrowser::readabilePage() {
bool WebBrowser::eventFilter(QObject* watched, QEvent* event) { bool WebBrowser::eventFilter(QObject* watched, QEvent* event) {
Q_UNUSED(watched) Q_UNUSED(watched)
if (event->type() == QEvent::KeyPress) { if (event->type() == QEvent::Type::Wheel) {
QWheelEvent* wh_event = static_cast<QWheelEvent*>(event);
// Zoom with mouse.
if ((wh_event->modifiers() & Qt::KeyboardModifier::ControlModifier) > 0) {
if (wh_event->angleDelta().y() > 0 && m_webView->canZoomIn()) {
m_webView->zoomIn();
onZoomFactorChanged();
return true;
}
else if (wh_event->angleDelta().y() < 0 && m_webView->canZoomOut()) {
m_webView->zoomOut();
onZoomFactorChanged();
return true;
}
}
}
else if (event->type() == QEvent::KeyPress) {
QKeyEvent* key_event = static_cast<QKeyEvent*>(event); QKeyEvent* key_event = static_cast<QKeyEvent*>(event);
// Find text.
if (key_event->matches(QKeySequence::StandardKey::Find)) { if (key_event->matches(QKeySequence::StandardKey::Find)) {
m_searchWidget->clear(); m_searchWidget->clear();
m_searchWidget->show(); m_searchWidget->show();
m_searchWidget->setFocus(); m_searchWidget->setFocus();
return true; return true;
} }
// Hide visible search box.
if (key_event->key() == Qt::Key::Key_Escape && m_searchWidget->isVisible()) {
m_searchWidget->hide();
return true;
}
// Zoom with keyboard.
if ((key_event->modifiers() & Qt::KeyboardModifier::ControlModifier) > 0) {
if (key_event->key() == Qt::Key::Key_Plus && m_webView->canZoomIn()) {
m_webView->zoomIn();
onZoomFactorChanged();
return true;
}
else if (key_event->key() == Qt::Key::Key_Minus && m_webView->canZoomOut()) {
m_webView->zoomOut();
onZoomFactorChanged();
return true;
}
else if (key_event->key() == Qt::Key::Key_0) {
m_webView->setZoomFactor(1.0f);
onZoomFactorChanged();
return true;
}
}
} }
return false; return false;

View File

@ -42,15 +42,11 @@ class WebBrowser : public TabContent {
public slots: public slots:
void reloadFontSettings(); void reloadFontSettings();
void increaseZoom();
void decreaseZoom();
void resetZoom();
void clear(bool also_hide); void clear(bool also_hide);
void loadUrl(const QString& url); void loadUrl(const QString& url);
void loadUrl(const QUrl& url); void loadUrl(const QUrl& url);
void loadMessages(const QList<Message>& messages, RootItem* root); void loadMessages(const QList<Message>& messages, RootItem* root);
void loadMessage(const Message& message, RootItem* root);
void setNavigationBarVisible(bool visible); void setNavigationBarVisible(bool visible);
protected: protected:

View File

@ -29,7 +29,7 @@
#include <QWebEngineProfile> #include <QWebEngineProfile>
#include <QWheelEvent> #include <QWheelEvent>
WebEngineViewer::WebEngineViewer(QWidget* parent) : QWebEngineView(parent), m_root(nullptr) { WebEngineViewer::WebEngineViewer(QWidget* parent) : QWebEngineView(parent), m_browser(nullptr), m_root(nullptr) {
WebEnginePage* page = new WebEnginePage(this); WebEnginePage* page = new WebEnginePage(this);
setPage(page); setPage(page);
@ -40,8 +40,8 @@ bool WebEngineViewer::event(QEvent* event) {
QChildEvent* child_ev = static_cast<QChildEvent*>(event); QChildEvent* child_ev = static_cast<QChildEvent*>(event);
QWidget* w = qobject_cast<QWidget*>(child_ev->child()); QWidget* w = qobject_cast<QWidget*>(child_ev->child());
if (w != nullptr) { if (w != nullptr && m_browser != nullptr) {
w->installEventFilter(this); w->installEventFilter(m_browser);
} }
} }
@ -227,49 +227,6 @@ QWebEngineView* WebEngineViewer::createWindow(QWebEnginePage::WebWindowType type
} }
} }
bool WebEngineViewer::eventFilter(QObject* object, QEvent* event) {
Q_UNUSED(object)
if (event->type() == QEvent::Type::Wheel) {
QWheelEvent* wh_event = static_cast<QWheelEvent*>(event);
if ((wh_event->modifiers() & Qt::KeyboardModifier::ControlModifier) > 0) {
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;
}
}
}
else if (event->type() == QEvent::Type::KeyPress) {
QKeyEvent* key_event = static_cast<QKeyEvent*>(event);
if ((key_event->modifiers() & Qt::KeyboardModifier::ControlModifier) > 0) {
if (key_event->key() == Qt::Key::Key_Plus) {
zoomIn();
return true;
}
else if (key_event->key() == Qt::Key::Key_Minus) {
zoomOut();
return true;
}
else if (key_event->key() == Qt::Key::Key_0) {
setZoomFactor(1.0f);
return true;
}
}
}
return false;
}
void WebEngineViewer::openUrlWithExternalTool(ExternalTool tool, const QString& target_url) { void WebEngineViewer::openUrlWithExternalTool(ExternalTool tool, const QString& target_url) {
tool.run(target_url); tool.run(target_url);
} }
@ -279,6 +236,8 @@ RootItem* WebEngineViewer::root() const {
} }
void WebEngineViewer::bindToBrowser(WebBrowser* browser) { void WebEngineViewer::bindToBrowser(WebBrowser* browser) {
m_browser = browser;
browser->m_actionBack = pageAction(QWebEnginePage::WebAction::Back); browser->m_actionBack = pageAction(QWebEnginePage::WebAction::Back);
browser->m_actionForward = pageAction(QWebEnginePage::WebAction::Forward); browser->m_actionForward = pageAction(QWebEnginePage::WebAction::Forward);
browser->m_actionReload = pageAction(QWebEnginePage::WebAction::Reload); browser->m_actionReload = pageAction(QWebEnginePage::WebAction::Reload);

View File

@ -12,6 +12,7 @@
#include "network-web/webengine/webenginepage.h" #include "network-web/webengine/webenginepage.h"
class RootItem; class RootItem;
class WebBrowser;
class WebEngineViewer : public QWebEngineView, public WebViewer { class WebEngineViewer : public QWebEngineView, public WebViewer {
Q_OBJECT Q_OBJECT
@ -47,7 +48,6 @@ class WebEngineViewer : public QWebEngineView, public WebViewer {
virtual QWebEngineView* createWindow(QWebEnginePage::WebWindowType type); virtual QWebEngineView* createWindow(QWebEnginePage::WebWindowType type);
virtual void contextMenuEvent(QContextMenuEvent* event); virtual void contextMenuEvent(QContextMenuEvent* event);
virtual bool event(QEvent* event); virtual bool event(QEvent* event);
virtual bool eventFilter(QObject* object, QEvent* event);
private slots: private slots:
void displayMessage(); void displayMessage();
@ -57,6 +57,7 @@ class WebEngineViewer : public QWebEngineView, public WebViewer {
WebEnginePage* page() const; WebEnginePage* page() const;
private: private:
WebBrowser* m_browser;
RootItem* m_root; RootItem* m_root;
QString m_messageBaseUrl; QString m_messageBaseUrl;
QString m_messageContents; QString m_messageContents;