Skip to content
Snippets Groups Projects
xmlview_widgets.py 2.89 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 = []

        #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