diff options
author | Allan Sandfeld Jensen <allan.jensen@digia.com> | 2014-07-28 16:35:16 +0200 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@digia.com> | 2014-07-30 12:45:12 +0200 |
commit | a99922affdc7953f092dcfcf34ea741567a5bddc (patch) | |
tree | 3c6a0c272060c4e35b345e17803d76c4e29b641e /examples/webenginewidgets | |
parent | e1b98a164799d90f51336c718724fe8e6db01568 (diff) |
Add api to get the favicon URL
Adds one of the missing pieces of the QWebFrame and QWebView APIs.
Unlike the QtWebKit version this only fetches the favicon URL, and not
the icon. This is because we do not want to implement an icon database,
and that the icon would be loaded asynchronous anyway, bringing no
guarantee to be a valid icon/image yet.
Change-Id: I227311ae3676044da850e687b82bee752b5079c8
Reviewed-by: Jocelyn Turcotte <jocelyn.turcotte@digia.com>
Diffstat (limited to 'examples/webenginewidgets')
-rw-r--r-- | examples/webenginewidgets/browser/browserapplication.cpp | 16 | ||||
-rw-r--r-- | examples/webenginewidgets/browser/browserapplication.h | 7 | ||||
-rw-r--r-- | examples/webenginewidgets/browser/tabwidget.cpp | 6 | ||||
-rw-r--r-- | examples/webenginewidgets/browser/urllineedit.cpp | 11 | ||||
-rw-r--r-- | examples/webenginewidgets/browser/webview.cpp | 32 | ||||
-rw-r--r-- | examples/webenginewidgets/browser/webview.h | 9 |
6 files changed, 60 insertions, 21 deletions
diff --git a/examples/webenginewidgets/browser/browserapplication.cpp b/examples/webenginewidgets/browser/browserapplication.cpp index d1563ef05..89fc698a7 100644 --- a/examples/webenginewidgets/browser/browserapplication.cpp +++ b/examples/webenginewidgets/browser/browserapplication.cpp @@ -74,7 +74,7 @@ DownloadManager *BrowserApplication::s_downloadManager = 0; HistoryManager *BrowserApplication::s_historyManager = 0; -NetworkAccessManager *BrowserApplication::s_networkAccessManager = 0; +QNetworkAccessManager *BrowserApplication::s_networkAccessManager = 0; BookmarksManager *BrowserApplication::s_bookmarksManager = 0; BrowserApplication::BrowserApplication(int &argc, char **argv) @@ -433,7 +433,7 @@ DownloadManager *BrowserApplication::downloadManager() return s_downloadManager; } -NetworkAccessManager *BrowserApplication::networkAccessManager() +QNetworkAccessManager *BrowserApplication::networkAccessManager() { #if defined(QWEBENGINEPAGE_SETNETWORKACCESSMANAGER) if (!s_networkAccessManager) { @@ -442,7 +442,10 @@ NetworkAccessManager *BrowserApplication::networkAccessManager() } return s_networkAccessManager; #else - return 0; + if (!s_networkAccessManager) { + s_networkAccessManager = new QNetworkAccessManager(); + } + return s_networkAccessManager; #endif } @@ -468,7 +471,12 @@ QIcon BrowserApplication::icon(const QUrl &url) const if (!icon.isNull()) return icon.pixmap(16, 16); #endif + return defaultIcon(); +} + +QIcon BrowserApplication::defaultIcon() const +{ if (m_defaultIcon.isNull()) m_defaultIcon = QIcon(QLatin1String(":defaulticon.png")); - return m_defaultIcon.pixmap(16, 16); + return m_defaultIcon; } diff --git a/examples/webenginewidgets/browser/browserapplication.h b/examples/webenginewidgets/browser/browserapplication.h index b17f1cea5..777bef06e 100644 --- a/examples/webenginewidgets/browser/browserapplication.h +++ b/examples/webenginewidgets/browser/browserapplication.h @@ -51,6 +51,7 @@ QT_BEGIN_NAMESPACE class QLocalServer; +class QNetworkAccessManager; QT_END_NAMESPACE class BookmarksManager; @@ -58,7 +59,6 @@ class BrowserMainWindow; class CookieJar; class DownloadManager; class HistoryManager; -class NetworkAccessManager; class BrowserApplication : public QApplication { Q_OBJECT @@ -73,6 +73,7 @@ public: BrowserMainWindow *mainWindow(); QList<BrowserMainWindow*> mainWindows(); QIcon icon(const QUrl &url) const; + QIcon defaultIcon() const; void saveSession(); bool canRestoreSession() const; @@ -80,7 +81,7 @@ public: static HistoryManager *historyManager(); static CookieJar *cookieJar(); static DownloadManager *downloadManager(); - static NetworkAccessManager *networkAccessManager(); + static QNetworkAccessManager *networkAccessManager(); static BookmarksManager *bookmarksManager(); #if defined(Q_WS_MAC) @@ -106,7 +107,7 @@ private: static HistoryManager *s_historyManager; static DownloadManager *s_downloadManager; - static NetworkAccessManager *s_networkAccessManager; + static QNetworkAccessManager *s_networkAccessManager; static BookmarksManager *s_bookmarksManager; QList<QPointer<BrowserMainWindow> > m_mainWindows; diff --git a/examples/webenginewidgets/browser/tabwidget.cpp b/examples/webenginewidgets/browser/tabwidget.cpp index 2503f4b8a..3cb17366f 100644 --- a/examples/webenginewidgets/browser/tabwidget.cpp +++ b/examples/webenginewidgets/browser/tabwidget.cpp @@ -452,12 +452,8 @@ WebView *TabWidget::newTab(bool makeCurrent) urlLineEdit->setWebView(webView); connect(webView, SIGNAL(loadStarted()), this, SLOT(webViewLoadStarted())); - connect(webView, SIGNAL(loadFinished(bool)), - this, SLOT(webViewIconChanged())); -#if defined(QWEBENGINEVIEW_ICONCHANGED) connect(webView, SIGNAL(iconChanged()), this, SLOT(webViewIconChanged())); -#endif connect(webView, SIGNAL(titleChanged(QString)), this, SLOT(webViewTitleChanged(QString))); connect(webView, SIGNAL(urlChanged(QUrl)), @@ -617,7 +613,7 @@ void TabWidget::webViewIconChanged() WebView *webView = qobject_cast<WebView*>(sender()); int index = webViewIndex(webView); if (-1 != index) { - QIcon icon = BrowserApplication::instance()->icon(webView->url()); + QIcon icon = webView->icon(); setTabIcon(index, icon); } } diff --git a/examples/webenginewidgets/browser/urllineedit.cpp b/examples/webenginewidgets/browser/urllineedit.cpp index 306e56e9a..730d3f771 100644 --- a/examples/webenginewidgets/browser/urllineedit.cpp +++ b/examples/webenginewidgets/browser/urllineedit.cpp @@ -263,8 +263,6 @@ UrlLineEdit::UrlLineEdit(QWidget *parent) m_iconLabel->resize(16, 16); setLeftWidget(m_iconLabel); m_defaultBaseColor = palette().color(QPalette::Base); - - webViewIconChanged(); } void UrlLineEdit::setWebView(WebView *webView) @@ -274,12 +272,8 @@ void UrlLineEdit::setWebView(WebView *webView) m_iconLabel->m_webView = webView; connect(webView, SIGNAL(urlChanged(QUrl)), this, SLOT(webViewUrlChanged(QUrl))); - connect(webView, SIGNAL(loadFinished(bool)), - this, SLOT(webViewIconChanged())); -#if defined(QWEBENGINEVIEW_ICONCHANGED) connect(webView, SIGNAL(iconChanged()), this, SLOT(webViewIconChanged())); -#endif connect(webView, SIGNAL(loadProgress(int)), this, SLOT(update())); } @@ -292,9 +286,8 @@ void UrlLineEdit::webViewUrlChanged(const QUrl &url) void UrlLineEdit::webViewIconChanged() { - QUrl url = (m_webView) ? m_webView->url() : QUrl(); - QIcon icon = BrowserApplication::instance()->icon(url); - QPixmap pixmap(icon.pixmap(16, 16)); + Q_ASSERT(m_webView); + QPixmap pixmap = m_webView->icon().pixmap(16, 16); m_iconLabel->setPixmap(pixmap); } diff --git a/examples/webenginewidgets/browser/webview.cpp b/examples/webenginewidgets/browser/webview.cpp index edbaadca9..d4dd2649c 100644 --- a/examples/webenginewidgets/browser/webview.cpp +++ b/examples/webenginewidgets/browser/webview.cpp @@ -311,6 +311,7 @@ WebView::WebView(QWidget* parent) : QWebEngineView(parent) , m_progress(0) , m_page(new WebPage(this)) + , m_iconReply(0) { setPage(m_page); #if defined(QWEBENGINEPAGE_STATUSBARMESSAGE) @@ -323,6 +324,8 @@ WebView::WebView(QWidget* parent) this, SLOT(loadFinished())); connect(page(), SIGNAL(loadingUrl(QUrl)), this, SIGNAL(urlChanged(QUrl))); + connect(page(), SIGNAL(iconUrlChanged(QUrl)), + this, SLOT(onIconUrlChanged(QUrl))); #if defined(QWEBENGINEPAGE_DOWNLOADREQUESTED) connect(page(), SIGNAL(downloadRequested(QNetworkRequest)), this, SLOT(downloadRequested(QNetworkRequest))); @@ -423,6 +426,35 @@ QUrl WebView::url() const return m_initialUrl; } +QIcon WebView::icon() const +{ + if (!m_icon.isNull()) + return m_icon; + return BrowserApplication::instance()->defaultIcon(); +} + +void WebView::onIconUrlChanged(const QUrl &url) +{ + QNetworkRequest iconRequest(url); + m_iconReply = BrowserApplication::networkAccessManager()->get(iconRequest); + m_iconReply->setParent(this); + connect(m_iconReply, SIGNAL(finished()), this, SLOT(iconLoaded())); +} + +void WebView::iconLoaded() +{ + m_icon = QIcon(); + if (m_iconReply) { + QByteArray data = m_iconReply->readAll(); + QPixmap pixmap; + pixmap.loadFromData(data); + m_icon.addPixmap(pixmap); + m_iconReply->deleteLater(); + m_iconReply = 0; + } + emit iconChanged(); +} + void WebView::mousePressEvent(QMouseEvent *event) { m_page->m_pressedButtons = event->buttons(); diff --git a/examples/webenginewidgets/browser/webview.h b/examples/webenginewidgets/browser/webview.h index aaf2aab13..352954c8d 100644 --- a/examples/webenginewidgets/browser/webview.h +++ b/examples/webenginewidgets/browser/webview.h @@ -42,6 +42,7 @@ #ifndef WEBVIEW_H #define WEBVIEW_H +#include <QIcon> #include <QWebEngineView> QT_BEGIN_NAMESPACE @@ -98,6 +99,7 @@ public: void loadUrl(const QUrl &url); QUrl url() const; + QIcon icon() const; QString lastStatusBarText() const; inline int progress() const { return m_progress; } @@ -108,6 +110,9 @@ protected: void contextMenuEvent(QContextMenuEvent *event); void wheelEvent(QWheelEvent *event); +signals: + void iconChanged(); + private slots: void setProgress(int progress); void loadFinished(); @@ -115,12 +120,16 @@ private slots: void downloadRequested(const QNetworkRequest &request); void openLinkInNewTab(); void onFeaturePermissionRequested(const QUrl &securityOrigin, QWebEnginePage::Feature); + void onIconUrlChanged(const QUrl &url); + void iconLoaded(); private: QString m_statusBarText; QUrl m_initialUrl; int m_progress; WebPage *m_page; + QIcon m_icon; + QNetworkReply *m_iconReply; }; #endif |