85 lines
2.6 KiB
Python
85 lines
2.6 KiB
Python
# Copyright 2021 (c) Anna Schumaker.
|
|
from gi.repository import Gtk, Gio, GObject
|
|
import tagdb
|
|
|
|
Exclude = [ "Collection", "New Tracks", "Previous" ]
|
|
Icons = { "Favorites" : "emblem-favorites",
|
|
"Up Next" : "edit-redo" }
|
|
|
|
|
|
class AddPopoverRow(Gtk.ListBoxRow):
|
|
def __init__(self, tag):
|
|
Gtk.ListBoxRow.__init__(self)
|
|
self.tag = tag
|
|
|
|
image = Icons.get(tag.name, "audio-x-generic")
|
|
self.icon = Gtk.Image.new_from_icon_name(image)
|
|
self.label = Gtk.Label.new(str=str(tag))
|
|
|
|
self.box = Gtk.Box.new(Gtk.Orientation.HORIZONTAL, 5)
|
|
self.box.append(self.icon)
|
|
self.box.append(self.label)
|
|
self.set_child(self.box)
|
|
|
|
|
|
class AddPopover(Gtk.Popover):
|
|
def __init__(self):
|
|
Gtk.Popover.__init__(self)
|
|
self.box = Gtk.Box.new(Gtk.Orientation.VERTICAL, 0)
|
|
|
|
self.listbox = Gtk.ListBox()
|
|
self.listbox.set_sort_func(self.sort_func)
|
|
self.listbox.connect("row-activated", self.row_activated)
|
|
self.box.append(self.listbox)
|
|
|
|
for tag in tagdb.tags.User.tags():
|
|
if tag.name not in Exclude:
|
|
self.user_added(tag)
|
|
|
|
self.entry = Gtk.Entry()
|
|
self.entry.set_placeholder_text("New Playlist")
|
|
self.entry.set_icon_from_icon_name(Gtk.EntryIconPosition.PRIMARY, "list-add")
|
|
self.entry.connect("activate", self.entry_activate)
|
|
self.box.append(self.entry)
|
|
|
|
tagdb.tags.User.Added.register(self.user_added)
|
|
tagdb.tags.User.Removed.register(self.user_removed)
|
|
self.set_child(self.box)
|
|
|
|
def do_add_selected(self, tag):
|
|
from . import FilterModel
|
|
from . import Selection
|
|
|
|
bitset = Selection.get_selection()
|
|
for i in range(bitset.get_minimum(), bitset.get_maximum() + 1):
|
|
track = FilterModel.get_item(i)
|
|
track.add_to_playlist(tag.name)
|
|
|
|
tagdb.save()
|
|
self.popdown()
|
|
|
|
def row_activated(self, listbox, row):
|
|
self.do_add_selected(row.tag)
|
|
|
|
def entry_activate(self, entry):
|
|
tag = tagdb.tags.User.add(entry.get_text())
|
|
self.do_add_selected(tag)
|
|
entry.set_text("")
|
|
|
|
def sort_func(self, lhs, rhs):
|
|
if lhs.tag < rhs.tag:
|
|
return -1
|
|
if rhs.tag < lhs.tag:
|
|
return 1
|
|
return 0
|
|
|
|
def user_added(self, tag):
|
|
self.listbox.append(AddPopoverRow(tag))
|
|
|
|
def user_removed(self, tag):
|
|
for i in range(len(tagdb.tags.User)):
|
|
row = self.listbox.get_row_at_index(i)
|
|
if row is not None and row.tag == tag:
|
|
self.listbox.remove(row)
|
|
return
|