]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blobdiff - src/mainview.cpp
Create a home icon.
[PrivacyBrowserPC.git] / src / mainview.cpp
index 5c596624d3a130192f6aae13eea3363d07ac2c07..25f3b4c5ea0d1b6f39de2439abea2bff200d58f3 100644 (file)
 // Application headers
 #include "browserwindow.h"
 #include "mainview.h"
+#include "mouseeventfilter.h"
 #include "settings.h"
 
 // KDE Framework headers.
 #include <KLineEdit>
 
-// Qt framework headers.
+// Qt headers.
+#include <QWebEngineHistory>
 #include <QWebEngineView>
 
 MainView::MainView(QWidget *parent) : QWidget(parent)
@@ -37,19 +39,34 @@ MainView::MainView(QWidget *parent) : QWidget(parent)
     mainViewUi.setupUi(this);
 
     // Get handles for the views.
+    backButtonPointer = mainViewUi.backButton;
+    forwardButtonPointer = mainViewUi.forwardButton;
+    QPushButton *homeButtonPointer = mainViewUi.homeButton;
     urlLineEditPointer = mainViewUi.urlLineEdit;
     webEngineViewPointer = mainViewUi.webEngineView;
 
-    // Get a handle for the webpage.
+    // Get handles for the webpage and history.
     QWebEnginePage *webEnginePagePointer = webEngineViewPointer->page();
+    webEngineHistoryPointer = webEnginePagePointer->history();
 
     // Update the webengine view from the URL line edit.
     connect(urlLineEditPointer, SIGNAL(returnKeyPressed(const QString)), this, SLOT(loadUrl(const QString)));
 
     // Update the URL line edit form the webengine view.
-    connect(webEngineViewPointer, SIGNAL(loadStarted()), this, SLOT(updateUrlLineEdit()));
-    connect(webEngineViewPointer, SIGNAL(loadProgress(int)), this, SLOT(updateUrlLineEdit()));
-    connect(webEngineViewPointer, SIGNAL(loadFinished(bool)), this, SLOT(updateUrlLineEdit()));
+    connect(webEngineViewPointer, SIGNAL(loadStarted()), this, SLOT(updateInterface()));
+    connect(webEngineViewPointer, SIGNAL(loadProgress(int)), this, SLOT(updateInterface()));
+    connect(webEngineViewPointer, SIGNAL(loadFinished(bool)), this, SLOT(updateInterface()));
+
+    // Setup the URL bar buttons.
+    connect(backButtonPointer, SIGNAL(clicked()), webEngineViewPointer, SLOT(back()));
+    connect(forwardButtonPointer, SIGNAL(clicked()), webEngineViewPointer, SLOT(forward()));
+    connect(homeButtonPointer, SIGNAL(clicked()), this, SLOT(goHome()));
+
+    // Instantiate the mouse event pointer.
+    MouseEventFilter *mouseEventFilterPointer = new MouseEventFilter(webEngineViewPointer);
+
+    // Install the mouse event filter.
+    qApp->installEventFilter(mouseEventFilterPointer);
 
     // Listen for hovered link URLs.
     connect(webEnginePagePointer, SIGNAL(linkHovered(QString)), this, SLOT(pageLinkHovered(QString)));
@@ -60,6 +77,12 @@ MainView::MainView(QWidget *parent) : QWidget(parent)
     // Set the focus on the WebEngine view.
     webEngineViewPointer->setFocus();
 
+    // Load the homepage.
+    goHome();
+}
+
+void MainView::goHome()
+{
     // Load the homepage.  TODO.  Consider sanitizing the homepage input and adding things like protocols if they are missing.
     webEngineViewPointer->setUrl(Settings::homepage());
 }
@@ -79,7 +102,7 @@ void MainView::pageLinkHovered(const QString &linkUrl)
     emit linkHovered(linkUrl);
 }
 
-void MainView::updateUrlLineEdit()
+void MainView::updateInterface()
 {
     // Update the URL line edit if it does not have focus.
     if (!urlLineEditPointer->hasFocus())
@@ -88,6 +111,10 @@ void MainView::updateUrlLineEdit()
         urlLineEditPointer->setUrl(webEngineViewPointer->url().toString());
     }
 
+    // Update the status of the forward and back buttons.
+    backButtonPointer->setEnabled(webEngineHistoryPointer->canGoBack());
+    forwardButtonPointer->setEnabled(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.  <https://bugreports.qt.io/browse/QTBUG-51992>
     webEngineViewPointer->setZoomFactor(Settings::zoomFactor());
 }