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

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 connect(qApp, SIGNAL(aboutToQuit()), SLOT(cleanupOnQuit()));
76}
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))

References cleanupOnQuit(), connect(), and m_iconFetchThreadPool.

◆ ~KisRecentFileIconCache()

KisRecentFileIconCache::~KisRecentFileIconCache ( )

Definition at line 78 of file KisRecentFileIconCache.cpp.

78{}

Member Function Documentation

◆ cleanupOnQuit

void KisRecentFileIconCache::cleanupOnQuit ( )
privateslot

Definition at line 139 of file KisRecentFileIconCache.cpp.

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

References m_iconFetchThreadPool.

◆ fileIconChanged

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

◆ futureCanceled

void KisRecentFileIconCache::futureCanceled ( )
privateslot

Definition at line 175 of file KisRecentFileIconCache.cpp.

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

◆ 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 91 of file KisRecentFileIconCache.cpp.

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

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

◆ iconFetched

void KisRecentFileIconCache::iconFetched ( )
privateslot

Definition at line 147 of file KisRecentFileIconCache.cpp.

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

References fileIconChanged(), and m_iconCacheMap.

◆ instance()

KisRecentFileIconCache * KisRecentFileIconCache::instance ( )
static

Definition at line 82 of file KisRecentFileIconCache.cpp.

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

◆ 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 119 of file KisRecentFileIconCache.cpp.

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

References m_iconCacheMap.

◆ reloadFileIcon()

void KisRecentFileIconCache::reloadFileIcon ( const QUrl & url)

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

Definition at line 133 of file KisRecentFileIconCache.cpp.

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

References getOrQueueFileIcon(), and invalidateFileIcon().

Member Data Documentation

◆ 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: