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

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

References cleanupOnQuit(), m_devicePixelRatioF, and m_iconFetchThreadPool.

◆ ~KisRecentFileIconCache()

KisRecentFileIconCache::~KisRecentFileIconCache ( )

Definition at line 82 of file KisRecentFileIconCache.cpp.

82{}

Member Function Documentation

◆ cleanupOnQuit

void KisRecentFileIconCache::cleanupOnQuit ( )
privateslot

Definition at line 143 of file KisRecentFileIconCache.cpp.

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

References m_iconFetchThreadPool.

◆ fileIconChanged

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

◆ futureCanceled

void KisRecentFileIconCache::futureCanceled ( )
privateslot

Definition at line 179 of file KisRecentFileIconCache.cpp.

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

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

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

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

References fileIconChanged(), and m_iconCacheMap.

◆ instance()

KisRecentFileIconCache * KisRecentFileIconCache::instance ( )
static

Definition at line 86 of file KisRecentFileIconCache.cpp.

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

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

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

References m_iconCacheMap.

◆ reloadFileIcon()

void KisRecentFileIconCache::reloadFileIcon ( const QUrl & url)

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

Definition at line 137 of file KisRecentFileIconCache.cpp.

138{
141}
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: