From 8e2e267828a6fbd7ed9950204c50a100e3f413fa Mon Sep 17 00:00:00 2001 From: Soren Stoutner Date: Sat, 29 Jan 2022 13:01:40 -0700 Subject: [PATCH] Display hovered link URLs in the status bar. --- src/CMakeLists.txt | 2 +- src/{mainwindow.cpp => browserwindow.cpp} | 22 ++++++++++++++++----- src/{mainwindow.h => browserwindow.h} | 9 +++++---- src/main.cpp | 6 +++--- src/mainview.cpp | 24 +++++++++++++++++++++-- src/mainview.h | 5 +++++ 6 files changed, 53 insertions(+), 15 deletions(-) rename src/{mainwindow.cpp => browserwindow.cpp} (83%) rename src/{mainwindow.h => browserwindow.h} (86%) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2b5beb1..320ae9d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -21,8 +21,8 @@ add_executable(privacy-browser resources.qrc) # List the sources to include in the executable. target_sources(privacy-browser PRIVATE + browserwindow.cpp main.cpp - mainwindow.cpp mainview.cpp ) diff --git a/src/mainwindow.cpp b/src/browserwindow.cpp similarity index 83% rename from src/mainwindow.cpp rename to src/browserwindow.cpp index c48e495..d539fed 100644 --- a/src/mainwindow.cpp +++ b/src/browserwindow.cpp @@ -18,8 +18,8 @@ */ // Application headers. +#include "browserwindow.h" #include "mainview.h" -#include "mainwindow.h" #include "settings.h" #include "ui_settings.h" @@ -27,7 +27,10 @@ #include #include -MainWindow::MainWindow() : KXmlGuiWindow() +// Qt framework headers. +#include + +BrowserWindow::BrowserWindow() : KXmlGuiWindow() { // Instantiate the main view pointer. MainView *mainViewPointer = new MainView(this); @@ -43,17 +46,20 @@ MainWindow::MainWindow() : KXmlGuiWindow() KStandardAction::quit(qApp, SLOT(closeAllWindows()), actionCollectionPointer); KStandardAction::preferences(this, SLOT(settingsConfigure()), actionCollectionPointer); + // Update the status bar with the URL when a link is hovered. + connect(mainViewPointer, SIGNAL(linkHovered(QString)), this, SLOT(updateStatusBar(QString))); + // Setup the GUI based on the privacybrowserui.rc file. setupGUI(); } -void MainWindow::fileNew() +void BrowserWindow::fileNew() { // Display a new instance of Privacy Browser. - (new MainWindow)->show(); + (new BrowserWindow)->show(); } -void MainWindow::settingsConfigure() +void BrowserWindow::settingsConfigure() { // Check to make sure the dialog box isn't already displayed. if (!KConfigDialog::exists(QStringLiteral("settings"))) @@ -80,3 +86,9 @@ void MainWindow::settingsConfigure() configDialogPointer->show(); } } + +void BrowserWindow::updateStatusBar(const QString &statusBarMessage) +{ + // Display the status bar message. + statusBar()->showMessage(statusBarMessage); +} diff --git a/src/mainwindow.h b/src/browserwindow.h similarity index 86% rename from src/mainwindow.h rename to src/browserwindow.h index 588ac9e..613f210 100644 --- a/src/mainwindow.h +++ b/src/browserwindow.h @@ -17,24 +17,25 @@ * along with Privacy Browser PC. If not, see . */ -#ifndef MAINWINDOW_H -#define MAINWINDOW_H +#ifndef BROWSERWINDOW_H +#define BROWSERWINDOW_H // KDE Frameworks headers. #include -class MainWindow : public KXmlGuiWindow +class BrowserWindow : public KXmlGuiWindow { // Include the Q_OBJECT macro. Q_OBJECT public: // The default constructor. - MainWindow(); + BrowserWindow(); private Q_SLOTS: // Define the private slots. void fileNew(); void settingsConfigure(); + void updateStatusBar(const QString &statusBarMessage); }; #endif diff --git a/src/main.cpp b/src/main.cpp index 6bf5e74..80909e2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,7 +18,7 @@ */ // Application headers. -#include "mainwindow.h" +#include "browserwindow.h" // KDE Frameworks headers. #include @@ -79,10 +79,10 @@ int main(int argc, char *argv[]) KDBusService appDBusService(KDBusService::Multiple | KDBusService::NoExitOnFailure); // Create the main window. - MainWindow *mainWindowPointer = new MainWindow; + BrowserWindow *browserWindowPointer = new BrowserWindow; // Show the main window. - mainWindowPointer->show(); + browserWindowPointer->show(); // Return the application. return application.exec(); diff --git a/src/mainview.cpp b/src/mainview.cpp index e46f00d..b1adcbf 100644 --- a/src/mainview.cpp +++ b/src/mainview.cpp @@ -18,6 +18,7 @@ */ // Application headers +#include "browserwindow.h" #include "mainview.h" #include "settings.h" @@ -39,6 +40,9 @@ MainView::MainView(QWidget *parent) : QWidget(parent) urlLineEditPointer = mainViewUi.urlLineEdit; webEngineViewPointer = mainViewUi.webEngineView; + // Get a handle for the webpage. + QWebEnginePage *webEnginePagePointer = webEngineViewPointer->page(); + // Update the webengine view from the URL line edit. connect(urlLineEditPointer, SIGNAL(returnKeyPressed(const QString)), this, SLOT(loadUrl(const QString))); @@ -47,9 +51,15 @@ MainView::MainView(QWidget *parent) : QWidget(parent) connect(webEngineViewPointer, SIGNAL(loadProgress(int)), this, SLOT(updateUrlLineEdit())); connect(webEngineViewPointer, SIGNAL(loadFinished(bool)), this, SLOT(updateUrlLineEdit())); + // Listen for hovered link URLs. + connect(webEnginePagePointer, SIGNAL(linkHovered(QString)), this, SLOT(pageLinkHovered(QString))); + // Set the zoom factor. webEngineViewPointer->setZoomFactor(Settings::zoomFactor()); + // Set the focus on the WebEngine view. + webEngineViewPointer->setFocus(); + // Load a website. webEngineViewPointer->setUrl(QUrl(QStringLiteral("https://www.stoutner.com/"))); } @@ -60,10 +70,20 @@ void MainView::loadUrl(const QString &urlFromUser) webEngineViewPointer->setUrl(QUrl::fromUserInput(urlFromUser)); } +void MainView::pageLinkHovered(const QString &linkUrl) +{ + // Emit a signal so that the browser window can update the status bar. + emit linkHovered(linkUrl); +} + void MainView::updateUrlLineEdit() { - // Update the URL line edit. - urlLineEditPointer->setUrl(webEngineViewPointer->url().toString()); + // Update the URL line edit if it does not have focus. + if (!urlLineEditPointer->hasFocus()) + { + // Update the URL line edit. + urlLineEditPointer->setUrl(webEngineViewPointer->url().toString()); + } // Reapply the zoom factor. This is a bug in QWebEngineView that resets the zoom with every load. Hopefully it will be fixed in Qt6. webEngineViewPointer->setZoomFactor(Settings::zoomFactor()); diff --git a/src/mainview.h b/src/mainview.h index 9affb38..47a9f27 100644 --- a/src/mainview.h +++ b/src/mainview.h @@ -32,9 +32,14 @@ public: // The default contructor. explicit MainView(QWidget *parent); +signals: + // Define the signals. + void linkHovered(const QString &linkUrl); + private Q_SLOTS: // Define the private slots. void loadUrl(const QString &urlFromUser); + void pageLinkHovered(const QString &linkUrl); void updateUrlLineEdit(); private: -- 2.45.2