Krita Source Code Documentation
Loading...
Searching...
No Matches
KisRecentFileIconCache Class Reference

#include <KisRecentFileIconCache.h>

+ Inheritance diagram for KisRecentFileIconCache:

Classes

struct  CacheItem
 

Signals

void fileIconChanged (const QUrl &url, const QIcon &icon)
 

Public Member Functions

QIcon getOrQueueFileIcon (const QUrl &url)
 
void invalidateFileIcon (const QUrl &url)
 
 KisRecentFileIconCache ()
 
void reloadFileIcon (const QUrl &url)
 
 ~KisRecentFileIconCache ()
 

Static Public Member Functions

static KisRecentFileIconCacheinstance ()
 

Private Slots

void cleanupOnQuit ()
 
void futureCanceled ()
 
void iconFetched ()
 

Private Attributes

float m_devicePixelRatioF {1.0}
 
QMap< QUrl, CacheItemm_iconCacheMap
 
QThreadPool m_iconFetchThreadPool
 

Detailed Description

Definition at line 16 of file KisRecentFileIconCache.h.

Constructor & Destructor Documentation

◆ KisRecentFileIconCache()

KisRecentFileIconCache::KisRecentFileIconCache ( )

DO NOT USE! Use instance() instead. This constructor is public only because it is needed by Q_GLOBAL_STATIC.

Definition at line 67 of file KisRecentFileIconCache.cpp.

68{
69 // Limit the number of threads used for icon fetching to prevent it from
70 // impacting normal usage too much, and to prevent it from consuming too
71 // much memory by loading too many large files.
72 if (QThread::idealThreadCount() > 2) {
73 m_iconFetchThreadPool.setMaxThreadCount(2);
74 }
75 if (qApp) {
76 m_devicePixelRatioF = qMax(qApp->devicePixelRatio(), 1.0);
77 }
78 connect(qApp, SIGNAL(aboutToQuit()), SLOT(cleanupOnQuit()));
79}

References cleanupOnQuit(), m_devicePixelRatioF, and m_iconFetchThreadPool.

◆ ~KisRecentFileIconCache()

KisRecentFileIconCache::~KisRecentFileIconCache ( )

Definition at line 81 of file KisRecentFileIconCache.cpp.

81{}

Member Function Documentation

◆ cleanupOnQuit

void KisRecentFileIconCache::cleanupOnQuit ( )
privateslot

Definition at line 142 of file KisRecentFileIconCache.cpp.

143{
144 // We need to wait for the icon fetching to finish before letting qApp
145 // be deleted, because the icon generation relies on qApp.
146 m_iconFetchThreadPool.clear();
147 m_iconFetchThreadPool.waitForDone();
148}

References m_iconFetchThreadPool.

◆ fileIconChanged

void KisRecentFileIconCache::fileIconChanged ( const QUrl & url,
const QIcon & icon )
signal

◆ futureCanceled

void KisRecentFileIconCache::futureCanceled ( )
privateslot

Definition at line 178 of file KisRecentFileIconCache.cpp.

179{
180 auto *watcher = dynamic_cast<QFutureWatcher<IconFetchResult> *>(QObject::sender());
181 if (!watcher) {
182 qWarning() << "KisRecentFileIconCache::futureCanceled() called but sender is not a QFutureWatcher";
183 return;
184 }
185 watcher->deleteLater();
186}

◆ getOrQueueFileIcon()

QIcon KisRecentFileIconCache::getOrQueueFileIcon ( const QUrl & url)

Get a cached icon or queue fetching of the icon.

If the icon is cached and available, the cached icon is returned. Otherwise, a null default-constructed QIcon will be returned, and the fetching of the icon may be queued in a background thread.

Definition at line 94 of file KisRecentFileIconCache.cpp.

95{
96 const QMap<QUrl, CacheItem>::const_iterator findItem = m_iconCacheMap.constFind(url);
97 if (findItem != m_iconCacheMap.constEnd()) {
98 // If the icon is still being fetched, this returns `QIcon()`.
99 return findItem.value().cachedIcon;
100 } else {
101 if (!url.isLocalFile()) {
102 // We don't fetch thumbnails for non-local files.
103 return QIcon();
104 }
106 const GetFileIconParameters param = {
107 url, // m_documentUrl
108 iconSize, // m_iconSize
109 m_devicePixelRatioF, // m_devicePixelRatioF
110 };
111 QFuture<IconFetchResult> future = QtConcurrent::run(&m_iconFetchThreadPool, getFileIcon, param);
112 auto *watcher = new QFutureWatcher<IconFetchResult>(this);
113 watcher->setFuture(future);
114 connect(watcher, SIGNAL(finished()), SLOT(iconFetched()));
115 connect(watcher, SIGNAL(canceled()), SLOT(futureCanceled()));
116 const CacheItem cacheItem = { url, future, QIcon() };
117 m_iconCacheMap.insert(url, cacheItem);
118 return QIcon();
119 }
120}
int iconSize(qreal width, qreal height)
QMap< QUrl, CacheItem > m_iconCacheMap

References futureCanceled(), KisRecentDocumentsModelWrapper::ICON_SIZE_LENGTH, iconFetched(), iconSize(), m_devicePixelRatioF, m_iconCacheMap, and m_iconFetchThreadPool.

◆ iconFetched

void KisRecentFileIconCache::iconFetched ( )
privateslot

Definition at line 150 of file KisRecentFileIconCache.cpp.

151{
152 auto *watcher = dynamic_cast<QFutureWatcher<IconFetchResult> *>(QObject::sender());
153 if (!watcher) {
154 qWarning() << "KisRecentFileIconCache::iconFetched() called but sender is not a QFutureWatcher";
155 return;
156 }
157 QFuture<IconFetchResult> future = watcher->future();
158 watcher->deleteLater();
159 IconFetchResult result = future.result();
160 auto findItem = m_iconCacheMap.find(result.m_documentUrl);
161 if (findItem == m_iconCacheMap.end()) {
162 qWarning() << "KisRecentFileIconCache item not found!";
163 return;
164 }
165#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
166 if (findItem.value().fetchingFuture != future) {
167 qWarning() << "KisRecentFileIconCache item has a different QFuture";
168 return;
169 }
170#endif
171 findItem.value().fetchingFuture = QFuture<IconFetchResult>();
172 if (result.m_iconWasFetchedOk) {
173 findItem.value().cachedIcon = result.m_icon;
174 Q_EMIT fileIconChanged(result.m_documentUrl, result.m_icon);
175 }
176}
void fileIconChanged(const QUrl &url, const QIcon &icon)

References fileIconChanged(), and m_iconCacheMap.

◆ instance()

KisRecentFileIconCache * KisRecentFileIconCache::instance ( )
static

Definition at line 85 of file KisRecentFileIconCache.cpp.

86{
87 if (QThread::currentThread() != qApp->thread()) {
88 qWarning() << "KisRecentFileIconCache::instance() called from non-GUI thread!";
89 return nullptr;
90 }
91 return s_instance;
92}

◆ invalidateFileIcon()

void KisRecentFileIconCache::invalidateFileIcon ( const QUrl & url)

Invalidate (remove) a cached file icon. If the file icon is still in the process of being loaded, its result will be discarded.

Definition at line 122 of file KisRecentFileIconCache.cpp.

123{
124 QMap<QUrl, CacheItem>::iterator findItem = m_iconCacheMap.find(url);
125 if (findItem == m_iconCacheMap.end()) {
126 return;
127 }
128 // Note: Futures returned by `QtConcurrent::run` does not support
129 // cancellation, but we try anyway.
130 if (!findItem.value().fetchingFuture.isCanceled()) {
131 findItem.value().fetchingFuture.cancel();
132 }
133 m_iconCacheMap.erase(findItem);
134}

References m_iconCacheMap.

◆ reloadFileIcon()

void KisRecentFileIconCache::reloadFileIcon ( const QUrl & url)

Invalidate a cached file icon and trigger a reload of it.

Definition at line 136 of file KisRecentFileIconCache.cpp.

137{
140}
QIcon getOrQueueFileIcon(const QUrl &url)
void invalidateFileIcon(const QUrl &url)

References getOrQueueFileIcon(), and invalidateFileIcon().

Member Data Documentation

◆ m_devicePixelRatioF

float KisRecentFileIconCache::m_devicePixelRatioF {1.0}
private

Definition at line 24 of file KisRecentFileIconCache.h.

24{1.0};

◆ m_iconCacheMap

QMap<QUrl, CacheItem> KisRecentFileIconCache::m_iconCacheMap
private

Definition at line 22 of file KisRecentFileIconCache.h.

◆ m_iconFetchThreadPool

QThreadPool KisRecentFileIconCache::m_iconFetchThreadPool
private

Definition at line 23 of file KisRecentFileIconCache.h.


The documentation for this class was generated from the following files: