1) Make hierarchies display as complete tags in user categories.

2) Add a sublist template function for slicing names apart.
This commit is contained in:
Charles Haley 2011-02-22 14:22:42 +00:00
parent bfc53cd031
commit f946570332
2 changed files with 45 additions and 9 deletions

View File

@ -515,6 +515,12 @@ class TagTreeItem(object): # {{{
tweaks['categories_use_field_for_author_name'] == 'author_sort': tweaks['categories_use_field_for_author_name'] == 'author_sort':
name = tag.sort name = tag.sort
tt_author = True tt_author = True
else:
p = self
while p.parent.type != self.ROOT:
p = p.parent
if p.category_key.startswith('@'):
name = getattr(tag, 'original_name', tag.name)
else: else:
name = tag.name name = tag.name
tt_author = False tt_author = False
@ -903,18 +909,19 @@ class TagsModel(QAbstractItemModel): # {{{
node_parent = category node_parent = category
components = [t for t in tag.name.split('.')] components = [t for t in tag.name.split('.')]
if key in ['authors', 'publisher', 'title'] or len(components) == 1: if key in ['authors', 'publisher', 'title'] or len(components) == 1 or \
self.db.field_metadata[key]['kind'] == 'user':
self.beginInsertRows(category_index, 999999, 1) self.beginInsertRows(category_index, 999999, 1)
TagTreeItem(parent=node_parent, data=tag, tooltip=tt, TagTreeItem(parent=node_parent, data=tag, tooltip=tt,
icon_map=self.icon_state_map) icon_map=self.icon_state_map)
self.endInsertRows() self.endInsertRows()
else: else:
print components
for i,comp in enumerate(components): for i,comp in enumerate(components):
children = dict([(t.tag.name, t) for t in node_parent.children child_map = dict([(t.tag.name, t) for t in node_parent.children
if t.type != TagTreeItem.CATEGORY]) if t.type != TagTreeItem.CATEGORY])
if comp in children: if comp in child_map:
node_parent = children[comp] node_parent = child_map[comp]
node_parent.tag.count += tag.count
else: else:
if i < len(components)-1: if i < len(components)-1:
t = copy.copy(tag) t = copy.copy(tag)
@ -934,14 +941,14 @@ class TagsModel(QAbstractItemModel): # {{{
for category in self.category_nodes: for category in self.category_nodes:
if len(category.children) > 0: if len(category.children) > 0:
children = category.children child_map = category.children
states = [c.tag.state for c in category.child_tags()] states = [c.tag.state for c in category.child_tags()]
names = [(c.tag.name, c.tag.category) for c in category.child_tags()] names = [(c.tag.name, c.tag.category) for c in category.child_tags()]
state_map = dict(izip(names, states)) state_map = dict(izip(names, states))
ctags = [c for c in children if c.type == TagTreeItem.CATEGORY] ctags = [c for c in child_map if c.type == TagTreeItem.CATEGORY]
start = len(ctags) start = len(ctags)
self.beginRemoveRows(self.createIndex(category.row(), 0, category), self.beginRemoveRows(self.createIndex(category.row(), 0, category),
start, len(children)-1) start, len(child_map)-1)
category.children = ctags category.children = ctags
self.endRemoveRows() self.endRemoveRows()
else: else:

View File

@ -396,6 +396,34 @@ class BuiltinListitem(BuiltinFormatterFunction):
except: except:
return '' return ''
class BuiltinSublist(BuiltinFormatterFunction):
name = 'sublist'
arg_count = 4
doc = _('sublist(val, start_index, end_index, separator) -- interpret the '
' value as a list of items separated by `separator`, returning a '
' new list made from the `start_index`th to the `end_index`th item. '
'The first item is number zero. If an index is negative, then it '
'counts from the end of the list. As a special case, an end_index '
'of zero is assumed to be the length of the list. Examples using '
'basic template mode and assuming a #genre value if A.B.C: '
'{#genre:sublist(-1,0,.)} returns C<br/>'
'{#genre:sublist(0,1,.)} returns A<br/>'
'{#genre:sublist(0,-1,.)} returns A.B')
def evaluate(self, formatter, kwargs, mi, locals, val, start_index, end_index, sep):
if not val:
return ''
si = int(start_index)
ei = int(end_index)
val = val.split(sep)
try:
if ei == 0:
return sep.join(val[si:])
else:
return sep.join(val[si:ei])
except:
return ''
class BuiltinUppercase(BuiltinFormatterFunction): class BuiltinUppercase(BuiltinFormatterFunction):
name = 'uppercase' name = 'uppercase'
arg_count = 1 arg_count = 1
@ -447,6 +475,7 @@ builtin_re = BuiltinRe()
builtin_shorten = BuiltinShorten() builtin_shorten = BuiltinShorten()
builtin_strcat = BuiltinStrcat() builtin_strcat = BuiltinStrcat()
builtin_strcmp = BuiltinStrcmp() builtin_strcmp = BuiltinStrcmp()
builtin_sublist = BuiltinSublist()
builtin_substr = BuiltinSubstr() builtin_substr = BuiltinSubstr()
builtin_subtract = BuiltinSubtract() builtin_subtract = BuiltinSubtract()
builtin_switch = BuiltinSwitch() builtin_switch = BuiltinSwitch()