From 34816101e23ae2b489d64d540534125cf2c2e925 Mon Sep 17 00:00:00 2001 From: Soren Stoutner Date: Tue, 12 Apr 2022 14:41:29 -0700 Subject: [PATCH] Clear the URL line edit when navigating history. https://redmine.stoutner.com/issues/841 --- src/CMakeLists.txt | 2 +- src/filters/CMakeLists.txt | 22 +++++++++++++++++ src/{ => filters}/MouseEventFilter.cpp | 10 +++----- src/{ => filters}/MouseEventFilter.h | 11 +++++---- src/ui.rc/browser_ui.rc | 7 +++++- src/views/BrowserView.cpp | 34 ++++++++++++++++++++++++-- src/views/BrowserView.h | 3 +++ src/windows/BrowserWindow.cpp | 9 +++++++ src/windows/BrowserWindow.h | 1 + 9 files changed, 83 insertions(+), 16 deletions(-) create mode 100644 src/filters/CMakeLists.txt rename src/{ => filters}/MouseEventFilter.cpp (88%) rename src/{ => filters}/MouseEventFilter.h (89%) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8371822..4d33710 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -21,7 +21,6 @@ add_executable(privacy-browser resources.qrc) # List the sources to include in the executable. target_sources(privacy-browser PRIVATE main.cpp - MouseEventFilter.cpp ) # Add the Qt logging category. This will create the `debug.h` header file. @@ -62,6 +61,7 @@ target_link_libraries(privacy-browser # Add the subdirectories. add_subdirectory(dialogs) +add_subdirectory(filters) add_subdirectory(helpers) add_subdirectory(interceptors) add_subdirectory(ui.rc) diff --git a/src/filters/CMakeLists.txt b/src/filters/CMakeLists.txt new file mode 100644 index 0000000..c57b6ee --- /dev/null +++ b/src/filters/CMakeLists.txt @@ -0,0 +1,22 @@ +# Copyright © 2022 Soren Stoutner . +# +# This file is part of Privacy Browser PC . +# +# Privacy Browser PC is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Privacy Browser PC is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Privacy Browser PC. If not, see . + + +# List the sources to include in the executable. +target_sources(privacy-browser PRIVATE + MouseEventFilter.cpp +) diff --git a/src/MouseEventFilter.cpp b/src/filters/MouseEventFilter.cpp similarity index 88% rename from src/MouseEventFilter.cpp rename to src/filters/MouseEventFilter.cpp index 3454d06..51bbb09 100644 --- a/src/MouseEventFilter.cpp +++ b/src/filters/MouseEventFilter.cpp @@ -25,11 +25,7 @@ #include // The primary constructor. -MouseEventFilter::MouseEventFilter(QWebEngineView *webEngineView) : QObject() -{ - // Save a handle to the WebEngine view. - webEngineViewPointer = webEngineView; -}; +MouseEventFilter::MouseEventFilter() : QObject() {}; bool MouseEventFilter::eventFilter(QObject *objectPointer, QEvent *eventPointer) { @@ -47,14 +43,14 @@ bool MouseEventFilter::eventFilter(QObject *objectPointer, QEvent *eventPointer) { case (Qt::BackButton): // Tell the WebEngine to go back. - webEngineViewPointer->back(); + emit mouseBack(); // Consume the event. return true; case (Qt::ForwardButton): // Tell the WebEngine to go forward. - webEngineViewPointer->forward(); + emit mouseForward(); // Consume the event. return true; diff --git a/src/MouseEventFilter.h b/src/filters/MouseEventFilter.h similarity index 89% rename from src/MouseEventFilter.h rename to src/filters/MouseEventFilter.h index 92c015a..02e1b2d 100644 --- a/src/MouseEventFilter.h +++ b/src/filters/MouseEventFilter.h @@ -31,14 +31,15 @@ class MouseEventFilter : public QObject public: // The primary constructor. - MouseEventFilter(QWebEngineView *webEngineView); + MouseEventFilter(); + +signals: + // The signals. + void mouseBack() const; + void mouseForward() const; protected: // The protected functions. bool eventFilter(QObject *objectPointer, QEvent *eventPointer) override; - -private: - // The private variables. - QWebEngineView *webEngineViewPointer; }; #endif diff --git a/src/ui.rc/browser_ui.rc b/src/ui.rc/browser_ui.rc index 0e1d60b..4c42e77 100644 --- a/src/ui.rc/browser_ui.rc +++ b/src/ui.rc/browser_ui.rc @@ -29,6 +29,7 @@ + On-The-Fly Settings User Agent @@ -56,6 +57,11 @@ + + + + + @@ -73,6 +79,5 @@ URL Toolbar - diff --git a/src/views/BrowserView.cpp b/src/views/BrowserView.cpp index 2e29ea0..873b947 100644 --- a/src/views/BrowserView.cpp +++ b/src/views/BrowserView.cpp @@ -19,9 +19,9 @@ // Application headers. #include "BrowserView.h" -#include "MouseEventFilter.h" #include "Settings.h" #include "ui_BrowserView.h" +#include "filters/MouseEventFilter.h" #include "helpers/DomainsDatabaseHelper.h" #include "helpers/SearchEngineHelper.h" #include "helpers/UserAgentHelper.h" @@ -71,11 +71,15 @@ BrowserView::BrowserView(QWidget *parent) : QWidget(parent) connect(webEngineViewPointer, SIGNAL(loadFinished(const bool)), this, SLOT(loadFinished())); // Instantiate the mouse event filter pointer. - MouseEventFilter *mouseEventFilterPointer = new MouseEventFilter(webEngineViewPointer); + MouseEventFilter *mouseEventFilterPointer = new MouseEventFilter(); // Install the mouse event filter. qApp->installEventFilter(mouseEventFilterPointer); + // Process mouse forward and back commands. + connect(mouseEventFilterPointer, SIGNAL(mouseBack()), this, SLOT(mouseBack())); + connect(mouseEventFilterPointer, SIGNAL(mouseForward()), this, SLOT(mouseForward())); + // Listen for hovered link URLs. connect(webEnginePagePointer, SIGNAL(linkHovered(const QString)), this, SLOT(pageLinkHovered(const QString))); @@ -376,6 +380,32 @@ void BrowserView::loadUrlFromLineEdit(QString url) const } } +void BrowserView::mouseBack() const +{ + // Go back if possible. + if (webEngineHistoryPointer->canGoBack()) + { + // Clear the URL line edit focus. + emit clearUrlLineEditFocus(); + + // Go back. + webEngineViewPointer->back(); + } +} + +void BrowserView::mouseForward() const +{ + // Go forward if possible. + if (webEngineHistoryPointer->canGoForward()) + { + // Clear the URL line edit focus. + emit clearUrlLineEditFocus(); + + // Go forward. + webEngineViewPointer->forward(); + } +} + void BrowserView::pageLinkHovered(const QString &linkUrl) const { // Emit a signal so that the browser window can update the status bar. diff --git a/src/views/BrowserView.h b/src/views/BrowserView.h index d61ceae..29ae93c 100644 --- a/src/views/BrowserView.h +++ b/src/views/BrowserView.h @@ -49,6 +49,7 @@ public: signals: // The signals. + void clearUrlLineEditFocus() const; void hideProgressBar() const; void linkHovered(const QString &linkUrl) const; void showProgressBar(const int &progress) const; @@ -73,6 +74,8 @@ public Q_SLOTS: void forward() const; void home() const; void loadUrlFromLineEdit(QString url) const; + void mouseBack() const; + void mouseForward() const; void refresh() const; private Q_SLOTS: diff --git a/src/windows/BrowserWindow.cpp b/src/windows/BrowserWindow.cpp index a29b503..3ddba02 100644 --- a/src/windows/BrowserWindow.cpp +++ b/src/windows/BrowserWindow.cpp @@ -218,6 +218,9 @@ BrowserWindow::BrowserWindow() : KXmlGuiWindow() connect(browserViewPointer, SIGNAL(showProgressBar(const int)), this, SLOT(showProgressBar(const int))); connect(browserViewPointer, SIGNAL(hideProgressBar()), progressBarPointer, SLOT(hide())); + // Clear the URL line edit focus when requested. + connect(browserViewPointer, SIGNAL(clearUrlLineEditFocus()), this, SLOT(clearUrlLineEditFocus())); + // Get the URL line edit palettes. noDomainSettingsPalette = urlLineEditPointer->palette(); domainSettingsPalette = urlLineEditPointer->palette(); @@ -274,6 +277,12 @@ void BrowserWindow::back() const browserViewPointer->back(); } +void BrowserWindow::clearUrlLineEditFocus() const +{ + // Remove the focus from the URL line edit. + urlLineEditPointer->clearFocus(); +} + void BrowserWindow::fileNew() const { // Display a new instance of Privacy Browser. diff --git a/src/windows/BrowserWindow.h b/src/windows/BrowserWindow.h index 5be55f4..60dea63 100644 --- a/src/windows/BrowserWindow.h +++ b/src/windows/BrowserWindow.h @@ -47,6 +47,7 @@ private Q_SLOTS: // The private slots. void addOrEditDomainSettings() const; void back() const; + void clearUrlLineEditFocus() const; void fileNew() const; void forward() const; void getZoomFactorFromUser(); -- 2.43.0