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}".format( |
|
46 basename, size.width(), size.height()) |
|
47 else: |
|
48 key = basename |
|
49 |
37 try: |
50 try: |
38 return self.pixmapCache[key] |
51 return self.pixmapCache[key] |
39 except KeyError: |
52 except KeyError: |
40 if not os.path.isabs(key): |
53 pm = QPixmap() |
41 for path in self.searchPath: |
54 for extension in self.SupportedExtensions: |
42 pm = QPixmap(path + "/" + key) |
55 filename = basename + extension |
43 if not pm.isNull(): |
56 if not os.path.isabs(filename): |
44 break |
57 for path in self.searchPath: |
|
58 pm = QPixmap(path + "/" + filename) |
|
59 if not pm.isNull(): |
|
60 break |
45 else: |
61 else: |
46 pm = QPixmap() |
62 pm = QPixmap(filename) |
|
63 if not pm.isNull(): |
|
64 if size and not size.isEmpty(): |
|
65 pm = pm.scaled(size) |
|
66 break |
47 else: |
67 else: |
48 pm = QPixmap(key) |
68 pm = QPixmap() |
|
69 |
49 self.pixmapCache[key] = pm |
70 self.pixmapCache[key] = pm |
50 return self.pixmapCache[key] |
71 return self.pixmapCache[key] |
|
72 |
51 return QPixmap() |
73 return QPixmap() |
52 |
74 |
53 def addSearchPath(self, path): |
75 def addSearchPath(self, path): |
54 """ |
76 """ |
55 Public method to add a path to the search path. |
77 Public method to add a path to the search path. |
60 self.searchPath.append(path) |
82 self.searchPath.append(path) |
61 |
83 |
62 pixCache = PixmapCache() |
84 pixCache = PixmapCache() |
63 |
85 |
64 |
86 |
65 def getPixmap(key, cache=pixCache): |
87 def getPixmap(key, size=None, cache=pixCache): |
66 """ |
88 """ |
67 Module function to retrieve a pixmap. |
89 Module function to retrieve a pixmap. |
68 |
90 |
69 @param key name of the wanted pixmap (string) |
91 @param key name of the wanted pixmap |
70 @param cache reference to the pixmap cache object (PixmapCache) |
92 @type str |
71 @return the requested pixmap (QPixmap) |
93 @param size requested size |
|
94 @type QSize |
|
95 @param cache reference to the pixmap cache object |
|
96 @type PixmapCache |
|
97 @return the requested pixmap |
|
98 @rtype QPixmap |
72 """ |
99 """ |
73 return cache.getPixmap(key) |
100 return cache.getPixmap(key, size=size) |
74 |
101 |
75 |
102 |
76 def getIcon(key, cache=pixCache): |
103 def getIcon(key, size=None, cache=pixCache): |
77 """ |
104 """ |
78 Module function to retrieve an icon. |
105 Module function to retrieve an icon. |
79 |
106 |
80 @param key name of the wanted icon (string) |
107 @param key name of the wanted pixmap |
81 @param cache reference to the pixmap cache object (PixmapCache) |
108 @type str |
82 @return the requested icon (QIcon) |
109 @param size requested size |
|
110 @type QSize |
|
111 @param cache reference to the pixmap cache object |
|
112 @type PixmapCache |
|
113 @return the requested icon |
|
114 @rtype QIcon |
83 """ |
115 """ |
84 return QIcon(cache.getPixmap(key)) |
116 return QIcon(cache.getPixmap(key, size=size)) |
85 |
117 |
86 |
118 |
87 def getSymlinkIcon(key, cache=pixCache): |
119 def getSymlinkIcon(key, size=None, cache=pixCache): |
88 """ |
120 """ |
89 Module function to retrieve a symbolic link icon. |
121 Module function to retrieve a symbolic link icon. |
90 |
122 |
91 @param key name of the wanted icon (string) |
123 @param key name of the wanted pixmap |
92 @param cache reference to the pixmap cache object (PixmapCache) |
124 @type str |
93 @return the requested icon (QIcon) |
125 @param size requested size |
|
126 @type QSize |
|
127 @param cache reference to the pixmap cache object |
|
128 @type PixmapCache |
|
129 @return the requested icon |
|
130 @rtype QIcon |
94 """ |
131 """ |
95 pix1 = QPixmap(cache.getPixmap(key)) |
132 pix1 = QPixmap(cache.getPixmap(key, size=size)) |
96 pix2 = cache.getPixmap("symlink.png") |
133 pix2 = cache.getPixmap("symlink.png") |
97 painter = QPainter(pix1) |
134 painter = QPainter(pix1) |
98 painter.drawPixmap(0, 10, pix2) |
135 painter.drawPixmap(0, 10, pix2) |
99 painter.end() |
136 painter.end() |
100 return QIcon(pix1) |
137 return QIcon(pix1) |
101 |
138 |
102 |
139 |
103 def getCombinedIcon(keys, cache=pixCache): |
140 def getCombinedIcon(keys, size=None, cache=pixCache): |
104 """ |
141 """ |
105 Module function to retrieve a symbolic link icon. |
142 Module function to retrieve a symbolic link icon. |
106 |
143 |
107 @param keys list of names of icons (string) |
144 @param keys list of names of icons |
108 @param cache reference to the pixmap cache object (PixmapCache) |
145 @type list of str |
109 @return the requested icon (QIcon) |
146 @param size requested size of individual icons |
|
147 @type QSize |
|
148 @param cache reference to the pixmap cache object |
|
149 @type PixmapCache |
|
150 @return the requested icon |
|
151 @rtype QIcon |
110 """ |
152 """ |
111 height = width = 0 |
153 height = width = 0 |
112 pixmaps = [] |
154 pixmaps = [] |
113 for key in keys: |
155 for key in keys: |
114 pix = cache.getPixmap(key) |
156 pix = cache.getPixmap(key, size=size) |
115 if not pix.isNull(): |
157 if not pix.isNull(): |
116 height = max(height, pix.height()) |
158 height = max(height, pix.height()) |
117 width = max(width, pix.width()) |
159 width = max(width, pix.width()) |
118 pixmaps.append(pix) |
160 pixmaps.append(pix) |
119 if pixmaps: |
161 if pixmaps: |