-
Notifications
You must be signed in to change notification settings - Fork 219
/
models.py
263 lines (213 loc) · 8.32 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# Copyright 2010, 2012-2014 Christoph Reiter
# 2020-22 Nick Boultbee
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
from quodlibet import util
from quodlibet import config
from quodlibet import _
from quodlibet.pattern import XMLFromPattern
from quodlibet.qltk.models import ObjectTreeStore, ObjectModelFilter
from quodlibet.qltk.models import ObjectModelSort
EMPTY = _("Songs not in an album")
ALBUM_PATTERN = r"""
\<b\><album|<album>|%s>\</b\><date| \<small\>(<date>)\</small\>>
\<small\><~discs|<~discs> - ><~tracks> - <~long-length>\</small\>""" % EMPTY
ALBUM_PATTERN = ALBUM_PATTERN.lstrip()
PAT = XMLFromPattern(ALBUM_PATTERN)
UNKNOWN_PATTERN = util.bold_italic(_("Unknown %s"))
MULTI_PATTERN = util.bold_italic(_("Multiple %s Values"))
COUNT_PATTERN = " <span size='small' color='#777'>(%s)</span>"
class AlbumNode:
def __init__(self, album):
self.album = album
self.scanned = False
@property
def cover_size(self):
size = config.getint("browsers", "cover_size")
if size <= 0:
size = 48
return size
def scan_cover(self, scale_factor=1):
if self.scanned or not self.album.songs:
return
self.scanned = True
from quodlibet import app
s = self.cover_size * scale_factor * 0.5
self.cover = app.cover_manager.get_pixbuf_many(self.album.songs, s, s)
UnknownNode = object()
MultiNode = object()
_ORDERING = {t: (x + 1) for x, t in enumerate([MultiNode, UnknownNode, None])}
"""The ordering score by instance of singleton / "special" values"."""
def build_tree(tags, albums, cache=None):
if not tags:
return list(albums)
tag, merge = tags[0]
tree = {}
cache = cache or {}
for album in albums:
cache_key = (album, tag)
if cache_key not in cache:
cache[cache_key] = album.list(tag)
values = cache[cache_key]
if merge and len(values) > 1:
values = [MultiNode]
for value in values or [UnknownNode]:
tree.setdefault(value, []).append(album)
for key, value in tree.items():
tree[key] = build_tree(tags[1:], value, cache)
return tree
class CollectionModelMixin:
def get_path_for_album(self, album):
"""Returns the path for an album or None"""
def func(model, path, iter_, result):
item = model.get_value(iter_)
if getattr(item, "album", None) is album:
# pygobject bug: treepath only valid in callback,
# so make a copy
result[0] = path.copy()
return True
return False
res = [None]
self.foreach(func, res)
return res[0]
def get_albums_for_path(self, path):
return self.get_albums_for_iter(self.get_iter(path))
def get_albums_for_iter(self, iter_):
obj = self.get_value(iter_)
if isinstance(obj, AlbumNode):
return {obj.album}
albums = set()
for child_iter, value in self.iterrows(iter_):
if isinstance(value, AlbumNode):
albums.add(value.album)
else:
albums.update(self.get_albums_for_iter(child_iter))
return albums
def iter_albums(self, iter_):
"""Yields all albums below iter_"""
for child_iter, value in self.iterrows(iter_):
if isinstance(value, AlbumNode):
yield value.album
else:
yield from self.iter_albums(child_iter)
def get_markup(self, tags, iter_):
obj = self.get_value(iter_, 0)
if isinstance(obj, AlbumNode):
return PAT % obj.album
if isinstance(obj, str):
markup = util.escape(obj)
else:
tag = util.tag(tags[len(self.get_path(iter_).get_indices()) - 1])
if obj is UnknownNode:
markup = UNKNOWN_PATTERN % util.escape(tag)
else:
markup = MULTI_PATTERN % util.escape(tag)
num = len(self.get_albums_for_iter(iter_))
return markup + COUNT_PATTERN % num
def get_album(self, iter_):
obj = self.get_value(iter_, 0)
if isinstance(obj, AlbumNode):
return obj.album
class CollectionFilterModel(ObjectModelFilter, CollectionModelMixin):
pass
class CollectionSortModel(ObjectModelSort, CollectionModelMixin):
pass
class CollectionTreeStore(ObjectTreeStore, CollectionModelMixin):
def __init__(self):
super().__init__(object)
self.__tags = []
def set_albums(self, tags, albums):
self.clear()
self.__tags = tags
self.add_albums(albums)
@property
def tags(self):
return [t[0] for t in self.__tags]
def add_albums(self, albums):
def _add(tree, iter_=None):
# lowest level, add albums
if isinstance(tree, list):
for album in tree:
self.append(parent=iter_, row=[AlbumNode(album)])
return
# move into existing nodes and remove them from tree
child = self.iter_children(iter_)
while child:
obj = self[child][0]
if obj in tree:
_add(tree[obj], child)
del tree[obj]
child = self.iter_next(child)
# add missing ones
for key, value in tree.items():
_add(value, self.append(parent=iter_, row=[key]))
_add(build_tree(self.__tags, albums))
def remove_albums(self, albums):
# We can't get anything from the albums (they have no songs),
# so we have to look through everything.
def _remove_albums(albums, iter_=None):
child = self.iter_children(iter_)
while child:
_remove_albums(albums, child)
obj = self[child][0]
if isinstance(obj, AlbumNode):
# remove albums
if obj.album in albums:
if not self.remove(child):
child = None
continue
else:
child = self.iter_next(child)
else:
# clean up empty containers
if not self.iter_has_child(child):
if not self.remove(child):
child = None
continue
else:
child = self.iter_next(child)
_remove_albums(set(albums))
def change_albums(self, albums):
def _check_albums(tree, iter_=None, not_found=None):
if not_found is None:
not_found = set()
if isinstance(tree, list):
# save nodes that are not there anymore
child = self.iter_children(iter_)
while child:
row = self[child]
try:
tree.remove(row[0].album)
except ValueError:
pass
else:
# it's still in the same position, trigger a redraw
self.row_changed(row.path, row.iter)
child = self.iter_next(child)
not_found.update(tree)
return not_found
child = self.iter_children(iter_)
while child:
obj = self[child][0]
if obj in tree:
_check_albums(tree[obj], child, not_found)
del tree[obj]
child = self.iter_next(child)
# everything left over changed
def _get_all(sub, found=None):
if found is None:
found = set()
if isinstance(sub, list):
found.update(sub)
return found
for v in sub.values():
_get_all(v, found)
return found
not_found.update(_get_all(tree))
return not_found
not_found = _check_albums(build_tree(self.__tags, albums))
self.remove_albums(not_found)
self.add_albums(not_found)