From 979071cd1484bc06eac6e7f381144115ae8f7385 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Wed, 10 Nov 2021 11:50:40 -0500 Subject: [PATCH] playlist: Create a sort Factory For creating SortRows and handling their signals Signed-off-by: Anna Schumaker --- playlist/sort.py | 53 +++++++++++++++++++++++++++++++++++++++++++ playlist/test_sort.py | 7 ++++++ 2 files changed, 60 insertions(+) diff --git a/playlist/sort.py b/playlist/sort.py index a19c7df..fb43252 100644 --- a/playlist/sort.py +++ b/playlist/sort.py @@ -114,3 +114,56 @@ class SortRow(Gtk.Box): @GObject.Signal def move_down(self): pass + + +class Factory(Gtk.SignalListItemFactory): + def __init__(self, model): + self.model = model + Gtk.SignalListItemFactory.__init__(self) + self.connect("setup", self.setup) + self.connect("bind", self.bind) + self.connect("unbind", self.unbind) + self.connect("teardown", self.teardown) + + def setup(self, factory, listitem): + listitem.set_child(SortRow()) + + def bind(self, factory, listitem): + child = listitem.get_child() + child.set_item(listitem.get_item().get_string(), + listitem.get_position(), + self.model.get_n_enabled()) + child.connect("switched", self.row_switched) + child.connect("reversed", self.row_reversed) + child.connect("move-up", self.move_row_up) + child.connect("move-down", self.move_row_down) + + def unbind(self, factory, listitem): + child = listitem.get_child() + child.disconnect_by_func(self.row_switched) + child.disconnect_by_func(self.row_reversed) + child.disconnect_by_func(self.move_row_up) + child.disconnect_by_func(self.move_row_down) + + def teardown(self, factory, listitem): + listitem.set_child(None) + + def row_switched(self, row, enabled): + field = row.label.get_field() + if enabled: + self.model.enable(field) + index = self.model.get_enabled_model().get_index(field) + else: + index = self.model.get_enabled_model().get_index(field) + self.model.disable(field) + if index > 0: + self.model.emit("items-changed", index - 1, 1, 1) + + def row_reversed(self, row): + self.model.get_enabled_model().reverse(row.label.get_field()) + + def move_row_up(self, row): + self.model.get_enabled_model().move_up(row.label.get_field()) + + def move_row_down(self, row): + self.model.get_enabled_model().move_down(row.label.get_field()) diff --git a/playlist/test_sort.py b/playlist/test_sort.py index 9183beb..bce50db 100644 --- a/playlist/test_sort.py +++ b/playlist/test_sort.py @@ -1,6 +1,7 @@ # Copyright 2021 (c) Anna Schumaker. import unittest from gi.repository import Gtk +from . import model from . import sort class TestUpDownBox(unittest.TestCase): @@ -194,3 +195,9 @@ class TestSortRow(unittest.TestCase): row.updown.emit("move-down") self.assertTrue(self.move_down) + + +class TestFactory(unittest.TestCase): + def test_init(self): + factory = sort.Factory(model.FlatSortModel()) + self.assertIsInstance(factory, Gtk.SignalListItemFactory)