KGuiAddons 5.109.0
kimagecache.h
1/* This file is part of the KDE project.
2 SPDX-FileCopyrightText: 2010 Michael Pyne <mpyne@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7#ifndef KIMAGECACHE_H
8#define KIMAGECACHE_H
9
10// check that KGUIADDONS_LIB is defined in case the application is not using CMake
11// (if KGUIADDONS_LIB is not defined, we cannot assume that KCOREADDONS_LIB not being
12// defined means that we are not linked against KCoreAddons)
13#if defined(KGUIADDONS_LIB) && !defined(KCOREADDONS_LIB)
14#ifdef __GNUC__
15#warning "KImageCache requires KF5CoreAddons (for kshareddatacache.h)"
16#else
17#pragma message("KImageCache requires KF5CoreAddons (for kshareddatacache.h)")
18#endif
19#endif
20
21#include <klocalimagecacheimpl.h>
22#include <kshareddatacache.h>
23
24#include <QImage>
25#include <QPixmap>
26
27#define KImageCache KSharedPixmapCacheMixin<KSharedDataCache>
28
56template<class T>
57class KSharedPixmapCacheMixin : public T, private KLocalImageCacheImplementation
58{
59public:
73 KSharedPixmapCacheMixin(const QString &cacheName, unsigned defaultCacheSize, unsigned expectedItemSize = 0)
74 : T(cacheName, defaultCacheSize, expectedItemSize)
75 , KLocalImageCacheImplementation(defaultCacheSize)
76 {
77 }
78
92 bool insertPixmap(const QString &key, const QPixmap &pixmap)
93 {
94 insertLocalPixmap(key, pixmap);
95
96 // One thing to think about is only inserting things to the shared cache
97 // that are frequently used. But that would require tracking the use count
98 // in our local cache too, which I think is probably too much work.
99
100 return insertImage(key, pixmap.toImage());
101 }
102
114 bool insertImage(const QString &key, const QImage &image)
115 {
116 if (this->insert(key, serializeImage(image))) {
117 updateModifiedTime();
118 return true;
119 }
120
121 return false;
122 }
123
131 bool findPixmap(const QString &key, QPixmap *destination) const
132 {
133 if (findLocalPixmap(key, destination)) {
134 return true;
135 }
136
137 QByteArray cachedData;
138 if (!this->find(key, &cachedData) || cachedData.isNull()) {
139 return false;
140 }
141
142 if (destination) {
143 destination->loadFromData(cachedData, "PNG");
144
145 // Manually re-insert to pixmap cache if we'll be using this one.
146 insertLocalPixmap(key, *destination);
147 }
148
149 return true;
150 }
151
158 bool findImage(const QString &key, QImage *destination) const
159 {
160 QByteArray cachedData;
161 if (!this->find(key, &cachedData) || cachedData.isNull()) {
162 return false;
163 }
164
165 if (destination) {
166 destination->loadFromData(cachedData, "PNG");
167 }
168
169 return true;
170 }
171
176 void clear()
177 {
178 clearLocalCache();
179 T::clear();
180 }
181
185 using KLocalImageCacheImplementation::lastModifiedTime;
186
192 using KLocalImageCacheImplementation::pixmapCaching;
193
202 using KLocalImageCacheImplementation::setPixmapCaching;
203
208 using KLocalImageCacheImplementation::pixmapCacheLimit;
209
216 using KLocalImageCacheImplementation::setPixmapCacheLimit;
217};
218
219#endif /* KIMAGECACHE_H */
A simple wrapping layer over KSharedDataCache to support caching images and pixmaps.
Definition kimagecache.h:58
void clear()
Removes all entries from the cache.
Definition kimagecache.h:176
bool findPixmap(const QString &key, QPixmap *destination) const
Copies the cached pixmap identified by key to destination.
Definition kimagecache.h:131
KSharedPixmapCacheMixin(const QString &cacheName, unsigned defaultCacheSize, unsigned expectedItemSize=0)
Constructs an image cache, named by cacheName, with a default size of defaultCacheSize.
Definition kimagecache.h:73
bool findImage(const QString &key, QImage *destination) const
Copies the cached image identified by key to destination.
Definition kimagecache.h:158
bool insertPixmap(const QString &key, const QPixmap &pixmap)
Inserts the pixmap given by pixmap to the cache, accessible with key.
Definition kimagecache.h:92
bool insertImage(const QString &key, const QImage &image)
Inserts the image into the shared cache, accessible with key.
Definition kimagecache.h:114