src/gui/image/qpixmapcache.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.
00004 **
00005 ** This file is part of the QtGui module of the Qt Toolkit.
00006 **
00007 ** This file may be used under the terms of the GNU General Public
00008 ** License version 2.0 as published by the Free Software Foundation
00009 ** and appearing in the file LICENSE.GPL included in the packaging of
00010 ** this file.  Please review the following information to ensure GNU
00011 ** General Public Licensing requirements will be met:
00012 ** http://www.trolltech.com/products/qt/opensource.html
00013 **
00014 ** If you are unsure which license is appropriate for your use, please
00015 ** review the following information:
00016 ** http://www.trolltech.com/products/qt/licensing.html or contact the
00017 ** sales department at sales@trolltech.com.
00018 **
00019 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00020 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00021 **
00022 ****************************************************************************/
00023 
00024 #include "qpixmapcache.h"
00025 #include "qcache.h"
00026 #include "qobject.h"
00027 #include "qdebug.h"
00028 
00065 static int cache_limit = 1024;                        // 1024 KB cache limit
00066 
00067 class QPMCache : public QObject, public QCache<int, QPixmap>
00068 {
00069     Q_OBJECT
00070 public:
00071     QPMCache()
00072         : QObject(0),
00073           QCache<int, QPixmap>(cache_limit * 1024),
00074           id(0), ps(0), t(false) { }
00075     ~QPMCache() { }
00076 
00077     void timerEvent(QTimerEvent *);
00078     bool insert(const QString& key, const QPixmap &pixmap, int cost);
00079     bool remove(const QString &key);
00080 
00081     QPixmap *object(const QString &key) const;
00082 
00083 private:
00084     QHash<QString, int> serialNumbers;
00085     int id;
00086     int ps;
00087     bool t;
00088 };
00089 #include "qpixmapcache.moc"
00090 
00091 /*
00092   This is supposed to cut the cache size down by about 80-90% in a
00093   minute once the application becomes idle, to let any inserted pixmap
00094   remain in the cache for some time before it becomes a candidate for
00095   cleaning-up, and to not cut down the size of the cache while the
00096   cache is in active use.
00097 
00098   When the last pixmap has been deleted from the cache, kill the
00099   timer so Qt won't keep the CPU from going into sleep mode.
00100 */
00101 
00102 void QPMCache::timerEvent(QTimerEvent *)
00103 {
00104     int mc = maxCost();
00105     bool nt = totalCost() == ps;
00106     setMaxCost(nt ? totalCost() * 3 / 4 : totalCost() -1);
00107     setMaxCost(mc);
00108     ps = totalCost();
00109 
00110     QHash<QString,int>::iterator it = serialNumbers.begin();
00111     while (it != serialNumbers.end()) {
00112         if (!contains(it.value())) {
00113             it = serialNumbers.erase(it);
00114         }
00115         else
00116             ++it;
00117     }
00118 
00119 
00120     if (!size()) {
00121         killTimer(id);
00122         id = 0;
00123     } else if (nt != t) {
00124         killTimer(id);
00125         id = startTimer(nt ? 10000 : 30000);
00126         t = nt;
00127     }
00128 }
00129 
00130 QPixmap *QPMCache::object(const QString &key) const
00131 {
00132     return QCache<int,QPixmap>::object(serialNumbers.value(key, -1));
00133 }
00134 
00135 
00136 bool QPMCache::insert(const QString& key, const QPixmap &pixmap, int cost)
00137 {
00138     int serialNumber = pixmap.serialNumber();
00139     if (contains(serialNumber)) {
00140         serialNumbers.insert(key, serialNumber);
00141         return true;
00142     }
00143     bool success = QCache<int, QPixmap>::insert(serialNumber, new QPixmap(pixmap), cost);
00144     if (success) {
00145         serialNumbers.insert(key, serialNumber);
00146         if (!id) {
00147             id = startTimer(30000);
00148             t = false;
00149         }
00150     }
00151     return success;
00152 }
00153 
00154 bool QPMCache::remove(const QString &key)
00155 {
00156     int serialNumber = serialNumbers.value(key, -1);
00157     serialNumbers.remove(key);
00158     return QCache<int, QPixmap>::remove(serialNumber);
00159 }
00160 
00161 Q_GLOBAL_STATIC(QPMCache, pm_cache)
00162 
00163 
00189 QPixmap *QPixmapCache::find(const QString &key)
00190 {
00191     return pm_cache()->object(key);
00192 }
00193 
00194 
00211 bool QPixmapCache::find(const QString &key, QPixmap& pm)
00212 {
00213     QPixmap *ptr = pm_cache()->object(key);
00214     if (ptr)
00215         pm = *ptr;
00216     return ptr != 0;
00217 }
00218 
00219 
00240 bool QPixmapCache::insert(const QString &key, const QPixmap &pm)
00241 {
00242     return pm_cache()->insert(key, pm, pm.width() * pm.height() * pm.depth() / 8);
00243 }
00244 
00253 int QPixmapCache::cacheLimit()
00254 {
00255     return cache_limit;
00256 }
00257 
00266 void QPixmapCache::setCacheLimit(int n)
00267 {
00268     cache_limit = n;
00269     pm_cache()->setMaxCost(1024 * cache_limit);
00270 }
00271 
00275 void QPixmapCache::remove(const QString &key)
00276 {
00277     pm_cache()->remove(key);
00278 }
00279 
00280 
00285 void QPixmapCache::clear()
00286 {
00287     pm_cache()->clear();
00288 }
00289 

Generated on Thu Mar 15 11:54:41 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1