From f199b82941d34514783e4a4b85905d12999701d6 Mon Sep 17 00:00:00 2001 From: Soren Stoutner Date: Sat, 26 Mar 2022 14:10:32 -0700 Subject: [PATCH] Reimplement the URL bar as standard toolbars. --- src/BrowserView.ui | 126 ---------------------------------- src/views/BrowserView.cpp | 82 ++-------------------- src/views/BrowserView.h | 16 ++--- src/windows/BrowserWindow.cpp | 52 +++++++++++++- src/windows/BrowserWindow.h | 7 +- 5 files changed, 65 insertions(+), 218 deletions(-) diff --git a/src/BrowserView.ui b/src/BrowserView.ui index 43b9589..d78eb20 100644 --- a/src/BrowserView.ui +++ b/src/BrowserView.ui @@ -56,132 +56,6 @@ 0 - - - - - - - - - - - - - 24 - 24 - - - - - true - - - - - - - - - - - - - - 24 - 24 - - - - - true - - - - - - - - - - - - - - 24 - 24 - - - - - true - - - - - - - - - - - - - - 24 - 24 - - - - - true - - - - - - - - - - - - - - - 24 - 24 - - - - - true - - - - - - - - - - - - - - 24 - 24 - - - - - true - - - - - - diff --git a/src/views/BrowserView.cpp b/src/views/BrowserView.cpp index 07c1130..319f9e5 100644 --- a/src/views/BrowserView.cpp +++ b/src/views/BrowserView.cpp @@ -23,7 +23,6 @@ #include "Settings.h" #include "ui_BrowserView.h" #include "UrlRequestInterceptor.h" -#include "dialogs/DomainSettingsDialog.h" #include "helpers/DomainsDatabaseHelper.h" #include "helpers/SearchEngineHelper.h" #include "helpers/UserAgentHelper.h" @@ -42,13 +41,6 @@ BrowserView::BrowserView(QWidget *parent) : QWidget(parent) browserViewUi.setupUi(this); // Get handles for the views. - backButtonPointer = browserViewUi.backButton; - forwardButtonPointer = browserViewUi.forwardButton; - QPushButton *refreshButtonPointer = browserViewUi.refreshButton; - QPushButton *homeButtonPointer = browserViewUi.homeButton; - urlLineEditPointer = browserViewUi.urlLineEdit; - javaScriptButtonPointer = browserViewUi.javaScript; - QPushButton *domainSettingsButtonPointer = browserViewUi.domainSettingsButton; webEngineViewPointer = browserViewUi.webEngineView; // Get handles for the aspects of the WebEngine. @@ -57,30 +49,12 @@ BrowserView::BrowserView(QWidget *parent) : QWidget(parent) webEngineProfilePointer = webEnginePagePointer->profile(); webEngineSettingsPointer = webEngineViewPointer->settings(); - // Update the webengine view from the URL line edit. TODO. Remove. - connect(urlLineEditPointer, SIGNAL(returnKeyPressed(const QString)), this, SLOT(loadUrlFromLineEdit(const QString))); - // Update the URL line edit from the webengine view. connect(webEngineViewPointer, SIGNAL(loadStarted()), this, SLOT(updateInterface())); connect(webEngineViewPointer, SIGNAL(loadProgress(const int)), this, SLOT(updateInterface())); connect(webEngineViewPointer, SIGNAL(loadFinished(const bool)), this, SLOT(updateInterface())); - // Setup the URL bar buttons. TODO. Remove. - connect(backButtonPointer, SIGNAL(clicked()), webEngineViewPointer, SLOT(back())); - connect(forwardButtonPointer, SIGNAL(clicked()), webEngineViewPointer, SLOT(forward())); - connect(refreshButtonPointer, SIGNAL(clicked()), webEngineViewPointer, SLOT(reload())); - connect(homeButtonPointer, SIGNAL(clicked()), this, SLOT(home())); - connect(javaScriptButtonPointer, SIGNAL(clicked()), this, SLOT(toggleJavaScript())); - connect(domainSettingsButtonPointer, SIGNAL(clicked()), this, SLOT(openDomainSettings())); - - // Get the URL line edit palettes. TODO. Remove. - noDomainSettingsPalette = urlLineEditPointer->palette(); - domainSettingsPalette = urlLineEditPointer->palette(); - - // Modify the domain settings palette. TODO. Remove. - domainSettingsPalette.setColor(QPalette::Base, QColor("#C8E6C9")); - - // Instantiate the mouse event pointer. + // Instantiate the mouse event filter pointer. MouseEventFilter *mouseEventFilterPointer = new MouseEventFilter(webEngineViewPointer); // Install the mouse event filter. @@ -177,7 +151,6 @@ void BrowserView::applyDomainSettings(const QString &hostname, const bool reload webEngineViewPointer->setZoomFactor(Settings::zoomFactor()); // Apply the domain settings palette to the URL line edit. - urlLineEditPointer->setPalette(domainSettingsPalette); // TODO. Remove. emit updateDomainSettingsIndicator(true); } else // The hostname does not have domain settings. @@ -192,21 +165,11 @@ void BrowserView::applyDomainSettings(const QString &hostname, const bool reload webEngineViewPointer->setZoomFactor(Settings::zoomFactor()); // Apply the no domain settings palette to the URL line edit. - urlLineEditPointer->setPalette(noDomainSettingsPalette); // TODO. Remove. emit updateDomainSettingsIndicator(false); } - // Update the JavaScript button. - if (webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled)) - { - javaScriptButtonPointer->setIcon(QIcon(":/icons/javascript-warning")); - } - else - { - javaScriptButtonPointer->setIcon(QIcon(":/icons/privacy-mode")); - } - // Emit the on-the-fly menu update signals. + emit updateJavaScriptAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled)); emit userAgentUpdated(webEngineProfilePointer->httpUserAgent()); emit zoomFactorUpdated(Settings::zoomFactor()); @@ -291,9 +254,6 @@ void BrowserView::loadInitialWebsite() void BrowserView::loadUrlFromLineEdit(QString url) const { - // Remove the focus from the URL line edit. TODO. Remove. - urlLineEditPointer->clearFocus(); - // Decide if the text is more likely to be a URL or a search. if (url.contains(".")) // The text is likely a URL. { @@ -314,24 +274,6 @@ void BrowserView::loadUrlFromLineEdit(QString url) const } } -void BrowserView::openDomainSettings() const -{ - // Instantiate the domain settings window. - DomainSettingsDialog *domainSettingsDialogPointer = new DomainSettingsDialog(); - - // Set the dialog window title. - domainSettingsDialogPointer->setWindowTitle(i18nc("The domain settings dialog title", "Domain Settings")); - - // Set the modality. - domainSettingsDialogPointer->setWindowModality(Qt::WindowModality::WindowModal);; - - // Show the dialog. - domainSettingsDialogPointer->show(); - - // Reload the tabs when domain settings are updated. - connect(domainSettingsDialogPointer, SIGNAL(domainSettingsUpdated()), this, SLOT(applyDomainSettingsAndReload())); -} - void BrowserView::pageLinkHovered(const QString &linkUrl) const { // Emit a signal so that the browser window can update the status bar. @@ -349,15 +291,8 @@ void BrowserView::toggleJavaScript() const // Toggle JavaScript. webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, !webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled)); - // Update the JavaScript button. - if (webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled)) - { - javaScriptButtonPointer->setIcon(QIcon(":/icons/javascript-warning")); - } - else - { - javaScriptButtonPointer->setIcon(QIcon(":/icons/privacy-mode")); - } + // Update the JavaScript icon. + emit updateJavaScriptAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled)); // Reload the website. webEngineViewPointer->reload(); @@ -365,20 +300,11 @@ void BrowserView::toggleJavaScript() const void BrowserView::updateInterface() const { - // Update the URL line edit if it does not have focus. TODO. Remove block. - if (!urlLineEditPointer->hasFocus()) - { - // Update the URL line edit. - urlLineEditPointer->setText(webEngineViewPointer->url().toString()); - } - // Update the URL line edit. emit updateUrlLineEdit(webEngineViewPointer->url().toString()); // Update the status of the forward and back buttons. - backButtonPointer->setEnabled(webEngineHistoryPointer->canGoBack()); // TODO Remove. emit updateBackAction(webEngineHistoryPointer->canGoBack()); - forwardButtonPointer->setEnabled(webEngineHistoryPointer->canGoForward()); // TODO Remove. emit updateForwardAction(webEngineHistoryPointer->canGoForward()); // Reapply the zoom factor. This is a bug in QWebEngineView that resets the zoom with every load. Hopefully it will be fixed in Qt6. diff --git a/src/views/BrowserView.h b/src/views/BrowserView.h index 27c4439..3ba8bc7 100644 --- a/src/views/BrowserView.h +++ b/src/views/BrowserView.h @@ -45,13 +45,14 @@ public: signals: // The signals. void linkHovered(const QString &linkUrl) const; - void userAgentUpdated(const QString &userAgent) const; - void searchEngineUpdated(const QString &searchEngine) const; + void userAgentUpdated(const QString &userAgent) const; // TODO. Possibly rename. + void searchEngineUpdated(const QString &searchEngine) const; //TODO. Possibly rename. void updateBackAction(const bool &isEnabled) const; void updateDomainSettingsIndicator(const bool status) const; void updateForwardAction(const bool &isEnabled) const; + void updateJavaScriptAction(const bool &isEnabled) const; void updateUrlLineEdit(const QString &newUrl) const; - void zoomFactorUpdated(const double &zoomFactor) const; + void zoomFactorUpdated(const double &zoomFactor) const; //TODO. Possibly rename. public Q_SLOTS: // The public slots. @@ -64,24 +65,17 @@ public Q_SLOTS: void forward() const; void home() const; void loadUrlFromLineEdit(QString url) const; + void toggleJavaScript() const; void refresh() const; private Q_SLOTS: // The private slots. void pageLinkHovered(const QString &linkUrl) const; - void toggleJavaScript() const; - void openDomainSettings() const; void updateInterface() const; private: // The private variables. - QPushButton *backButtonPointer; - QPalette domainSettingsPalette; // TODO. Remove. - QPushButton *forwardButtonPointer; - QPushButton *javaScriptButtonPointer; - QPalette noDomainSettingsPalette; // TODO. Remove. QString searchEngineUrl; - KLineEdit *urlLineEditPointer; QWebEngineHistory *webEngineHistoryPointer; QWebEngineProfile *webEngineProfilePointer; QWebEngineSettings *webEngineSettingsPointer; diff --git a/src/windows/BrowserWindow.cpp b/src/windows/BrowserWindow.cpp index 5a84d40..d83dd69 100644 --- a/src/windows/BrowserWindow.cpp +++ b/src/windows/BrowserWindow.cpp @@ -22,6 +22,7 @@ #include "Settings.h" #include "ui_SettingsPrivacy.h" #include "ui_SettingsGeneral.h" +#include "dialogs/DomainSettingsDialog.h" #include "helpers/SearchEngineHelper.h" #include "helpers/UserAgentHelper.h" @@ -70,6 +71,8 @@ BrowserWindow::BrowserWindow() : KXmlGuiWindow() forwardActionPointer = actionCollectionPointer->addAction(QStringLiteral("forward")); refreshActionPointer = actionCollectionPointer->addAction(QStringLiteral("refresh")); homeActionPointer = actionCollectionPointer->addAction(QStringLiteral("home")); + javaScriptActionPointer = actionCollectionPointer->addAction(QStringLiteral("javascript")); + domainSettingsActionPointer = actionCollectionPointer->addAction(QStringLiteral("domain_settings")); // Create the action groups QActionGroup *userAgentActionGroupPointer = new QActionGroup(this); @@ -127,12 +130,15 @@ BrowserWindow::BrowserWindow() : KXmlGuiWindow() forwardActionPointer->setText(i18nc("Forward button", "Forward")); refreshActionPointer->setText(i18nc("Refresh button", "Refresh")); homeActionPointer->setText(i18nc("Home button", "Home")); + javaScriptActionPointer->setText(i18nc("JavaScript button", "JavaScript")); + domainSettingsActionPointer->setText(i18nc("Domain Settings button", "Domain Settings")); // Set the action icons. backActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("arrow-left"))); forwardActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("arrow-right"))); refreshActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("view-refresh"))); homeActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("home"))); + domainSettingsActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("network-server-symbolic"))); // Update the on-the-fly menus. connect(browserViewPointer, SIGNAL(userAgentUpdated(QString)), this, SLOT(updateOnTheFlyUserAgent(QString))); @@ -151,10 +157,13 @@ BrowserWindow::BrowserWindow() : KXmlGuiWindow() connect(forwardActionPointer, SIGNAL(triggered()), this, SLOT(forward())); connect(refreshActionPointer, SIGNAL(triggered()), this, SLOT(refresh())); connect(homeActionPointer, SIGNAL(triggered()), this, SLOT(home())); + connect(javaScriptActionPointer, SIGNAL(triggered()), this, SLOT(toggleJavaScript())); + connect(domainSettingsActionPointer, SIGNAL(triggered()), this, SLOT(openDomainSettings())); // Update the URL toolbar actions. connect(browserViewPointer, SIGNAL(updateBackAction(bool)), backActionPointer, SLOT(setEnabled(bool))); connect(browserViewPointer, SIGNAL(updateForwardAction(bool)), forwardActionPointer, SLOT(setEnabled(bool))); + connect(browserViewPointer, SIGNAL(updateJavaScriptAction(bool)), this, SLOT(updateJavaScriptAction(bool))); // Setup the GUI based on the browser_ui.rc file. setupGUI(StandardWindowOption::Default, ("browser_ui.rc")); @@ -165,8 +174,10 @@ BrowserWindow::BrowserWindow() : KXmlGuiWindow() // Create a URL line edit. urlLineEditPointer = new KLineEdit(); - // Add the URL line edit to the URL toolbar. + // Populate the URL toolbar. urlToolBarPointer->addWidget(urlLineEditPointer); + urlToolBarPointer->addAction(javaScriptActionPointer); + urlToolBarPointer->addAction(domainSettingsActionPointer); // Load a new URL from the URL line edit. connect(urlLineEditPointer, SIGNAL(returnKeyPressed(const QString)), this, SLOT(loadUrlFromLineEdit(const QString))); @@ -260,6 +271,27 @@ void BrowserWindow::loadUrlFromLineEdit(const QString &url) const browserViewPointer->loadUrlFromLineEdit(url); } +void BrowserWindow::openDomainSettings() const +{ + // Remove the focus from the URL line edit. + urlLineEditPointer->clearFocus(); + + // Instantiate the domain settings window. + DomainSettingsDialog *domainSettingsDialogPointer = new DomainSettingsDialog(); + + // Set the dialog window title. + domainSettingsDialogPointer->setWindowTitle(i18nc("The domain settings dialog title", "Domain Settings")); + + // Set the modality. + domainSettingsDialogPointer->setWindowModality(Qt::WindowModality::WindowModal);; + + // Show the dialog. + domainSettingsDialogPointer->show(); + + // Reload the tabs when domain settings are updated. + connect(domainSettingsDialogPointer, SIGNAL(domainSettingsUpdated()), browserViewPointer, SLOT(applyDomainSettingsAndReload())); +} + void BrowserWindow::refresh() const { // Remove the focus from the URL line edit. @@ -269,6 +301,15 @@ void BrowserWindow::refresh() const browserViewPointer->refresh(); } +void BrowserWindow::toggleJavaScript() const +{ + // Remove the focus from the URL line edit. + urlLineEditPointer->clearFocus(); + + // Toggle JavaScript. + browserViewPointer->toggleJavaScript(); +} + void BrowserWindow::settingsConfigure() { // Check to make sure the dialog box isn't already displayed. @@ -344,13 +385,20 @@ void BrowserWindow::settingsConfigure() } } -void BrowserWindow::updateDomainSettingsIndicator(const bool status) const +void BrowserWindow::updateDomainSettingsIndicator(const bool &status) const { // Set the domain palette according to the status. if (status) urlLineEditPointer->setPalette(domainSettingsPalette); else urlLineEditPointer->setPalette(noDomainSettingsPalette); } +void BrowserWindow::updateJavaScriptAction(const bool &isEnabled) const +{ + // Set the icon according to the status. + if (isEnabled) javaScriptActionPointer->setIcon(QIcon(":/icons/javascript-warning")); + else javaScriptActionPointer->setIcon(QIcon(":/icons/privacy-mode")); +} + void BrowserWindow::updateOnTheFlySearchEngine(const QString &searchEngine) const { // Initialize the custom search engine flag. diff --git a/src/windows/BrowserWindow.h b/src/windows/BrowserWindow.h index f86c1b2..7fa6050 100644 --- a/src/windows/BrowserWindow.h +++ b/src/windows/BrowserWindow.h @@ -47,9 +47,12 @@ private Q_SLOTS: void getZoomFactorFromUser(); void home() const; void loadUrlFromLineEdit(const QString &url) const; + void openDomainSettings() const; void refresh() const; void settingsConfigure(); - void updateDomainSettingsIndicator(const bool status) const; + void toggleJavaScript() const; + void updateDomainSettingsIndicator(const bool &status) const; + void updateJavaScriptAction(const bool &isEnabled) const; void updateOnTheFlySearchEngine(const QString &searchEngine) const; void updateOnTheFlyUserAgent(const QString &userAgent) const; void updateOnTheFlyZoomFactor(const double &zoomFactor); @@ -62,10 +65,12 @@ private: QAction *backActionPointer; BrowserView *browserViewPointer; KConfigDialog *configDialogPointer; + QAction *domainSettingsActionPointer; QPalette domainSettingsPalette; double currentZoomFactor; QAction *forwardActionPointer; QAction *homeActionPointer; + QAction *javaScriptActionPointer; QPalette noDomainSettingsPalette; QLabel *searchEngineLabelPointer; QAction *searchEngineMojeekActionPointer; -- 2.45.2