diff --git a/emmental/sidebar/row.py b/emmental/sidebar/row.py index 7776891..198380d 100644 --- a/emmental/sidebar/row.py +++ b/emmental/sidebar/row.py @@ -2,6 +2,8 @@ """Widgets for sidebar rows.""" from gi.repository import GObject from gi.repository import Gtk +from .title import PlaylistTitle +from .icon import Icon from .. import db @@ -11,3 +13,35 @@ class BaseRow(Gtk.Box): playlist = GObject.Property(type=db.playlist.Playlist) name = GObject.Property(type=str) count = GObject.Property(type=int) + + +class Row(BaseRow): + """A basic row representing a playlist without extra actions.""" + + image = GObject.Property(type=GObject.TYPE_PYOBJECT) + + def __init__(self, **kwargs): + """Initialize a sidebar Row.""" + super().__init__(spacing=12, **kwargs) + self._icon = Icon(text=self.name, show_initials=True) + self._title = PlaylistTitle(title=self.name, count=self.count) + self._year = None + + self.bind_property("image", self._icon, "filepath") + self.bind_property("name", self._icon, "text") + self.bind_property("name", self._title, "title") + self.bind_property("count", self._title, "count") + + self.append(self._icon) + self.append(self._title) + + @GObject.Property(type=int) + def year(self) -> int | None: + """Set the year displayed in the icon.""" + return self._year + + @year.setter + def year(self, year: int) -> None: + digits = str(year) + self._year = year + self._icon.text = f"{digits[-2]} {digits[-1]}" diff --git a/tests/sidebar/test_row.py b/tests/sidebar/test_row.py index 6f85060..b2cfcf6 100644 --- a/tests/sidebar/test_row.py +++ b/tests/sidebar/test_row.py @@ -1,6 +1,8 @@ # Copyright 2022 (c) Anna Schumaker. """Tests our sidebar row widgets.""" import unittest +import tests.util +import emmental.sidebar.icon import emmental.sidebar.row from gi.repository import Gio from gi.repository import Gtk @@ -38,3 +40,56 @@ class TestBaseRow(unittest.TestCase): self.assertIsNone(self.row.playlist) row2 = emmental.sidebar.row.BaseRow(playlist=plist) self.assertEqual(row2.playlist, plist) + + +class TestRow(unittest.TestCase): + """Test a playlist Row.""" + + def setUp(self): + """Set up common variables.""" + self.row = emmental.sidebar.row.Row() + + def test_init(self): + """Test that the Row is configured correctly.""" + self.assertIsInstance(self.row, emmental.sidebar.row.BaseRow) + self.assertEqual(self.row.get_spacing(), 12) + + def test_image(self): + """Test the image property and icon widget.""" + self.assertIsInstance(self.row._icon, emmental.sidebar.icon.Icon) + self.assertEqual(self.row.get_first_child(), self.row._icon) + + self.assertIsNone(self.row.image) + self.assertIsNone(self.row._icon.filepath) + self.assertTrue(self.row._icon.show_initials) + + self.row.image = tests.util.COVER_JPG + self.assertEqual(self.row._icon.filepath, tests.util.COVER_JPG) + + def test_name(self): + """Test the name property and title widgets.""" + self.assertIsInstance(self.row._title, + emmental.sidebar.title.PlaylistTitle) + self.assertEqual(self.row._icon.get_next_sibling(), self.row._title) + + self.row.name = "Test Playlist" + self.assertEqual(self.row._title.title, "Test Playlist") + self.assertEqual(self.row._icon.text, "Test Playlist") + + def test_count(self): + """Test the count property.""" + self.assertEqual(self.row.count, 0) + self.assertEqual(self.row._title.count, 0) + + self.row.count = 1 + self.assertEqual(self.row._title.count, 1) + + def test_year(self): + """Test setting a year as the icon text.""" + self.assertIsNone(self.row.year) + self.row.year = 1985 + self.assertEqual(self.row._icon.text, "8 5") + self.row.year = 1988 + self.assertEqual(self.row._icon.text, "8 8") + self.row.year = 2000 + self.assertEqual(self.row._icon.text, "0 0")