src/gui/image/qpixmapcache.cpp
changeset 33 3e2da88830cd
parent 30 5dc02b23752f
--- a/src/gui/image/qpixmapcache.cpp	Tue Jul 06 15:10:48 2010 +0300
+++ b/src/gui/image/qpixmapcache.cpp	Wed Aug 18 10:37:55 2010 +0300
@@ -196,9 +196,10 @@
     static QPixmapCache::KeyData* getKeyData(QPixmapCache::Key *key);
 
     QList< QPair<QString,QPixmap> > allPixmaps() const;
-    void flushDetachedPixmaps(bool nt);
+    bool flushDetachedPixmaps(bool nt);
 
 private:
+    enum { soon_time = 10000, flush_time = 30000 };
     int *keyArray;
     int theid;
     int ps;
@@ -236,38 +237,44 @@
   cleaning-up, and to not cut down the size of the cache while the
   cache is in active use.
 
-  When the last pixmap has been deleted from the cache, kill the
-  timer so Qt won't keep the CPU from going into sleep mode.
+  When the last detached pixmap has been deleted from the cache, kill the
+  timer so Qt won't keep the CPU from going into sleep mode. Currently
+  the timer is not restarted when the pixmap becomes unused, but it does
+  restart once something else is added (i.e. the cache space is actually needed).
+
+  Returns true if any were removed.
 */
-void QPMCache::flushDetachedPixmaps(bool nt)
+bool QPMCache::flushDetachedPixmaps(bool nt)
 {
     int mc = maxCost();
     setMaxCost(nt ? totalCost() * 3 / 4 : totalCost() -1);
     setMaxCost(mc);
     ps = totalCost();
 
+    bool any = false;
     QHash<QString, QPixmapCache::Key>::iterator it = cacheKeys.begin();
     while (it != cacheKeys.end()) {
         if (!contains(it.value())) {
             releaseKey(it.value());
             it = cacheKeys.erase(it);
+            any = true;
         } else {
             ++it;
         }
     }
+
+    return any;
 }
 
 void QPMCache::timerEvent(QTimerEvent *)
 {
     bool nt = totalCost() == ps;
-    flushDetachedPixmaps(nt);
-
-    if (!size()) {
+    if (!flushDetachedPixmaps(nt)) {
         killTimer(theid);
         theid = 0;
     } else if (nt != t) {
         killTimer(theid);
-        theid = startTimer(nt ? 10000 : 30000);
+        theid = startTimer(nt ? soon_time : flush_time);
         t = nt;
     }
 }
@@ -315,7 +322,7 @@
     if (success) {
         cacheKeys.insert(key, cacheKey);
         if (!theid) {
-            theid = startTimer(30000);
+            theid = startTimer(flush_time);
             t = false;
         }
     } else {
@@ -331,7 +338,7 @@
     bool success = QCache<QPixmapCache::Key, QPixmapCacheEntry>::insert(cacheKey, new QPixmapCacheEntry(cacheKey, pixmap), cost);
     if (success) {
         if (!theid) {
-            theid = startTimer(30000);
+            theid = startTimer(flush_time);
             t = false;
         }
     } else {
@@ -352,7 +359,7 @@
     bool success = QCache<QPixmapCache::Key, QPixmapCacheEntry>::insert(cacheKey, new QPixmapCacheEntry(cacheKey, pixmap), cost);
     if (success) {
         if(!theid) {
-            theid = startTimer(30000);
+            theid = startTimer(flush_time);
             t = false;
         }
         const_cast<QPixmapCache::Key&>(key) = cacheKey;