Set the tab's icon to the icon of the zim file.

Properly set the icon of the tab when the favicon of the KiwixWebView
is updated.

By default, the icon of QWebEngineView is updated from the favicon set
in the html. But it sometime it is not set.

So we need to load the icon from the zim file, so we have to react
to urlChanged signal, to load the icon when the url's host (the zim file)
changes.
This commit is contained in:
Matthieu Gautier 2018-07-18 17:40:39 +02:00
parent cb21a6816e
commit d1b5624f04
4 changed files with 34 additions and 0 deletions

View File

@ -11,6 +11,7 @@ KiwixWebView::KiwixWebView(QWidget *parent)
auto app = KiwixApp::instance();
profile->installUrlSchemeHandler("zim", app->getSchemeHandler());
profile->setRequestInterceptor(app->getRequestInterceptor());
QObject::connect(this, &QWebEngineView::urlChanged, this, &KiwixWebView::onUrlChanged);
}
KiwixWebView::~KiwixWebView()
@ -27,4 +28,16 @@ QWebEngineView* KiwixWebView::createWindow(QWebEnginePage::WebWindowType type)
return nullptr;
}
void KiwixWebView::onUrlChanged(const QUrl& url) {
if (currentHost != url.host() ) {
currentHost = url.host();
auto app = KiwixApp::instance();
auto reader = app->getLibrary()->getReader(currentHost);
std::string favicon, _mimetype;
reader->getFavicon(favicon, _mimetype);
QPixmap pixmap;
pixmap.loadFromData((const uchar*)favicon.data(), favicon.size());
_icon = QIcon(pixmap);
emit iconChanged(_icon);
}
}

View File

@ -2,18 +2,31 @@
#define KIWIXWEBVIEW_H
#include <QWebEngineView>
#include <QIcon>
#include <kiwix/reader.h>
class KiwixWebView : public QWebEngineView
{
Q_OBJECT
Q_PROPERTY(const QIcon icon READ icon NOTIFY iconChanged);
public:
KiwixWebView(QWidget *parent = Q_NULLPTR);
virtual ~KiwixWebView();
const QIcon &icon() { return _icon; }
public slots:
void onUrlChanged(const QUrl& url);
signals:
void iconChanged(const QIcon& icon);
protected:
virtual QWebEngineView* createWindow(QWebEnginePage::WebWindowType type);
QString currentHost;
QIcon _icon;
};
#endif // KIWIXWEBVIEW_H

View File

@ -11,6 +11,8 @@ KiwixWebView* KTabWidget::createNewTab(bool setCurrent)
KiwixWebView* webView = new KiwixWebView();
QObject::connect(webView, &KiwixWebView::titleChanged, this,
[=](const QString& str) { setTitleOf(webView, str); });
QObject::connect(webView, &KiwixWebView::iconChanged, this,
[=](const QIcon& icon) { setIconOf(webView, icon); });
// Ownership of webview is passed to the tabWidget
addTab(webView, "");
if (setCurrent) {
@ -37,3 +39,8 @@ void KTabWidget::setTitleOf(KiwixWebView* webView, const QString& title)
setTabText(indexOf(webView), title);
}
}
void KTabWidget::setIconOf(KiwixWebView *webView, const QIcon &icon)
{
setTabIcon(indexOf(webView), icon);
}

View File

@ -14,6 +14,7 @@ public:
KiwixWebView* createNewTab(bool setCurrent);
void openUrl(std::shared_ptr<kiwix::Reader> reader, const QUrl &url, bool newTab);
void setTitleOf(KiwixWebView* webView, const QString& title);
void setIconOf(KiwixWebView* webView, const QIcon& icon);
};
#endif // KTABWIDGET_H