From 34cc74ab18db2248b4048eb3e3202f3e50636390 Mon Sep 17 00:00:00 2001 From: David Rosca Date: Tue, 20 Dec 2016 12:16:12 +0100 Subject: [PATCH] Drop the threaded icon loading completely Also fix icons in buttons in bookmarks toolbar --- src/lib/bookmarks/bookmarksmenu.cpp | 4 +-- src/lib/bookmarks/bookmarkstoolbarbutton.cpp | 20 ++++++++++----- src/lib/bookmarks/bookmarkstoolbarbutton.h | 1 + src/lib/bookmarks/bookmarkstools.cpp | 27 ++++++++++++-------- src/lib/bookmarks/bookmarkstools.h | 1 + src/lib/history/historymenu.cpp | 10 ++------ src/lib/tools/iconprovider.cpp | 10 -------- src/lib/tools/iconprovider.h | 1 - 8 files changed, 34 insertions(+), 40 deletions(-) diff --git a/src/lib/bookmarks/bookmarksmenu.cpp b/src/lib/bookmarks/bookmarksmenu.cpp index 8fd06ce3e..355ff8a3b 100644 --- a/src/lib/bookmarks/bookmarksmenu.cpp +++ b/src/lib/bookmarks/bookmarksmenu.cpp @@ -87,9 +87,7 @@ void BookmarksMenu::menuAboutToShow() foreach (QAction *action, menu->actions()) { BookmarkItem *item = static_cast(action->data().value()); if (item && item->type() == BookmarkItem::Url && action->icon().isNull()) { - IconProvider::imageForUrlAsync(item->url(), action, [=](const QImage &img) { - action->setIcon(QIcon(QPixmap::fromImage(img))); - }); + action->setIcon(item->icon()); } } } diff --git a/src/lib/bookmarks/bookmarkstoolbarbutton.cpp b/src/lib/bookmarks/bookmarkstoolbarbutton.cpp index 23376eb6f..b04d802ae 100644 --- a/src/lib/bookmarks/bookmarkstoolbarbutton.cpp +++ b/src/lib/bookmarks/bookmarkstoolbarbutton.cpp @@ -126,12 +126,19 @@ void BookmarksToolbarButton::createMenu() Menu* m = qobject_cast(menu()); Q_ASSERT(m); - foreach (BookmarkItem* child, m_bookmark->children()) { - BookmarksTools::addActionToMenu(this, m, child); - } + BookmarksTools::addFolderContentsToMenu(this, m, m_bookmark); +} - if (m->isEmpty()) { - m->addAction(Bookmarks::tr("Empty"))->setDisabled(true); +void BookmarksToolbarButton::menuAboutToShow() +{ + Q_ASSERT(qobject_cast(sender())); + Menu *menu = static_cast(sender()); + + foreach (QAction *action, menu->actions()) { + BookmarkItem *item = static_cast(action->data().value()); + if (item && item->type() == BookmarkItem::Url && action->icon().isNull()) { + action->setIcon(item->icon()); + } } } @@ -216,9 +223,8 @@ void BookmarksToolbarButton::init() if (m_bookmark->isFolder()) { Menu* m = new Menu(this); - connect(m, SIGNAL(aboutToShow()), this, SLOT(createMenu())); - connect(m, SIGNAL(menuMiddleClicked(Menu*)), this, SLOT(menuMiddleClicked(Menu*))); setMenu(m); + createMenu(); } } diff --git a/src/lib/bookmarks/bookmarkstoolbarbutton.h b/src/lib/bookmarks/bookmarkstoolbarbutton.h index 929cc05cf..1d5bac932 100644 --- a/src/lib/bookmarks/bookmarkstoolbarbutton.h +++ b/src/lib/bookmarks/bookmarkstoolbarbutton.h @@ -49,6 +49,7 @@ public: private slots: void createMenu(); + void menuAboutToShow(); void menuMiddleClicked(Menu* menu); void bookmarkActivated(BookmarkItem* item = 0); void bookmarkCtrlActivated(BookmarkItem* item = 0); diff --git a/src/lib/bookmarks/bookmarkstools.cpp b/src/lib/bookmarks/bookmarkstools.cpp index d7e370ffd..f5f9c5e88 100644 --- a/src/lib/bookmarks/bookmarkstools.cpp +++ b/src/lib/bookmarks/bookmarkstools.cpp @@ -365,20 +365,12 @@ void BookmarksTools::addFolderToMenu(QObject* receiver, Menu* menu, BookmarkItem QString title = QFontMetrics(m->font()).elidedText(folder->title(), Qt::ElideRight, 250); m->setTitle(title); m->setIcon(folder->icon()); - QObject::connect(m, SIGNAL(aboutToShow()), receiver, SLOT(menuAboutToShow())); - QObject::connect(m, SIGNAL(menuMiddleClicked(Menu*)), receiver, SLOT(menuMiddleClicked(Menu*))); + + addFolderContentsToMenu(receiver, m, folder); QAction* act = menu->addMenu(m); act->setData(QVariant::fromValue(static_cast(folder))); act->setIconVisibleInMenu(true); - - foreach (BookmarkItem* child, folder->children()) { - addActionToMenu(receiver, m, child); - } - - if (m->isEmpty()) { - m->addAction(Bookmarks::tr("Empty"))->setDisabled(true); - } } void BookmarksTools::addUrlToMenu(QObject* receiver, Menu* menu, BookmarkItem* bookmark) @@ -390,7 +382,6 @@ 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(false)); act->setData(QVariant::fromValue(static_cast(bookmark))); act->setIconVisibleInMenu(true); @@ -411,6 +402,20 @@ void BookmarksTools::addSeparatorToMenu(Menu* menu, BookmarkItem* separator) menu->addSeparator(); } +void BookmarksTools::addFolderContentsToMenu(QObject *receiver, Menu *menu, BookmarkItem *folder) +{ + QObject::connect(menu, SIGNAL(aboutToShow()), receiver, SLOT(menuAboutToShow())); + QObject::connect(menu, SIGNAL(menuMiddleClicked(Menu*)), receiver, SLOT(menuMiddleClicked(Menu*))); + + foreach (BookmarkItem* child, folder->children()) { + addActionToMenu(receiver, menu, child); + } + + if (menu->isEmpty()) { + menu->addAction(Bookmarks::tr("Empty"))->setDisabled(true); + } +} + bool BookmarksTools::migrateBookmarksIfNecessary(Bookmarks* bookmarks) { QSqlQuery query; diff --git a/src/lib/bookmarks/bookmarkstools.h b/src/lib/bookmarks/bookmarkstools.h index f2f2a9616..a31f34b3d 100644 --- a/src/lib/bookmarks/bookmarkstools.h +++ b/src/lib/bookmarks/bookmarkstools.h @@ -97,6 +97,7 @@ public: static void addFolderToMenu(QObject* receiver, Menu* menu, BookmarkItem* folder); static void addUrlToMenu(QObject* receiver, Menu* menu, BookmarkItem* bookmark); static void addSeparatorToMenu(Menu* menu, BookmarkItem* separator); + static void addFolderContentsToMenu(QObject* receiver, Menu* menu, BookmarkItem* folder); // Migration from Sql Bookmarks (returns true if bookmarks migrated) static bool migrateBookmarksIfNecessary(Bookmarks* bookmarks); diff --git a/src/lib/history/historymenu.cpp b/src/lib/history/historymenu.cpp index dd73abe98..aa83f9079 100644 --- a/src/lib/history/historymenu.cpp +++ b/src/lib/history/historymenu.cpp @@ -99,14 +99,11 @@ void HistoryMenu::aboutToShow() Action* act = new Action(title); act->setData(url); + act->setIcon(IconProvider::iconForUrl(url)); connect(act, SIGNAL(triggered()), this, SLOT(historyEntryActivated())); connect(act, SIGNAL(ctrlTriggered()), this, SLOT(historyEntryCtrlActivated())); connect(act, SIGNAL(shiftTriggered()), this, SLOT(historyEntryShiftActivated())); addAction(act); - - IconProvider::imageForUrlAsync(url, act, [=](const QImage &img) { - act->setIcon(QIcon(QPixmap::fromImage(img))); - }); } } @@ -126,14 +123,11 @@ void HistoryMenu::aboutToShowMostVisited() foreach (const HistoryEntry &entry, mostVisited) { Action* act = new Action(QzTools::truncatedText(entry.title, 40)); act->setData(entry.url); + act->setIcon(IconProvider::iconForUrl(entry.url)); connect(act, SIGNAL(triggered()), this, SLOT(historyEntryActivated())); connect(act, SIGNAL(ctrlTriggered()), this, SLOT(historyEntryCtrlActivated())); connect(act, SIGNAL(shiftTriggered()), this, SLOT(historyEntryShiftActivated())); m_menuMostVisited->addAction(act); - - IconProvider::imageForUrlAsync(entry.url, act, [=](const QImage &img) { - act->setIcon(QIcon(QPixmap::fromImage(img))); - }); } if (m_menuMostVisited->isEmpty()) { diff --git a/src/lib/tools/iconprovider.cpp b/src/lib/tools/iconprovider.cpp index fbb6a10f6..70ff5b186 100644 --- a/src/lib/tools/iconprovider.cpp +++ b/src/lib/tools/iconprovider.cpp @@ -190,16 +190,6 @@ QImage IconProvider::imageForUrl(const QUrl &url, bool allowEmpty) return allowEmpty ? QImage() : IconProvider::emptyWebImage(); } -void IconProvider::imageForUrlAsync(const QUrl &url, const QObject *receiver, std::function callback) -{ - QFutureWatcher *watcher = new QFutureWatcher(); - connect(watcher, &QFutureWatcher::finished, receiver, [=]() { - watcher->deleteLater(); - callback(watcher->result()); - }); - watcher->setFuture(QtConcurrent::run(imageForUrl, url, false)); -} - QIcon IconProvider::iconForDomain(const QUrl &url, bool allowEmpty) { return instance()->iconFromImage(imageForDomain(url, allowEmpty)); diff --git a/src/lib/tools/iconprovider.h b/src/lib/tools/iconprovider.h index 6ed6913fe..b33fcd97d 100644 --- a/src/lib/tools/iconprovider.h +++ b/src/lib/tools/iconprovider.h @@ -61,7 +61,6 @@ public: // Icon for url (only available for urls in history) static QIcon iconForUrl(const QUrl &url, bool allowEmpty = false); static QImage imageForUrl(const QUrl &url, bool allowEmpty = false); - static void imageForUrlAsync(const QUrl &url, const QObject *receiver, std::function callback); // Icon for domain (only available for urls in history) static QIcon iconForDomain(const QUrl &url, bool allowEmpty = false);