X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyBrowserPC.git;a=blobdiff_plain;f=src%2Fmainview.cpp;h=8749760773e6acdd9540a236855ad761d805a6e2;hp=c437c2c4ac130b31739961ddb65898c99c23e7f5;hb=3d2e70c352736d809d6d0b705864ce17ffde68ae;hpb=153c5c0d60eaf3185cb4419032fb0fdaeb78907c diff --git a/src/mainview.cpp b/src/mainview.cpp index c437c2c..8749760 100644 --- a/src/mainview.cpp +++ b/src/mainview.cpp @@ -1,45 +1,112 @@ /* - SPDX-FileCopyrightText: %{CURRENT_YEAR} %{AUTHOR} <%{EMAIL}> + * Copyright © 2022 Soren Stoutner . + * + * This file is part of Privacy Browser PC . + * + * Privacy Browser PC is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Privacy Browser PC is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Privacy Browser PC. If not, see . + */ - SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL -*/ - -// application headers +// Application headers +#include "browserwindow.h" #include "mainview.h" - +#include "mouseeventfilter.h" #include "settings.h" -#include "privacybrowserdebug.h" +// KDE Framework headers. +#include + +// Qt headers. +#include +#include -MainView::MainView(QWidget *parent) - : QWidget(parent) +MainView::MainView(QWidget *parent) : QWidget(parent) { - m_ui.setupUi(this); - handleSettingsChanged(); + // Instantiate the mainview UI. + Ui::MainView mainViewUi; + + // Setup the UI. + mainViewUi.setupUi(this); + + // Get handles for the views. + backButtonPointer = mainViewUi.backButton; + forwardButtonPointer = mainViewUi.forwardButton; + urlLineEditPointer = mainViewUi.urlLineEdit; + webEngineViewPointer = mainViewUi.webEngineView; + + // 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(updateInterface())); + connect(webEngineViewPointer, SIGNAL(loadProgress(int)), this, SLOT(updateInterface())); + connect(webEngineViewPointer, SIGNAL(loadFinished(bool)), this, SLOT(updateInterface())); + + // Setup the forward and back buttons. + connect(backButtonPointer, SIGNAL(clicked()), webEngineViewPointer, SLOT(back())); + connect(forwardButtonPointer, SIGNAL(clicked()), webEngineViewPointer, SLOT(forward())); + + // 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))); + + // Set the zoom factor. + webEngineViewPointer->setZoomFactor(Settings::zoomFactor()); + + // Set the focus on the WebEngine view. + webEngineViewPointer->setFocus(); + + // Load the homepage. TODO. Consider sanitizing the homepage input and adding things like protocols if they are missing. + webEngineViewPointer->setUrl(Settings::homepage()); } -MainView::~MainView() +void MainView::loadUrl(const QString &urlFromUser) { + // Remove the focus from the URL line edit. + urlLineEditPointer->clearFocus(); + + // Load the URL, adding standard protocol sections if needed. TODO. Replace this with logic that prefers HTTPS. + webEngineViewPointer->setUrl(QUrl::fromUserInput(urlFromUser)); } -void MainView::switchColors() +void MainView::pageLinkHovered(const QString &linkUrl) { - // switch the foreground/background colors of the label - QColor color = Settings::colorBackground(); - Settings::setColorBackground(Settings::colorForeground()); - Settings::setColorForeground(color); - - handleSettingsChanged(); + // Emit a signal so that the browser window can update the status bar. + emit linkHovered(linkUrl); } -void MainView::handleSettingsChanged() +void MainView::updateInterface() { - qCDebug(PRIVACYBROWSER) << "MainView::handleSettingsChanged()"; - QPalette palette = m_ui.templateLabel->palette(); - palette.setColor(QPalette::Window, Settings::colorBackground()); - palette.setColor(QPalette::WindowText, Settings::colorForeground()); - m_ui.templateLabel->setPalette(palette); - - // i18n : internationalization - m_ui.templateLabel->setText(i18n("This project is %1 days old", Settings::ageInDays())); + // Update the URL line edit if it does not have focus. + if (!urlLineEditPointer->hasFocus()) + { + // Update the URL line edit. + 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. + webEngineViewPointer->setZoomFactor(Settings::zoomFactor()); }