gtk: Create a LabelFactory

This is a Gtk.ListItemFactory that creates a Label widget and binds a
generic property to it.

Signed-off-by: Anna Schumaker <anna@nowheycreamery.com>
This commit is contained in:
Anna Schumaker 2023-08-01 15:30:56 -04:00
parent 7adbb5938d
commit 4bce147e45
2 changed files with 85 additions and 0 deletions

47
tests/gtk/test_row.py Normal file
View File

@ -0,0 +1,47 @@
# Copyright 2023 (c) Anna Schumaker.
"""Tests our row widgets and factories."""
import unittest
import xfstestsdb.gtk.row
from gi.repository import Gtk
class TestLabelFactory(unittest.TestCase):
"""Tests our Gtk.Factory to make Gtk.Labels."""
def setUp(self):
"""Set up common variables."""
self.testcase = xfstestsdb.gtk.model.TestCase("test/case")
self.listitem = Gtk.ListItem()
self.listitem.get_item = unittest.mock.Mock(return_value=self.testcase)
self.factory = xfstestsdb.gtk.row.LabelFactory("name")
def test_init(self):
"""Test that the factory was initialized correctly."""
self.assertIsInstance(self.factory, Gtk.SignalListItemFactory)
self.assertEqual(self.factory.property, "name")
def test_setup(self):
"""Test that the factory implements the 'setup' signal."""
self.factory.emit("setup", self.listitem)
self.assertIsInstance(self.listitem.get_child(), Gtk.Label)
self.assertTrue(self.listitem.get_child().has_css_class("numeric"))
def test_bind(self):
"""Test that the factory implements the 'bind' signal."""
self.factory.emit("setup", self.listitem)
self.factory.emit("bind", self.listitem)
self.assertEqual(self.listitem.get_child().get_text(), "test/case")
def test_unbind(self):
"""Test that the factory implements the 'unbind' signal."""
self.factory.emit("setup", self.listitem)
self.factory.emit("bind", self.listitem)
self.factory.emit("unbind", self.listitem)
self.assertEqual(self.listitem.get_child().get_text(), "")
def test_teardown(self):
"""Test that the factory implements the 'teardown' signal."""
self.factory.emit("setup", self.listitem)
self.factory.emit("teardown", self.listitem)
self.assertIsNone(self.listitem.get_child())

38
xfstestsdb/gtk/row.py Normal file
View File

@ -0,0 +1,38 @@
# Copyright 2023 (c) Anna Schumaker.
"""Our TestCase row widgets and factory."""
import typing
from gi.repository import GObject
from gi.repository import Gtk
class LabelFactory(Gtk.SignalListItemFactory):
"""Create Gtk.Labels for each testcase."""
property = GObject.Property(type=str)
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."""
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."""
text = listitem.get_item().get_property(self.property)
listitem.get_child().set_text(text)
def do_unbind(self, factory: typing.Self, listitem: Gtk.ListItem) -> None:
"""Unbind a ListItem from the child widget."""
listitem.get_child().set_text("")
def do_teardown(self, factory: typing.Self,
listitem: Gtk.ListItem) -> None:
"""Clean up a ListItem child widget."""
listitem.set_child(None)