22 #include <QtGui/QPixmap>
23 #include <QtGui/QImage>
24 #include <QtCore/QBuffer>
25 #include <QtCore/QCache>
26 #include <QtCore/QCoreApplication>
34 class KImageCache::Private :
public QObject
42 , enablePixmapCaching(true)
44 QObject::connect(QCoreApplication::instance(), SIGNAL(aboutToQuit()),
45 this, SLOT(clearPixmaps()));
54 if (enablePixmapCaching && pixmap && !pixmap->isNull()) {
57 return pixmapCache.insert(key, pixmap,
58 pixmap->width() * pixmap->height() * pixmap->depth() / 8);
77 QCache<QString, QPixmap> pixmapCache;
79 bool enablePixmapCaching;
83 unsigned defaultCacheSize,
84 unsigned expectedItemSize)
89 d->pixmapCache.setMaxCost(qMax(defaultCacheSize / 8, (
unsigned int) 16384));
100 buffer.open(QBuffer::WriteOnly);
101 image.save(&buffer,
"PNG");
103 if (this->
insert(key, buffer.buffer())) {
104 d->timestamp = ::time(0);
113 d->insertPixmap(key,
new QPixmap(pixmap));
124 QByteArray cachedData;
125 if (!this->
find(key, &cachedData) || cachedData.isNull()) {
130 destination->loadFromData(cachedData,
"PNG");
138 if (d->enablePixmapCaching) {
139 QPixmap *cachedPixmap = d->pixmapCache.object(key);
142 *destination = *cachedPixmap;
149 QByteArray cachedData;
150 if (!this->
find(key, &cachedData) || cachedData.isNull()) {
155 destination->loadFromData(cachedData,
"PNG");
158 d->insertPixmap(key,
new QPixmap(*destination));
166 d->pixmapCache.clear();
177 return d->enablePixmapCaching;
182 if (enable != d->enablePixmapCaching) {
183 d->enablePixmapCaching = enable;
185 d->pixmapCache.clear();
192 return d->pixmapCache.maxCost();
197 d->pixmapCache.setMaxCost(size);
200 #include "kimagecache.moc"