25 : QObject(parent), maxThumbnails_(maxThumbnails), maxHeroImages_(maxHeroImages)
37 thumbnailCache_.clear();
38 thumbnailLRU_.clear();
41 imagePathCache_.clear();
55 std::vector<ImageType> fallbackChain;
76 fallbackChain = {preferred};
83 const std::string cacheKey = game->
id +
"_" + std::to_string(
static_cast<int>(type));
84 auto cacheIt = imagePathCache_.find(cacheKey);
86 if (cacheIt != imagePathCache_.end())
89 if (!cacheIt->second.isEmpty())
90 return cacheIt->second;
95 const QString qpath = QString::fromStdString(path);
97 if (!path.empty() && QFile::exists(qpath))
99 imagePathCache_[cacheKey] = qpath;
103 imagePathCache_[cacheKey] = QString();
113void GameThumbnailCache::touchThumbEntry(
const std::string& key)
115 auto it = std::find(thumbnailLRU_.begin(), thumbnailLRU_.end(), key);
116 if (it != thumbnailLRU_.end())
117 thumbnailLRU_.erase(it);
118 thumbnailLRU_.push_front(key);
129 auto quantize = [](
int v) {
return ((v + 8) / 16) * 16; };
130 const QSize qSize(quantize(logicalSize.width()), quantize(logicalSize.height()));
134 const QSize physicalSize(
static_cast<int>(qRound(qSize.width() * devicePixelRatio)),
135 static_cast<int>(qRound(qSize.height() * devicePixelRatio)));
137 const std::string cacheKey = game->
id +
"_" + std::to_string(qSize.width()) +
"x" +
138 std::to_string(qSize.height()) +
"_" +
139 std::to_string(
static_cast<int>(preferred));
141 auto it = thumbnailCache_.find(cacheKey);
142 if (it != thumbnailCache_.end())
144 touchThumbEntry(cacheKey);
149 if (imagePath.isEmpty())
152 QPixmap original(imagePath);
153 if (original.isNull())
157 QPixmap scaled = original.scaled(physicalSize, Qt::KeepAspectRatio, Qt::FastTransformation);
158 scaled.setDevicePixelRatio(devicePixelRatio);
161 while (thumbnailCache_.size() >= maxThumbnails_ && !thumbnailLRU_.empty())
163 const std::string& lruKey = thumbnailLRU_.back();
164 thumbnailCache_.erase(lruKey);
165 thumbnailLRU_.pop_back();
168 thumbnailCache_[cacheKey] = scaled;
169 thumbnailLRU_.push_front(cacheKey);
177void GameThumbnailCache::touchHeroEntry(
const std::string& key)
179 auto it = std::find(heroLRU_.begin(), heroLRU_.end(), key);
180 if (it != heroLRU_.end())
182 heroLRU_.push_front(key);
191 int roundedWidth = ((targetWidth + 50) / 100) * 100;
192 roundedWidth = qMax(roundedWidth, 300);
194 const std::string cacheKey = game->
id +
"_" + std::to_string(roundedWidth);
196 auto it = heroCache_.find(cacheKey);
197 if (it != heroCache_.end())
199 touchHeroEntry(cacheKey);
206 if (!heroPath.isEmpty())
208 QPixmap heroPixmap(heroPath);
209 if (!heroPixmap.isNull())
213 const int physicalWidth =
static_cast<int>(qRound(roundedWidth * devicePixelRatio));
214 scaledHero = heroPixmap.scaledToWidth(physicalWidth, Qt::FastTransformation);
215 scaledHero.setDevicePixelRatio(devicePixelRatio);
221 if (heroCache_.size() >= maxHeroImages_)
223 if (!heroLRU_.empty())
225 heroCache_.erase(heroLRU_.back());
229 heroCache_[cacheKey] = scaledHero;
230 heroLRU_.push_front(cacheKey);
240 if (shuttingDown_.load())
245 std::vector<const Game*> gamesCopy = games;
247 QThreadPool::globalInstance()->start(
248 [
this, gamesCopy = std::move(gamesCopy)]()
mutable
250 if (shuttingDown_.load())
257 std::unordered_map<std::string, QString> localCache;
258 for (
const Game* game : gamesCopy)
260 if (shuttingDown_.load())
267 const std::string cacheKey =
268 game->id +
"_" + std::to_string(
static_cast<int>(type));
269 const std::string path = game->getImagePath(type);
270 const QString qpath = QString::fromStdString(path);
271 if (!path.empty() && QFile::exists(qpath))
272 localCache[cacheKey] = qpath;
274 localCache[cacheKey] = QString();
278 if (shuttingDown_.load())
287 QMetaObject::invokeMethod(
288 this, [
this, local = std::move(localCache)]()
mutable
289 { imagePathCache_.merge(local); }, Qt::QueuedConnection);
QPixmap getGameThumbnail(const Game *game, const QSize &logicalSize, ImageType preferred)
Get a logical-size thumbnail with DPR-aware backing store.
~GameThumbnailCache() override
QPixmap getHeroImage(const Game *game, int targetWidth)
Get a hero image scaled to targetWidth logical pixels (preserving aspect ratio).
void clearAll()
Clear all caches (thumbnails, hero images, path validation).
GameThumbnailCache(size_t maxThumbnails=300, size_t maxHeroImages=30, QObject *parent=nullptr)
void buildImagePathCache(const std::vector< const Game * > &games)
Pre-populate the image-path validation cache by stat-ing every image file for every game.
QString findBestImage(const Game *game, ImageType preferred) const
Return the best-available image file path for game, falling back through a type-specific chain when t...
Complete representation of a game in the library.
std::string id
Unique identifier (auto-generated or user-specified).
std::string getImagePath(ImageType type) const
Get path for a specific image type.
ImageType
Types of game images available.
@ HeroImage
Wide banner/hero image (landscape).
@ Capsule
Store capsule image (616x353).
@ VerticalCover
Vertical cover art (600x900, portrait).
@ Header
Small header image (460x215).
qreal primaryRatio()
The primary screen's device pixel ratio.