From 7ef0f9408e3ef1325e2f505809138006485ab422 Mon Sep 17 00:00:00 2001 From: leashi Date: Thu, 2 Jun 2022 17:21:00 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0faviconURLChanged?= =?UTF-8?q?=EF=BC=8CfaviconChanged=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/QCefView.h | 12 +++++++ src/details/CCefClientDelegate.h | 2 ++ .../CCefClientDelegate_DisplayHandler.cpp | 35 ++++++++++++++++++- 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/include/QCefView.h b/include/QCefView.h index d2d79f44..c3d47828 100644 --- a/include/QCefView.h +++ b/include/QCefView.h @@ -257,6 +257,18 @@ class QCEFVIEW_EXPORT QCefView : public QWidget /// The title void titleChanged(const QString& title); + /// + /// Gets called on title changed + /// + /// The urls + void faviconURLChanged(const QStringList& urls); + + /// + /// Gets called on title changed + /// + /// The icon + void faviconChanged(const QIcon &icon); + /// /// Gets called on fullscreen mode changed /// diff --git a/src/details/CCefClientDelegate.h b/src/details/CCefClientDelegate.h index b78115ab..33dcf475 100644 --- a/src/details/CCefClientDelegate.h +++ b/src/details/CCefClientDelegate.h @@ -100,6 +100,8 @@ class CCefClientDelegate : public CefViewBrowserClientDelegateInterface const std::vector& regions) override; virtual void addressChanged(CefRefPtr& browser, int64_t frameId, const std::string& url) override; virtual void titleChanged(CefRefPtr& browser, const std::string& title) override; + virtual void faviconURLChanged(CefRefPtr browser, const std::vector& icon_urls) override; + virtual void faviconChanged(CefRefPtr image) override; virtual void fullscreenModeChanged(CefRefPtr& browser, bool fullscreen) override; virtual bool tooltipMessage(CefRefPtr& browser, const std::string& text) override; virtual void statusMessage(CefRefPtr& browser, const std::string& value) override; diff --git a/src/details/CCefClientDelegate_DisplayHandler.cpp b/src/details/CCefClientDelegate_DisplayHandler.cpp index 9277440a..95672b7a 100644 --- a/src/details/CCefClientDelegate_DisplayHandler.cpp +++ b/src/details/CCefClientDelegate_DisplayHandler.cpp @@ -1,7 +1,8 @@ #include "CCefClientDelegate.h" #include - +#include +#include #include "QCefViewPrivate.h" Qt::CursorShape @@ -104,6 +105,38 @@ CCefClientDelegate::titleChanged(CefRefPtr& browser, const std::stri pCefViewPrivate_->q_ptr->titleChanged(t); } +void +CCefClientDelegate::faviconURLChanged(CefRefPtr browser, const std::vector& icon_urls) +{ + if (!IsValidBrowser(browser)) + return; + + QStringList urls; + for (int i = 0; i < icon_urls.size(); i++) + { + urls.append(QString::fromStdString(icon_urls.at(i).ToString())); + } + pCefViewPrivate_->q_ptr->faviconURLChanged(urls); +} + +void +CCefClientDelegate::faviconChanged(CefRefPtr image) +{ + //图像转换 + int pixel_width = 0; + int pixel_height = 0; + CefRefPtr data = image->GetAsBitmap(1, CEF_COLOR_TYPE_RGBA_8888, CEF_ALPHA_TYPE_OPAQUE, pixel_width, pixel_height); + + size_t size = data->GetSize(); + QScopedPointer buffer(new uchar(size)); + data->GetData(buffer.data(), size, 0); + + QBitmap bitmap = QBitmap::fromData(QSize(pixel_width, pixel_height), buffer.data(), QImage::Format_Mono);//QImage::Format_RGBA8888); + QIcon icon(bitmap); + + pCefViewPrivate_->q_ptr->faviconChanged(icon); +} + void CCefClientDelegate::fullscreenModeChanged(CefRefPtr& browser, bool fullscreen) { From ae3d0612dd3c17cfcdd1dde36246ce3a4f1546a9 Mon Sep 17 00:00:00 2001 From: leashi Date: Fri, 3 Jun 2022 07:50:11 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=AE=8C=E6=88=90favicon=E4=BF=A1=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CCefClientDelegate_DisplayHandler.cpp | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/details/CCefClientDelegate_DisplayHandler.cpp b/src/details/CCefClientDelegate_DisplayHandler.cpp index 95672b7a..000f8487 100644 --- a/src/details/CCefClientDelegate_DisplayHandler.cpp +++ b/src/details/CCefClientDelegate_DisplayHandler.cpp @@ -122,18 +122,25 @@ CCefClientDelegate::faviconURLChanged(CefRefPtr browser, const std:: void CCefClientDelegate::faviconChanged(CefRefPtr image) { + QIcon icon; + //图像转换 - int pixel_width = 0; - int pixel_height = 0; - CefRefPtr data = image->GetAsBitmap(1, CEF_COLOR_TYPE_RGBA_8888, CEF_ALPHA_TYPE_OPAQUE, pixel_width, pixel_height); + int width = 0; + int height = 0; + CefRefPtr data = image->GetAsPNG(1.0, true, width, height); + int bufsize = (int)data->GetSize(); + if (bufsize > 0) + { + QByteArray buffer(bufsize + 4, char(0)); + data->GetData(buffer.data(), bufsize, 0); - size_t size = data->GetSize(); - QScopedPointer buffer(new uchar(size)); - data->GetData(buffer.data(), size, 0); + QBitmap bitmap; + bitmap.loadFromData(buffer); - QBitmap bitmap = QBitmap::fromData(QSize(pixel_width, pixel_height), buffer.data(), QImage::Format_Mono);//QImage::Format_RGBA8888); - QIcon icon(bitmap); + icon = QIcon(bitmap); + } + //发出信号 pCefViewPrivate_->q_ptr->faviconChanged(icon); }