From 8270604d8ff9c8f2009fc945040b8ce8545741d0 Mon Sep 17 00:00:00 2001 From: Lu Zhen Date: Wed, 3 Apr 2024 14:44:55 +0800 Subject: [PATCH] fix: [extension] auto zoom web view Log: Change-Id: Ib1755c34dfd394675631d76371c663697ba709d7 --- src/plugins/core/gui/plugindetailsview.cpp | 35 +++++++++++++++++++++- src/plugins/core/gui/plugindetailsview.h | 5 ++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/plugins/core/gui/plugindetailsview.cpp b/src/plugins/core/gui/plugindetailsview.cpp index dbab32cc8..9f0a16d7f 100644 --- a/src/plugins/core/gui/plugindetailsview.cpp +++ b/src/plugins/core/gui/plugindetailsview.cpp @@ -18,10 +18,43 @@ #include #include #include +#include +#include DWIDGET_USE_NAMESPACE using namespace dpfservice; +class AutoZoomWebEngineView : public QWebEngineView { +public: + explicit AutoZoomWebEngineView(QWidget *parent = nullptr) + : QWebEngineView(parent) {} + +protected: + void resizeEvent(QResizeEvent *event) override { + QWebEngineView::resizeEvent(event); + + // first resize is full screen + if (isFirstResize) { + isFirstResize = false; + return; + } + + QSize newSize = event->size(); + qreal zoomFactor = calculateZoomFactor(newSize); + setZoomFactor(zoomFactor); + } + + qreal calculateZoomFactor(const QSize &size) { + if (size.width() > maxWidth) + maxWidth = size.width(); + qreal zoomFactor = static_cast(size.width()) / maxWidth; + return zoomFactor; + } +private: + int maxWidth = 0; + bool isFirstResize = true; +}; + DetailsView::DetailsView(QWidget *parent) : DWidget(parent) { @@ -135,7 +168,7 @@ void DetailsView::setupUi() logoLabel->setPixmap(logo.pixmap(QSize(96, 96))); auto webViewLayout = new QHBoxLayout(); - webView = new QWebEngineView(this); + webView = new AutoZoomWebEngineView(this); webView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); webViewLayout->addWidget(webView); diff --git a/src/plugins/core/gui/plugindetailsview.h b/src/plugins/core/gui/plugindetailsview.h index f0ed50c13..3e7aedb9d 100644 --- a/src/plugins/core/gui/plugindetailsview.h +++ b/src/plugins/core/gui/plugindetailsview.h @@ -14,7 +14,6 @@ QT_BEGIN_NAMESPACE class QGridLayout; class QVBoxLayout; class QSpacerItem; -class QWebEngineView; class QLabel; QT_END_NAMESPACE @@ -22,6 +21,8 @@ DWIDGET_BEGIN_NAMESPACE class DTextEdit; class DLabel; DWIDGET_END_NAMESPACE + +class AutoZoomWebEngineView; class DetailsView : public DTK_WIDGET_NAMESPACE::DWidget { Q_OBJECT @@ -50,7 +51,7 @@ private slots: DTK_WIDGET_NAMESPACE::DPushButton *loadBtn {nullptr}; dpf::PluginMetaObjectPointer pluginMetaInfo; - QWebEngineView *webView {nullptr}; + AutoZoomWebEngineView *webView {nullptr}; QLabel *logoLabel {nullptr}; };