UI/PixmapCache.py

changeset 0
de9c2efb9d02
child 13
1af94a91f439
diff -r 000000000000 -r de9c2efb9d02 UI/PixmapCache.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/UI/PixmapCache.py	Mon Dec 28 16:03:33 2009 +0000
@@ -0,0 +1,84 @@
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2004 - 2009 Detlev Offenbach <detlev@die-offenbachs.de>
+#
+
+"""
+Module implementing a pixmap cache for icons.
+"""
+
+import os
+
+from PyQt4.QtGui import QPixmap, QIcon
+
+class PixmapCache(object):
+    """
+    Class implementing a pixmap cache for icons.
+    """
+    def __init__(self):
+        """
+        Constructor
+        """
+        self.pixmapCache = {}
+        self.searchPath = []
+
+    def getPixmap(self, key):
+        """
+        Public method to retrieve a pixmap.
+
+        @param key name of the wanted pixmap (string)
+        @return the requested pixmap (QPixmap)
+        """
+        if key:
+            try:
+                return self.pixmapCache[key]
+            except KeyError:
+                if not os.path.isabs(key):
+                    for path in self.searchPath:
+                        pm = QPixmap(path + "/" + key)
+                        if not pm.isNull():
+                            break
+                    else:
+                        pm = QPixmap()
+                else:
+                    pm = QPixmap(key)
+                self.pixmapCache[key] = pm
+                return self.pixmapCache[key]
+        return QPixmap()
+
+    def addSearchPath(self, path):
+        """
+        Public method to add a path to the search path.
+
+        @param path path to add (string)
+        """
+        if not path in self.searchPath:
+            self.searchPath.append(path)
+
+pixCache = PixmapCache()
+
+def getPixmap(key, cache = pixCache):
+    """
+    Module function to retrieve a pixmap.
+
+    @param key name of the wanted pixmap (string)
+    @return the requested pixmap (QPixmap)
+    """
+    return cache.getPixmap(key)
+
+def getIcon(key, cache = pixCache):
+    """
+    Module function to retrieve an icon.
+
+    @param key name of the wanted icon (string)
+    @return the requested icon (QIcon)
+    """
+    return QIcon(cache.getPixmap(key))
+
+def addSearchPath(path, cache = pixCache):
+    """
+    Module function to add a path to the search path.
+
+    @param path path to add (string)
+    """
+    cache.addSearchPath(path)

eric ide

mercurial