sidebar: Create a TreeListModel helper function

I can't inherit from Gtk.TreeListModel since the new() constructor is
the only way to set the create_child_func. Instead, use this wrapper
function to do the same thing.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2021-10-11 21:49:05 -04:00
parent 17391acd4d
commit c982fe624b
2 changed files with 27 additions and 0 deletions

View File

@ -1,6 +1,7 @@
# Copyright 2021 (c) Anna Schumaker.
from gi.repository import GObject
from gi.repository import Gio
from gi.repository import Gtk
class ChildModel(GObject.GObject, Gio.ListModel):
def __init__(self, parent):
@ -14,3 +15,11 @@ class ChildModel(GObject.GObject, Gio.ListModel):
def children_changed(self, item, pos, rm, add):
self.emit("items-changed", pos, rm, add)
def create_child_func(item):
return ChildModel(item) if item.has_children() else None
def TreeListModel(table):
return Gtk.TreeListModel.new(table, passthrough=False, autoexpand=False,
create_func=create_child_func)

View File

@ -4,6 +4,7 @@ import db
import unittest
from gi.repository import GObject
from gi.repository import Gio
from gi.repository import Gtk
from . import model
class TestChildModel(unittest.TestCase):
@ -33,3 +34,20 @@ class TestChildModel(unittest.TestCase):
self.assertEqual(child.get_n_items(), 1)
self.assertEqual(child.get_item(0), album)
self.assertEqual(self.changed, (0, 0, 1))
class TestTreeListModel(unittest.TestCase):
def test_init(self):
tree = model.TreeListModel(db.artist.Table)
self.assertIsInstance(tree, Gtk.TreeListModel)
self.assertEqual(tree.get_model(), db.artist.Table)
self.assertFalse(tree.get_autoexpand())
self.assertFalse(tree.get_passthrough())
def test_create_child_func(self):
artist = db.artist.Table.find("Test Artist", "Test Sort")
child = model.create_child_func(artist)
self.assertEqual(child.parent, artist)
genre = db.genre.Table.find("Test Genre")
self.assertIsNone(model.create_child_func(genre))