KGuiAddons 5.109.0
Public Member Functions | List of all members
KSharedPixmapCacheMixin< T >

A simple wrapping layer over KSharedDataCache to support caching images and pixmaps. More...

#include <kimagecache.h>

Public Member Functions

 KSharedPixmapCacheMixin (const QString &cacheName, unsigned defaultCacheSize, unsigned expectedItemSize=0)
 Constructs an image cache, named by cacheName, with a default size of defaultCacheSize.
 
void clear ()
 Removes all entries from the cache.
 
bool findImage (const QString &key, QImage *destination) const
 Copies the cached image identified by key to destination.
 
bool findPixmap (const QString &key, QPixmap *destination) const
 Copies the cached pixmap identified by key to destination.
 
bool insertImage (const QString &key, const QImage &image)
 Inserts the image into the shared cache, accessible with key.
 
bool insertPixmap (const QString &key, const QPixmap &pixmap)
 Inserts the pixmap given by pixmap to the cache, accessible with key.
 

Detailed Description

template<class T>
class KSharedPixmapCacheMixin< T >

A simple wrapping layer over KSharedDataCache to support caching images and pixmaps.

This class can be used to share images between different processes, which is useful when it is known that such images will be used across many processes, or when creating the image is expensive.

In addition, the class also supports caching QPixmaps in a single process using the setPixmapCaching() function.

Tips for use: If you already have QPixmaps that you intend to use, and you do not need access to the actual image data, then try to store and retrieve QPixmaps for use.

On the other hand, if you will need to store and retrieve actual image data (to modify the image after retrieval for instance) then you should use QImage to save the conversion cost from QPixmap to QImage.

KSharedPixmapCacheMixin is a subclass of KSharedDataCache, so all of the methods that can be used with KSharedDataCache can be used with KSharedPixmapCacheMixin, with the exception of KSharedDataCache::insert() and KSharedDataCache::find().

Author
Michael Pyne mpyne.nosp@m.@kde.nosp@m..org
Since
4.5

Constructor & Destructor Documentation

◆ KSharedPixmapCacheMixin()

template<class T >
KSharedPixmapCacheMixin< T >::KSharedPixmapCacheMixin ( const QString &  cacheName,
unsigned  defaultCacheSize,
unsigned  expectedItemSize = 0 
)
inline

Constructs an image cache, named by cacheName, with a default size of defaultCacheSize.

Parameters
cacheNameName of the cache to use.
defaultCacheSizeThe default size, in bytes, of the cache. The actual on-disk size will be slightly larger. If the cache already exists, it will not be resized. If it is required to resize the cache then use the deleteCache() function to remove that cache first.
expectedItemSizeThe expected general size of the items to be added to the image cache, in bytes. Use 0 if you just want a default item size.

Member Function Documentation

◆ clear()

template<class T >
void KSharedPixmapCacheMixin< T >::clear ( )
inline

Removes all entries from the cache.

In addition any cached pixmaps (as per setPixmapCaching()) are also removed.

◆ findImage()

template<class T >
bool KSharedPixmapCacheMixin< T >::findImage ( const QString &  key,
QImage *  destination 
) const
inline

Copies the cached image identified by key to destination.

If no such image exists destination is unchanged.

Returns
true if the image identified by key existed, false otherwise.

◆ findPixmap()

template<class T >
bool KSharedPixmapCacheMixin< T >::findPixmap ( const QString &  key,
QPixmap *  destination 
) const
inline

Copies the cached pixmap identified by key to destination.

If no such pixmap exists destination is unchanged.

Returns
true if the pixmap identified by key existed, false otherwise.
See also
setPixmapCaching()

◆ insertImage()

template<class T >
bool KSharedPixmapCacheMixin< T >::insertImage ( const QString &  key,
const QImage &  image 
)
inline

Inserts the image into the shared cache, accessible with key.

This variant is preferred over insertPixmap() if your source data is already a QImage, if it is essential that the image be in shared memory (such as for SVG icons which have a high render time), or if it will need to be in QImage form after it is retrieved from the cache.

Parameters
keyName to access image with.
imageThe image to add to the shared cache.
Returns
true if the image was successfully cached, false otherwise.

◆ insertPixmap()

template<class T >
bool KSharedPixmapCacheMixin< T >::insertPixmap ( const QString &  key,
const QPixmap &  pixmap 
)
inline

Inserts the pixmap given by pixmap to the cache, accessible with key.

The pixmap must be converted to a QImage in order to be stored into shared memory. In order to prevent unnecessary conversions from taking place pixmap will also be cached (but not in shared memory) and would be accessible using findPixmap() if pixmap caching is enabled.

Parameters
keyName to access pixmap with.
pixmapThe pixmap to add to the cache.
Returns
true if the pixmap was successfully cached, false otherwise.
See also
setPixmapCaching()