| 1 |
"""Icons that are used by Gaphor. |
|---|
| 2 |
""" |
|---|
| 3 |
|
|---|
| 4 |
import os.path |
|---|
| 5 |
import pkg_resources |
|---|
| 6 |
from xml.sax import handler |
|---|
| 7 |
import gtk |
|---|
| 8 |
|
|---|
| 9 |
from gaphor import UML |
|---|
| 10 |
from gaphor.parser import ParserException |
|---|
| 11 |
|
|---|
| 12 |
XMLNS='http://gaphor.sourceforge.net/gaphor/stock-icons' |
|---|
| 13 |
|
|---|
| 14 |
_icon_factory = gtk.IconFactory() |
|---|
| 15 |
_icon_factory.add_default() |
|---|
| 16 |
|
|---|
| 17 |
_uml_to_stock_id_map = { } |
|---|
| 18 |
|
|---|
| 19 |
def get_stock_id(element): |
|---|
| 20 |
if issubclass(element, UML.Element): |
|---|
| 21 |
try: |
|---|
| 22 |
return _uml_to_stock_id_map[element] |
|---|
| 23 |
except KeyError: |
|---|
| 24 |
log.warning ('Stock id for %s not found' % element) |
|---|
| 25 |
return None |
|---|
| 26 |
|
|---|
| 27 |
def add_stock_icon(id, icon_dir, icon_files, uml_class=None): |
|---|
| 28 |
global _uml_to_stock_id_map |
|---|
| 29 |
global _icon_factory |
|---|
| 30 |
set = gtk.IconSet() |
|---|
| 31 |
for icon in icon_files: |
|---|
| 32 |
source = gtk.IconSource() |
|---|
| 33 |
if icon.find('16') != -1: |
|---|
| 34 |
source.set_size(gtk.ICON_SIZE_MENU) |
|---|
| 35 |
elif icon.find('24') != -1: |
|---|
| 36 |
source.set_size(gtk.ICON_SIZE_SMALL_TOOLBAR) |
|---|
| 37 |
elif icon.find('48') != -1: |
|---|
| 38 |
source.set_size(gtk.ICON_SIZE_LARGE_TOOLBAR) |
|---|
| 39 |
source.set_filename(os.path.join(icon_dir, icon)) |
|---|
| 40 |
set.add_source(source) |
|---|
| 41 |
_icon_factory.add(id, set) |
|---|
| 42 |
if uml_class: |
|---|
| 43 |
_uml_to_stock_id_map[uml_class] = id |
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
class StockIconLoader(handler.ContentHandler): |
|---|
| 47 |
"""Load stock icons from an xml file in the icons directory. |
|---|
| 48 |
""" |
|---|
| 49 |
|
|---|
| 50 |
def __init__(self, icon_dir): |
|---|
| 51 |
handler.ContentHandler.__init__(self) |
|---|
| 52 |
self.icon_dir = icon_dir |
|---|
| 53 |
|
|---|
| 54 |
def endDTD(self): |
|---|
| 55 |
pass |
|---|
| 56 |
|
|---|
| 57 |
def startDocument(self): |
|---|
| 58 |
"""Start of document: all our attributes are initialized. |
|---|
| 59 |
""" |
|---|
| 60 |
self.id = '' |
|---|
| 61 |
self.files = [] |
|---|
| 62 |
self.data = '' |
|---|
| 63 |
self.element = None |
|---|
| 64 |
|
|---|
| 65 |
def endDocument(self): |
|---|
| 66 |
pass |
|---|
| 67 |
|
|---|
| 68 |
def startElement(self, name, attrs): |
|---|
| 69 |
self.data = '' |
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
if name == 'icon': |
|---|
| 73 |
self.id = attrs['id'] |
|---|
| 74 |
self.files = [] |
|---|
| 75 |
self.element = None |
|---|
| 76 |
|
|---|
| 77 |
elif name in ('element', 'file', 'stock-icons'): |
|---|
| 78 |
pass |
|---|
| 79 |
else: |
|---|
| 80 |
raise ParserException, 'Invalid XML: tag <%s> not known' % name |
|---|
| 81 |
|
|---|
| 82 |
def endElement(self, name): |
|---|
| 83 |
if name == 'icon': |
|---|
| 84 |
assert self.id |
|---|
| 85 |
assert self.files |
|---|
| 86 |
add_stock_icon(self.id, self.icon_dir, self.files, self.element) |
|---|
| 87 |
elif name == 'element': |
|---|
| 88 |
try: |
|---|
| 89 |
self.element = getattr(UML, self.data) |
|---|
| 90 |
except: |
|---|
| 91 |
raise ParserException, 'No element found with name %s' % self.data |
|---|
| 92 |
elif name == 'file': |
|---|
| 93 |
self.files.append(self.data) |
|---|
| 94 |
elif name == 'stock-icons': |
|---|
| 95 |
pass |
|---|
| 96 |
|
|---|
| 97 |
def startElementNS(self, name, qname, attrs): |
|---|
| 98 |
if not name[0] or name[0] == XMLNS: |
|---|
| 99 |
a = { } |
|---|
| 100 |
for key, val in attrs.items(): |
|---|
| 101 |
a[key[1]] = val |
|---|
| 102 |
self.startElement(name[1], a) |
|---|
| 103 |
|
|---|
| 104 |
def endElementNS(self, name, qname): |
|---|
| 105 |
if not name[0] or name[0] == XMLNS: |
|---|
| 106 |
self.endElement(name[1]) |
|---|
| 107 |
|
|---|
| 108 |
def characters(self, content): |
|---|
| 109 |
"""Read characters.""" |
|---|
| 110 |
self.data = self.data + content |
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
def load_stock_icons(): |
|---|
| 114 |
"""Load stock icon definitions from the DataDir location |
|---|
| 115 |
(usually /usr/local/share/gaphor). |
|---|
| 116 |
""" |
|---|
| 117 |
from xml.sax import make_parser |
|---|
| 118 |
parser = make_parser() |
|---|
| 119 |
icon_dir = os.path.abspath(pkg_resources.resource_filename('gaphor.ui', 'pixmaps')) |
|---|
| 120 |
log.info('Icon dir: %s' % icon_dir) |
|---|
| 121 |
|
|---|
| 122 |
loader = StockIconLoader(icon_dir) |
|---|
| 123 |
|
|---|
| 124 |
parser.setFeature(handler.feature_namespaces, 1) |
|---|
| 125 |
parser.setContentHandler(loader) |
|---|
| 126 |
|
|---|
| 127 |
filename = pkg_resources.resource_filename('gaphor.ui', 'icons.xml') |
|---|
| 128 |
if os.name == 'nt': |
|---|
| 129 |
|
|---|
| 130 |
filename = 'file:' + filename.replace('\\\\', '/') |
|---|
| 131 |
|
|---|
| 132 |
parser.parse(filename) |
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 |
|
|---|