diff options
author | Tarja Sundqvist <tarja.sundqvist@qt.io> | 2024-06-11 13:21:32 +0300 |
---|---|---|
committer | Tarja Sundqvist <tarja.sundqvist@qt.io> | 2024-06-11 13:31:01 +0300 |
commit | 3cd4e0aa7961889eb06374885ed648f5b9bd85bc (patch) | |
tree | 6a089aedea155b1f1a74b553f4d1b57c70554a8f /examples/webenginewidgets/simplebrowser | |
parent | 6d029165d1593c514db08c34d6b08a00a4435d5e (diff) | |
parent | 38063a6332b9f948a0381763271e9a9ac7af0999 (diff) |
Merge tag 'v6.2.9-lts' into tqtc/lts-6.2-opensource6.2.9
Qt 6.2.9-lts release
Conflicts solved:
dependencies.yaml
examples/webenginewidgets/push-notifications/CMakeLists.txt
Change-Id: I0127c2575369f6939df89f3301659470d481b9bf
Diffstat (limited to 'examples/webenginewidgets/simplebrowser')
14 files changed, 70 insertions, 95 deletions
diff --git a/examples/webenginewidgets/simplebrowser/CMakeLists.txt b/examples/webenginewidgets/simplebrowser/CMakeLists.txt index f23bce709..2dd4fd57a 100644 --- a/examples/webenginewidgets/simplebrowser/CMakeLists.txt +++ b/examples/webenginewidgets/simplebrowser/CMakeLists.txt @@ -1,3 +1,6 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + cmake_minimum_required(VERSION 3.16) project(simplebrowser LANGUAGES CXX) diff --git a/examples/webenginewidgets/simplebrowser/browserwindow.cpp b/examples/webenginewidgets/simplebrowser/browserwindow.cpp index 0458cd1f4..d5b097b22 100644 --- a/examples/webenginewidgets/simplebrowser/browserwindow.cpp +++ b/examples/webenginewidgets/simplebrowser/browserwindow.cpp @@ -25,14 +25,6 @@ BrowserWindow::BrowserWindow(Browser *browser, QWebEngineProfile *profile, bool : m_browser(browser) , m_profile(profile) , m_tabWidget(new TabWidget(profile, this)) - , m_progressBar(nullptr) - , m_historyBackAction(nullptr) - , m_historyForwardAction(nullptr) - , m_stopAction(nullptr) - , m_reloadAction(nullptr) - , m_stopReloadAction(nullptr) - , m_urlLineEdit(nullptr) - , m_favAction(nullptr) { setAttribute(Qt::WA_DeleteOnClose, true); setFocusPolicy(Qt::ClickFocus); @@ -58,7 +50,8 @@ BrowserWindow::BrowserWindow(Browser *browser, QWebEngineProfile *profile, bool m_progressBar->setMaximumHeight(1); m_progressBar->setTextVisible(false); - m_progressBar->setStyleSheet(QStringLiteral("QProgressBar {border: 0px} QProgressBar::chunk {background-color: #da4453}")); + m_progressBar->setStyleSheet(QStringLiteral( + "QProgressBar {border: 0px} QProgressBar::chunk {background-color: #da4453}")); layout->addWidget(m_progressBar); } @@ -266,11 +259,19 @@ QMenu *BrowserWindow::createWindowMenu(TabWidget *tabWidget) previousTabAction->setShortcuts(shortcuts); connect(previousTabAction, &QAction::triggered, tabWidget, &TabWidget::previousTab); - connect(menu, &QMenu::aboutToShow, [this, menu, nextTabAction, previousTabAction]() { + QAction *inspectorAction = new QAction(tr("Open inspector in new window"), this); + shortcuts.clear(); + shortcuts.append(QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_I)); + inspectorAction->setShortcuts(shortcuts); + connect(inspectorAction, &QAction::triggered, [this]() { emit currentTab()->devToolsRequested(currentTab()->page()); }); + + connect(menu, &QMenu::aboutToShow, [this, menu, nextTabAction, previousTabAction, inspectorAction]() { menu->clear(); menu->addAction(nextTabAction); menu->addAction(previousTabAction); menu->addSeparator(); + menu->addAction(inspectorAction); + menu->addSeparator(); QList<BrowserWindow*> windows = m_browser->windows(); int index(-1); @@ -292,6 +293,20 @@ QMenu *BrowserWindow::createHelpMenu() return helpMenu; } +static bool isBackspace(const QKeySequence &k) +{ + return (k[0].key() & Qt::Key_unknown) == Qt::Key_Backspace; +} + +// Chromium already handles navigate on backspace when appropriate. +static QList<QKeySequence> removeBackspace(QList<QKeySequence> keys) +{ + const auto it = std::find_if(keys.begin(), keys.end(), isBackspace); + if (it != keys.end()) + keys.erase(it); + return keys; +} + QToolBar *BrowserWindow::createToolBar() { QToolBar *navigationBar = new QToolBar(tr("Navigation")); @@ -299,14 +314,7 @@ QToolBar *BrowserWindow::createToolBar() navigationBar->toggleViewAction()->setEnabled(false); m_historyBackAction = new QAction(this); - QList<QKeySequence> backShortcuts = QKeySequence::keyBindings(QKeySequence::Back); - for (auto it = backShortcuts.begin(); it != backShortcuts.end();) { - // Chromium already handles navigate on backspace when appropriate. - if ((*it)[0].key() == Qt::Key_Backspace) - it = backShortcuts.erase(it); - else - ++it; - } + auto backShortcuts = removeBackspace(QKeySequence::keyBindings(QKeySequence::Back)); // For some reason Qt doesn't bind the dedicated Back key to Back. backShortcuts.append(QKeySequence(Qt::Key_Back)); m_historyBackAction->setShortcuts(backShortcuts); @@ -319,13 +327,7 @@ QToolBar *BrowserWindow::createToolBar() navigationBar->addAction(m_historyBackAction); m_historyForwardAction = new QAction(this); - QList<QKeySequence> fwdShortcuts = QKeySequence::keyBindings(QKeySequence::Forward); - for (auto it = fwdShortcuts.begin(); it != fwdShortcuts.end();) { - if (((*it)[0].key() & Qt::Key_unknown) == Qt::Key_Backspace) - it = fwdShortcuts.erase(it); - else - ++it; - } + auto fwdShortcuts = removeBackspace(QKeySequence::keyBindings(QKeySequence::Forward)); fwdShortcuts.append(QKeySequence(Qt::Key_Forward)); m_historyForwardAction->setShortcuts(fwdShortcuts); m_historyForwardAction->setIconVisibleInMenu(false); @@ -352,9 +354,8 @@ QToolBar *BrowserWindow::createToolBar() downloadsAction->setIcon(QIcon(QStringLiteral(":go-bottom.png"))); downloadsAction->setToolTip(tr("Show downloads")); navigationBar->addAction(downloadsAction); - connect(downloadsAction, &QAction::triggered, [this]() { - m_browser->downloadManagerWidget().show(); - }); + connect(downloadsAction, &QAction::triggered, + &m_browser->downloadManagerWidget(), &QWidget::show); return navigationBar; } diff --git a/examples/webenginewidgets/simplebrowser/browserwindow.h b/examples/webenginewidgets/simplebrowser/browserwindow.h index 47fdf6314..55eeb46c2 100644 --- a/examples/webenginewidgets/simplebrowser/browserwindow.h +++ b/examples/webenginewidgets/simplebrowser/browserwindow.h @@ -22,7 +22,8 @@ class BrowserWindow : public QMainWindow Q_OBJECT public: - BrowserWindow(Browser *browser, QWebEngineProfile *profile, bool forDevTools = false); + explicit BrowserWindow(Browser *browser, QWebEngineProfile *profile, + bool forDevTools = false); QSize sizeHint() const override; TabWidget *tabWidget() const; WebView *currentTab() const; @@ -55,14 +56,14 @@ private: Browser *m_browser; QWebEngineProfile *m_profile; TabWidget *m_tabWidget; - QProgressBar *m_progressBar; - QAction *m_historyBackAction; - QAction *m_historyForwardAction; - QAction *m_stopAction; - QAction *m_reloadAction; - QAction *m_stopReloadAction; - QLineEdit *m_urlLineEdit; - QAction *m_favAction; + QProgressBar *m_progressBar = nullptr; + QAction *m_historyBackAction = nullptr; + QAction *m_historyForwardAction = nullptr; + QAction *m_stopAction = nullptr; + QAction *m_reloadAction = nullptr; + QAction *m_stopReloadAction = nullptr; + QLineEdit *m_urlLineEdit = nullptr; + QAction *m_favAction = nullptr; QString m_lastSearch; }; diff --git a/examples/webenginewidgets/simplebrowser/downloadmanagerwidget.cpp b/examples/webenginewidgets/simplebrowser/downloadmanagerwidget.cpp index bfb857cd8..fdddc4fb0 100644 --- a/examples/webenginewidgets/simplebrowser/downloadmanagerwidget.cpp +++ b/examples/webenginewidgets/simplebrowser/downloadmanagerwidget.cpp @@ -13,7 +13,6 @@ DownloadManagerWidget::DownloadManagerWidget(QWidget *parent) : QWidget(parent) - , m_numDownloads(0) { setupUi(this); } diff --git a/examples/webenginewidgets/simplebrowser/downloadmanagerwidget.h b/examples/webenginewidgets/simplebrowser/downloadmanagerwidget.h index b9d5e9bd7..67df492b9 100644 --- a/examples/webenginewidgets/simplebrowser/downloadmanagerwidget.h +++ b/examples/webenginewidgets/simplebrowser/downloadmanagerwidget.h @@ -30,7 +30,7 @@ private: void add(DownloadWidget *downloadWidget); void remove(DownloadWidget *downloadWidget); - int m_numDownloads; + int m_numDownloads = 0; }; #endif // DOWNLOADMANAGERWIDGET_H diff --git a/examples/webenginewidgets/simplebrowser/downloadwidget.cpp b/examples/webenginewidgets/simplebrowser/downloadwidget.cpp index d4998853e..f8b96c6e6 100644 --- a/examples/webenginewidgets/simplebrowser/downloadwidget.cpp +++ b/examples/webenginewidgets/simplebrowser/downloadwidget.cpp @@ -38,12 +38,11 @@ inline QString DownloadWidget::withUnit(qreal bytes) { if (bytes < (1 << 10)) return tr("%L1 B").arg(bytes); - else if (bytes < (1 << 20)) + if (bytes < (1 << 20)) return tr("%L1 KiB").arg(bytes / (1 << 10), 0, 'f', 2); - else if (bytes < (1 << 30)) + if (bytes < (1 << 30)) return tr("%L1 MiB").arg(bytes / (1 << 20), 0, 'f', 2); - else - return tr("%L1 GiB").arg(bytes / (1 << 30), 0, 'f', 2); + return tr("%L1 GiB").arg(bytes / (1 << 30), 0, 'f', 2); } void DownloadWidget::updateWidget() @@ -63,16 +62,14 @@ void DownloadWidget::updateWidget() m_progressBar->setDisabled(false); m_progressBar->setFormat( tr("%p% - %1 of %2 downloaded - %3/s") - .arg(withUnit(receivedBytes)) - .arg(withUnit(totalBytes)) - .arg(withUnit(bytesPerSecond))); + .arg(withUnit(receivedBytes), withUnit(totalBytes), + withUnit(bytesPerSecond))); } else { m_progressBar->setValue(0); m_progressBar->setDisabled(false); m_progressBar->setFormat( tr("unknown size - %1 downloaded - %2/s") - .arg(withUnit(receivedBytes)) - .arg(withUnit(bytesPerSecond))); + .arg(withUnit(receivedBytes), withUnit(bytesPerSecond))); } break; case QWebEngineDownloadRequest::DownloadCompleted: @@ -80,16 +77,14 @@ void DownloadWidget::updateWidget() m_progressBar->setDisabled(true); m_progressBar->setFormat( tr("completed - %1 downloaded - %2/s") - .arg(withUnit(receivedBytes)) - .arg(withUnit(bytesPerSecond))); + .arg(withUnit(receivedBytes), withUnit(bytesPerSecond))); break; case QWebEngineDownloadRequest::DownloadCancelled: m_progressBar->setValue(0); m_progressBar->setDisabled(true); m_progressBar->setFormat( tr("cancelled - %1 downloaded - %2/s") - .arg(withUnit(receivedBytes)) - .arg(withUnit(bytesPerSecond))); + .arg(withUnit(receivedBytes), withUnit(bytesPerSecond))); break; case QWebEngineDownloadRequest::DownloadInterrupted: m_progressBar->setValue(0); diff --git a/examples/webenginewidgets/simplebrowser/main.cpp b/examples/webenginewidgets/simplebrowser/main.cpp index 477a8171b..b945ef27e 100644 --- a/examples/webenginewidgets/simplebrowser/main.cpp +++ b/examples/webenginewidgets/simplebrowser/main.cpp @@ -5,6 +5,7 @@ #include "browserwindow.h" #include "tabwidget.h" #include <QApplication> +#include <QLoggingCategory> #include <QWebEngineProfile> #include <QWebEngineSettings> @@ -24,6 +25,7 @@ int main(int argc, char **argv) QApplication app(argc, argv); app.setWindowIcon(QIcon(QStringLiteral(":AppLogoColor.png"))); + QLoggingCategory::setFilterRules(QStringLiteral("qt.webenginecontext.debug=true")); QWebEngineProfile::defaultProfile()->settings()->setAttribute(QWebEngineSettings::PluginsEnabled, true); QWebEngineProfile::defaultProfile()->settings()->setAttribute(QWebEngineSettings::DnsPrefetchEnabled, true); diff --git a/examples/webenginewidgets/simplebrowser/tabwidget.cpp b/examples/webenginewidgets/simplebrowser/tabwidget.cpp index 9e19cf782..e4e204ce9 100644 --- a/examples/webenginewidgets/simplebrowser/tabwidget.cpp +++ b/examples/webenginewidgets/simplebrowser/tabwidget.cpp @@ -34,8 +34,8 @@ TabWidget::TabWidget(QWebEngineProfile *profile, QWidget *parent) QLabel *icon = new QLabel(this); QPixmap pixmap(QStringLiteral(":ninja.png")); icon->setPixmap(pixmap.scaledToHeight(tabBar->height())); - setStyleSheet(QStringLiteral("QTabWidget::tab-bar { left: %1px; }"). - arg(icon->pixmap().width())); + setStyleSheet( + QStringLiteral("QTabWidget::tab-bar { left: %1px; }").arg(icon->pixmap().width())); } } diff --git a/examples/webenginewidgets/simplebrowser/tabwidget.h b/examples/webenginewidgets/simplebrowser/tabwidget.h index 08caab52c..a1a893b62 100644 --- a/examples/webenginewidgets/simplebrowser/tabwidget.h +++ b/examples/webenginewidgets/simplebrowser/tabwidget.h @@ -19,7 +19,7 @@ class TabWidget : public QTabWidget Q_OBJECT public: - TabWidget(QWebEngineProfile *profile, QWidget *parent = nullptr); + explicit TabWidget(QWebEngineProfile *profile, QWidget *parent = nullptr); WebView *currentWebView() const; diff --git a/examples/webenginewidgets/simplebrowser/webpage.cpp b/examples/webenginewidgets/simplebrowser/webpage.cpp index 66de5d6d9..699e3d2ed 100644 --- a/examples/webenginewidgets/simplebrowser/webpage.cpp +++ b/examples/webenginewidgets/simplebrowser/webpage.cpp @@ -22,29 +22,6 @@ void WebPage::handleCertificateError(QWebEngineCertificateError error) [this, error]() mutable { emit createCertificateErrorDialog(error); }); } -inline QString questionForFeature(QWebEnginePage::Feature feature) -{ - switch (feature) { - case QWebEnginePage::Geolocation: - return WebPage::tr("Allow %1 to access your location information?"); - case QWebEnginePage::MediaAudioCapture: - return WebPage::tr("Allow %1 to access your microphone?"); - case QWebEnginePage::MediaVideoCapture: - return WebPage::tr("Allow %1 to access your webcam?"); - case QWebEnginePage::MediaAudioVideoCapture: - return WebPage::tr("Allow %1 to access your microphone and webcam?"); - case QWebEnginePage::MouseLock: - return WebPage::tr("Allow %1 to lock your mouse cursor?"); - case QWebEnginePage::DesktopVideoCapture: - return WebPage::tr("Allow %1 to capture video of your desktop?"); - case QWebEnginePage::DesktopAudioVideoCapture: - return WebPage::tr("Allow %1 to capture audio and video of your desktop?"); - case QWebEnginePage::Notifications: - return WebPage::tr("Allow %1 to show notification on your desktop?"); - } - return QString(); -} - void WebPage::handleSelectClientCertificate(QWebEngineClientCertificateSelection selection) { // Just select one. diff --git a/examples/webenginewidgets/simplebrowser/webpage.h b/examples/webenginewidgets/simplebrowser/webpage.h index 7fa2be335..83a4e833f 100644 --- a/examples/webenginewidgets/simplebrowser/webpage.h +++ b/examples/webenginewidgets/simplebrowser/webpage.h @@ -13,7 +13,7 @@ class WebPage : public QWebEnginePage Q_OBJECT public: - WebPage(QWebEngineProfile *profile, QObject *parent = nullptr); + explicit WebPage(QWebEngineProfile *profile, QObject *parent = nullptr); signals: void createCertificateErrorDialog(QWebEngineCertificateError error); diff --git a/examples/webenginewidgets/simplebrowser/webpopupwindow.h b/examples/webenginewidgets/simplebrowser/webpopupwindow.h index d13f5f183..0726bf0c2 100644 --- a/examples/webenginewidgets/simplebrowser/webpopupwindow.h +++ b/examples/webenginewidgets/simplebrowser/webpopupwindow.h @@ -19,7 +19,7 @@ class WebPopupWindow : public QWidget Q_OBJECT public: - WebPopupWindow(QWebEngineProfile *profile); + explicit WebPopupWindow(QWebEngineProfile *profile); WebView *view() const; private slots: diff --git a/examples/webenginewidgets/simplebrowser/webview.cpp b/examples/webenginewidgets/simplebrowser/webview.cpp index 16a58ce6c..e41d4334e 100644 --- a/examples/webenginewidgets/simplebrowser/webview.cpp +++ b/examples/webenginewidgets/simplebrowser/webview.cpp @@ -19,7 +19,6 @@ WebView::WebView(QWidget *parent) : QWebEngineView(parent) - , m_loadProgress(100) { connect(this, &QWebEngineView::loadStarted, [this]() { m_loadProgress = 0; @@ -57,7 +56,7 @@ WebView::WebView(QWidget *parent) tr("Render process exited with code: %1\n" "Do you want to reload the page ?").arg(statusCode)); if (btn == QMessageBox::Yes) - QTimer::singleShot(0, [this] { reload(); }); + QTimer::singleShot(0, this, &WebView::reload); }); } @@ -149,13 +148,15 @@ QIcon WebView::favIcon() const if (m_loadProgress < 0) { static QIcon errorIcon(QStringLiteral(":dialog-error.png")); return errorIcon; - } else if (m_loadProgress < 100) { + } + + if (m_loadProgress < 100) { static QIcon loadingIcon(QStringLiteral(":view-refresh.png")); return loadingIcon; - } else { - static QIcon defaultIcon(QStringLiteral(":text-html.png")); - return defaultIcon; } + + static QIcon defaultIcon(":text-html.png"); + return defaultIcon; } QWebEngineView *WebView::createWindow(QWebEnginePage::WebWindowType type) @@ -193,12 +194,8 @@ void WebView::contextMenuEvent(QContextMenuEvent *event) if (viewSource == actions.cend()) menu->addSeparator(); - QAction *action = new QAction(menu); - action->setText("Open inspector in new window"); + QAction *action = menu->addAction("Open inspector in new window"); connect(action, &QAction::triggered, [this]() { emit devToolsRequested(page()); }); - - QAction *before(inspectElement == actions.cend() ? nullptr : *inspectElement); - menu->insertAction(before, action); } else { (*inspectElement)->setText(tr("Inspect element")); } @@ -239,8 +236,8 @@ void WebView::handleAuthenticationRequired(const QUrl &requestUrl, QAuthenticato passwordDialog.m_iconLabel->setPixmap(icon.pixmap(32, 32)); QString introMessage(tr("Enter username and password for \"%1\" at %2") - .arg(auth->realm()) - .arg(requestUrl.toString().toHtmlEscaped())); + .arg(auth->realm(), + requestUrl.toString().toHtmlEscaped())); passwordDialog.m_infoLabel->setText(introMessage); passwordDialog.m_infoLabel->setWordWrap(true); diff --git a/examples/webenginewidgets/simplebrowser/webview.h b/examples/webenginewidgets/simplebrowser/webview.h index 63f82c3c9..41bc04ac0 100644 --- a/examples/webenginewidgets/simplebrowser/webview.h +++ b/examples/webenginewidgets/simplebrowser/webview.h @@ -20,7 +20,7 @@ class WebView : public QWebEngineView Q_OBJECT public: - WebView(QWidget *parent = nullptr); + explicit WebView(QWidget *parent = nullptr); void setPage(WebPage *page); int loadProgress() const; @@ -51,7 +51,7 @@ private: void createWebActionTrigger(QWebEnginePage *page, QWebEnginePage::WebAction); private: - int m_loadProgress; + int m_loadProgress = 100; }; #endif |