]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blobdiff - src/widgets/TabWidget.cpp
Populate the UI when the initial URL is a local file. https://redmine.stoutner.com...
[PrivacyBrowserPC.git] / src / widgets / TabWidget.cpp
index 59e424331fd78643d2ae7927a56c87239ee300ff..ff8563c96a9b5d8b311aca160f51b3098e5ab41c 100644 (file)
@@ -141,7 +141,7 @@ void TabWidget::addFirstTab()
     tabWidgetPointer->currentWidget()->setFocus();
 }
 
-void TabWidget::addTab()
+PrivacyWebEngineView* TabWidget::addTab(const bool focusNewWebEngineView)
 {
     // Create a privacy WebEngine view.
     PrivacyWebEngineView *privacyWebEngineViewPointer = new PrivacyWebEngineView();
@@ -166,13 +166,58 @@ void TabWidget::addTab()
     QWebEngineSettings *webEngineSettingsPointer = webEnginePagePointer->settings();
 
     // Update the URL line edit when the URL changes.
-    connect(privacyWebEngineViewPointer, SIGNAL(urlChanged(const QUrl)), this, SLOT(updateUrl(const QUrl)));
+    connect(privacyWebEngineViewPointer, &PrivacyWebEngineView::urlChanged, [privacyWebEngineViewPointer, this] (const QUrl &newUrl)
+    {
+        // Only update the UI if this is the current tab.
+        if (privacyWebEngineViewPointer == currentPrivacyWebEngineViewPointer)
+        {
+            // Update the URL line edit.
+            emit updateUrlLineEdit(newUrl);
 
+            // Update the status of the forward and back buttons.
+            emit updateBackAction(currentWebEngineHistoryPointer->canGoBack());
+            emit updateForwardAction(currentWebEngineHistoryPointer->canGoForward());
+        }
 
-    // Update the progress bar.
-    connect(privacyWebEngineViewPointer, SIGNAL(loadStarted()), this, SLOT(loadStarted()));
-    connect(privacyWebEngineViewPointer, SIGNAL(loadProgress(const int)), this, SLOT(loadProgress(const int)));
-    connect(privacyWebEngineViewPointer, SIGNAL(loadFinished(const bool)), this, SLOT(loadFinished()));
+        // Reapply the zoom factor.  This is a bug in QWebEngineView that resets the zoom with every load.  It can be removed once <https://redmine.stoutner.com/issues/799> is fixed.
+        privacyWebEngineViewPointer->setZoomFactor(currentZoomFactor);
+    });
+
+    // Update the progress bar when a load is started.
+    connect(privacyWebEngineViewPointer, &PrivacyWebEngineView::loadStarted, [privacyWebEngineViewPointer, this] ()
+    {
+        // Store the load progress.
+        privacyWebEngineViewPointer->loadProgressInt = 0;
+
+        // Show the progress bar if this is the current tab.
+        if (privacyWebEngineViewPointer == currentPrivacyWebEngineViewPointer)
+            emit showProgressBar(0);
+    });
+
+    // Update the progress bar when a load progresses.
+    connect(privacyWebEngineViewPointer, &PrivacyWebEngineView::loadProgress, [privacyWebEngineViewPointer, this] (const int progress)
+    {
+        // Store the load progress.
+        privacyWebEngineViewPointer->loadProgressInt = progress;
+
+        // Update the progress bar if this is the current tab.
+        if (privacyWebEngineViewPointer == currentPrivacyWebEngineViewPointer)
+            emit showProgressBar(progress);
+    });
+
+    // Update the progress bar when a load finishes.
+    connect(privacyWebEngineViewPointer, &PrivacyWebEngineView::loadFinished, [privacyWebEngineViewPointer, this] ()
+    {
+        // Store the load progress.
+        privacyWebEngineViewPointer->loadProgressInt = -1;
+
+        // Hide the progress bar if this is the current tab.
+        if (privacyWebEngineViewPointer == currentPrivacyWebEngineViewPointer)
+            emit hideProgressBar();
+    });
+
+    // Display find text results.
+    connect(webEnginePagePointer, SIGNAL(findTextFinished(const QWebEngineFindTextResult &)), this, SLOT(findTextFinished(const QWebEngineFindTextResult &)));
 
     // Handle full screen requests.
     connect(webEnginePagePointer, SIGNAL(fullScreenRequested(QWebEngineFullScreenRequest)), this, SLOT(fullScreenRequested(QWebEngineFullScreenRequest)));
@@ -237,16 +282,13 @@ void TabWidget::addTab()
     // Limit WebRTC to public IP addresses.
     webEngineSettingsPointer->setAttribute(QWebEngineSettings::WebRTCPublicInterfacesOnly, true);
 
-    // Define an update cookie count lambda.
-    auto updateCookieCount = [privacyWebEngineViewPointer, this] (const int numberOfCookies)
+    // Update the cookies action.
+    connect(privacyWebEngineViewPointer, &PrivacyWebEngineView::updateCookiesAction, [privacyWebEngineViewPointer, this] (const int numberOfCookies)
     {
         // Update the cookie action if the specified privacy WebEngine view is the current privacy WebEngine view.
         if (privacyWebEngineViewPointer == currentPrivacyWebEngineViewPointer)
             emit updateCookiesAction(numberOfCookies);
-    };
-
-    // Update the cookies action.
-    connect(privacyWebEngineViewPointer, &PrivacyWebEngineView::updateCookiesAction, this, updateCookieCount);
+    });
 
     // Process cookie changes.
     connect(webEngineCookieStorePointer, SIGNAL(cookieAdded(QNetworkCookie)), privacyWebEngineViewPointer, SLOT(addCookieToList(QNetworkCookie)));
@@ -259,21 +301,22 @@ void TabWidget::addTab()
     for (QNetworkCookie *cookiePointer : *durableCookiesListPointer)
         addCookieToStore(*cookiePointer, webEngineCookieStorePointer);
 
-    // Define an update tab title lambda.
-    auto updateTabTitle = [privacyWebEngineViewPointer, this] (const QString &title)
+    // Update the title when it changes.
+    connect(privacyWebEngineViewPointer, &PrivacyWebEngineView::titleChanged, [this, privacyWebEngineViewPointer] (const QString &title)
     {
         // Get the index for this tab.
         int tabIndex = tabWidgetPointer->indexOf(privacyWebEngineViewPointer);
 
         // Update the title for this tab.
         tabWidgetPointer->setTabText(tabIndex, title);
-    };
 
-    // Update the title when it changes.
-    connect(privacyWebEngineViewPointer, &PrivacyWebEngineView::titleChanged, this, updateTabTitle);
+        // Update the window title if this is the current tab.
+        if (tabIndex == tabWidgetPointer->currentIndex())
+            emit updateWindowTitle(title);
+    });
 
-    // Define an update tab icon lambda.
-    auto updateTabIcon = [privacyWebEngineViewPointer, this] (const QIcon &icon)
+    // Update the icon when it changes.
+    connect(privacyWebEngineViewPointer, &PrivacyWebEngineView::iconChanged, [privacyWebEngineViewPointer, this] (const QIcon &icon)
     {
         // Get the index for this tab.
         int tabIndex = tabWidgetPointer->indexOf(privacyWebEngineViewPointer);
@@ -283,13 +326,26 @@ void TabWidget::addTab()
             tabWidgetPointer->setTabIcon(tabIndex, defaultTabIcon);
         else
             tabWidgetPointer->setTabIcon(tabIndex, icon);
-    };
+    });
 
-    // Update the icon when it changes.
-    connect(privacyWebEngineViewPointer, &PrivacyWebEngineView::iconChanged, this, updateTabIcon);
+    // Enable spell checking.
+    webEngineProfilePointer->setSpellCheckEnabled(true);
+
+    // Set the spell check language.
+    webEngineProfilePointer->setSpellCheckLanguages({QStringLiteral("en_US")});
+
+    // Populate the zoom factor.  This is necessary if a URL is being loaded, like a local URL, that does not trigger `applyDomainSettings()`.
+    privacyWebEngineViewPointer->setZoomFactor(Settings::zoomFactor());
 
     // Move to the new tab.
     tabWidgetPointer->setCurrentIndex(newTabIndex);
+
+    // Clear the URL line edit focus so that it populates correctly when opening a new tab from the context menu.
+    if (focusNewWebEngineView)
+        emit clearUrlLineEditFocus();
+
+    // Return the privacy WebEngine view pointer.
+    return privacyWebEngineViewPointer;
 }
 
 void TabWidget::applyApplicationSettings()
@@ -562,6 +618,52 @@ void TabWidget::deleteTab(const int tabIndex)
     }
 }
 
+void TabWidget::findPrevious(const QString &text) const
+{
+    // Store the current text.
+    currentPrivacyWebEngineViewPointer->findString = text;
+
+    // Find the previous text in the current privacy WebEngine.
+    if (currentPrivacyWebEngineViewPointer->findCaseSensitive)
+        currentPrivacyWebEngineViewPointer->findText(text, QWebEnginePage::FindCaseSensitively|QWebEnginePage::FindBackward);
+    else
+        currentPrivacyWebEngineViewPointer->findText(text, QWebEnginePage::FindBackward);
+}
+
+void TabWidget::findText(const QString &text) const
+{
+    // Store the current text.
+    currentPrivacyWebEngineViewPointer->findString = text;
+
+    // Find the text in the current privacy WebEngine.
+    if (currentPrivacyWebEngineViewPointer->findCaseSensitive)
+        currentPrivacyWebEngineViewPointer->findText(text, QWebEnginePage::FindCaseSensitively);
+    else
+        currentPrivacyWebEngineViewPointer->findText(text);
+
+    // Clear the currently selected text in the WebEngine page if the find text is empty.
+    if (text.isEmpty())
+        currentWebEnginePagePointer->action(QWebEnginePage::Unselect)->activate(QAction::Trigger);
+}
+
+void TabWidget::findTextFinished(const QWebEngineFindTextResult &findTextResult)
+{
+    // Update the find text UI if it wasn't simply wiping the current find text selection.  Otherwise the UI temporarially flashes `0/0`.
+    if (wipingCurrentFindTextSelection)  // The current selection is being wiped.
+    {
+        // Reset the flag.
+        wipingCurrentFindTextSelection = false;
+    }
+    else  // A new search has been performed.
+    {
+        // Store the result.
+        currentPrivacyWebEngineViewPointer->findTextResult = findTextResult;
+
+        // Update the UI.
+        emit updateFindTextResults(findTextResult);
+    }
+}
+
 void TabWidget::forward() const
 {
     // Go forward.
@@ -595,10 +697,13 @@ void TabWidget::home() const
     currentPrivacyWebEngineViewPointer->load(QUrl::fromUserInput(Settings::homepage()));
 }
 
-void TabWidget::loadFinished() const
+PrivacyWebEngineView* TabWidget::loadBlankInitialWebsite()
 {
-    // Hide the progress bar.
-    emit hideProgressBar();
+    // Apply the application settings.
+    applyApplicationSettings();
+
+    // Return the current privacy WebEngine view pointer.
+    return currentPrivacyWebEngineViewPointer;
 }
 
 void TabWidget::loadInitialWebsite()
@@ -622,18 +727,6 @@ void TabWidget::loadInitialWebsite()
     }
 }
 
-void TabWidget::loadProgress(const int &progress) const
-{
-    // Show the progress bar.
-    emit showProgressBar(progress);
-}
-
-void TabWidget::loadStarted() const
-{
-    // Show the progress bar.
-    emit showProgressBar(0);
-}
-
 void TabWidget::loadUrlFromLineEdit(QString url) const
 {
     // Decide if the text is more likely to be a URL or a search.
@@ -826,6 +919,21 @@ void TabWidget::toggleDomStorage() const
     currentPrivacyWebEngineViewPointer->reload();
 }
 
+void TabWidget::toggleFindCaseSensitive(const QString &text)
+{
+    // Toggle find case sensitive.
+    currentPrivacyWebEngineViewPointer->findCaseSensitive = !currentPrivacyWebEngineViewPointer->findCaseSensitive;
+
+    // Set the wiping current find text selection flag.
+    wipingCurrentFindTextSelection = true;
+
+    // Wipe the previous search.  Otherwise currently highlighted words will remain highlighted.
+    findText(QStringLiteral(""));
+
+    // Update the find text.
+    findText(text);
+}
+
 void TabWidget::toggleJavaScript() const
 {
     // Toggle JavaScript.
@@ -863,26 +971,28 @@ void TabWidget::updateUiWithTabSettings()
     // Clear the URL line edit focus.
     emit clearUrlLineEditFocus();
 
-    // Update the UI.
-    emit updateDomainSettingsIndicator(currentPrivacyWebEngineViewPointer->domainSettingsName != QStringLiteral(""));
+    // Update the actions.
+    emit updateBackAction(currentWebEngineHistoryPointer->canGoBack());
+    emit updateCookiesAction(currentPrivacyWebEngineViewPointer->cookieListPointer->size());
+    emit updateDomStorageAction(currentWebEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled));
+    emit updateForwardAction(currentWebEngineHistoryPointer->canGoForward());
     emit updateJavaScriptAction(currentWebEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
     emit updateLocalStorageAction(currentPrivacyWebEngineViewPointer->localStorageEnabled);
-    emit updateDomStorageAction(currentWebEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled));
     emit updateUserAgentActions(currentWebEngineProfilePointer->httpUserAgent(), true);
     emit updateZoomFactorAction(currentPrivacyWebEngineViewPointer->zoomFactor());
-    emit updateUrlLineEdit(currentPrivacyWebEngineViewPointer->url());
-    emit updateCookiesAction(currentPrivacyWebEngineViewPointer->cookieListPointer->size());
-}
 
-void TabWidget::updateUrl(const QUrl &url) const
-{
-    // Update the URL line edit.
-    emit updateUrlLineEdit(url);
+    // Update the URL.
+    emit updateWindowTitle(currentPrivacyWebEngineViewPointer->title());
+    emit updateDomainSettingsIndicator(currentPrivacyWebEngineViewPointer->domainSettingsName != QStringLiteral(""));
+    emit updateUrlLineEdit(currentPrivacyWebEngineViewPointer->url());
 
-    // Update the status of the forward and back buttons.
-    emit updateBackAction(currentWebEngineHistoryPointer->canGoBack());
-    emit updateForwardAction(currentWebEngineHistoryPointer->canGoForward());
+    // Update the find text.
+    emit updateFindText(currentPrivacyWebEngineViewPointer->findString, currentPrivacyWebEngineViewPointer->findCaseSensitive);
+    emit updateFindTextResults(currentPrivacyWebEngineViewPointer->findTextResult);
 
-    // Reapply the zoom factor.  This is a bug in QWebEngineView that resets the zoom with every load.  <https://redmine.stoutner.com/issues/799>
-    currentPrivacyWebEngineViewPointer->setZoomFactor(currentZoomFactor);
+    // Update the progress bar.
+    if (currentPrivacyWebEngineViewPointer->loadProgressInt >= 0)
+        emit showProgressBar(currentPrivacyWebEngineViewPointer->loadProgressInt);
+    else
+        emit hideProgressBar();
 }