# class KPixmapCache

###

Module | kdeui
Namespace |
Class | KPixmapCache
Inherits |

General-purpose pixmap cache for KDE.

The pixmap cache can be used to store pixmaps which can later be loaded from the cache very quickly.

Its most common use is storing SVG images which might be expensive to render every time they are used. With the cache you can render each SVG only once and later use the stored version unless the SVG file or requested pixmap size changes.

KPixmapCache's API is similar to that of the QPixmapCache so if you're already using the latter then all you need to do is creating a KPixmapCache object (unlike QPixmapCache, KPixmapCache doesn't have many static methods) and calling insert() and find() method on that object:

    // Create KPixmapCache object
    KPixmapCache* cache = new KPixmapCache("myapp-pixmaps");
    // Load a pixmap
    QPixmap pix;
    if (!cache->find("pixmap-1", pix)) {
    // Pixmap isn't in the cache, create it and insert to cache
    pix = createPixmapFromData();
    cache->insert("pixmap-1", pix);
    }
    // Use pix

The above example illustrates that you can also cache pixmaps created from some data. In case such data is updated, you might need to discard cache contents using discard() method:

    // Discard the cache if it's too old
    if (cache->timestamp() < mydataTimestamp()) {
    cache->discard();
    }
    // Now the cache contains up-to-date data

As demonstrated, you can use cache's timestamp() method to see when the cache was created. If necessary, you can also change the timestamp using setTimestamp() method.

### enums

- RemoveStrategy { RemoveOldest, RemoveSeldomUsed, RemoveLeastRecentlyUsed }

enum details

### methods

- KPixmapCache (name)

  returns ctor
  KDE 4.1.0 and later only

  Argument | Type | Default
  --- | --- | ---
  name | QString |

  Constucts the pixmap cache object.

  name - unique name of the cache

- KPixmapCache (a0)

  returns ctor
  before KDE 4.1.0 only

  Argument | Type | Default
  --- | --- | ---
  a0 | QString |

- cacheLimit ( )

  returns int

  Returns maximum size of the cache (in kilobytes). Default setting is 3 megabytes.

- deleteCache (name)

  returns Nothing
  static

  Argument | Type | Default
  --- | --- | ---
  name | QString |

  Deletes a pixmap cache.

  name - unique name of the cache to be deleted

- discard ( )

  returns Nothing

  Deletes all entries and reinitializes this cache.

- ensureInited ( )

  returns Nothing

  Makes sure that the cache is initialized correctly.

- find (key, pix)

  returns bool

  Argument | Type | Default
  --- | --- | ---
  key | QString |
  pix | QPixmap |

  Tries to load pixmap with the specified key from cache. Returns true when pixmap was found and loaded from cache, false otherwise

- insert (key, pix)

  returns Nothing

  Argument | Type | Default
  --- | --- | ---
  key | QString |
  pix | QPixmap |

  Insert specified pixmap into the cache. If the cache already contains pixmap with the specified key then it is overwritten.

- isEnabled ( )

  returns bool

  Returns true when the cache is enabled. Cache will be disabled when e.g. its data file cannot be created or read.

- isValid ( )

  returns bool

  Returns true when the cache is ready to be used. False usually means that some additional initing has to be done before the cache can be used.

- loadCustomData (stream)

  returns bool

  Argument | Type | Default
  --- | --- | ---
  stream | QDataStream |

  Can be used by subclasses to write custom data into the the stream.

- loadCustomIndexHeader (stream)

  returns bool

  Argument | Type | Default
  --- | --- | ---
  stream | QDataStream |

  Can be used by subclasses to write custom data into cache's header.

- loadFromFile (filename)

  returns QPixmap

  Argument | Type | Default
  --- | --- | ---
  filename | QString |

  Loads pixmap from given file, using the cache. If file's modified-time is more recent than cache's timestamp() , then the cache is discarded.

- loadFromSvg (filename, size)

  returns QPixmap

  Argument | Type | Default
  --- | --- | ---
  filename | QString |
  size | QSize | QSize()

  Same as above, but uses SVG file instead.

  size - size of the pixmap where the SVG is render to. If not given then SVG's default size is used.

- recreateCacheFiles ( )

  returns bool

  Recreates the cache files.

- removeEntries (newsize)

  returns Nothing

  Argument | Type | Default
  --- | --- | ---
  newsize | int | 0

  Removes some of the entries in the cache according to current removeEntryStrategy().

  newsize - wanted size of the cache, in bytes. If 0 is given then current cacheLimit() is used.

  Warning: this works by copying some entries to a new cache and then replacing the old cache with the new one. Thus it might be slow and will temporarily use extra disk space.

- removeEntryStrategy ( )

  returns KPixmapCache.RemoveStrategy

  Returns current entry removal strategy. Default is RemoveLeastRecentlyUsed.

- setCacheLimit (kbytes)

  returns Nothing

  Argument | Type | Default
  --- | --- | ---
  kbytes | int |

  Sets the maximum size of the cache (in kilobytes). If cache gets bigger the limit then some entries are removed (according to removeEntryStrategy() ). Setting cache limit to 0 disables automatic cache size limiting.

  Note that the cleanup might not be done immediately, so the cache might temporarily (for a few seconds) grow bigger than the limit.

- setRemoveEntryStrategy (strategy)

  returns Nothing

  Argument | Type | Default
  --- | --- | ---
  strategy | KPixmapCache.RemoveStrategy |

  Sets the removeEntryStrategy used when removing entries.

- setTimestamp (time)

  returns Nothing

  Argument | Type | Default
  --- | --- | ---
  time | unsigned int |

  Sets the timestamp of app-specific cache. It's saved in the cache file and can later be retrieved using timestamp() method. By default the timestamp is set to the cache creation time.

- setUseQPixmapCache (use)

  returns Nothing

  Argument | Type | Default
  --- | --- | ---
  use | bool |

  Sets whether QPixmapCache (memory caching) should be used in addition to disk cache. QPixmapCache is used by default.

- setValid (valid)

  returns Nothing

  Argument | Type | Default
  --- | --- | ---
  valid | bool |

  Can be used by subclasses to indicate that cache needs some additional initing before it can be used.

- size ( )

  returns int

  Returns approximate size of the cache, in kilobytes.

- timestamp ( )

  returns unsigned int

  Returns timestamp of the cache, set using the setTimestamp method. It can be used by the application to check whether app-specific cache has outdated.

- useQPixmapCache ( )

  returns bool

  Whether QPixmapCache should be used to cache pixmaps in memory in addition to caching them on the disk.

- writeCustomData (stream)

  returns bool

  Argument | Type | Default
  --- | --- | ---
  stream | QDataStream |

  Can be used by subclasses to load custom data from the the stream.

- writeCustomIndexHeader (stream)

  returns Nothing

  Argument | Type | Default
  --- | --- | ---
  stream | QDataStream |

  Can be used by subclasses to load custom data from cache's header.

### enums - detail

- RemoveStrategy { RemoveOldest, RemoveSeldomUsed, RemoveLeastRecentlyUsed }

  Describes which entries will be removed first during cache cleanup.

- RemoveOldest oldest entries are removed first.
- RemoveSeldomUsed least used entries are removed first.
- RemoveLeastRecentlyUsed least recently used entries are removed first.

| RemoveOldest | - | - |
| RemoveSeldomUsed | - | - |
| RemoveLeastRecentlyUsed | - | - |