Skip to content
Snippets Groups Projects
xmlview_widgets.py 3.18 KiB
Newer Older
Benoit Favre's avatar
Benoit Favre committed
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 = []

        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.EventBox):
Benoit Favre's avatar
Benoit Favre committed
    def __init__(self, text):
        super(Line, self).__init__()
        self.text = text
        self.label = Gtk.Label()
        self.label.set_text('    ' + text)
        self.label.set_halign(Gtk.Align.START)
        self.label.get_style_context().add_class('text-line')
        self.add(self.label)
    def set_click_handler(self, handler):
        self.connect('button-press-event', handler)
        cursor = Gdk.Cursor(Gdk.CursorType.HAND1)
        self.get_window().set_cursor(cursor)

    def highlight(self, highlighted=True):
        if highlighted:
            self.label.get_style_context().add_class('highlighted')
        else:
            self.label.get_style_context().remove_class('highlighted')
Benoit Favre's avatar
Benoit Favre committed

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

    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 get_line_iterator(self):
        for section in self.sections:
            for sequence in section.sequences:
                for line in sequence.lines:
                    yield line