factory: Create a LabelRow and LabelFactory
The LabelRow is an implementation of the ListRow for the common case of displaying text to the user. It has some convenience properties for setting the xalign property and adding the "numeric" class to the Gtk.Label. The LabelFactory creates LabelRows. Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
parent
711fa0da5b
commit
236a1e60c2
|
@ -68,6 +68,24 @@ class ListRow(GObject.GObject):
|
|||
return self.listitem.get_item()
|
||||
|
||||
|
||||
class InscriptionRow(ListRow):
|
||||
"""A ListRow for displaying Gtk.Inscription widgets."""
|
||||
|
||||
item_property = GObject.Property(type=str)
|
||||
|
||||
def __init__(self, listitem: Gtk.ListItem, item_property: str,
|
||||
xalign: float = 0.0, numeric: bool = False) -> None:
|
||||
"""Create a new Gtk.Label."""
|
||||
super().__init__(listitem, item_property=item_property)
|
||||
self.child = Gtk.Inscription(xalign=xalign)
|
||||
if numeric:
|
||||
self.child.add_css_class("numeric")
|
||||
|
||||
def do_bind(self) -> None:
|
||||
"""Bind a ListItem to the Label."""
|
||||
self.bind_and_set_property(self.item_property, "text")
|
||||
|
||||
|
||||
class Factory(Gtk.SignalListItemFactory):
|
||||
"""A customized Factory for making list row widgets."""
|
||||
|
||||
|
@ -97,3 +115,14 @@ class Factory(Gtk.SignalListItemFactory):
|
|||
listitem: Gtk.ListItem) -> None:
|
||||
listitem.set_child(None)
|
||||
listitem.listrow = None
|
||||
|
||||
|
||||
class InscriptionFactory(Factory):
|
||||
"""A Factory that creates InscriptionRows."""
|
||||
|
||||
def __init__(self, item_property: str,
|
||||
script_type: typing.Type[InscriptionRow] = InscriptionRow,
|
||||
xalign: float = 0.0, numeric: bool = False):
|
||||
"""Initialize a LabelFactory."""
|
||||
super().__init__(row_type=script_type, item_property=item_property,
|
||||
xalign=xalign, numeric=numeric)
|
||||
|
|
|
@ -70,6 +70,38 @@ class TestListRow(unittest.TestCase):
|
|||
self.assertEqual(len(self.row.bindings), 0)
|
||||
|
||||
|
||||
class TestInscriptionRow(unittest.TestCase):
|
||||
"""Test our pre-configured InscriptionRow."""
|
||||
|
||||
def setUp(self):
|
||||
"""Set up common variables."""
|
||||
self.item = Gtk.Inscription(text="Test")
|
||||
self.listitem = Gtk.ListItem()
|
||||
self.listitem.get_item = unittest.mock.Mock(return_value=self.item)
|
||||
|
||||
def test_inscription_row(self):
|
||||
"""Test that the LabelRow works as expected."""
|
||||
row = emmental.factory.InscriptionRow(self.listitem,
|
||||
item_property="text")
|
||||
self.assertIsInstance(row, emmental.factory.ListRow)
|
||||
self.assertIsInstance(row.child, Gtk.Inscription)
|
||||
|
||||
self.assertEqual(row.item_property, "text")
|
||||
self.assertEqual(row.child.get_xalign(), 0.0)
|
||||
self.assertFalse(row.child.has_css_class("numeric"))
|
||||
|
||||
row.bind()
|
||||
self.assertEqual(row.child.get_text(), "Test")
|
||||
|
||||
def test_numeric_inscription(self):
|
||||
"""Test that we can create numeric Labels."""
|
||||
row = emmental.factory.InscriptionRow(self.listitem,
|
||||
item_property="text",
|
||||
xalign=1.0, numeric=True)
|
||||
self.assertEqual(row.child.get_xalign(), 1.0)
|
||||
self.assertTrue(row.child.has_css_class("numeric"))
|
||||
|
||||
|
||||
class TestFactory(unittest.TestCase):
|
||||
"""Test a Factory."""
|
||||
|
||||
|
@ -112,3 +144,32 @@ class TestFactory(unittest.TestCase):
|
|||
self.factory.emit("teardown", self.listitem)
|
||||
self.assertIsNone(self.listitem.get_child())
|
||||
self.assertIsNone(self.listitem.listrow)
|
||||
|
||||
|
||||
class TestInscriptionFactory(unittest.TestCase):
|
||||
"""Test an Inscription Factory."""
|
||||
|
||||
def setUp(self):
|
||||
"""Set up common variables."""
|
||||
self.item = Gtk.Inscription(text="Text")
|
||||
self.listitem = Gtk.ListItem()
|
||||
self.listitem.get_item = unittest.mock.Mock(return_value=self.item)
|
||||
|
||||
def test_init(self):
|
||||
"""Test a basic LabelFactory."""
|
||||
factory = emmental.factory.InscriptionFactory(item_property="text",
|
||||
xalign=1.0, numeric=True)
|
||||
self.assertIsInstance(factory, emmental.factory.Factory)
|
||||
self.assertEqual(factory.row_type, emmental.factory.InscriptionRow)
|
||||
|
||||
factory.emit("setup", self.listitem)
|
||||
self.assertIsInstance(self.listitem.listrow,
|
||||
emmental.factory.InscriptionRow)
|
||||
self.assertEqual(self.listitem.get_child().get_xalign(), 1.0)
|
||||
self.assertTrue(self.listitem.get_child().has_css_class("numeric"))
|
||||
|
||||
def test_script_type(self):
|
||||
"""Test a LabelFactory that makes other types of LabelRows."""
|
||||
factory = emmental.factory.InscriptionFactory(
|
||||
script_type=Gtk.EditableLabel, item_property="label")
|
||||
self.assertEqual(factory.row_type, Gtk.EditableLabel)
|
||||
|
|
Loading…
Reference in New Issue