eric6/UI/PixmapCache.py

changeset 6942
2602857055c5
parent 6787
3de088ae0782
child 7229
53054eb5b15a
equal deleted inserted replaced
6941:f99d60d6b59b 6942:2602857055c5
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2004 - 2019 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a pixmap cache for icons.
8 """
9
10 from __future__ import unicode_literals
11
12 import os
13
14 from PyQt5.QtCore import Qt, QSize
15 from PyQt5.QtGui import QPixmap, QIcon, QPainter
16
17
18 class PixmapCache(object):
19 """
20 Class implementing a pixmap cache for icons.
21 """
22 SupportedExtensions = [".svgz", ".svg", ".png"]
23
24 def __init__(self):
25 """
26 Constructor
27 """
28 self.pixmapCache = {}
29 self.searchPath = []
30
31 def getPixmap(self, key, size=None):
32 """
33 Public method to retrieve a pixmap.
34
35 @param key name of the wanted pixmap
36 @type str
37 @param size requested size
38 @type QSize
39 @return the requested pixmap
40 @rtype QPixmap
41 """
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
50 try:
51 return self.pixmapCache[key]
52 except KeyError:
53 pm = QPixmap()
54 for extension in self.SupportedExtensions:
55 filename = basename + extension
56 if not os.path.isabs(filename):
57 for path in self.searchPath:
58 pm = QPixmap(path + "/" + filename)
59 if not pm.isNull():
60 break
61 else:
62 pm = QPixmap(filename)
63 if not pm.isNull():
64 if size and not size.isEmpty():
65 pm = pm.scaled(size)
66 break
67 else:
68 pm = QPixmap()
69
70 self.pixmapCache[key] = pm
71 return self.pixmapCache[key]
72
73 return QPixmap()
74
75 def addSearchPath(self, path):
76 """
77 Public method to add a path to the search path.
78
79 @param path path to add (string)
80 """
81 if path not in self.searchPath:
82 self.searchPath.append(path)
83
84 pixCache = PixmapCache()
85
86
87 def getPixmap(key, size=None, cache=pixCache):
88 """
89 Module function to retrieve a pixmap.
90
91 @param key name of the wanted pixmap
92 @type str
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
99 """
100 return cache.getPixmap(key, size=size)
101
102
103 def getIcon(key, size=None, cache=pixCache):
104 """
105 Module function to retrieve an icon.
106
107 @param key name of the wanted pixmap
108 @type str
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
115 """
116 return QIcon(cache.getPixmap(key, size=size))
117
118
119 def getSymlinkIcon(key, size=None, cache=pixCache):
120 """
121 Module function to retrieve a symbolic link icon.
122
123 @param key name of the wanted pixmap
124 @type str
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
131 """
132 pix1 = QPixmap(cache.getPixmap(key, size=size))
133 pix2 = cache.getPixmap("symlink.png")
134 painter = QPainter(pix1)
135 painter.drawPixmap(0, 10, pix2)
136 painter.end()
137 return QIcon(pix1)
138
139
140 def getCombinedIcon(keys, size=None, cache=pixCache):
141 """
142 Module function to retrieve a symbolic link icon.
143
144 @param keys list of names of icons
145 @type list of str
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
152 """
153 height = width = 0
154 pixmaps = []
155 for key in keys:
156 pix = cache.getPixmap(key, size=size)
157 if not pix.isNull():
158 height = max(height, pix.height())
159 width = max(width, pix.width())
160 pixmaps.append(pix)
161 if pixmaps:
162 pix = QPixmap(len(pixmaps) * width, height)
163 pix.fill(Qt.transparent)
164 painter = QPainter(pix)
165 x = 0
166 for pixmap in pixmaps:
167 painter.drawPixmap(x, 0, pixmap.scaled(QSize(width, height)))
168 x += width
169 painter.end()
170 icon = QIcon(pix)
171 else:
172 icon = QIcon()
173 return icon
174
175
176 def addSearchPath(path, cache=pixCache):
177 """
178 Module function to add a path to the search path.
179
180 @param path path to add (string)
181 @param cache reference to the pixmap cache object (PixmapCache)
182 """
183 cache.addSearchPath(path)

eric ide

mercurial