Skip to content
This repository has been archived by the owner on May 10, 2018. It is now read-only.

Commit

Permalink
Load bookmark icons for menus in separate threads
Browse files Browse the repository at this point in the history
Should help with #1679
  • Loading branch information
nowrep committed Dec 10, 2016
1 parent 5298dc9 commit c480460
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/lib/bookmarks/bookmarkstools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,9 @@ void BookmarksTools::addUrlToMenu(QObject* receiver, Menu* menu, BookmarkItem* b
Action* act = new Action(menu);
QString title = QFontMetrics(act->font()).elidedText(bookmark->title(), Qt::ElideRight, 250);
act->setText(title);
act->setIcon(bookmark->icon());
IconProvider::imageForUrlAsync(bookmark->url(), act, [=](const QImage &img) {
act->setIcon(QIcon(QPixmap::fromImage(img)));
});

act->setData(QVariant::fromValue<void*>(static_cast<void*>(bookmark)));
act->setIconVisibleInMenu(true);
Expand Down
14 changes: 13 additions & 1 deletion src/lib/tools/iconprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

#include <QTimer>
#include <QBuffer>
#include <QFutureWatcher>
#include <QtConcurrent/QtConcurrentRun>

Q_GLOBAL_STATIC(IconProvider, qz_icon_provider)

Expand Down Expand Up @@ -179,7 +181,7 @@ QImage IconProvider::imageForUrl(const QUrl &url)

query.addBindValue(QString("%1%").arg(QzTools::escapeSqlString(QString::fromUtf8(url.toEncoded(QUrl::RemoveFragment)))));
query.addBindValue(QL1S("!"));
query.exec();
query = SqlDatabase::instance()->exec(query);

if (query.next()) {
return QImage::fromData(query.value(0).toByteArray());
Expand All @@ -188,6 +190,16 @@ QImage IconProvider::imageForUrl(const QUrl &url)
return IconProvider::emptyWebImage();
}

void IconProvider::imageForUrlAsync(const QUrl &url, QObject *receiver, std::function<void(const QImage &)> callback)
{
QFutureWatcher<QImage> *watcher = new QFutureWatcher<QImage>();
connect(watcher, &QFutureWatcher<QImage>::finished, receiver, [=]() {
watcher->deleteLater();
callback(watcher->result());
});
watcher->setFuture(QtConcurrent::run(imageForUrl, url));
}

QIcon IconProvider::iconForDomain(const QUrl &url)
{
return instance()->iconFromImage(imageForDomain(url));
Expand Down
3 changes: 3 additions & 0 deletions src/lib/tools/iconprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <QImage>
#include <QUrl>

#include <functional>

#include "qzcommon.h"

class QIcon;
Expand Down Expand Up @@ -59,6 +61,7 @@ class QUPZILLA_EXPORT IconProvider : public QWidget
// Icon for url (only available for urls in history)
static QIcon iconForUrl(const QUrl &url);
static QImage imageForUrl(const QUrl &url);
static void imageForUrlAsync(const QUrl &url, QObject *receiver, std::function<void(const QImage&)> callback);

// Icon for domain (only available for urls in history)
static QIcon iconForDomain(const QUrl &url);
Expand Down

0 comments on commit c480460

Please sign in to comment.