From a5b044e58532f7bb3106730cae53329258cec2dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9F=B3=E5=8D=9A=E6=96=87?= Date: Thu, 15 Jun 2017 16:41:40 +0800 Subject: [PATCH] appitem: make fetch icon asynchronous Change-Id: I89c32ccddc7529df75c24ddc4beef83a89037fe5 --- frame/frame.pro | 2 +- frame/item/appitem.cpp | 51 ++++++++++++++++++++++++++++++++----- frame/item/appitem.h | 4 +++ frame/util/themeappicon.cpp | 2 +- frame/util/themeappicon.h | 2 +- 5 files changed, 51 insertions(+), 10 deletions(-) diff --git a/frame/frame.pro b/frame/frame.pro index 192ecbb72..ce14de2f8 100644 --- a/frame/frame.pro +++ b/frame/frame.pro @@ -1,7 +1,7 @@ include(../interfaces/interfaces.pri) -QT += core gui widgets dbus x11extras svg +QT += core gui widgets dbus x11extras svg concurrent TARGET = dde-dock DESTDIR = $$_PRO_FILE_PWD_/../ diff --git a/frame/item/appitem.cpp b/frame/item/appitem.cpp index c90efee2e..b10b627c7 100644 --- a/frame/item/appitem.cpp +++ b/frame/item/appitem.cpp @@ -15,6 +15,9 @@ #include #include #include +#include +#include +#include #define APP_DRAG_THRESHOLD 20 @@ -86,8 +89,10 @@ AppItem::AppItem(const QDBusObjectPath &entry, QWidget *parent) connect(m_appPreviewTips, &_PreviewContainer::requestHidePreview, this, &AppItem::hidePopup, Qt::QueuedConnection); connect(m_appPreviewTips, &_PreviewContainer::requestCheckWindows, m_itemEntry, &DBusDockEntry::Check); - updateTitle(); - refershIcon(); + QTimer::singleShot(1, this, [=] { + updateTitle(); + refershIcon(); + }); } AppItem::~AppItem() @@ -474,16 +479,24 @@ void AppItem::refershIcon() const QString icon = m_itemEntry->icon(); const int iconSize = qMin(width(), height()); + QFutureWatcher *smallWatcher = new QFutureWatcher(this); + QFutureWatcher *largeWatcher = new QFutureWatcher(this); + if (DockDisplayMode == Efficient) { - m_smallIcon = ThemeAppIcon::getIcon(icon, iconSize * 0.7); - m_largeIcon = ThemeAppIcon::getIcon(icon, iconSize * 0.9); +// m_smallIcon = ThemeAppIcon::getIcon(icon, iconSize * 0.7); +// m_largeIcon = ThemeAppIcon::getIcon(icon, iconSize * 0.9); + smallWatcher->setFuture(QtConcurrent::run(ThemeAppIcon::getIcon, icon, iconSize * 0.7)); + largeWatcher->setFuture(QtConcurrent::run(ThemeAppIcon::getIcon, icon, iconSize * 0.9)); } else { - m_smallIcon = ThemeAppIcon::getIcon(icon, iconSize * 0.6); - m_largeIcon = ThemeAppIcon::getIcon(icon, iconSize * 0.8); +// m_smallIcon = ThemeAppIcon::getIcon(icon, iconSize * 0.6); +// m_largeIcon = ThemeAppIcon::getIcon(icon, iconSize * 0.8); + smallWatcher->setFuture(QtConcurrent::run(ThemeAppIcon::getIcon, icon, iconSize * 0.6)); + largeWatcher->setFuture(QtConcurrent::run(ThemeAppIcon::getIcon, icon, iconSize * 0.8)); } - update(); + connect(smallWatcher, &QFutureWatcher::finished, this, &AppItem::gotSmallIcon); + connect(largeWatcher, &QFutureWatcher::finished, this, &AppItem::gotLargeIcon); m_updateIconGeometryTimer->start(); } @@ -517,3 +530,27 @@ void AppItem::showPreview() showPopupWindow(m_appPreviewTips, true); } + +void AppItem::gotSmallIcon() +{ + QFutureWatcher *fw = dynamic_cast *>(sender()); + Q_ASSERT(fw); + + m_smallIcon = fw->result(); + + fw->deleteLater(); + + update(); +} + +void AppItem::gotLargeIcon() +{ + QFutureWatcher *fw = dynamic_cast *>(sender()); + Q_ASSERT(fw); + + m_largeIcon = fw->result(); + + fw->deleteLater(); + + update(); +} diff --git a/frame/item/appitem.h b/frame/item/appitem.h index 1d6f5fb43..97b5e75e6 100644 --- a/frame/item/appitem.h +++ b/frame/item/appitem.h @@ -8,6 +8,7 @@ #include #include +#include class AppItem : public DockItem { @@ -56,6 +57,9 @@ private slots: void activeChanged(); void showPreview(); + void gotSmallIcon(); + void gotLargeIcon(); + private: QLabel *m_appNameTips; _PreviewContainer *m_appPreviewTips; diff --git a/frame/util/themeappicon.cpp b/frame/util/themeappicon.cpp index bf4b2ab8c..66cad9758 100644 --- a/frame/util/themeappicon.cpp +++ b/frame/util/themeappicon.cpp @@ -14,7 +14,7 @@ ThemeAppIcon::~ThemeAppIcon() } -const QPixmap ThemeAppIcon::getIcon(const QString iconName, const int size) +QPixmap ThemeAppIcon::getIcon(const QString iconName, const int size) { const int s = size & ~1; diff --git a/frame/util/themeappicon.h b/frame/util/themeappicon.h index e13222ec0..8c6e4e10d 100644 --- a/frame/util/themeappicon.h +++ b/frame/util/themeappicon.h @@ -10,7 +10,7 @@ public: explicit ThemeAppIcon(QObject *parent = 0); ~ThemeAppIcon(); - static const QPixmap getIcon(const QString iconName, const int size); + static QPixmap getIcon(const QString iconName, const int size); }; #endif // THEMEAPPICON_H