00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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;
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
00093
00094
00095
00096
00097
00098
00099
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