1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-11 09:32:12 +01:00

WebView: Implement icon loading

This commit is contained in:
David Rosca 2015-02-08 11:13:12 +01:00
parent 41d9c418e9
commit b94135ead5
7 changed files with 72 additions and 40 deletions

View File

@ -53,7 +53,7 @@ void History::loadSettings()
// AddHistoryEntry
void History::addHistoryEntry(WebView* view)
{
if (!m_isSaving || view->loadingError()) {
if (!m_isSaving) {
return;
}

View File

@ -126,9 +126,7 @@ void LocationBar::setWebView(TabbedWebView* view)
connect(m_webView, SIGNAL(urlChanged(QUrl)), this, SLOT(showUrl(QUrl)));
connect(m_webView, SIGNAL(rssChanged(bool)), this, SLOT(setRssIconVisible(bool)));
connect(m_webView, SIGNAL(privacyChanged(bool)), this, SLOT(setPrivacyState(bool)));
#if QTWEBENGINE_DISABLED
connect(m_webView, SIGNAL(iconChanged()), this, SLOT(updateSiteIcon()));
#endif
}
void LocationBar::setText(const QString &text)

View File

@ -50,9 +50,7 @@ void TabIcon::setWebTab(WebTab* tab)
connect(m_tab->webView(), SIGNAL(loadStarted()), this, SLOT(showLoadingAnimation()));
connect(m_tab->webView(), SIGNAL(loadFinished(bool)), this, SLOT(hideLoadingAnimation()));
#if QTWEBENGINE_DISABLED
connect(m_tab->webView(), SIGNAL(iconChanged()), this, SLOT(showIcon()));
#endif
showIcon();
}

View File

@ -17,6 +17,7 @@
* ============================================================ */
#include "iconprovider.h"
#include "mainapplication.h"
#include "followredirectreply.h"
#include "sqldatabase.h"
#include "autosaver.h"
#include "webview.h"
@ -255,3 +256,30 @@ QIcon IconProvider::iconFromImage(const QImage &image)
{
return QIcon(QPixmap::fromImage(image));
}
// IconLoader
IconLoader::IconLoader(const QUrl &url, QObject* parent)
: QObject(parent)
{
m_reply = new FollowRedirectReply(url, mApp->networkManager());
connect(m_reply, &FollowRedirectReply::finished, this, &IconLoader::finished);
}
IconLoader::~IconLoader()
{
delete m_reply;
}
void IconLoader::finished()
{
if (m_reply->error() != QNetworkReply::NoError) {
emit error();
}
else {
const QByteArray data = m_reply->readAll();
emit iconLoaded(QIcon(QPixmap::fromImage(QImage::fromData(data))));
}
delete m_reply;
m_reply = 0;
}

View File

@ -30,6 +30,7 @@ class QIcon;
class WebView;
class AutoSaver;
class FollowRedirectReply;
// Needs to be QWidget subclass, otherwise qproperty- setting won't work
class QUPZILLA_EXPORT IconProvider : public QWidget
@ -83,4 +84,23 @@ private:
AutoSaver* m_autoSaver;
};
class QUPZILLA_EXPORT IconLoader : public QObject
{
Q_OBJECT
public:
explicit IconLoader(const QUrl &url, QObject* parent = 0);
~IconLoader();
signals:
void iconLoaded(const QIcon &icon);
void error();
private slots:
void finished();
private:
FollowRedirectReply* m_reply;
};
#endif // ICONPROVIDER_H

View File

@ -54,6 +54,7 @@ bool WebView::s_forceContextMenuOnMouseRelease = false;
WebView::WebView(QWidget* parent)
: QWebEngineView(parent)
, m_siteIconLoader(0)
, m_isLoading(false)
, m_progress(0)
#if QTWEBENGINE_DISABLED
@ -69,9 +70,7 @@ WebView::WebView(QWidget* parent)
connect(this, SIGNAL(loadProgress(int)), this, SLOT(slotLoadProgress(int)));
connect(this, SIGNAL(loadFinished(bool)), this, SLOT(slotLoadFinished()));
connect(this, SIGNAL(urlChanged(QUrl)), this, SLOT(slotUrlChanged(QUrl)));
#if QTWEBENGINE_DISABLED
connect(this, SIGNAL(iconChanged()), this, SLOT(slotIconChanged()));
#endif
connect(this, SIGNAL(iconUrlChanged(QUrl)), this, SLOT(slotIconUrlChanged(QUrl)));
m_zoomLevels = zoomLevels();
m_currentZoomLevel = m_zoomLevels.indexOf(100);
@ -218,11 +217,6 @@ void WebView::load(const LoadRequest &request)
loadRequest(searchRequest);
}
bool WebView::loadingError() const
{
return page()->loadingError();
}
bool WebView::isLoading() const
{
return m_isLoading;
@ -244,14 +238,6 @@ bool WebView::hasRss() const
return m_hasRss;
}
#if QTWEBENGINE_DISABLED
QWebElement WebView::activeElement() const
{
QRect activeRect = inputMethodQuery(Qt::ImMicroFocus).toRect();
return page()->mainFrame()->hitTestContent(activeRect.center()).element();
}
#endif
int WebView::zoomLevel() const
{
return m_currentZoomLevel;
@ -436,9 +422,6 @@ void WebView::back()
history->back();
emit urlChanged(url());
#if QTWEBENGINE_DISABLED
emit iconChanged();
#endif
}
}
@ -450,9 +433,6 @@ void WebView::forward()
history->forward();
emit urlChanged(url());
#if QTWEBENGINE_DISABLED
emit iconChanged();
#endif
}
}
@ -514,14 +494,26 @@ void WebView::checkRss()
#endif
}
void WebView::slotIconChanged()
void WebView::slotIconUrlChanged(const QUrl &url)
{
if (!loadingError()) {
m_siteIcon = icon();
m_siteIconUrl = url();
if (m_siteIconUrl == url) {
emit iconChanged();
return;
}
delete m_siteIconLoader;
m_siteIconLoader = new IconLoader(url, this);
connect(m_siteIconLoader, &IconLoader::iconLoaded, [this, url](const QIcon &icon) {
if (icon.isNull())
return;
m_siteIcon = icon;
m_siteIconUrl = url;
emit iconChanged();
IconProvider::instance()->saveIcon(this);
}
});
}
void WebView::slotUrlChanged(const QUrl &url)

View File

@ -20,15 +20,13 @@
#include <QIcon>
#include <QWebEngineView>
#if QTWEBENGINE_DISABLED
#include <QWebElement>
#endif
#include "qzcommon.h"
#include "loadrequest.h"
class WebPage;
class LoadRequest;
class IconLoader;
class QUPZILLA_EXPORT WebView : public QWebEngineView
{
@ -47,16 +45,12 @@ public:
void setPage(QWebEnginePage* page);
void load(const LoadRequest &request);
bool loadingError() const;
bool isLoading() const;
int loadingProgress() const;
void fakeLoadingProgress(int progress);
bool hasRss() const;
#if QTWEBENGINE_DISABLED
QWebElement activeElement() const;
#endif
// Set zoom level (0 - 17)
int zoomLevel() const;
@ -80,6 +74,7 @@ public:
static void setForceContextMenuOnMouseRelease(bool force);
signals:
void iconChanged();
void viewportResized(QSize);
void showNotification(QWidget*);
void privacyChanged(bool);
@ -121,7 +116,7 @@ protected slots:
void slotLoadStarted();
void slotLoadProgress(int progress);
void slotLoadFinished();
void slotIconChanged();
void slotIconUrlChanged(const QUrl &url);
void slotUrlChanged(const QUrl &url);
// Context menu slots
@ -205,6 +200,7 @@ private:
QIcon m_siteIcon;
QUrl m_siteIconUrl;
IconLoader* m_siteIconLoader;
bool m_isLoading;
int m_progress;