WIP dynamic menu for playlist

New playlist shows faves on submenu
This commit is contained in:
Keith Edmunds 2025-02-22 22:27:05 +00:00
parent 639f006a10
commit 632937101a

View File

@ -191,7 +191,7 @@ class ItemlistManager(QDialog):
if not hh:
raise ApplicationError("ItemlistManager failed to create horizontalHeader")
hh.setStretchLastSection(True)
self.table.setColumnWidth(0, 200)
self.table.setColumnWidth(0, 288)
self.table.setColumnWidth(1, 300)
self.populate_table()
@ -267,7 +267,6 @@ class ItemlistManager(QDialog):
break
def toggle_favourite(self, item_id: int, checked: bool) -> None:
print(f"Toggle favourite for item {item_id}: {checked}")
self.callbacks.favourite(item_id, checked)
for row in range(self.table.rowCount()):
@ -671,23 +670,52 @@ class Window(QMainWindow):
items = getattr(self, f"get_{key}_items")()
for item in items:
action = QAction(item["text"], self)
action.triggered.connect(
lambda _, i=item["handler"]: getattr(self, i)()
)
# Extract handler and arguments
handler = getattr(self, item["handler"], None)
args = item.get("args", ())
if handler:
# Use a lambda to pass arguments to the function
action.triggered.connect(lambda _, h=handler, a=args: h(*a))
submenu.addAction(action)
break
def get_new_playlist_dynamic_submenu_items(self):
"""Returns dynamically generated menu items for Submenu 1."""
return [
{"text": "Option A", "handler": "option_a_handler"},
{"text": "Option B", "handler": "option_b_handler"},
]
def get_new_playlist_dynamic_submenu_items(
self,
) -> list[dict[str, str | tuple[Session, int]]]:
"""
Return dynamically generated menu items, in this case
templates marked as favourite from which to generate a
new playlist.
The handler is to call create_playlist with a session
and template_id.
"""
submenu_items: list[dict[str, str | tuple[Session, int]]] = []
with db.Session() as session:
templates = Playlists.get_all_templates(session)
for template in templates:
submenu_items.append(
{
"text": template.name,
"handler": "create_and_show_playlist",
"args": (
session,
template.id,
),
}
)
return submenu_items
def get_query_dynamic_submenu_items(self):
"""Returns dynamically generated menu items for Submenu 2."""
return [
{"text": "Action X", "handler": "action_x_handler"},
{"text": "Action Xargs", "handler": "kae", "args": (21,)},
{"text": "Action Y", "handler": "action_y_handler"},
]
@ -870,7 +898,7 @@ class Window(QMainWindow):
self.signals.search_songfacts_signal.connect(self.open_songfacts_browser)
self.signals.search_wikipedia_signal.connect(self.open_wikipedia_browser)
def create_playlist(
def create_and_show_playlist(
self, session: Session, template_id: int
) -> Optional[Playlists]:
"""Create new playlist"""
@ -890,13 +918,15 @@ class Window(QMainWindow):
)
if playlist:
return playlist
self.open_and_show_playlist(session, playlist)
else:
log.error(f"Failed to create playlist, {playlist_name=}")
return None
def create_playlist_tab(self, playlist: Playlists, is_template: bool = False) -> int:
def create_playlist_tab(
self, playlist: Playlists, is_template: bool = False
) -> int:
"""
Take the passed playlist, create a playlist tab and
add tab to display. Return index number of tab.
@ -1246,8 +1276,9 @@ class Window(QMainWindow):
def favourite(template_id: int, favourite: bool) -> None:
"""Mark template as (not) favourite"""
print(f"manage_templates.favourite({template_id=}")
print(f"{session=}")
template = session.get(Playlists, template_id)
template.favourite = favourite
session.commit()
def new_item() -> None:
"""Create new template"""
@ -1259,9 +1290,9 @@ class Window(QMainWindow):
if template_id is None:
return
new_template = self.create_playlist(session, template_id)
new_template = self.create_and_show_playlist(session, template_id)
if new_template:
self.open_playlist(session, new_template, is_template=True)
self.open_and_show_playlist(session, new_template, is_template=True)
def rename(template_id: int) -> Optional[str]:
"""rename template"""
@ -1295,8 +1326,11 @@ class Window(QMainWindow):
with db.Session() as session:
for template in Playlists.get_all_templates(session):
# TODO: need to add in favourites
template_list.append(ItemlistItem(name=template.name, id=template.id))
template_list.append(
ItemlistItem(
name=template.name, id=template.id, favourite=template.favourite
)
)
# We need to retain a reference to the dialog box to stop it
# going out of scope and being garbage-collected.
self.dlg = ItemlistManager(template_list, callbacks)
@ -1395,7 +1429,7 @@ class Window(QMainWindow):
if not template_id:
return None # User cancelled
playlist = self.create_playlist(session, template_id)
playlist = self.create_and_show_playlist(session, template_id)
if playlist:
playlist.mark_open()
@ -1419,9 +1453,9 @@ class Window(QMainWindow):
dlg.exec()
playlist = dlg.playlist
if playlist:
self.open_playlist(session, playlist)
self.open_and_show_playlist(session, playlist)
def open_playlist(
def open_and_show_playlist(
self, session: Session, playlist: Playlists, is_template: bool = False
) -> None:
"""Open passed playlist"""