UI/PixmapCache.py

changeset 6756
7556c951dce8
parent 6645
ad476851d7e0
child 6772
b48db2a31d21
equal deleted inserted replaced
6755:009812744917 6756:7556c951dce8
17 17
18 class PixmapCache(object): 18 class PixmapCache(object):
19 """ 19 """
20 Class implementing a pixmap cache for icons. 20 Class implementing a pixmap cache for icons.
21 """ 21 """
22 SupportedExtensions = [".svgz", ".svg", ".png"]
23
22 def __init__(self): 24 def __init__(self):
23 """ 25 """
24 Constructor 26 Constructor
25 """ 27 """
26 self.pixmapCache = {} 28 self.pixmapCache = {}
27 self.searchPath = [] 29 self.searchPath = []
28 30
29 def getPixmap(self, key): 31 def getPixmap(self, key, size=None):
30 """ 32 """
31 Public method to retrieve a pixmap. 33 Public method to retrieve a pixmap.
32 34
33 @param key name of the wanted pixmap (string) 35 @param key name of the wanted pixmap
34 @return the requested pixmap (QPixmap) 36 @type str
37 @param size requested size
38 @type QSize
39 @return the requested pixmap
40 @rtype QPixmap
35 """ 41 """
36 if key: 42 if key:
43 basename, ext = os.path.splitext(key)
44 if size and not size.isEmpty():
45 key = "{0}_{1}_{2}"
46 else:
47 key = basename
48
37 try: 49 try:
38 return self.pixmapCache[key] 50 return self.pixmapCache[key]
39 except KeyError: 51 except KeyError:
40 if not os.path.isabs(key): 52 pm = QPixmap()
41 for path in self.searchPath: 53 for extension in self.SupportedExtensions:
42 pm = QPixmap(path + "/" + key) 54 filename = basename + extension
43 if not pm.isNull(): 55 if not os.path.isabs(filename):
44 break 56 for path in self.searchPath:
57 pm = QPixmap(path + "/" + filename)
58 if not pm.isNull():
59 break
45 else: 60 else:
46 pm = QPixmap() 61 pm = QPixmap(filename)
62 if not pm.isNull():
63 if size and not size.isEmpty():
64 pm = pm.scaled(size)
65 break
47 else: 66 else:
48 pm = QPixmap(key) 67 pm = QPixmap()
68
49 self.pixmapCache[key] = pm 69 self.pixmapCache[key] = pm
50 return self.pixmapCache[key] 70 return self.pixmapCache[key]
71
51 return QPixmap() 72 return QPixmap()
52 73
53 def addSearchPath(self, path): 74 def addSearchPath(self, path):
54 """ 75 """
55 Public method to add a path to the search path. 76 Public method to add a path to the search path.
60 self.searchPath.append(path) 81 self.searchPath.append(path)
61 82
62 pixCache = PixmapCache() 83 pixCache = PixmapCache()
63 84
64 85
65 def getPixmap(key, cache=pixCache): 86 def getPixmap(key, size=None, cache=pixCache):
66 """ 87 """
67 Module function to retrieve a pixmap. 88 Module function to retrieve a pixmap.
68 89
69 @param key name of the wanted pixmap (string) 90 @param key name of the wanted pixmap
70 @param cache reference to the pixmap cache object (PixmapCache) 91 @type str
71 @return the requested pixmap (QPixmap) 92 @param size requested size
93 @type QSize
94 @param cache reference to the pixmap cache object
95 @type PixmapCache
96 @return the requested pixmap
97 @rtype QPixmap
72 """ 98 """
73 return cache.getPixmap(key) 99 return cache.getPixmap(key, size=size)
74 100
75 101
76 def getIcon(key, cache=pixCache): 102 def getIcon(key, size=None, cache=pixCache):
77 """ 103 """
78 Module function to retrieve an icon. 104 Module function to retrieve an icon.
79 105
80 @param key name of the wanted icon (string) 106 @param key name of the wanted pixmap
81 @param cache reference to the pixmap cache object (PixmapCache) 107 @type str
82 @return the requested icon (QIcon) 108 @param size requested size
109 @type QSize
110 @param cache reference to the pixmap cache object
111 @type PixmapCache
112 @return the requested icon
113 @rtype QIcon
83 """ 114 """
84 return QIcon(cache.getPixmap(key)) 115 return QIcon(cache.getPixmap(key, size=size))
85 116
86 117
87 def getSymlinkIcon(key, cache=pixCache): 118 def getSymlinkIcon(key, size=None, cache=pixCache):
88 """ 119 """
89 Module function to retrieve a symbolic link icon. 120 Module function to retrieve a symbolic link icon.
90 121
91 @param key name of the wanted icon (string) 122 @param key name of the wanted pixmap
92 @param cache reference to the pixmap cache object (PixmapCache) 123 @type str
93 @return the requested icon (QIcon) 124 @param size requested size
125 @type QSize
126 @param cache reference to the pixmap cache object
127 @type PixmapCache
128 @return the requested icon
129 @rtype QIcon
94 """ 130 """
95 pix1 = QPixmap(cache.getPixmap(key)) 131 pix1 = QPixmap(cache.getPixmap(key, size=size))
96 pix2 = cache.getPixmap("symlink.png") 132 pix2 = cache.getPixmap("symlink.png")
97 painter = QPainter(pix1) 133 painter = QPainter(pix1)
98 painter.drawPixmap(0, 10, pix2) 134 painter.drawPixmap(0, 10, pix2)
99 painter.end() 135 painter.end()
100 return QIcon(pix1) 136 return QIcon(pix1)
101 137
102 138
103 def getCombinedIcon(keys, cache=pixCache): 139 def getCombinedIcon(keys, size=None, cache=pixCache):
104 """ 140 """
105 Module function to retrieve a symbolic link icon. 141 Module function to retrieve a symbolic link icon.
106 142
107 @param keys list of names of icons (string) 143 @param keys list of names of icons
108 @param cache reference to the pixmap cache object (PixmapCache) 144 @type list of str
109 @return the requested icon (QIcon) 145 @param size requested size of individual icons
146 @type QSize
147 @param cache reference to the pixmap cache object
148 @type PixmapCache
149 @return the requested icon
150 @rtype QIcon
110 """ 151 """
111 height = width = 0 152 height = width = 0
112 pixmaps = [] 153 pixmaps = []
113 for key in keys: 154 for key in keys:
114 pix = cache.getPixmap(key) 155 pix = cache.getPixmap(key, size=size)
115 if not pix.isNull(): 156 if not pix.isNull():
116 height = max(height, pix.height()) 157 height = max(height, pix.height())
117 width = max(width, pix.width()) 158 width = max(width, pix.width())
118 pixmaps.append(pix) 159 pixmaps.append(pix)
119 if pixmaps: 160 if pixmaps:

eric ide

mercurial