src/eric7/UI/PixmapCache.py

branch
eric7
changeset 9413
80c06d472826
parent 9221
bf71ee032bb4
child 9653
e67609152c5e
equal deleted inserted replaced
9412:45e7bb09c120 9413:80c06d472826
1 # -*- coding: utf-8 -*- 1 # -*- coding: utf-8 -*-
2 2
3 # Copyright (c) 2004 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> 3 # Copyright (c) 2022 Detlev Offenbach <detlev@die-offenbachs.de>
4 # 4 #
5 5
6 """ 6 """
7 Module implementing a pixmap cache for icons. 7 Module implementing a compatibility interface of the EricPixmapCache functionality to
8 satisfy old plug-ins.
8 """ 9 """
9 10
10 import os 11 from eric7.EricGui import EricPixmapCache
11 12
12 from PyQt6.QtCore import Qt, QSize 13 getPixmap = EricPixmapCache.getPixmap
13 from PyQt6.QtGui import QPixmap, QIcon, QPainter 14 getIcon = EricPixmapCache.getIcon
15 getSymlinkIcon = EricPixmapCache.getSymlinkIcon
16 getCombinedIcon = EricPixmapCache.getCombinedIcon
17 addSearchPath = EricPixmapCache.addSearchPath
18 removeSearchPath = EricPixmapCache.removeSearchPath
14 19
15 20 # TODO: remove this module with release 23.4
16 class PixmapCache:
17 """
18 Class implementing a pixmap cache for icons.
19 """
20
21 SupportedExtensions = [".svgz", ".svg", ".png"]
22
23 def __init__(self):
24 """
25 Constructor
26 """
27 self.pixmapCache = {}
28 self.searchPath = []
29
30 def getPixmap(self, key, size=None):
31 """
32 Public method to retrieve a pixmap.
33
34 @param key name of the wanted pixmap
35 @type str
36 @param size requested size
37 @type QSize
38 @return the requested pixmap
39 @rtype QPixmap
40 """
41 if key:
42 basename, ext = os.path.splitext(key)
43 if size and not size.isEmpty():
44 key = "{0}_{1}_{2}".format(basename, size.width(), size.height())
45 else:
46 key = basename
47
48 try:
49 return self.pixmapCache[key]
50 except KeyError:
51 pm = QPixmap()
52 for extension in self.SupportedExtensions:
53 filename = basename + extension
54 if not os.path.isabs(filename):
55 for path in self.searchPath:
56 pm = QPixmap(path + "/" + filename)
57 if not pm.isNull():
58 break
59 else:
60 pm = QPixmap(filename)
61 if not pm.isNull():
62 if size and not size.isEmpty():
63 pm = pm.scaled(size)
64 break
65 else:
66 pm = QPixmap()
67
68 self.pixmapCache[key] = pm
69 return self.pixmapCache[key]
70
71 return QPixmap()
72
73 def addSearchPath(self, path):
74 """
75 Public method to add a path to the search path.
76
77 @param path path to add
78 @type str
79 """
80 if path not in self.searchPath:
81 self.searchPath.append(path)
82
83 def removeSearchPath(self, path):
84 """
85 Public method to remove a path from the search path.
86
87 @param path path to remove
88 @type str
89 """
90 if path in self.searchPath:
91 self.searchPath.remove(path)
92
93
94 pixCache = PixmapCache()
95
96
97 def getPixmap(key, size=None, cache=pixCache):
98 """
99 Module function to retrieve a pixmap.
100
101 @param key name of the wanted pixmap
102 @type str
103 @param size requested size
104 @type QSize
105 @param cache reference to the pixmap cache object
106 @type PixmapCache
107 @return the requested pixmap
108 @rtype QPixmap
109 """
110 return cache.getPixmap(key, size=size)
111
112
113 def getIcon(key, size=None, cache=pixCache):
114 """
115 Module function to retrieve an icon.
116
117 @param key name of the wanted pixmap
118 @type str
119 @param size requested size
120 @type QSize
121 @param cache reference to the pixmap cache object
122 @type PixmapCache
123 @return the requested icon
124 @rtype QIcon
125 """
126 return QIcon(cache.getPixmap(key, size=size))
127
128
129 def getSymlinkIcon(key, size=None, cache=pixCache):
130 """
131 Module function to retrieve a symbolic link icon.
132
133 @param key name of the wanted pixmap
134 @type str
135 @param size requested size
136 @type QSize
137 @param cache reference to the pixmap cache object
138 @type PixmapCache
139 @return the requested icon
140 @rtype QIcon
141 """
142 pix1 = QPixmap(cache.getPixmap(key, size=size))
143 pix2 = cache.getPixmap("symlink")
144 painter = QPainter(pix1)
145 painter.drawPixmap(0, 10, pix2)
146 painter.end()
147 return QIcon(pix1)
148
149
150 def getCombinedIcon(keys, size=None, cache=pixCache):
151 """
152 Module function to retrieve a symbolic link icon.
153
154 @param keys list of names of icons
155 @type list of str
156 @param size requested size of individual icons
157 @type QSize
158 @param cache reference to the pixmap cache object
159 @type PixmapCache
160 @return the requested icon
161 @rtype QIcon
162 """
163 height = width = 0
164 pixmaps = []
165 for key in keys:
166 pix = cache.getPixmap(key, size=size)
167 if not pix.isNull():
168 height = max(height, pix.height())
169 width = max(width, pix.width())
170 pixmaps.append(pix)
171 if pixmaps:
172 pix = QPixmap(len(pixmaps) * width, height)
173 pix.fill(Qt.GlobalColor.transparent)
174 painter = QPainter(pix)
175 x = 0
176 for pixmap in pixmaps:
177 painter.drawPixmap(x, 0, pixmap.scaled(QSize(width, height)))
178 x += width
179 painter.end()
180 icon = QIcon(pix)
181 else:
182 icon = QIcon()
183 return icon
184
185
186 def addSearchPath(path, cache=pixCache):
187 """
188 Module function to add a path to the search path.
189
190 @param path path to add
191 @type str
192 @param cache reference to the pixmap cache object
193 @type PixmapCache
194 """
195 cache.addSearchPath(path)
196
197
198 def removeSearchPath(path, cache=pixCache):
199 """
200 Public method to remove a path from the search path.
201
202 @param path path to remove
203 @type str
204 @param cache reference to the pixmap cache object
205 @type PixmapCache
206 """
207 cache.removeSearchPath(path)

eric ide

mercurial