sidebar: Create a row Grid

This contains the actual widgets used to display playlist information. I
override the GtkGrid.attach() function so it returns the attached widget
to simplify setup a little bit.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2021-10-12 11:17:49 -04:00
parent b75fc78de7
commit d1d84af228
2 changed files with 81 additions and 0 deletions

View File

@ -8,3 +8,35 @@ class Label(Gtk.Label):
self.set_ellipsize(Pango.EllipsizeMode.MIDDLE)
self.set_halign(Gtk.Align.START)
self.set_hexpand(True)
class Grid(Gtk.Grid):
def __init__(self):
Gtk.Grid.__init__(self)
self.icon = self.attach(Gtk.Image(), 0, 0, 1, 2)
self.name = self.attach(Label(), 1, 0, 1, 1)
self.count = self.attach(Label(), 1, 1, 1, 1)
self.set_column_spacing(5)
def attach(self, widget, x, y, w, h):
super().attach(widget, x, y, w, h)
return widget
def set_count(self, item, *args):
n = item.get_n_tracks()
self.count.set_text(f"{n} Track{'s' if n != 1 else ''}")
def set_item(self, item):
self.icon.set_from_icon_name(item.get_property("icon-name"))
self.name.set_text(item.get_property("name"))
item.connect("track-added", self.set_count)
item.connect("track-removed", self.set_count)
item.connect("refreshed", self.set_count)
self.set_count(item)
def unset_item(self, item):
if item:
item.disconnect_by_func(self.set_count)
item.disconnect_by_func(self.set_count)
item.disconnect_by_func(self.set_count)

View File

@ -1,4 +1,5 @@
# Copyright 2021 (c) Anna Schumaker.
import db
import unittest
from gi.repository import Gtk
from gi.repository import Pango
@ -11,3 +12,51 @@ class TestLabel(unittest.TestCase):
self.assertEqual(label.get_ellipsize(), Pango.EllipsizeMode.MIDDLE)
self.assertEqual(label.get_halign(), Gtk.Align.START)
self.assertTrue(label.get_hexpand())
class TestGrid(unittest.TestCase):
def setUp(self): db.reset()
def test_init(self):
grid = row.Grid()
self.assertIsInstance(grid, Gtk.Grid)
self.assertIsInstance(grid.icon, Gtk.Image)
self.assertIsInstance(grid.name, row.Label)
self.assertIsInstance(grid.count, row.Label)
self.assertEqual(grid.get_column_spacing(), 5)
def test_attach(self):
grid = row.Grid()
self.assertEqual(grid.get_child_at(0, 0), grid.icon)
self.assertEqual(grid.get_child_at(1, 0), grid.name)
self.assertEqual(grid.get_child_at(1, 1), grid.count)
def test_set_item(self):
artist = db.artist.Table.find("Test Artist", "Test Sort")
grid = row.Grid()
grid.set_item(artist)
self.assertEqual(grid.icon.get_icon_name(), artist.icon_name)
self.assertEqual(grid.name.get_text(), "Test Artist")
self.assertEqual(grid.count.get_text(), "0 Tracks")
grid.unset_item(artist)
track = db.make_fake_track(1, 1, "Test Track", "/a/b/c/1.ogg")
self.assertEqual(grid.count.get_text(), "0 Tracks")
def test_count_label(self):
artist = db.artist.Table.find("Test Artist", "Test Sort")
grid = row.Grid()
grid.set_item(artist)
self.assertEqual(grid.count.get_text(), "0 Tracks")
track = db.make_fake_track(1, 1, "Test Track", "/a/b/c/1.ogg")
self.assertEqual(grid.count.get_text(), "1 Track")
grid.count.set_text("abcde")
artist.refresh()
self.assertEqual(grid.count.get_text(), "1 Track")
db.track.Table.delete(track)
artist.remove_track(track, False)
self.assertEqual(grid.count.get_text(), "0 Tracks")