UI/PixmapCache.py

changeset 0
de9c2efb9d02
child 13
1af94a91f439
equal deleted inserted replaced
-1:000000000000 0:de9c2efb9d02
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2004 - 2009 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a pixmap cache for icons.
8 """
9
10 import os
11
12 from PyQt4.QtGui import QPixmap, QIcon
13
14 class PixmapCache(object):
15 """
16 Class implementing a pixmap cache for icons.
17 """
18 def __init__(self):
19 """
20 Constructor
21 """
22 self.pixmapCache = {}
23 self.searchPath = []
24
25 def getPixmap(self, key):
26 """
27 Public method to retrieve a pixmap.
28
29 @param key name of the wanted pixmap (string)
30 @return the requested pixmap (QPixmap)
31 """
32 if key:
33 try:
34 return self.pixmapCache[key]
35 except KeyError:
36 if not os.path.isabs(key):
37 for path in self.searchPath:
38 pm = QPixmap(path + "/" + key)
39 if not pm.isNull():
40 break
41 else:
42 pm = QPixmap()
43 else:
44 pm = QPixmap(key)
45 self.pixmapCache[key] = pm
46 return self.pixmapCache[key]
47 return QPixmap()
48
49 def addSearchPath(self, path):
50 """
51 Public method to add a path to the search path.
52
53 @param path path to add (string)
54 """
55 if not path in self.searchPath:
56 self.searchPath.append(path)
57
58 pixCache = PixmapCache()
59
60 def getPixmap(key, cache = pixCache):
61 """
62 Module function to retrieve a pixmap.
63
64 @param key name of the wanted pixmap (string)
65 @return the requested pixmap (QPixmap)
66 """
67 return cache.getPixmap(key)
68
69 def getIcon(key, cache = pixCache):
70 """
71 Module function to retrieve an icon.
72
73 @param key name of the wanted icon (string)
74 @return the requested icon (QIcon)
75 """
76 return QIcon(cache.getPixmap(key))
77
78 def addSearchPath(path, cache = pixCache):
79 """
80 Module function to add a path to the search path.
81
82 @param path path to add (string)
83 """
84 cache.addSearchPath(path)

eric ide

mercurial