| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/gui/image/qpixmapcache.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count |
|---|---|---|
| 1 | /**************************************************************************** | - |
| 2 | ** | - |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | - |
| 4 | ** Contact: https://www.qt.io/licensing/ | - |
| 5 | ** | - |
| 6 | ** This file is part of the QtGui module of the Qt Toolkit. | - |
| 7 | ** | - |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | - |
| 9 | ** Commercial License Usage | - |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | - |
| 11 | ** accordance with the commercial license agreement provided with the | - |
| 12 | ** Software or, alternatively, in accordance with the terms contained in | - |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | - |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | - |
| 16 | ** | - |
| 17 | ** GNU Lesser General Public License Usage | - |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - |
| 19 | ** General Public License version 3 as published by the Free Software | - |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - |
| 21 | ** packaging of this file. Please review the following information to | - |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements | - |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - |
| 24 | ** | - |
| 25 | ** GNU General Public License Usage | - |
| 26 | ** Alternatively, this file may be used under the terms of the GNU | - |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General | - |
| 28 | ** Public license version 3 or any later version approved by the KDE Free | - |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software | - |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - |
| 31 | ** included in the packaging of this file. Please review the following | - |
| 32 | ** information to ensure the GNU General Public License requirements will | - |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - |
| 35 | ** | - |
| 36 | ** $QT_END_LICENSE$ | - |
| 37 | ** | - |
| 38 | ****************************************************************************/ | - |
| 39 | - | |
| 40 | #define Q_TEST_QPIXMAPCACHE | - |
| 41 | #include "qpixmapcache.h" | - |
| 42 | #include "qobject.h" | - |
| 43 | #include "qdebug.h" | - |
| 44 | #include "qpixmapcache_p.h" | - |
| 45 | - | |
| 46 | QT_BEGIN_NAMESPACE | - |
| 47 | - | |
| 48 | /*! | - |
| 49 | \class QPixmapCache | - |
| 50 | \inmodule QtGui | - |
| 51 | - | |
| 52 | \brief The QPixmapCache class provides an application-wide cache for pixmaps. | - |
| 53 | - | |
| 54 | This class is a tool for optimized drawing with QPixmap. You can | - |
| 55 | use it to store temporary pixmaps that are expensive to generate | - |
| 56 | without using more storage space than cacheLimit(). Use insert() | - |
| 57 | to insert pixmaps, find() to find them, and clear() to empty the | - |
| 58 | cache. | - |
| 59 | - | |
| 60 | QPixmapCache contains no member data, only static functions to | - |
| 61 | access the global pixmap cache. It creates an internal QCache | - |
| 62 | object for caching the pixmaps. | - |
| 63 | - | |
| 64 | The cache associates a pixmap with a user-provided string as a key, | - |
| 65 | or with a QPixmapCache::Key that the cache generates. | - |
| 66 | Using QPixmapCache::Key for keys is faster than using strings. The string API is | - |
| 67 | very convenient for complex keys but the QPixmapCache::Key API will be very | - |
| 68 | efficient and convenient for a one-to-one object-to-pixmap mapping - in | - |
| 69 | this case, you can store the keys as members of an object. | - |
| 70 | - | |
| 71 | If two pixmaps are inserted into the cache using equal keys then the | - |
| 72 | last pixmap will replace the first pixmap in the cache. This follows the | - |
| 73 | behavior of the QHash and QCache classes. | - |
| 74 | - | |
| 75 | The cache becomes full when the total size of all pixmaps in the | - |
| 76 | cache exceeds cacheLimit(). The initial cache limit is 10240 KB (10 MB); | - |
| 77 | you can change this by calling setCacheLimit() with the required value. | - |
| 78 | A pixmap takes roughly (\e{width} * \e{height} * \e{depth})/8 bytes of | - |
| 79 | memory. | - |
| 80 | - | |
| 81 | The \e{Qt Quarterly} article | - |
| 82 | \l{http://doc.qt.io/archives/qq/qq12-qpixmapcache.html}{Optimizing | - |
| 83 | with QPixmapCache} explains how to use QPixmapCache to speed up | - |
| 84 | applications by caching the results of painting. | - |
| 85 | - | |
| 86 | \sa QCache, QPixmap | - |
| 87 | */ | - |
| 88 | - | |
| 89 | static int cache_limit = 10240; // 10 MB cache limit | - |
| 90 | - | |
| 91 | /*! | - |
| 92 | \class QPixmapCache::Key | - |
| 93 | \brief The QPixmapCache::Key class can be used for efficient access | - |
| 94 | to the QPixmapCache. | - |
| 95 | \inmodule QtGui | - |
| 96 | \since 4.6 | - |
| 97 | - | |
| 98 | Use QPixmapCache::insert() to receive an instance of Key generated | - |
| 99 | by the pixmap cache. You can store the key in your own objects for | - |
| 100 | a very efficient one-to-one object-to-pixmap mapping. | - |
| 101 | */ | - |
| 102 | - | |
| 103 | /*! | - |
| 104 | Constructs an empty Key object. | - |
| 105 | */ | - |
| 106 | QPixmapCache::Key::Key() : d(0) | - |
| 107 | { | - |
| 108 | } | - |
| 109 | - | |
| 110 | /*! | - |
| 111 | \internal | - |
| 112 | Constructs a copy of \a other. | - |
| 113 | */ | - |
| 114 | QPixmapCache::Key::Key(const Key &other) | - |
| 115 | { | - |
| 116 | if (other.d) | - |
| 117 | ++(other.d->ref); | - |
| 118 | d = other.d; | - |
| 119 | } | - |
| 120 | - | |
| 121 | /*! | - |
| 122 | Destroys the key. | - |
| 123 | */ | - |
| 124 | QPixmapCache::Key::~Key() | - |
| 125 | { | - |
| 126 | if (d && --(d->ref) == 0) | - |
| 127 | delete d; | - |
| 128 | } | - |
| 129 | - | |
| 130 | /*! | - |
| 131 | \internal | - |
| 132 | - | |
| 133 | Returns \c true if this key is the same as the given \a key; otherwise returns | - |
| 134 | false. | - |
| 135 | */ | - |
| 136 | bool QPixmapCache::Key::operator ==(const Key &key) const | - |
| 137 | { | - |
| 138 | return (d == key.d); | - |
| 139 | } | - |
| 140 | - | |
| 141 | /*! | - |
| 142 | \fn bool QPixmapCache::Key::operator !=(const Key &key) const | - |
| 143 | \internal | - |
| 144 | */ | - |
| 145 | - | |
| 146 | /*! | - |
| 147 | \fn QPixmapCache::Key::Key(Key &&) | - |
| 148 | \internal | - |
| 149 | \since 5.6 | - |
| 150 | */ | - |
| 151 | - | |
| 152 | /*! | - |
| 153 | \fn QPixmapCache::Key &QPixmapCache::Key::operator=(Key &&) | - |
| 154 | \internal | - |
| 155 | \since 5.6 | - |
| 156 | */ | - |
| 157 | - | |
| 158 | /*! | - |
| 159 | \fn void QPixmapCache::Key::swap(Key &) | - |
| 160 | \internal | - |
| 161 | \since 5.6 | - |
| 162 | */ | - |
| 163 | - | |
| 164 | /*! | - |
| 165 | Returns \c true if there is a cached pixmap associated with this key. | - |
| 166 | Otherwise, if pixmap was flushed, the key is no longer valid. | - |
| 167 | \since 5.7 | - |
| 168 | */ | - |
| 169 | bool QPixmapCache::Key::isValid() const Q_DECL_NOTHROW | - |
| 170 | { | - |
| 171 | return d && d->isValid; never executed: return d && d->isValid; | 0 |
| 172 | } | - |
| 173 | - | |
| 174 | /*! | - |
| 175 | \internal | - |
| 176 | */ | - |
| 177 | QPixmapCache::Key &QPixmapCache::Key::operator =(const Key &other) | - |
| 178 | { | - |
| 179 | if (d != other.d) { | - |
| 180 | if (other.d) | - |
| 181 | ++(other.d->ref); | - |
| 182 | if (d && --(d->ref) == 0) | - |
| 183 | delete d; | - |
| 184 | d = other.d; | - |
| 185 | } | - |
| 186 | return *this; | - |
| 187 | } | - |
| 188 | - | |
| 189 | class QPMCache : public QObject, public QCache<QPixmapCache::Key, QPixmapCacheEntry> | - |
| 190 | { | - |
| 191 | Q_OBJECT | - |
| 192 | public: | - |
| 193 | QPMCache(); | - |
| 194 | ~QPMCache(); | - |
| 195 | - | |
| 196 | void timerEvent(QTimerEvent *) Q_DECL_OVERRIDE; | - |
| 197 | bool insert(const QString& key, const QPixmap &pixmap, int cost); | - |
| 198 | QPixmapCache::Key insert(const QPixmap &pixmap, int cost); | - |
| 199 | bool replace(const QPixmapCache::Key &key, const QPixmap &pixmap, int cost); | - |
| 200 | bool remove(const QString &key); | - |
| 201 | bool remove(const QPixmapCache::Key &key); | - |
| 202 | - | |
| 203 | void resizeKeyArray(int size); | - |
| 204 | QPixmapCache::Key createKey(); | - |
| 205 | void releaseKey(const QPixmapCache::Key &key); | - |
| 206 | void clear(); | - |
| 207 | - | |
| 208 | QPixmap *object(const QString &key) const; | - |
| 209 | QPixmap *object(const QPixmapCache::Key &key) const; | - |
| 210 | - | |
| 211 | static inline QPixmapCache::KeyData *get(const QPixmapCache::Key &key) | - |
| 212 | {return key.d;} | - |
| 213 | - | |
| 214 | static QPixmapCache::KeyData* getKeyData(QPixmapCache::Key *key); | - |
| 215 | - | |
| 216 | bool flushDetachedPixmaps(bool nt); | - |
| 217 | - | |
| 218 | private: | - |
| 219 | enum { soon_time = 10000, flush_time = 30000 }; | - |
| 220 | int *keyArray; | - |
| 221 | int theid; | - |
| 222 | int ps; | - |
| 223 | int keyArraySize; | - |
| 224 | int freeKey; | - |
| 225 | QHash<QString, QPixmapCache::Key> cacheKeys; | - |
| 226 | bool t; | - |
| 227 | }; | - |
| 228 | - | |
| 229 | QT_BEGIN_INCLUDE_NAMESPACE | - |
| 230 | #include "qpixmapcache.moc" | - |
| 231 | QT_END_INCLUDE_NAMESPACE | - |
| 232 | - | |
| 233 | uint qHash(const QPixmapCache::Key &k) | - |
| 234 | { | - |
| 235 | return qHash(QPMCache::get(k)->key); | - |
| 236 | } | - |
| 237 | - | |
| 238 | QPMCache::QPMCache() | - |
| 239 | : QObject(0), | - |
| 240 | QCache<QPixmapCache::Key, QPixmapCacheEntry>(cache_limit * 1024), | - |
| 241 | keyArray(0), theid(0), ps(0), keyArraySize(0), freeKey(0), t(false) | - |
| 242 | { | - |
| 243 | } | - |
| 244 | QPMCache::~QPMCache() | - |
| 245 | { | - |
| 246 | clear(); | - |
| 247 | free(keyArray); | - |
| 248 | } | - |
| 249 | - | |
| 250 | /* | - |
| 251 | This is supposed to cut the cache size down by about 25% in a | - |
| 252 | minute once the application becomes idle, to let any inserted pixmap | - |
| 253 | remain in the cache for some time before it becomes a candidate for | - |
| 254 | cleaning-up, and to not cut down the size of the cache while the | - |
| 255 | cache is in active use. | - |
| 256 | - | |
| 257 | When the last detached pixmap has been deleted from the cache, kill the | - |
| 258 | timer so Qt won't keep the CPU from going into sleep mode. Currently | - |
| 259 | the timer is not restarted when the pixmap becomes unused, but it does | - |
| 260 | restart once something else is added (i.e. the cache space is actually needed). | - |
| 261 | - | |
| 262 | Returns \c true if any were removed. | - |
| 263 | */ | - |
| 264 | bool QPMCache::flushDetachedPixmaps(bool nt) | - |
| 265 | { | - |
| 266 | int mc = maxCost(); | - |
| 267 | setMaxCost(nt ? totalCost() * 3 / 4 : totalCost() -1); | - |
| 268 | setMaxCost(mc); | - |
| 269 | ps = totalCost(); | - |
| 270 | - | |
| 271 | bool any = false; | - |
| 272 | QHash<QString, QPixmapCache::Key>::iterator it = cacheKeys.begin(); | - |
| 273 | while (it != cacheKeys.end()) { | - |
| 274 | if (!contains(it.value())) { | - |
| 275 | releaseKey(it.value()); | - |
| 276 | it = cacheKeys.erase(it); | - |
| 277 | any = true; | - |
| 278 | } else { | - |
| 279 | ++it; | - |
| 280 | } | - |
| 281 | } | - |
| 282 | - | |
| 283 | return any; | - |
| 284 | } | - |
| 285 | - | |
| 286 | void QPMCache::timerEvent(QTimerEvent *) | - |
| 287 | { | - |
| 288 | bool nt = totalCost() == ps; | - |
| 289 | if (!flushDetachedPixmaps(nt)) { | - |
| 290 | killTimer(theid); | - |
| 291 | theid = 0; | - |
| 292 | } else if (nt != t) { | - |
| 293 | killTimer(theid); | - |
| 294 | theid = startTimer(nt ? soon_time : flush_time); | - |
| 295 | t = nt; | - |
| 296 | } | - |
| 297 | } | - |
| 298 | - | |
| 299 | - | |
| 300 | QPixmap *QPMCache::object(const QString &key) const | - |
| 301 | { | - |
| 302 | QPixmapCache::Key cacheKey = cacheKeys.value(key); | - |
| 303 | if (!cacheKey.d || !cacheKey.d->isValid) { | - |
| 304 | const_cast<QPMCache *>(this)->cacheKeys.remove(key); | - |
| 305 | return 0; | - |
| 306 | } | - |
| 307 | QPixmap *ptr = QCache<QPixmapCache::Key, QPixmapCacheEntry>::object(cacheKey); | - |
| 308 | //We didn't find the pixmap in the cache, the key is not valid anymore | - |
| 309 | if (!ptr) { | - |
| 310 | const_cast<QPMCache *>(this)->cacheKeys.remove(key); | - |
| 311 | } | - |
| 312 | return ptr; | - |
| 313 | } | - |
| 314 | - | |
| 315 | QPixmap *QPMCache::object(const QPixmapCache::Key &key) const | - |
| 316 | { | - |
| 317 | Q_ASSERT(key.d->isValid); | - |
| 318 | QPixmap *ptr = QCache<QPixmapCache::Key, QPixmapCacheEntry>::object(key); | - |
| 319 | //We didn't find the pixmap in the cache, the key is not valid anymore | - |
| 320 | if (!ptr) | - |
| 321 | const_cast<QPMCache *>(this)->releaseKey(key); | - |
| 322 | return ptr; | - |
| 323 | } | - |
| 324 | - | |
| 325 | bool QPMCache::insert(const QString& key, const QPixmap &pixmap, int cost) | - |
| 326 | { | - |
| 327 | QPixmapCache::Key cacheKey; | - |
| 328 | QPixmapCache::Key oldCacheKey = cacheKeys.value(key); | - |
| 329 | //If for the same key we add already a pixmap we should delete it | - |
| 330 | if (oldCacheKey.d) { | - |
| 331 | QCache<QPixmapCache::Key, QPixmapCacheEntry>::remove(oldCacheKey); | - |
| 332 | cacheKeys.remove(key); | - |
| 333 | } | - |
| 334 | - | |
| 335 | //we create a new key the old one has been removed | - |
| 336 | cacheKey = createKey(); | - |
| 337 | - | |
| 338 | bool success = QCache<QPixmapCache::Key, QPixmapCacheEntry>::insert(cacheKey, new QPixmapCacheEntry(cacheKey, pixmap), cost); | - |
| 339 | if (success) { | - |
| 340 | cacheKeys.insert(key, cacheKey); | - |
| 341 | if (!theid) { | - |
| 342 | theid = startTimer(flush_time); | - |
| 343 | t = false; | - |
| 344 | } | - |
| 345 | } else { | - |
| 346 | //Insertion failed we released the new allocated key | - |
| 347 | releaseKey(cacheKey); | - |
| 348 | } | - |
| 349 | return success; | - |
| 350 | } | - |
| 351 | - | |
| 352 | QPixmapCache::Key QPMCache::insert(const QPixmap &pixmap, int cost) | - |
| 353 | { | - |
| 354 | QPixmapCache::Key cacheKey = createKey(); | - |
| 355 | bool success = QCache<QPixmapCache::Key, QPixmapCacheEntry>::insert(cacheKey, new QPixmapCacheEntry(cacheKey, pixmap), cost); | - |
| 356 | if (success) { | - |
| 357 | if (!theid) { | - |
| 358 | theid = startTimer(flush_time); | - |
| 359 | t = false; | - |
| 360 | } | - |
| 361 | } else { | - |
| 362 | //Insertion failed we released the key and return an invalid one | - |
| 363 | releaseKey(cacheKey); | - |
| 364 | } | - |
| 365 | return cacheKey; | - |
| 366 | } | - |
| 367 | - | |
| 368 | bool QPMCache::replace(const QPixmapCache::Key &key, const QPixmap &pixmap, int cost) | - |
| 369 | { | - |
| 370 | Q_ASSERT(key.d->isValid); | - |
| 371 | //If for the same key we had already an entry so we should delete the pixmap and use the new one | - |
| 372 | QCache<QPixmapCache::Key, QPixmapCacheEntry>::remove(key); | - |
| 373 | - | |
| 374 | QPixmapCache::Key cacheKey = createKey(); | - |
| 375 | - | |
| 376 | bool success = QCache<QPixmapCache::Key, QPixmapCacheEntry>::insert(cacheKey, new QPixmapCacheEntry(cacheKey, pixmap), cost); | - |
| 377 | if (success) { | - |
| 378 | if(!theid) { | - |
| 379 | theid = startTimer(flush_time); | - |
| 380 | t = false; | - |
| 381 | } | - |
| 382 | const_cast<QPixmapCache::Key&>(key) = cacheKey; | - |
| 383 | } else { | - |
| 384 | //Insertion failed we released the key | - |
| 385 | releaseKey(cacheKey); | - |
| 386 | } | - |
| 387 | return success; | - |
| 388 | } | - |
| 389 | - | |
| 390 | bool QPMCache::remove(const QString &key) | - |
| 391 | { | - |
| 392 | QPixmapCache::Key cacheKey = cacheKeys.value(key); | - |
| 393 | //The key was not in the cache | - |
| 394 | if (!cacheKey.d) | - |
| 395 | return false; | - |
| 396 | cacheKeys.remove(key); | - |
| 397 | return QCache<QPixmapCache::Key, QPixmapCacheEntry>::remove(cacheKey); | - |
| 398 | } | - |
| 399 | - | |
| 400 | bool QPMCache::remove(const QPixmapCache::Key &key) | - |
| 401 | { | - |
| 402 | return QCache<QPixmapCache::Key, QPixmapCacheEntry>::remove(key); | - |
| 403 | } | - |
| 404 | - | |
| 405 | void QPMCache::resizeKeyArray(int size) | - |
| 406 | { | - |
| 407 | if (size <= keyArraySize || size == 0) | - |
| 408 | return; | - |
| 409 | keyArray = q_check_ptr(reinterpret_cast<int *>(realloc(keyArray, | - |
| 410 | size * sizeof(int)))); | - |
| 411 | for (int i = keyArraySize; i != size; ++i) | - |
| 412 | keyArray[i] = i + 1; | - |
| 413 | keyArraySize = size; | - |
| 414 | } | - |
| 415 | - | |
| 416 | QPixmapCache::Key QPMCache::createKey() | - |
| 417 | { | - |
| 418 | if (freeKey == keyArraySize) | - |
| 419 | resizeKeyArray(keyArraySize ? keyArraySize << 1 : 2); | - |
| 420 | int id = freeKey; | - |
| 421 | freeKey = keyArray[id]; | - |
| 422 | QPixmapCache::Key key; | - |
| 423 | QPixmapCache::KeyData *d = QPMCache::getKeyData(&key); | - |
| 424 | d->key = ++id; | - |
| 425 | return key; | - |
| 426 | } | - |
| 427 | - | |
| 428 | void QPMCache::releaseKey(const QPixmapCache::Key &key) | - |
| 429 | { | - |
| 430 | if (key.d->key > keyArraySize || key.d->key <= 0) | - |
| 431 | return; | - |
| 432 | key.d->key--; | - |
| 433 | keyArray[key.d->key] = freeKey; | - |
| 434 | freeKey = key.d->key; | - |
| 435 | key.d->isValid = false; | - |
| 436 | key.d->key = 0; | - |
| 437 | } | - |
| 438 | - | |
| 439 | void QPMCache::clear() | - |
| 440 | { | - |
| 441 | free(keyArray); | - |
| 442 | keyArray = 0; | - |
| 443 | freeKey = 0; | - |
| 444 | keyArraySize = 0; | - |
| 445 | //Mark all keys as invalid | - |
| 446 | QList<QPixmapCache::Key> keys = QCache<QPixmapCache::Key, QPixmapCacheEntry>::keys(); | - |
| 447 | for (int i = 0; i < keys.size(); ++i) | - |
| 448 | keys.at(i).d->isValid = false; | - |
| 449 | QCache<QPixmapCache::Key, QPixmapCacheEntry>::clear(); | - |
| 450 | } | - |
| 451 | - | |
| 452 | QPixmapCache::KeyData* QPMCache::getKeyData(QPixmapCache::Key *key) | - |
| 453 | { | - |
| 454 | if (!key->d) | - |
| 455 | key->d = new QPixmapCache::KeyData; | - |
| 456 | return key->d; | - |
| 457 | } | - |
| 458 | - | |
| 459 | Q_GLOBAL_STATIC(QPMCache, pm_cache) | - |
| 460 | - | |
| 461 | int Q_AUTOTEST_EXPORT q_QPixmapCache_keyHashSize() | - |
| 462 | { | - |
| 463 | return pm_cache()->size(); | - |
| 464 | } | - |
| 465 | - | |
| 466 | QPixmapCacheEntry::~QPixmapCacheEntry() | - |
| 467 | { | - |
| 468 | pm_cache()->releaseKey(key); | - |
| 469 | } | - |
| 470 | - | |
| 471 | /*! | - |
| 472 | \obsolete | - |
| 473 | \overload | - |
| 474 | - | |
| 475 | Returns the pixmap associated with the \a key in the cache, or | - |
| 476 | null if there is no such pixmap. | - |
| 477 | - | |
| 478 | \warning If valid, you should copy the pixmap immediately (this is | - |
| 479 | fast). Subsequent insertions into the cache could cause the | - |
| 480 | pointer to become invalid. For this reason, we recommend you use | - |
| 481 | bool find(const QString&, QPixmap*) instead. | - |
| 482 | - | |
| 483 | Example: | - |
| 484 | \snippet code/src_gui_image_qpixmapcache.cpp 0 | - |
| 485 | */ | - |
| 486 | - | |
| 487 | QPixmap *QPixmapCache::find(const QString &key) | - |
| 488 | { | - |
| 489 | return pm_cache()->object(key); | - |
| 490 | } | - |
| 491 | - | |
| 492 | - | |
| 493 | /*! | - |
| 494 | \obsolete | - |
| 495 | - | |
| 496 | Use bool find(const QString&, QPixmap*) instead. | - |
| 497 | */ | - |
| 498 | - | |
| 499 | bool QPixmapCache::find(const QString &key, QPixmap& pixmap) | - |
| 500 | { | - |
| 501 | return find(key, &pixmap); | - |
| 502 | } | - |
| 503 | - | |
| 504 | /*! | - |
| 505 | Looks for a cached pixmap associated with the given \a key in the cache. | - |
| 506 | If the pixmap is found, the function sets \a pixmap to that pixmap and | - |
| 507 | returns \c true; otherwise it leaves \a pixmap alone and returns \c false. | - |
| 508 | - | |
| 509 | \since 4.6 | - |
| 510 | - | |
| 511 | Example: | - |
| 512 | \snippet code/src_gui_image_qpixmapcache.cpp 1 | - |
| 513 | */ | - |
| 514 | - | |
| 515 | bool QPixmapCache::find(const QString &key, QPixmap* pixmap) | - |
| 516 | { | - |
| 517 | QPixmap *ptr = pm_cache()->object(key); | - |
| 518 | if (ptr && pixmap) | - |
| 519 | *pixmap = *ptr; | - |
| 520 | return ptr != 0; | - |
| 521 | } | - |
| 522 | - | |
| 523 | /*! | - |
| 524 | Looks for a cached pixmap associated with the given \a key in the cache. | - |
| 525 | If the pixmap is found, the function sets \a pixmap to that pixmap and | - |
| 526 | returns \c true; otherwise it leaves \a pixmap alone and returns \c false. If | - |
| 527 | the pixmap is not found, it means that the \a key is no longer valid, | - |
| 528 | so it will be released for the next insertion. | - |
| 529 | - | |
| 530 | \since 4.6 | - |
| 531 | */ | - |
| 532 | bool QPixmapCache::find(const Key &key, QPixmap* pixmap) | - |
| 533 | { | - |
| 534 | //The key is not valid anymore, a flush happened before probably | - |
| 535 | if (!key.d || !key.d->isValid) | - |
| 536 | return false; | - |
| 537 | QPixmap *ptr = pm_cache()->object(key); | - |
| 538 | if (ptr && pixmap) | - |
| 539 | *pixmap = *ptr; | - |
| 540 | return ptr != 0; | - |
| 541 | } | - |
| 542 | - | |
| 543 | /*! | - |
| 544 | Inserts a copy of the pixmap \a pixmap associated with the \a key into | - |
| 545 | the cache. | - |
| 546 | - | |
| 547 | All pixmaps inserted by the Qt library have a key starting with | - |
| 548 | "$qt", so your own pixmap keys should never begin "$qt". | - |
| 549 | - | |
| 550 | When a pixmap is inserted and the cache is about to exceed its | - |
| 551 | limit, it removes pixmaps until there is enough room for the | - |
| 552 | pixmap to be inserted. | - |
| 553 | - | |
| 554 | The oldest pixmaps (least recently accessed in the cache) are | - |
| 555 | deleted when more space is needed. | - |
| 556 | - | |
| 557 | The function returns \c true if the object was inserted into the | - |
| 558 | cache; otherwise it returns \c false. | - |
| 559 | - | |
| 560 | \sa setCacheLimit() | - |
| 561 | */ | - |
| 562 | - | |
| 563 | bool QPixmapCache::insert(const QString &key, const QPixmap &pixmap) | - |
| 564 | { | - |
| 565 | return pm_cache()->insert(key, pixmap, pixmap.width() * pixmap.height() * pixmap.depth() / 8); | - |
| 566 | } | - |
| 567 | - | |
| 568 | /*! | - |
| 569 | Inserts a copy of the given \a pixmap into the cache and returns a key | - |
| 570 | that can be used to retrieve it. | - |
| 571 | - | |
| 572 | When a pixmap is inserted and the cache is about to exceed its | - |
| 573 | limit, it removes pixmaps until there is enough room for the | - |
| 574 | pixmap to be inserted. | - |
| 575 | - | |
| 576 | The oldest pixmaps (least recently accessed in the cache) are | - |
| 577 | deleted when more space is needed. | - |
| 578 | - | |
| 579 | \sa setCacheLimit(), replace() | - |
| 580 | - | |
| 581 | \since 4.6 | - |
| 582 | */ | - |
| 583 | QPixmapCache::Key QPixmapCache::insert(const QPixmap &pixmap) | - |
| 584 | { | - |
| 585 | return pm_cache()->insert(pixmap, pixmap.width() * pixmap.height() * pixmap.depth() / 8); | - |
| 586 | } | - |
| 587 | - | |
| 588 | /*! | - |
| 589 | Replaces the pixmap associated with the given \a key with the \a pixmap | - |
| 590 | specified. Returns \c true if the \a pixmap has been correctly inserted into | - |
| 591 | the cache; otherwise returns \c false. | - |
| 592 | - | |
| 593 | \sa setCacheLimit(), insert() | - |
| 594 | - | |
| 595 | \since 4.6 | - |
| 596 | */ | - |
| 597 | bool QPixmapCache::replace(const Key &key, const QPixmap &pixmap) | - |
| 598 | { | - |
| 599 | //The key is not valid anymore, a flush happened before probably | - |
| 600 | if (!key.d || !key.d->isValid) | - |
| 601 | return false; | - |
| 602 | return pm_cache()->replace(key, pixmap, pixmap.width() * pixmap.height() * pixmap.depth() / 8); | - |
| 603 | } | - |
| 604 | - | |
| 605 | /*! | - |
| 606 | Returns the cache limit (in kilobytes). | - |
| 607 | - | |
| 608 | The default cache limit is 10240 KB. | - |
| 609 | - | |
| 610 | \sa setCacheLimit() | - |
| 611 | */ | - |
| 612 | - | |
| 613 | int QPixmapCache::cacheLimit() | - |
| 614 | { | - |
| 615 | return cache_limit; | - |
| 616 | } | - |
| 617 | - | |
| 618 | /*! | - |
| 619 | Sets the cache limit to \a n kilobytes. | - |
| 620 | - | |
| 621 | The default setting is 10240 KB. | - |
| 622 | - | |
| 623 | \sa cacheLimit() | - |
| 624 | */ | - |
| 625 | - | |
| 626 | void QPixmapCache::setCacheLimit(int n) | - |
| 627 | { | - |
| 628 | cache_limit = n; | - |
| 629 | pm_cache()->setMaxCost(1024 * cache_limit); | - |
| 630 | } | - |
| 631 | - | |
| 632 | /*! | - |
| 633 | Removes the pixmap associated with \a key from the cache. | - |
| 634 | */ | - |
| 635 | void QPixmapCache::remove(const QString &key) | - |
| 636 | { | - |
| 637 | pm_cache()->remove(key); | - |
| 638 | } | - |
| 639 | - | |
| 640 | /*! | - |
| 641 | Removes the pixmap associated with \a key from the cache and releases | - |
| 642 | the key for a future insertion. | - |
| 643 | - | |
| 644 | \since 4.6 | - |
| 645 | */ | - |
| 646 | void QPixmapCache::remove(const Key &key) | - |
| 647 | { | - |
| 648 | //The key is not valid anymore, a flush happened before probably | - |
| 649 | if (!key.d || !key.d->isValid) | - |
| 650 | return; | - |
| 651 | pm_cache()->remove(key); | - |
| 652 | } | - |
| 653 | - | |
| 654 | /*! | - |
| 655 | Removes all pixmaps from the cache. | - |
| 656 | */ | - |
| 657 | - | |
| 658 | void QPixmapCache::clear() | - |
| 659 | { | - |
| 660 | QT_TRY { | - |
| 661 | if (pm_cache.exists()) | - |
| 662 | pm_cache->clear(); | - |
| 663 | } QT_CATCH(const std::bad_alloc &) { dead code: { } | - |
| 664 | // if we ran out of memory during pm_cache(), it's no leak, dead code: { } | - |
| 665 | // so just ignore it. dead code: { } | - |
| 666 | } dead code: { } | - |
| 667 | } | - |
| 668 | - | |
| 669 | void QPixmapCache::flushDetachedPixmaps() | - |
| 670 | { | - |
| 671 | pm_cache()->flushDetachedPixmaps(true); | - |
| 672 | } | - |
| 673 | - | |
| 674 | int QPixmapCache::totalUsed() | - |
| 675 | { | - |
| 676 | return (pm_cache()->totalCost()+1023) / 1024; | - |
| 677 | } | - |
| 678 | - | |
| 679 | /*! | - |
| 680 | \fn QPixmapCache::KeyData::KeyData() | - |
| 681 | - | |
| 682 | \internal | - |
| 683 | */ | - |
| 684 | /*! | - |
| 685 | \fn QPixmapCache::KeyData::KeyData(const KeyData &other) | - |
| 686 | \internal | - |
| 687 | */ | - |
| 688 | /*! | - |
| 689 | \fn QPixmapCache::KeyData::~KeyData() | - |
| 690 | - | |
| 691 | \internal | - |
| 692 | */ | - |
| 693 | QT_END_NAMESPACE | - |
| Source code | Switch to Preprocessed file |