Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from gi.repository import GObject, Gtk, Pango, Gdk
from xml.etree import ElementTree as ET
class Section(Gtk.VBox):
def __init__(self, section):
super(Section, self).__init__()
self.name = section.get('id')
self.get_style_context().add_class('section-body')
self.label = Gtk.Label('Section %s' % self.name)
self.label.get_style_context().add_class('section-title')
self.pack_start(self.label, True, True, 5)
self.sequences = []
#self.override_background_color(Gtk.StateFlags.NORMAL, Gdk.RGBA(1,1,1,1))
#self.label.override_background_color(Gtk.StateFlags.NORMAL, Gdk.RGBA(.5,.5,.5,1))
#self.label.override_color(Gtk.StateFlags.NORMAL, Gdk.RGBA(1,1,1,1))
#self.label.override_font(Pango.FontDescription("bold 18"))
num = 1
for sequence in section.findall('./sequence'):
self.sequences.append(Sequence(sequence, section.get('id') + '.' + str(num)))
self.pack_start(self.sequences[-1], True, True, 5)
num += 1
class Sequence(Gtk.VBox):
def __init__(self, sequence, name):
super(Sequence, self).__init__()
self.name = name
self.get_style_context().add_class('sequence-body')
self.label = Gtk.Label('Sequence %s' % name)
self.label.get_style_context().add_class('sequence-title')
self.pack_start(self.label, True, True, 5)
self.lines = []
text = str(sequence.text)
for node in sequence:
text += node.text
text += node.tail
for line in text.split('\n'):
line = line.strip()
if line != '':
self.lines.append(Line(line))
self.pack_start(self.lines[-1], True, True, 5)
class Line(Gtk.Label):
def __init__(self, text):
super(Line, self).__init__()
self.text = text
self.set_text(' ' + text)
self.set_halign(Gtk.Align.START)
self.get_style_context().add_class('text-line')
class Word:
def __init__(self, text, start, end):
self.text = text
self.start = start
self.end = end
class XmlView(Gtk.ScrolledWindow):
def __init__(self, filename):
super(XmlView, self).__init__()
self.sections = []
self.words = []
self.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.ALWAYS)
self.add_with_viewport(self.parse_xml(filename))
self.last_section = None
self.show_section(0)
def get_view(self):
return self
def parse_xml(self, filename):
self.sections = []
root = ET.parse(filename)
vbox = Gtk.VBox()
vbox.get_style_context().add_class('xmlview')
for section in root.findall(".//section"):
self.sections.append(Section(section))
vbox.pack_start(self.sections[-1], True, True, 5)
return vbox
def show_section(self, section):
pass