File: kimagecache.cpp

package info (click to toggle)
kde4libs 4%3A4.14.38-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 82,620 kB
  • sloc: cpp: 763,763; xml: 12,007; ansic: 5,223; java: 4,060; perl: 2,938; yacc: 2,484; python: 1,219; sh: 1,152; ruby: 337; lex: 278; makefile: 28
file content (200 lines) | stat: -rw-r--r-- 5,215 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
 * This file is part of the KDE project.
 * Copyright © 2010 Michael Pyne <mpyne@kde.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License version 2 as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "kimagecache.h"

#include <QtGui/QPixmap>
#include <QtGui/QImage>
#include <QtCore/QBuffer>
#include <QtCore/QCache>
#include <QtCore/QCoreApplication>

#include <time.h>

/**
 * This is a QObject subclass so we can catch the signal that the application is about
 * to close and properly release any QPixmaps we have cached.
 */
class KImageCache::Private : public QObject
{
    Q_OBJECT

    public:
    Private(QObject *parent = 0)
        : QObject(parent)
        , timestamp(::time(0))
        , enablePixmapCaching(true)
    {
        QObject::connect(QCoreApplication::instance(), SIGNAL(aboutToQuit()),
                         this, SLOT(clearPixmaps()));
    }

    /**
     * Inserts a pixmap into the pixmap cache if the pixmap cache is enabled, with
     * weighting based on image size and bit depth.
     */
    bool insertPixmap(const QString &key, QPixmap *pixmap)
    {
        if (enablePixmapCaching && pixmap && !pixmap->isNull()) {
            // "cost" parameter is based on both image size and depth to make cost
            // based on size in bytes instead of area on-screen.
            return pixmapCache.insert(key, pixmap,
                pixmap->width() * pixmap->height() * pixmap->depth() / 8);
        }

        return false;
    }

    public slots:
    void clearPixmaps()
    {
        pixmapCache.clear();
    }

    public:
    time_t timestamp;

    /**
     * This is used to cache pixmaps as they are inserted, instead of always
     * converting to image data and storing that in shared memory.
     */
    QCache<QString, QPixmap> pixmapCache;

    bool enablePixmapCaching;
};

KImageCache::KImageCache(const QString &cacheName,
                         unsigned defaultCacheSize,
                         unsigned expectedItemSize)
    : KSharedDataCache(cacheName, defaultCacheSize, expectedItemSize)
    , d(new Private)
{
    // Use at least 16 KiB for the pixmap cache
    d->pixmapCache.setMaxCost(qMax(defaultCacheSize / 8, (unsigned int) 16384));
}

KImageCache::~KImageCache()
{
    delete d;
}

bool KImageCache::insertImage(const QString &key, const QImage &image)
{
    QBuffer buffer;
    buffer.open(QBuffer::WriteOnly);
    image.save(&buffer, "PNG");

    if (this->insert(key, buffer.buffer())) {
        d->timestamp = ::time(0);
        return true;
    }

    return false;
}

bool KImageCache::insertPixmap(const QString &key, const QPixmap &pixmap)
{
    d->insertPixmap(key, new QPixmap(pixmap));

    // One thing to think about is only inserting things to the shared cache
    // that are frequently used. But that would require tracking the use count
    // in our local cache too, which I think is probably too much work.

    return insertImage(key, pixmap.toImage());
}

bool KImageCache::findImage(const QString &key, QImage *destination) const
{
    QByteArray cachedData;
    if (!this->find(key, &cachedData) || cachedData.isNull()) {
        return false;
    }

    if (destination) {
        destination->loadFromData(cachedData, "PNG");
    }

    return true;
}

bool KImageCache::findPixmap(const QString &key, QPixmap *destination) const
{
    if (d->enablePixmapCaching) {
        QPixmap *cachedPixmap = d->pixmapCache.object(key);
        if (cachedPixmap) {
            if (destination) {
                *destination = *cachedPixmap;
            }

            return true;
        }
    }

    QByteArray cachedData;
    if (!this->find(key, &cachedData) || cachedData.isNull()) {
        return false;
    }

    if (destination) {
        destination->loadFromData(cachedData, "PNG");

        // Manually re-insert to pixmap cache if we'll be using this one.
        d->insertPixmap(key, new QPixmap(*destination));
    }

    return true;
}

void KImageCache::clear()
{
    d->pixmapCache.clear();
    KSharedDataCache::clear();
}

time_t KImageCache::lastModifiedTime() const
{
    return d->timestamp;
}

bool KImageCache::pixmapCaching() const
{
    return d->enablePixmapCaching;
}

void KImageCache::setPixmapCaching(bool enable)
{
    if (enable != d->enablePixmapCaching) {
        d->enablePixmapCaching = enable;
        if (!enable) {
            d->pixmapCache.clear();
        }
    }
}

int KImageCache::pixmapCacheLimit() const
{
    return d->pixmapCache.maxCost();
}

void KImageCache::setPixmapCacheLimit(int size)
{
    d->pixmapCache.setMaxCost(size);
}

#include "kimagecache.moc"