]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/mainview.cpp
5c596624d3a130192f6aae13eea3363d07ac2c07
[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 "settings.h"
24
25 // KDE Framework headers.
26 #include <KLineEdit>
27
28 // Qt framework headers.
29 #include <QWebEngineView>
30
31 MainView::MainView(QWidget *parent) : QWidget(parent)
32 {
33     // Instantiate the mainview UI.
34     Ui::MainView mainViewUi;
35
36     // Setup the UI.
37     mainViewUi.setupUi(this);
38
39     // Get handles for the views.
40     urlLineEditPointer = mainViewUi.urlLineEdit;
41     webEngineViewPointer = mainViewUi.webEngineView;
42
43     // Get a handle for the webpage.
44     QWebEnginePage *webEnginePagePointer = webEngineViewPointer->page();
45
46     // Update the webengine view from the URL line edit.
47     connect(urlLineEditPointer, SIGNAL(returnKeyPressed(const QString)), this, SLOT(loadUrl(const QString)));
48
49     // Update the URL line edit form the webengine view.
50     connect(webEngineViewPointer, SIGNAL(loadStarted()), this, SLOT(updateUrlLineEdit()));
51     connect(webEngineViewPointer, SIGNAL(loadProgress(int)), this, SLOT(updateUrlLineEdit()));
52     connect(webEngineViewPointer, SIGNAL(loadFinished(bool)), this, SLOT(updateUrlLineEdit()));
53
54     // Listen for hovered link URLs.
55     connect(webEnginePagePointer, SIGNAL(linkHovered(QString)), this, SLOT(pageLinkHovered(QString)));
56
57     // Set the zoom factor.
58     webEngineViewPointer->setZoomFactor(Settings::zoomFactor());
59
60     // Set the focus on the WebEngine view.
61     webEngineViewPointer->setFocus();
62
63     // Load the homepage.  TODO.  Consider sanitizing the homepage input and adding things like protocols if they are missing.
64     webEngineViewPointer->setUrl(Settings::homepage());
65 }
66
67 void MainView::loadUrl(const QString &urlFromUser)
68 {
69     // Remove the focus from the URL line edit.
70     urlLineEditPointer->clearFocus();
71
72     // Load the URL, adding standard protocol sections if needed.  TODO.  Replace this with logic that prefers HTTPS.
73     webEngineViewPointer->setUrl(QUrl::fromUserInput(urlFromUser));
74 }
75
76 void MainView::pageLinkHovered(const QString &linkUrl)
77 {
78     // Emit a signal so that the browser window can update the status bar.
79     emit linkHovered(linkUrl);
80 }
81
82 void MainView::updateUrlLineEdit()
83 {
84     // Update the URL line edit if it does not have focus.
85     if (!urlLineEditPointer->hasFocus())
86     {
87         // Update the URL line edit.
88         urlLineEditPointer->setUrl(webEngineViewPointer->url().toString());
89     }
90
91     // 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>
92     webEngineViewPointer->setZoomFactor(Settings::zoomFactor());
93 }