Development/Tutorials/KPixmapCache: Difference between revisions

From KDE TechBase
No edit summary
(8 intermediate revisions by 6 users not shown)
Line 1: Line 1:
== Introduction ==
== Introduction ==
KPixmapCache provides disk-caching of QPixmaps. If you're using SVGs or generating pixmaps from some data, you might consider using it as it eliminates the need to generate the same pixmaps over and over again.
{{class|KPixmapCache}} provides disk-caching of QPixmaps. If you're using SVGs or generating pixmaps from some data, you might consider using it as it eliminates the need to generate the same pixmaps over and over again.


== Using it ==
== Using it ==
=== Creating the cache object ===
=== Creating the cache object ===
It's API is similar to that of the QPixmapCache.
Its API is similar to that of the QPixmapCache.
To use it, you first need to create the cache object, using a unique name:
To use it, you first need to create the cache object, using a unique name:
<code cppqt>
<syntaxhighlight lang="cpp-qt">
KPixmapCache* cache = new KPixmapCache("myapp-images");
KPixmapCache* cache = new KPixmapCache("myapp-images");
</code>
</syntaxhighlight>


=== Inserting and finding pixmaps ===
=== Inserting and finding pixmaps ===
Once that is done you can easily retrieve and insert images with insert() and find() methods:
Once that is done you can easily retrieve and insert images with insert() and find() methods:
<code cppqt>
<syntaxhighlight lang="cpp-qt">
QPixmap pix("file1.png");
QPixmap pix("file1.png");
cache->insert("file1.png", pix);
cache->insert("file1.png", pix);
Line 25: Line 26:
     cache->insert("file2.png", pix2);
     cache->insert("file2.png", pix2);
}
}
</code>
</syntaxhighlight>


=== The Easy Way ===
=== The Easy Way ===
As loading pixmaps from files with the aid of cache is a very common operation, KPixmapCache provides a convenience method for that:
As loading pixmaps from files with the aid of cache is a very common operation, KPixmapCache provides a convenience method for that:
<code cppqt>
<syntaxhighlight lang="cpp-qt">
QString filename("myfile.png");
QString filename("myfile.png");
QPixmap pix = cache->loadFromFile(filename);
QPixmap pix = cache->loadFromFile(filename);
</code>
</syntaxhighlight>
This first checks if the given file is in the cache. If it is, it's loaded from the cache, otherwise it's loaded from the file and inserted into cache.
This first checks if the given file is in the cache. If it is, it's loaded from the cache, otherwise it's loaded from the file and inserted into cache.


Similar method is provided for loading SVG files:
Similar method is provided for loading SVG files:
<code cppqt>
<syntaxhighlight lang="cpp-qt">
QString filename("myfile.svg");
QString filename("myfile.svg");
QPixmap pix = cache->loadFromSVG(filename);
QPixmap pix = cache->loadFromSvg(filename);
</code>
</syntaxhighlight>
This renders contents of the SVG file onto pixmap, using SVG's default size by default. If you need a different size, you can specify it as the second argument of that method:
This renders contents of the SVG file onto pixmap, using SVG's default size by default. If you need a different size, you can specify it as the second argument of that method:
<code cppqt>
<syntaxhighlight lang="cpp-qt">
QString filename("myfile.svg");
QString filename("myfile.svg");
QSize size(150, 250);
QSize size(150, 250);
QPixmap pix = cache->loadFromSVG(filename);
QPixmap pix = cache->loadFromSvg(filename, size);
</code>
</syntaxhighlight>
This renders the SVG onto a pixmap with the size of 150x250 pixels.
This renders the SVG onto a pixmap with the size of 150x250 pixels.


Line 52: Line 53:
Often you need to make sure the cache is up-to-date and you're not using obsolete pixmaps. For this, the cache provides timestamp() method. By default it returns time when the cache was created, but you can set your own timestamp with setTimestamp() method.
Often you need to make sure the cache is up-to-date and you're not using obsolete pixmaps. For this, the cache provides timestamp() method. By default it returns time when the cache was created, but you can set your own timestamp with setTimestamp() method.


<code cppqt>
<syntaxhighlight lang="cpp-qt">
// Our data
// Our data
MyDataObject data;
MyDataObject data;
// Make sure the cache is up to date
if (cache->timestamp() < data.timestamp()) {
if (cache->timestamp() < data.timestamp()) {
     // Cache is obsolete, delete and reinitialize it
     // Data is newer than the cache
    // Thus the cache is obsolete, delete and reinitialize it
     cache->discard();
     cache->discard();
}
}
Line 70: Line 73:
}
}
// Show the visualizedData pixmap to user
// Show the visualizedData pixmap to user
</code>
</syntaxhighlight>
This example creates a visual representation of some data. If the data is more recent than the cache, then contents of the cache are deleted using the discard() method. Next, we check if cache already contains the necessary pixmap. If it does, then there is no need to recreate it from the data (which might be expensive).
This example creates a visual representation of some data. If the data is more recent than the cache, then contents of the cache are deleted using the discard() method. Next, we check if cache already contains the necessary pixmap. If it does, then there is no need to recreate it from the data (which might be expensive).


=== API docs ===
=== API docs ===
For a list of all KPixmapCache methods, check out its [http://api.kde.org/classmapper.php?class=KPixmapCache&module=kdelibs&version=4.0 API docs].
For a list of all KPixmapCache methods, check out its [http://api.kde.org/classmapper.php?class=KPixmapCache&module=kdelibs&version=4.0 API docs].

Revision as of 09:17, 14 July 2012

Introduction

KPixmapCache provides disk-caching of QPixmaps. If you're using SVGs or generating pixmaps from some data, you might consider using it as it eliminates the need to generate the same pixmaps over and over again.

Using it

Creating the cache object

Its API is similar to that of the QPixmapCache. To use it, you first need to create the cache object, using a unique name:

KPixmapCache* cache = new KPixmapCache("myapp-images");

Inserting and finding pixmaps

Once that is done you can easily retrieve and insert images with insert() and find() methods:

QPixmap pix("file1.png");
cache->insert("file1.png", pix);

QPixmap pix2;
if (cache->find("file2.png", pix2)) {
    // The pixmap was loaded from cache.
} else {
    // Pixmap was not found in the cache.
    // Load and insert it:
    pix2 = QPixmap("file2.png");
    cache->insert("file2.png", pix2);
}

The Easy Way

As loading pixmaps from files with the aid of cache is a very common operation, KPixmapCache provides a convenience method for that:

QString filename("myfile.png");
QPixmap pix = cache->loadFromFile(filename);

This first checks if the given file is in the cache. If it is, it's loaded from the cache, otherwise it's loaded from the file and inserted into cache.

Similar method is provided for loading SVG files:

QString filename("myfile.svg");
QPixmap pix = cache->loadFromSvg(filename);

This renders contents of the SVG file onto pixmap, using SVG's default size by default. If you need a different size, you can specify it as the second argument of that method:

QString filename("myfile.svg");
QSize size(150, 250);
QPixmap pix = cache->loadFromSvg(filename, size);

This renders the SVG onto a pixmap with the size of 150x250 pixels.


Timestamps

Often you need to make sure the cache is up-to-date and you're not using obsolete pixmaps. For this, the cache provides timestamp() method. By default it returns time when the cache was created, but you can set your own timestamp with setTimestamp() method.

// Our data
MyDataObject data;
// Make sure the cache is up to date
if (cache->timestamp() < data.timestamp()) {
    // Data is newer than the cache
    // Thus the cache is obsolete, delete and reinitialize it
    cache->discard();
}
// Here the cache is always recent enough
QPixmap visualizedData;
QString key("data-visualization");
// Try to find the visualization from the cache
if (!cache->find(key, visualizedData)) {
    // Visualization is not in the cache, recreate it...
    visualizedData = createVisualization(data);
    // ...and put the resulting image into cache
    cache->insert(key, visualizedData);
}
// Show the visualizedData pixmap to user

This example creates a visual representation of some data. If the data is more recent than the cache, then contents of the cache are deleted using the discard() method. Next, we check if cache already contains the necessary pixmap. If it does, then there is no need to recreate it from the data (which might be expensive).

API docs

For a list of all KPixmapCache methods, check out its API docs.