xfstestsdb/xfstestsdb/gtk/row.py

98 lines
3.5 KiB
Python

# Copyright 2023 (c) Anna Schumaker.
"""Our TestCase row widgets and factory."""
import typing
from gi.repository import GObject
from gi.repository import Gtk
STYLES = {"passed": "success", "failed": "error",
"skipped": "warning", "time": "accent"}
class LabelFactory(Gtk.SignalListItemFactory):
"""Create Gtk.Labels for each testcase."""
property = GObject.Property(type=str)
group = Gtk.SizeGroup()
def __init__(self, property: str):
"""Initialize our InscriptionFactory."""
super().__init__(property=property)
self.connect("setup", self.do_setup)
self.connect("bind", self.do_bind)
self.connect("unbind", self.do_unbind)
self.connect("teardown", self.do_teardown)
def do_setup(self, factory: typing.Self, listitem: Gtk.ListItem) -> None:
"""Set up a ListItem child widget."""
child = Gtk.Label()
child.add_css_class("numeric")
listitem.set_child(child)
LabelFactory.group.add_widget(child)
def do_bind(self, factory: typing.Self, listitem: Gtk.ListItem) -> None:
"""Bind a ListItem to the child widget."""
text = listitem.get_item().get_property(self.property)
child = listitem.get_child()
if style := STYLES.get(text):
child.add_css_class(style)
child.set_text(text)
def do_unbind(self, factory: typing.Self, listitem: Gtk.ListItem) -> None:
"""Unbind a ListItem from the child widget."""
child = listitem.get_child()
for style in STYLES.values():
child.remove_css_class(style)
child.set_text("")
def do_teardown(self, factory: typing.Self,
listitem: Gtk.ListItem) -> None:
"""Clean up a ListItem child widget."""
if (child := listitem.get_child()) is not None:
LabelFactory.group.remove_widget(child)
listitem.set_child(None)
class ResultFactory(Gtk.SignalListItemFactory):
"""Factory for making test result widgets."""
xunit = GObject.Property(type=str)
def __init__(self, xunit: str):
"""Initialize our ResultFactory."""
super().__init__(xunit=xunit)
self.connect("setup", self.do_setup)
self.connect("bind", self.do_bind)
self.connect("unbind", self.do_unbind)
self.connect("teardown", self.do_teardown)
def do_setup(self, factory: typing.Self, listitem: Gtk.ListItem) -> None:
"""Set up a ListItem child widget."""
listitem.set_child(Gtk.Label())
listitem.get_child().add_css_class("numeric")
def do_bind(self, factory: typing.Self, listitem: Gtk.ListItem) -> None:
"""Bind a ListItem to the child widget."""
if (result := listitem.get_item()[self.xunit]) is None:
return
if (text := result.status) == "passed":
text = f"{result.time} seconds"
child = listitem.get_child()
child.set_text(text)
child.set_tooltip_text(result.message.lstrip(" -"))
child.get_parent().add_css_class(result.status)
def do_unbind(self, factory: typing.Self, listitem: Gtk.ListItem) -> None:
"""Unbind a ListItem from the child widget."""
if (result := listitem.get_item()[self.xunit]) is not None:
child = listitem.get_child()
child.set_text("")
child.get_parent().remove_css_class(result.status)
def do_teardown(self, factory: typing.Self,
listitem: Gtk.ListItem) -> None:
"""Clean up a ListItem child widget."""
listitem.set_child(None)