game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
AsyncImageLoader Class Reference

Async image loader using QThreadPool. More...

#include <async_image_loader.h>

Inheritance diagram for AsyncImageLoader:
Collaboration diagram for AsyncImageLoader:

Classes

class  ImageLoadTask

Public Member Functions

 AsyncImageLoader (QObject *parent=nullptr)
 ~AsyncImageLoader () override
void cancelAll ()
 Cancel all pending requests (useful when changing views).
void clearCache ()
 Clear the image cache.
QPixmap getCached (const QString &cacheKey)
 Get cached image directly (for synchronous fallback).
void getCacheStats (size_t &size, size_t &hits, size_t &misses) const
 Get cache statistics.
void imageLoaded (const QString &cacheKey, const QPixmap &pixmap)
 Emitted when an image has been loaded (on main thread).
bool isCached (const QString &cacheKey) const
 Check if an image is already cached.
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 setMaxCacheSize (size_t maxSize)
 Set maximum cache size.

Detailed Description

Async image loader using QThreadPool.

Loads and scales images in background threads, emits signals when complete. Automatically manages a thread-safe LRU cache.

Usage: auto* loader = new AsyncImageLoader(this); connect(loader, &AsyncImageLoader::imageLoaded, this, &MyWidget::onImageLoaded); loader->requestImage("game123", "/path/to/image.png", QSize(150, 225));

The loader will:

  1. Check cache first (fast path, no thread spawn)
  2. If miss, queue load task to thread pool
  3. Emit imageLoaded() on main thread when complete

Definition at line 103 of file async_image_loader.h.

Constructor & Destructor Documentation

◆ AsyncImageLoader()

AsyncImageLoader::AsyncImageLoader ( QObject * parent = nullptr)
explicit

◆ ~AsyncImageLoader()

AsyncImageLoader::~AsyncImageLoader ( )
override

Definition at line 214 of file async_image_loader.cpp.

References cancelAll().

Member Function Documentation

◆ cancelAll()

void AsyncImageLoader::cancelAll ( )

Cancel all pending requests (useful when changing views).

Definition at line 255 of file async_image_loader.cpp.

Referenced by ~AsyncImageLoader().

◆ clearCache()

void AsyncImageLoader::clearCache ( )

Clear the image cache.

Definition at line 268 of file async_image_loader.cpp.

◆ getCached()

QPixmap AsyncImageLoader::getCached ( const QString & cacheKey)

Get cached image directly (for synchronous fallback).

Returns
Cached pixmap or null pixmap if not cached

Definition at line 278 of file async_image_loader.cpp.

◆ getCacheStats()

void AsyncImageLoader::getCacheStats ( size_t & size,
size_t & hits,
size_t & misses ) const

Get cache statistics.

Definition at line 290 of file async_image_loader.cpp.

◆ imageLoaded()

void AsyncImageLoader::imageLoaded ( const QString & cacheKey,
const QPixmap & pixmap )

Emitted when an image has been loaded (on main thread).

Parameters
cacheKeyThe key used in requestImage()
pixmapThe loaded and scaled pixmap (null if failed)

Referenced by MainWindow::MainWindow(), and requestImage().

◆ isCached()

bool AsyncImageLoader::isCached ( const QString & cacheKey) const

Check if an image is already cached.

Definition at line 273 of file async_image_loader.cpp.

◆ requestImage()

void AsyncImageLoader::requestImage ( const QString & cacheKey,
const QString & imagePath,
const QSize & targetSize,
bool useFastScaling = true,
int priority = 0 )

Request an image to be loaded asynchronously.

Parameters
cacheKeyUnique key for caching (e.g., "gameId_width_height")
imagePathPath to the image file
targetSizeDesired scaled size
useFastScalingUse Qt::FastTransformation (true) or SmoothTransformation (false)
priorityHigher priority tasks run first (default 0)

If image is already cached, emits imageLoaded immediately (on next event loop). Otherwise queues a background task.

Definition at line 219 of file async_image_loader.cpp.

References ImageLoadRequest::cacheKey, imageLoaded(), ImageLoadRequest::imagePath, ImageLoadRequest::targetSize, and ImageLoadRequest::useFastScaling.

◆ setMaxCacheSize()

void AsyncImageLoader::setMaxCacheSize ( size_t maxSize)

Set maximum cache size.

Definition at line 283 of file async_image_loader.cpp.


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