UI/PixmapCache.py

changeset 6756
7556c951dce8
parent 6645
ad476851d7e0
child 6772
b48db2a31d21
diff -r 009812744917 -r 7556c951dce8 UI/PixmapCache.py
--- a/UI/PixmapCache.py	Fri Feb 15 18:58:49 2019 +0100
+++ b/UI/PixmapCache.py	Fri Feb 15 19:19:20 2019 +0100
@@ -19,6 +19,8 @@
     """
     Class implementing a pixmap cache for icons.
     """
+    SupportedExtensions = [".svgz", ".svg", ".png"]
+    
     def __init__(self):
         """
         Constructor
@@ -26,28 +28,47 @@
         self.pixmapCache = {}
         self.searchPath = []
 
-    def getPixmap(self, key):
+    def getPixmap(self, key, size=None):
         """
         Public method to retrieve a pixmap.
 
-        @param key name of the wanted pixmap (string)
-        @return the requested pixmap (QPixmap)
+        @param key name of the wanted pixmap
+        @type str
+        @param size requested size
+        @type QSize
+        @return the requested pixmap
+        @rtype QPixmap
         """
         if key:
+            basename, ext = os.path.splitext(key)
+            if size and not size.isEmpty():
+                key = "{0}_{1}_{2}"
+            else:
+                key = basename
+            
             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
+                pm = QPixmap()
+                for extension in self.SupportedExtensions:
+                    filename = basename + extension
+                    if not os.path.isabs(filename):
+                        for path in self.searchPath:
+                            pm = QPixmap(path + "/" + filename)
+                            if not pm.isNull():
+                                break
                     else:
-                        pm = QPixmap()
+                        pm = QPixmap(filename)
+                    if not pm.isNull():
+                        if size and not size.isEmpty():
+                            pm = pm.scaled(size)
+                        break
                 else:
-                    pm = QPixmap(key)
+                    pm = QPixmap()
+                
                 self.pixmapCache[key] = pm
                 return self.pixmapCache[key]
+        
         return QPixmap()
 
     def addSearchPath(self, path):
@@ -62,37 +83,52 @@
 pixCache = PixmapCache()
 
 
-def getPixmap(key, cache=pixCache):
+def getPixmap(key, size=None, cache=pixCache):
     """
     Module function to retrieve a pixmap.
 
-    @param key name of the wanted pixmap (string)
-    @param cache reference to the pixmap cache object (PixmapCache)
-    @return the requested pixmap (QPixmap)
+    @param key name of the wanted pixmap
+    @type str
+    @param size requested size
+    @type QSize
+    @param cache reference to the pixmap cache object
+    @type PixmapCache
+    @return the requested pixmap
+    @rtype QPixmap
     """
-    return cache.getPixmap(key)
+    return cache.getPixmap(key, size=size)
 
 
-def getIcon(key, cache=pixCache):
+def getIcon(key, size=None, cache=pixCache):
     """
     Module function to retrieve an icon.
 
-    @param key name of the wanted icon (string)
-    @param cache reference to the pixmap cache object (PixmapCache)
-    @return the requested icon (QIcon)
+    @param key name of the wanted pixmap
+    @type str
+    @param size requested size
+    @type QSize
+    @param cache reference to the pixmap cache object
+    @type PixmapCache
+    @return the requested icon
+    @rtype QIcon
     """
-    return QIcon(cache.getPixmap(key))
+    return QIcon(cache.getPixmap(key, size=size))
 
 
-def getSymlinkIcon(key, cache=pixCache):
+def getSymlinkIcon(key, size=None, cache=pixCache):
     """
     Module function to retrieve a symbolic link icon.
 
-    @param key name of the wanted icon (string)
-    @param cache reference to the pixmap cache object (PixmapCache)
-    @return the requested icon (QIcon)
+    @param key name of the wanted pixmap
+    @type str
+    @param size requested size
+    @type QSize
+    @param cache reference to the pixmap cache object
+    @type PixmapCache
+    @return the requested icon
+    @rtype QIcon
     """
-    pix1 = QPixmap(cache.getPixmap(key))
+    pix1 = QPixmap(cache.getPixmap(key, size=size))
     pix2 = cache.getPixmap("symlink.png")
     painter = QPainter(pix1)
     painter.drawPixmap(0, 10, pix2)
@@ -100,18 +136,23 @@
     return QIcon(pix1)
 
 
-def getCombinedIcon(keys, cache=pixCache):
+def getCombinedIcon(keys, size=None, cache=pixCache):
     """
     Module function to retrieve a symbolic link icon.
 
-    @param keys list of names of icons (string)
-    @param cache reference to the pixmap cache object (PixmapCache)
-    @return the requested icon (QIcon)
+    @param keys list of names of icons
+    @type list of str
+    @param size requested size of individual icons
+    @type QSize
+    @param cache reference to the pixmap cache object
+    @type PixmapCache
+    @return the requested icon
+    @rtype QIcon
     """
     height = width = 0
     pixmaps = []
     for key in keys:
-        pix = cache.getPixmap(key)
+        pix = cache.getPixmap(key, size=size)
         if not pix.isNull():
             height = max(height, pix.height())
             width = max(width, pix.width())

eric ide

mercurial