]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/mainview.cpp
Create a home icon.
[PrivacyBrowserPC.git] / src / mainview.cpp
1 /*
2  * Copyright © 2022 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
5  *
6  * Privacy Browser PC is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Privacy Browser PC is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Privacy Browser PC.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 // Application headers
21 #include "browserwindow.h"
22 #include "mainview.h"
23 #include "mouseeventfilter.h"
24 #include "settings.h"
25
26 // KDE Framework headers.
27 #include <KLineEdit>
28
29 // Qt headers.
30 #include <QWebEngineHistory>
31 #include <QWebEngineView>
32
33 MainView::MainView(QWidget *parent) : QWidget(parent)
34 {
35     // Instantiate the mainview UI.
36     Ui::MainView mainViewUi;
37
38     // Setup the UI.
39     mainViewUi.setupUi(this);
40
41     // Get handles for the views.
42     backButtonPointer = mainViewUi.backButton;
43     forwardButtonPointer = mainViewUi.forwardButton;
44     QPushButton *homeButtonPointer = mainViewUi.homeButton;
45     urlLineEditPointer = mainViewUi.urlLineEdit;
46     webEngineViewPointer = mainViewUi.webEngineView;
47
48     // Get handles for the webpage and history.
49     QWebEnginePage *webEnginePagePointer = webEngineViewPointer->page();
50     webEngineHistoryPointer = webEnginePagePointer->history();
51
52     // Update the webengine view from the URL line edit.
53     connect(urlLineEditPointer, SIGNAL(returnKeyPressed(const QString)), this, SLOT(loadUrl(const QString)));
54
55     // Update the URL line edit form the webengine view.
56     connect(webEngineViewPointer, SIGNAL(loadStarted()), this, SLOT(updateInterface()));
57     connect(webEngineViewPointer, SIGNAL(loadProgress(int)), this, SLOT(updateInterface()));
58     connect(webEngineViewPointer, SIGNAL(loadFinished(bool)), this, SLOT(updateInterface()));
59
60     // Setup the URL bar buttons.
61     connect(backButtonPointer, SIGNAL(clicked()), webEngineViewPointer, SLOT(back()));
62     connect(forwardButtonPointer, SIGNAL(clicked()), webEngineViewPointer, SLOT(forward()));
63     connect(homeButtonPointer, SIGNAL(clicked()), this, SLOT(goHome()));
64
65     // Instantiate the mouse event pointer.
66     MouseEventFilter *mouseEventFilterPointer = new MouseEventFilter(webEngineViewPointer);
67
68     // Install the mouse event filter.
69     qApp->installEventFilter(mouseEventFilterPointer);
70
71     // Listen for hovered link URLs.
72     connect(webEnginePagePointer, SIGNAL(linkHovered(QString)), this, SLOT(pageLinkHovered(QString)));
73
74     // Set the zoom factor.
75     webEngineViewPointer->setZoomFactor(Settings::zoomFactor());
76
77     // Set the focus on the WebEngine view.
78     webEngineViewPointer->setFocus();
79
80     // Load the homepage.
81     goHome();
82 }
83
84 void MainView::goHome()
85 {
86     // Load the homepage.  TODO.  Consider sanitizing the homepage input and adding things like protocols if they are missing.
87     webEngineViewPointer->setUrl(Settings::homepage());
88 }
89
90 void MainView::loadUrl(const QString &urlFromUser)
91 {
92     // Remove the focus from the URL line edit.
93     urlLineEditPointer->clearFocus();
94
95     // Load the URL, adding standard protocol sections if needed.  TODO.  Replace this with logic that prefers HTTPS.
96     webEngineViewPointer->setUrl(QUrl::fromUserInput(urlFromUser));
97 }
98
99 void MainView::pageLinkHovered(const QString &linkUrl)
100 {
101     // Emit a signal so that the browser window can update the status bar.
102     emit linkHovered(linkUrl);
103 }
104
105 void MainView::updateInterface()
106 {
107     // Update the URL line edit if it does not have focus.
108     if (!urlLineEditPointer->hasFocus())
109     {
110         // Update the URL line edit.
111         urlLineEditPointer->setUrl(webEngineViewPointer->url().toString());
112     }
113
114     // Update the status of the forward and back buttons.
115     backButtonPointer->setEnabled(webEngineHistoryPointer->canGoBack());
116     forwardButtonPointer->setEnabled(webEngineHistoryPointer->canGoForward());
117
118     // 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>
119     webEngineViewPointer->setZoomFactor(Settings::zoomFactor());
120 }