10#include <QCoreApplication>
12#include <QImageReader>
24 QMutexLocker lock(&mutex_);
25 return cache_.find(key) != cache_.end();
30 QMutexLocker lock(&mutex_);
32 auto it = cache_.find(key);
33 if (it == cache_.end())
47 QMutexLocker lock(&mutex_);
50 auto it = cache_.find(key);
51 if (it != cache_.end())
59 while (cache_.size() >= maxSize_ && !lruOrder_.empty())
66 lruOrder_.push_front(key);
71 QMutexLocker lock(&mutex_);
78 QMutexLocker lock(&mutex_);
82void ImageCache::evictOldest()
85 if (lruOrder_.empty())
88 QString oldest = lruOrder_.back();
93void ImageCache::touchEntry(
const QString& key)
97 auto it = std::find(lruOrder_.begin(), lruOrder_.end(), key);
98 if (it != lruOrder_.end())
102 lruOrder_.push_front(key);
115 request_(request), generation_(generation)
131 if (loader->generation_.load() != generation_)
135 result.
cacheKey = request_.cacheKey;
139 if (QFile::exists(request_.imagePath))
142 QImageReader reader(request_.imagePath);
143 reader.setAutoTransform(
true);
147 if (request_.targetSize.isValid())
149 QSize originalSize = reader.size();
150 if (originalSize.isValid() &&
151 (originalSize.width() > request_.targetSize.width() * 2 ||
152 originalSize.height() > request_.targetSize.height() * 2))
155 reader.setScaledSize(request_.targetSize);
159 QImage image = reader.read();
163 loader = loader_.data();
164 if (!loader || loader->generation_.load() != generation_)
168 if (request_.targetSize.isValid() && image.size() != request_.targetSize)
170 Qt::TransformationMode mode =
171 request_.useFastScaling ? Qt::FastTransformation : Qt::SmoothTransformation;
173 image = image.scaled(request_.targetSize, Qt::KeepAspectRatio, mode);
178 result.
image = std::move(image);
185 loader = loader_.data();
186 if (!loader || loader->generation_.load() != generation_)
192 QMetaObject::invokeMethod(loader,
"onImageLoadComplete", Qt::QueuedConnection,
197 QPointer<AsyncImageLoader> loader_;
199 uint64_t generation_;
207 : QObject(parent), cache_(std::make_unique<
ImageCache>(300)),
208 threadPool_(QThreadPool::globalInstance())
211 qRegisterMetaType<ImageLoadResult>(
"ImageLoadResult");
220 const QSize& targetSize,
bool useFastScaling,
int priority)
223 if (cache_->contains(cacheKey))
225 QPixmap cached = cache_->get(cacheKey);
227 QMetaObject::invokeMethod(
228 this, [
this, cacheKey, cached]() { emit
imageLoaded(cacheKey, cached); },
229 Qt::QueuedConnection);
235 QMutexLocker lock(&pendingMutex_);
236 if (pendingRequests_.find(cacheKey) != pendingRequests_.end())
241 pendingRequests_[cacheKey] =
true;
251 auto* task =
new ImageLoadTask(
this, request, generation_.load());
252 threadPool_->start(task, priority);
261 QMutexLocker lock(&pendingMutex_);
262 pendingRequests_.clear();
275 return cache_->contains(cacheKey);
280 return cache_->get(cacheKey);
286 auto newCache = std::make_unique<ImageCache>(maxSize);
287 cache_ = std::move(newCache);
292 size = cache_->size();
293 hits = cache_->hitCount();
294 misses = cache_->missCount();
297void AsyncImageLoader::onImageLoadComplete(
const ImageLoadResult& result)
301 QMutexLocker lock(&pendingMutex_);
302 pendingRequests_.erase(result.
cacheKey);
313 pixmap = QPixmap::fromImage(result.
image);
314 cache_->insert(result.
cacheKey, pixmap);
ImageLoadTask(AsyncImageLoader *loader, const ImageLoadRequest &request, uint64_t generation)
void imageLoaded(const QString &cacheKey, const QPixmap &pixmap)
Emitted when an image has been loaded (on main thread).
QPixmap getCached(const QString &cacheKey)
Get cached image directly (for synchronous fallback).
bool isCached(const QString &cacheKey) const
Check if an image is already cached.
~AsyncImageLoader() override
void requestImage(const QString &cacheKey, const QString &imagePath, const QSize &targetSize, bool useFastScaling=true, int priority=0)
Request an image to be loaded asynchronously.
void clearCache()
Clear the image cache.
AsyncImageLoader(QObject *parent=nullptr)
void getCacheStats(size_t &size, size_t &hits, size_t &misses) const
Get cache statistics.
void cancelAll()
Cancel all pending requests (useful when changing views).
void setMaxCacheSize(size_t maxSize)
Set maximum cache size.
Thread-safe LRU cache for scaled images.
bool contains(const QString &key) const
ImageCache(size_t maxSize=300)
QPixmap get(const QString &key)
void insert(const QString &key, const QPixmap &pixmap)
Request for loading an image asynchronously.
Result of an async image load operation.