• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • kdeui
  • util
kimagecache.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the KDE project.
3 * Copyright © 2010 Michael Pyne <mpyne@kde.org>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License version 2 as published by the Free Software Foundation.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include "kimagecache.h"
21
22#include <QtGui/QPixmap>
23#include <QtGui/QImage>
24#include <QtCore/QBuffer>
25#include <QtCore/QCache>
26#include <QtCore/QCoreApplication>
27
28#include <time.h>
29
34class KImageCache::Private : public QObject
35{
36 Q_OBJECT
37
38 public:
39 Private(QObject *parent = 0)
40 : QObject(parent)
41 , timestamp(::time(0))
42 , enablePixmapCaching(true)
43 {
44 QObject::connect(QCoreApplication::instance(), SIGNAL(aboutToQuit()),
45 this, SLOT(clearPixmaps()));
46 }
47
52 bool insertPixmap(const QString &key, QPixmap *pixmap)
53 {
54 if (enablePixmapCaching && pixmap && !pixmap->isNull()) {
55 // "cost" parameter is based on both image size and depth to make cost
56 // based on size in bytes instead of area on-screen.
57 return pixmapCache.insert(key, pixmap,
58 pixmap->width() * pixmap->height() * pixmap->depth() / 8);
59 }
60
61 return false;
62 }
63
64 public slots:
65 void clearPixmaps()
66 {
67 pixmapCache.clear();
68 }
69
70 public:
71 time_t timestamp;
72
77 QCache<QString, QPixmap> pixmapCache;
78
79 bool enablePixmapCaching;
80};
81
82KImageCache::KImageCache(const QString &cacheName,
83 unsigned defaultCacheSize,
84 unsigned expectedItemSize)
85 : KSharedDataCache(cacheName, defaultCacheSize, expectedItemSize)
86 , d(new Private)
87{
88 // Use at least 16 KiB for the pixmap cache
89 d->pixmapCache.setMaxCost(qMax(defaultCacheSize / 8, (unsigned int) 16384));
90}
91
92KImageCache::~KImageCache()
93{
94 delete d;
95}
96
97bool KImageCache::insertImage(const QString &key, const QImage &image)
98{
99 QBuffer buffer;
100 buffer.open(QBuffer::WriteOnly);
101 image.save(&buffer, "PNG");
102
103 if (this->insert(key, buffer.buffer())) {
104 d->timestamp = ::time(0);
105 return true;
106 }
107
108 return false;
109}
110
111bool KImageCache::insertPixmap(const QString &key, const QPixmap &pixmap)
112{
113 d->insertPixmap(key, new QPixmap(pixmap));
114
115 // One thing to think about is only inserting things to the shared cache
116 // that are frequently used. But that would require tracking the use count
117 // in our local cache too, which I think is probably too much work.
118
119 return insertImage(key, pixmap.toImage());
120}
121
122bool KImageCache::findImage(const QString &key, QImage *destination) const
123{
124 QByteArray cachedData;
125 if (!this->find(key, &cachedData) || cachedData.isNull()) {
126 return false;
127 }
128
129 if (destination) {
130 destination->loadFromData(cachedData, "PNG");
131 }
132
133 return true;
134}
135
136bool KImageCache::findPixmap(const QString &key, QPixmap *destination) const
137{
138 if (d->enablePixmapCaching) {
139 QPixmap *cachedPixmap = d->pixmapCache.object(key);
140 if (cachedPixmap) {
141 if (destination) {
142 *destination = *cachedPixmap;
143 }
144
145 return true;
146 }
147 }
148
149 QByteArray cachedData;
150 if (!this->find(key, &cachedData) || cachedData.isNull()) {
151 return false;
152 }
153
154 if (destination) {
155 destination->loadFromData(cachedData, "PNG");
156
157 // Manually re-insert to pixmap cache if we'll be using this one.
158 d->insertPixmap(key, new QPixmap(*destination));
159 }
160
161 return true;
162}
163
164void KImageCache::clear()
165{
166 d->pixmapCache.clear();
167 KSharedDataCache::clear();
168}
169
170time_t KImageCache::lastModifiedTime() const
171{
172 return d->timestamp;
173}
174
175bool KImageCache::pixmapCaching() const
176{
177 return d->enablePixmapCaching;
178}
179
180void KImageCache::setPixmapCaching(bool enable)
181{
182 if (enable != d->enablePixmapCaching) {
183 d->enablePixmapCaching = enable;
184 if (!enable) {
185 d->pixmapCache.clear();
186 }
187 }
188}
189
190int KImageCache::pixmapCacheLimit() const
191{
192 return d->pixmapCache.maxCost();
193}
194
195void KImageCache::setPixmapCacheLimit(int size)
196{
197 d->pixmapCache.setMaxCost(size);
198}
199
200#include "kimagecache.moc"
KImageCache::findPixmap
bool findPixmap(const QString &key, QPixmap *destination) const
Copies the cached pixmap identified by key to destination.
Definition: kimagecache.cpp:136
KImageCache::pixmapCaching
bool pixmapCaching() const
Definition: kimagecache.cpp:175
KImageCache::clear
void clear()
Removes all entries from the cache.
Definition: kimagecache.cpp:164
KImageCache::findImage
bool findImage(const QString &key, QImage *destination) const
Copies the cached image identified by key to destination.
Definition: kimagecache.cpp:122
KImageCache::lastModifiedTime
time_t lastModifiedTime() const
Definition: kimagecache.cpp:170
KImageCache::~KImageCache
~KImageCache()
Deconstructor.
Definition: kimagecache.cpp:92
KImageCache::insertImage
bool insertImage(const QString &key, const QImage &image)
Inserts the image into the shared cache, accessible with key.
Definition: kimagecache.cpp:97
KImageCache::setPixmapCacheLimit
void setPixmapCacheLimit(int size)
Sets the highest memory size the pixmap cache should use.
Definition: kimagecache.cpp:195
KImageCache::KImageCache
KImageCache(const QString &cacheName, unsigned defaultCacheSize, unsigned expectedItemSize=0)
Constructs an image cache, named by cacheName, with a default size of defaultCacheSize.
Definition: kimagecache.cpp:82
KImageCache::insertPixmap
bool insertPixmap(const QString &key, const QPixmap &pixmap)
Inserts the pixmap given by pixmap to the cache, accessible with key.
Definition: kimagecache.cpp:111
KImageCache::pixmapCacheLimit
int pixmapCacheLimit() const
Definition: kimagecache.cpp:190
KImageCache::setPixmapCaching
void setPixmapCaching(bool enable)
Enables or disables local pixmap caching.
Definition: kimagecache.cpp:180
KSharedDataCache
KSharedDataCache::clear
void clear()
KSharedDataCache::insert
bool insert(const QString &key, const QByteArray &data)
KSharedDataCache::find
bool find(const QString &key, QByteArray *destination) const
QObject
kimagecache.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal